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 |
Ingo Molnar | c31f2e8 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 19 | * 2007-04-15 Work begun on replacing all interactivity tuning with a |
| 20 | * fair scheduling design by Con Kolivas. |
| 21 | * 2007-05-05 Load balancing (smp-nice) and other improvements |
| 22 | * by Peter Williams |
| 23 | * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith |
| 24 | * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri |
Ingo Molnar | b913176 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 25 | * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins, |
| 26 | * Thomas Gleixner, Mike Kravetz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/nmi.h> |
| 32 | #include <linux/init.h> |
Ingo Molnar | dff06c1 | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 33 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/highmem.h> |
| 35 | #include <linux/smp_lock.h> |
| 36 | #include <asm/mmu_context.h> |
| 37 | #include <linux/interrupt.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 38 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/completion.h> |
| 40 | #include <linux/kernel_stat.h> |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 41 | #include <linux/debug_locks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/security.h> |
| 43 | #include <linux/notifier.h> |
| 44 | #include <linux/profile.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 45 | #include <linux/freezer.h> |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 46 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/blkdev.h> |
| 48 | #include <linux/delay.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 49 | #include <linux/pid_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/smp.h> |
| 51 | #include <linux/threads.h> |
| 52 | #include <linux/timer.h> |
| 53 | #include <linux/rcupdate.h> |
| 54 | #include <linux/cpu.h> |
| 55 | #include <linux/cpuset.h> |
| 56 | #include <linux/percpu.h> |
| 57 | #include <linux/kthread.h> |
| 58 | #include <linux/seq_file.h> |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 59 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <linux/syscalls.h> |
| 61 | #include <linux/times.h> |
Jay Lan | 8f0ab51 | 2006-09-30 23:28:59 -0700 | [diff] [blame] | 62 | #include <linux/tsacct_kern.h> |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 63 | #include <linux/kprobes.h> |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 64 | #include <linux/delayacct.h> |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 65 | #include <linux/reciprocal_div.h> |
Ingo Molnar | dff06c1 | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 66 | #include <linux/unistd.h> |
Jens Axboe | f5ff842 | 2007-09-21 09:19:54 +0200 | [diff] [blame] | 67 | #include <linux/pagemap.h> |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 68 | #include <linux/hrtimer.h> |
Reynes Philippe | 30914a5 | 2008-03-17 16:19:05 -0700 | [diff] [blame] | 69 | #include <linux/tick.h> |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 70 | #include <linux/bootmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 72 | #include <asm/tlb.h> |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 73 | #include <asm/irq_regs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
Alexey Dobriyan | b035b6d | 2007-02-10 01:45:10 -0800 | [diff] [blame] | 76 | * Scheduler clock - returns current time in nanosec units. |
| 77 | * This is default implementation. |
| 78 | * Architectures and sub-architectures can override this. |
| 79 | */ |
| 80 | unsigned long long __attribute__((weak)) sched_clock(void) |
| 81 | { |
Eric Dumazet | d6322fa | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 82 | return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ); |
Alexey Dobriyan | b035b6d | 2007-02-10 01:45:10 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * Convert user-nice values [ -20 ... 0 ... 19 ] |
| 87 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], |
| 88 | * and back. |
| 89 | */ |
| 90 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) |
| 91 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) |
| 92 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) |
| 93 | |
| 94 | /* |
| 95 | * 'User priority' is the nice value converted to something we |
| 96 | * can work with better when scaling various scheduler parameters, |
| 97 | * it's a [ 0 ... 39 ] range. |
| 98 | */ |
| 99 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) |
| 100 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) |
| 101 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) |
| 102 | |
| 103 | /* |
Ingo Molnar | d7876a0 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 104 | * Helpers for converting nanosecond timing to jiffy resolution |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | */ |
Eric Dumazet | d6322fa | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 106 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 108 | #define NICE_0_LOAD SCHED_LOAD_SCALE |
| 109 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /* |
| 112 | * These are the 'tuning knobs' of the scheduler: |
| 113 | * |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 114 | * default timeslice is 100 msecs (used only for SCHED_RR tasks). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * Timeslices get refilled after they expire. |
| 116 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | #define DEF_TIMESLICE (100 * HZ / 1000) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 118 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 119 | /* |
| 120 | * single value that denotes runtime == period, ie unlimited time. |
| 121 | */ |
| 122 | #define RUNTIME_INF ((u64)~0ULL) |
| 123 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 124 | #ifdef CONFIG_SMP |
| 125 | /* |
| 126 | * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) |
| 127 | * Since cpu_power is a 'constant', we can use a reciprocal divide. |
| 128 | */ |
| 129 | static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load) |
| 130 | { |
| 131 | return reciprocal_divide(load, sg->reciprocal_cpu_power); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Each time a sched group cpu_power is changed, |
| 136 | * we must compute its reciprocal value |
| 137 | */ |
| 138 | static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val) |
| 139 | { |
| 140 | sg->__cpu_power += val; |
| 141 | sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power); |
| 142 | } |
| 143 | #endif |
| 144 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 145 | static inline int rt_policy(int policy) |
| 146 | { |
| 147 | if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR)) |
| 148 | return 1; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static inline int task_has_rt_policy(struct task_struct *p) |
| 153 | { |
| 154 | return rt_policy(p->policy); |
| 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 158 | * This is the priority-queue data structure of the RT scheduling class: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | */ |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 160 | struct rt_prio_array { |
| 161 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ |
| 162 | struct list_head queue[MAX_RT_PRIO]; |
| 163 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 165 | struct rt_bandwidth { |
Ingo Molnar | ea736ed | 2008-03-25 13:51:45 +0100 | [diff] [blame] | 166 | /* nests inside the rq lock: */ |
| 167 | spinlock_t rt_runtime_lock; |
| 168 | ktime_t rt_period; |
| 169 | u64 rt_runtime; |
| 170 | struct hrtimer rt_period_timer; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | static struct rt_bandwidth def_rt_bandwidth; |
| 174 | |
| 175 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); |
| 176 | |
| 177 | static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) |
| 178 | { |
| 179 | struct rt_bandwidth *rt_b = |
| 180 | container_of(timer, struct rt_bandwidth, rt_period_timer); |
| 181 | ktime_t now; |
| 182 | int overrun; |
| 183 | int idle = 0; |
| 184 | |
| 185 | for (;;) { |
| 186 | now = hrtimer_cb_get_time(timer); |
| 187 | overrun = hrtimer_forward(timer, now, rt_b->rt_period); |
| 188 | |
| 189 | if (!overrun) |
| 190 | break; |
| 191 | |
| 192 | idle = do_sched_rt_period_timer(rt_b, overrun); |
| 193 | } |
| 194 | |
| 195 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; |
| 196 | } |
| 197 | |
| 198 | static |
| 199 | void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) |
| 200 | { |
| 201 | rt_b->rt_period = ns_to_ktime(period); |
| 202 | rt_b->rt_runtime = runtime; |
| 203 | |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 204 | spin_lock_init(&rt_b->rt_runtime_lock); |
| 205 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 206 | hrtimer_init(&rt_b->rt_period_timer, |
| 207 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 208 | rt_b->rt_period_timer.function = sched_rt_period_timer; |
| 209 | rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; |
| 210 | } |
| 211 | |
| 212 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) |
| 213 | { |
| 214 | ktime_t now; |
| 215 | |
| 216 | if (rt_b->rt_runtime == RUNTIME_INF) |
| 217 | return; |
| 218 | |
| 219 | if (hrtimer_active(&rt_b->rt_period_timer)) |
| 220 | return; |
| 221 | |
| 222 | spin_lock(&rt_b->rt_runtime_lock); |
| 223 | for (;;) { |
| 224 | if (hrtimer_active(&rt_b->rt_period_timer)) |
| 225 | break; |
| 226 | |
| 227 | now = hrtimer_cb_get_time(&rt_b->rt_period_timer); |
| 228 | hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period); |
| 229 | hrtimer_start(&rt_b->rt_period_timer, |
| 230 | rt_b->rt_period_timer.expires, |
| 231 | HRTIMER_MODE_ABS); |
| 232 | } |
| 233 | spin_unlock(&rt_b->rt_runtime_lock); |
| 234 | } |
| 235 | |
| 236 | #ifdef CONFIG_RT_GROUP_SCHED |
| 237 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) |
| 238 | { |
| 239 | hrtimer_cancel(&rt_b->rt_period_timer); |
| 240 | } |
| 241 | #endif |
| 242 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 243 | #ifdef CONFIG_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 244 | |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 245 | #include <linux/cgroup.h> |
| 246 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 247 | struct cfs_rq; |
| 248 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 249 | static LIST_HEAD(task_groups); |
| 250 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 251 | /* task group related information */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 252 | struct task_group { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 253 | #ifdef CONFIG_CGROUP_SCHED |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 254 | struct cgroup_subsys_state css; |
| 255 | #endif |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 256 | |
| 257 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 258 | /* schedulable entities of this group on each cpu */ |
| 259 | struct sched_entity **se; |
| 260 | /* runqueue "owned" by this group on each cpu */ |
| 261 | struct cfs_rq **cfs_rq; |
| 262 | unsigned long shares; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 263 | #endif |
| 264 | |
| 265 | #ifdef CONFIG_RT_GROUP_SCHED |
| 266 | struct sched_rt_entity **rt_se; |
| 267 | struct rt_rq **rt_rq; |
| 268 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 269 | struct rt_bandwidth rt_bandwidth; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 270 | #endif |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 271 | |
Srivatsa Vaddagiri | ae8393e | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 272 | struct rcu_head rcu; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 273 | struct list_head list; |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 274 | |
| 275 | struct task_group *parent; |
| 276 | struct list_head siblings; |
| 277 | struct list_head children; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 278 | }; |
| 279 | |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 280 | #ifdef CONFIG_USER_SCHED |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 281 | |
| 282 | /* |
| 283 | * Root task group. |
| 284 | * Every UID task group (including init_task_group aka UID-0) will |
| 285 | * be a child to this group. |
| 286 | */ |
| 287 | struct task_group root_task_group; |
| 288 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 289 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 290 | /* Default task group's sched entity on each cpu */ |
| 291 | static DEFINE_PER_CPU(struct sched_entity, init_sched_entity); |
| 292 | /* Default task group's cfs_rq on each cpu */ |
| 293 | static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 294 | #endif |
| 295 | |
| 296 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 297 | static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity); |
| 298 | static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 299 | #endif |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 300 | #else |
| 301 | #define root_task_group init_task_group |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 302 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 303 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 304 | /* task_group_lock serializes add/remove of task groups and also changes to |
Srivatsa Vaddagiri | ec2c507 | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 305 | * a task group's cpu shares. |
| 306 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 307 | static DEFINE_SPINLOCK(task_group_lock); |
Srivatsa Vaddagiri | ec2c507 | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 308 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 309 | /* doms_cur_mutex serializes access to doms_cur[] array */ |
| 310 | static DEFINE_MUTEX(doms_cur_mutex); |
| 311 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 312 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 313 | #ifdef CONFIG_USER_SCHED |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 314 | # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD) |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 315 | #else |
Srivatsa Vaddagiri | 93f992c | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 316 | # define INIT_TASK_GROUP_LOAD NICE_0_LOAD |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 317 | #endif |
| 318 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 319 | #define MIN_SHARES 2 |
| 320 | |
Srivatsa Vaddagiri | 93f992c | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 321 | static int init_task_group_load = INIT_TASK_GROUP_LOAD; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 322 | #endif |
| 323 | |
| 324 | /* Default task group. |
| 325 | * Every task in system belong to this group at bootup. |
| 326 | */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 327 | struct task_group init_task_group; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 328 | |
| 329 | /* return group to which a task belongs */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 330 | static inline struct task_group *task_group(struct task_struct *p) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 331 | { |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 332 | struct task_group *tg; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 333 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 334 | #ifdef CONFIG_USER_SCHED |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 335 | tg = p->user->tg; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 336 | #elif defined(CONFIG_CGROUP_SCHED) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 337 | tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id), |
| 338 | struct task_group, css); |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 339 | #else |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 340 | tg = &init_task_group; |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 341 | #endif |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 342 | return tg; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 346 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 347 | { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 348 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Dmitry Adamushko | ce96b5a | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 349 | p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; |
| 350 | p->se.parent = task_group(p)->se[cpu]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 351 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 352 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 353 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 354 | p->rt.rt_rq = task_group(p)->rt_rq[cpu]; |
| 355 | p->rt.parent = task_group(p)->rt_se[cpu]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 356 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 359 | static inline void lock_doms_cur(void) |
| 360 | { |
| 361 | mutex_lock(&doms_cur_mutex); |
| 362 | } |
| 363 | |
| 364 | static inline void unlock_doms_cur(void) |
| 365 | { |
| 366 | mutex_unlock(&doms_cur_mutex); |
| 367 | } |
| 368 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 369 | #else |
| 370 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 371 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 372 | static inline void lock_doms_cur(void) { } |
| 373 | static inline void unlock_doms_cur(void) { } |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 374 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 375 | #endif /* CONFIG_GROUP_SCHED */ |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 376 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 377 | /* CFS-related fields in a runqueue */ |
| 378 | struct cfs_rq { |
| 379 | struct load_weight load; |
| 380 | unsigned long nr_running; |
| 381 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 382 | u64 exec_clock; |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 383 | u64 min_vruntime; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 384 | |
| 385 | struct rb_root tasks_timeline; |
| 386 | struct rb_node *rb_leftmost; |
Peter Zijlstra | 4a55bd5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 387 | |
| 388 | struct list_head tasks; |
| 389 | struct list_head *balance_iterator; |
| 390 | |
| 391 | /* |
| 392 | * 'curr' points to currently running entity on this cfs_rq. |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 393 | * It is set to NULL otherwise (i.e when none are currently running). |
| 394 | */ |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 395 | struct sched_entity *curr, *next; |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 396 | |
| 397 | unsigned long nr_spread_over; |
| 398 | |
Ingo Molnar | 62160e3 | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 399 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 400 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ |
| 401 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 402 | /* |
| 403 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 404 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities |
| 405 | * (like users, containers etc.) |
| 406 | * |
| 407 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This |
| 408 | * list is used during load balance. |
| 409 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 410 | struct list_head leaf_cfs_rq_list; |
| 411 | struct task_group *tg; /* group that "owns" this runqueue */ |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 412 | |
| 413 | #ifdef CONFIG_SMP |
| 414 | unsigned long task_weight; |
| 415 | unsigned long shares; |
| 416 | /* |
| 417 | * We need space to build a sched_domain wide view of the full task |
| 418 | * group tree, in order to avoid depending on dynamic memory allocation |
| 419 | * during the load balancing we place this in the per cpu task group |
| 420 | * hierarchy. This limits the load balancing to one instance per cpu, |
| 421 | * but more should not be needed anyway. |
| 422 | */ |
| 423 | struct aggregate_struct { |
| 424 | /* |
| 425 | * load = weight(cpus) * f(tg) |
| 426 | * |
| 427 | * Where f(tg) is the recursive weight fraction assigned to |
| 428 | * this group. |
| 429 | */ |
| 430 | unsigned long load; |
| 431 | |
| 432 | /* |
| 433 | * part of the group weight distributed to this span. |
| 434 | */ |
| 435 | unsigned long shares; |
| 436 | |
| 437 | /* |
| 438 | * The sum of all runqueue weights within this span. |
| 439 | */ |
| 440 | unsigned long rq_weight; |
| 441 | |
| 442 | /* |
| 443 | * Weight contributed by tasks; this is the part we can |
| 444 | * influence by moving tasks around. |
| 445 | */ |
| 446 | unsigned long task_weight; |
| 447 | } aggregate; |
| 448 | #endif |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 449 | #endif |
| 450 | }; |
| 451 | |
| 452 | /* Real-Time classes' related field in a runqueue: */ |
| 453 | struct rt_rq { |
| 454 | struct rt_prio_array active; |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 455 | unsigned long rt_nr_running; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 456 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 457 | int highest_prio; /* highest queued rt task prio */ |
| 458 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 459 | #ifdef CONFIG_SMP |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 460 | unsigned long rt_nr_migratory; |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 461 | int overloaded; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 462 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 463 | int rt_throttled; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 464 | u64 rt_time; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 465 | u64 rt_runtime; |
Ingo Molnar | ea736ed | 2008-03-25 13:51:45 +0100 | [diff] [blame] | 466 | /* Nests inside the rq lock: */ |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 467 | spinlock_t rt_runtime_lock; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 468 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 469 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 470 | unsigned long rt_nr_boosted; |
| 471 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 472 | struct rq *rq; |
| 473 | struct list_head leaf_rt_rq_list; |
| 474 | struct task_group *tg; |
| 475 | struct sched_rt_entity *rt_se; |
| 476 | #endif |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 477 | }; |
| 478 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 479 | #ifdef CONFIG_SMP |
| 480 | |
| 481 | /* |
| 482 | * We add the notion of a root-domain which will be used to define per-domain |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 483 | * variables. Each exclusive cpuset essentially defines an island domain by |
| 484 | * fully partitioning the member cpus from any other cpuset. Whenever a new |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 485 | * exclusive cpuset is created, we also create and attach a new root-domain |
| 486 | * object. |
| 487 | * |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 488 | */ |
| 489 | struct root_domain { |
| 490 | atomic_t refcount; |
| 491 | cpumask_t span; |
| 492 | cpumask_t online; |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 493 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 494 | /* |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 495 | * The "RT overload" flag: it gets set if a CPU has more than |
| 496 | * one runnable RT task. |
| 497 | */ |
| 498 | cpumask_t rto_mask; |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 499 | atomic_t rto_count; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 500 | }; |
| 501 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 502 | /* |
| 503 | * By default the system creates a single root-domain with all cpus as |
| 504 | * members (mimicking the global state we have today). |
| 505 | */ |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 506 | static struct root_domain def_root_domain; |
| 507 | |
| 508 | #endif |
| 509 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 510 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | * This is the main, per-CPU runqueue data structure. |
| 512 | * |
| 513 | * Locking rule: those places that want to lock multiple runqueues |
| 514 | * (such as the load balancing or the thread migration code), lock |
| 515 | * acquire operations must be ordered by ascending &runqueue. |
| 516 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 517 | struct rq { |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 518 | /* runqueue lock: */ |
| 519 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | /* |
| 522 | * nr_running and cpu_load should be in the same cacheline because |
| 523 | * remote CPUs use both these fields when doing load calculation. |
| 524 | */ |
| 525 | unsigned long nr_running; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 526 | #define CPU_LOAD_IDX_MAX 5 |
| 527 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; |
Siddha, Suresh B | bdecea3 | 2007-05-08 00:32:48 -0700 | [diff] [blame] | 528 | unsigned char idle_at_tick; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 529 | #ifdef CONFIG_NO_HZ |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 530 | unsigned long last_tick_seen; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 531 | unsigned char in_nohz_recently; |
| 532 | #endif |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 533 | /* capture load from *all* tasks on this cpu: */ |
| 534 | struct load_weight load; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 535 | unsigned long nr_load_updates; |
| 536 | u64 nr_switches; |
| 537 | |
| 538 | struct cfs_rq cfs; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 539 | struct rt_rq rt; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 540 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 541 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 542 | /* list of leaf cfs_rq on this cpu: */ |
| 543 | struct list_head leaf_cfs_rq_list; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 544 | #endif |
| 545 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 546 | struct list_head leaf_rt_rq_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
| 549 | /* |
| 550 | * This is part of a global counter where only the total sum |
| 551 | * over all CPUs matters. A task can increase this counter on |
| 552 | * one CPU and if it got migrated afterwards it may decrease |
| 553 | * it on another CPU. Always updated under the runqueue lock: |
| 554 | */ |
| 555 | unsigned long nr_uninterruptible; |
| 556 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 557 | struct task_struct *curr, *idle; |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 558 | unsigned long next_balance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | struct mm_struct *prev_mm; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 560 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 561 | u64 clock, prev_clock_raw; |
| 562 | s64 clock_max_delta; |
| 563 | |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 564 | unsigned int clock_warps, clock_overflows, clock_underflows; |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 565 | u64 idle_clock; |
| 566 | unsigned int clock_deep_idle_events; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 567 | u64 tick_timestamp; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | atomic_t nr_iowait; |
| 570 | |
| 571 | #ifdef CONFIG_SMP |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 572 | struct root_domain *rd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | struct sched_domain *sd; |
| 574 | |
| 575 | /* For active balancing */ |
| 576 | int active_balance; |
| 577 | int push_cpu; |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 578 | /* cpu of this runqueue: */ |
| 579 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 581 | struct task_struct *migration_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | struct list_head migration_queue; |
| 583 | #endif |
| 584 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 585 | #ifdef CONFIG_SCHED_HRTICK |
| 586 | unsigned long hrtick_flags; |
| 587 | ktime_t hrtick_expire; |
| 588 | struct hrtimer hrtick_timer; |
| 589 | #endif |
| 590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | #ifdef CONFIG_SCHEDSTATS |
| 592 | /* latency stats */ |
| 593 | struct sched_info rq_sched_info; |
| 594 | |
| 595 | /* sys_sched_yield() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 596 | unsigned int yld_exp_empty; |
| 597 | unsigned int yld_act_empty; |
| 598 | unsigned int yld_both_empty; |
| 599 | unsigned int yld_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
| 601 | /* schedule() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 602 | unsigned int sched_switch; |
| 603 | unsigned int sched_count; |
| 604 | unsigned int sched_goidle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
| 606 | /* try_to_wake_up() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 607 | unsigned int ttwu_count; |
| 608 | unsigned int ttwu_local; |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 609 | |
| 610 | /* BKL stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 611 | unsigned int bkl_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | #endif |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 613 | struct lock_class_key rq_lock_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | }; |
| 615 | |
Fenghua Yu | f34e3b6 | 2007-07-19 01:48:13 -0700 | [diff] [blame] | 616 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 618 | static inline void check_preempt_curr(struct rq *rq, struct task_struct *p) |
| 619 | { |
| 620 | rq->curr->sched_class->check_preempt_curr(rq, p); |
| 621 | } |
| 622 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 623 | static inline int cpu_of(struct rq *rq) |
| 624 | { |
| 625 | #ifdef CONFIG_SMP |
| 626 | return rq->cpu; |
| 627 | #else |
| 628 | return 0; |
| 629 | #endif |
| 630 | } |
| 631 | |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 632 | #ifdef CONFIG_NO_HZ |
| 633 | static inline bool nohz_on(int cpu) |
| 634 | { |
| 635 | return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE; |
| 636 | } |
| 637 | |
| 638 | static inline u64 max_skipped_ticks(struct rq *rq) |
| 639 | { |
| 640 | return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1; |
| 641 | } |
| 642 | |
| 643 | static inline void update_last_tick_seen(struct rq *rq) |
| 644 | { |
| 645 | rq->last_tick_seen = jiffies; |
| 646 | } |
| 647 | #else |
| 648 | static inline u64 max_skipped_ticks(struct rq *rq) |
| 649 | { |
| 650 | return 1; |
| 651 | } |
| 652 | |
| 653 | static inline void update_last_tick_seen(struct rq *rq) |
| 654 | { |
| 655 | } |
| 656 | #endif |
| 657 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 658 | /* |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 659 | * Update the per-runqueue clock, as finegrained as the platform can give |
| 660 | * us, but without assuming monotonicity, etc.: |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 661 | */ |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 662 | static void __update_rq_clock(struct rq *rq) |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 663 | { |
| 664 | u64 prev_raw = rq->prev_clock_raw; |
| 665 | u64 now = sched_clock(); |
| 666 | s64 delta = now - prev_raw; |
| 667 | u64 clock = rq->clock; |
| 668 | |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 669 | #ifdef CONFIG_SCHED_DEBUG |
| 670 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 671 | #endif |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 672 | /* |
| 673 | * Protect against sched_clock() occasionally going backwards: |
| 674 | */ |
| 675 | if (unlikely(delta < 0)) { |
| 676 | clock++; |
| 677 | rq->clock_warps++; |
| 678 | } else { |
| 679 | /* |
| 680 | * Catch too large forward jumps too: |
| 681 | */ |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 682 | u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC; |
| 683 | u64 max_time = rq->tick_timestamp + max_jump; |
| 684 | |
| 685 | if (unlikely(clock + delta > max_time)) { |
| 686 | if (clock < max_time) |
| 687 | clock = max_time; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 688 | else |
| 689 | clock++; |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 690 | rq->clock_overflows++; |
| 691 | } else { |
| 692 | if (unlikely(delta > rq->clock_max_delta)) |
| 693 | rq->clock_max_delta = delta; |
| 694 | clock += delta; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | rq->prev_clock_raw = now; |
| 699 | rq->clock = clock; |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 700 | } |
| 701 | |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 702 | static void update_rq_clock(struct rq *rq) |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 703 | { |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 704 | if (likely(smp_processor_id() == cpu_of(rq))) |
| 705 | __update_rq_clock(rq); |
| 706 | } |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 707 | |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 708 | /* |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 709 | * 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] | 710 | * See detach_destroy_domains: synchronize_sched for details. |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 711 | * |
| 712 | * The domain tree of any CPU may only be accessed from within |
| 713 | * preempt-disabled sections. |
| 714 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 715 | #define for_each_domain(cpu, __sd) \ |
| 716 | for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
| 719 | #define this_rq() (&__get_cpu_var(runqueues)) |
| 720 | #define task_rq(p) cpu_rq(task_cpu(p)) |
| 721 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) |
| 722 | |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 723 | /* |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 724 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: |
| 725 | */ |
| 726 | #ifdef CONFIG_SCHED_DEBUG |
| 727 | # define const_debug __read_mostly |
| 728 | #else |
| 729 | # define const_debug static const |
| 730 | #endif |
| 731 | |
| 732 | /* |
| 733 | * Debugging: various feature bits |
| 734 | */ |
| 735 | enum { |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 736 | SCHED_FEAT_NEW_FAIR_SLEEPERS = 1, |
Ingo Molnar | 9612633 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 737 | SCHED_FEAT_WAKEUP_PREEMPT = 2, |
| 738 | SCHED_FEAT_START_DEBIT = 4, |
Ingo Molnar | d25ce4c | 2008-03-17 09:36:53 +0100 | [diff] [blame] | 739 | SCHED_FEAT_AFFINE_WAKEUPS = 8, |
| 740 | SCHED_FEAT_CACHE_HOT_BUDDY = 16, |
Ingo Molnar | 02e2b83 | 2008-03-19 01:37:10 +0100 | [diff] [blame] | 741 | SCHED_FEAT_SYNC_WAKEUPS = 32, |
| 742 | SCHED_FEAT_HRTICK = 64, |
| 743 | SCHED_FEAT_DOUBLE_TICK = 128, |
Peter Zijlstra | 112f53f | 2008-03-19 11:43:36 +0100 | [diff] [blame] | 744 | SCHED_FEAT_NORMALIZED_SLEEPER = 256, |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 745 | }; |
| 746 | |
| 747 | const_debug unsigned int sysctl_sched_features = |
Ingo Molnar | 8401f77 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 748 | SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 | |
Ingo Molnar | 9612633 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 749 | SCHED_FEAT_WAKEUP_PREEMPT * 1 | |
Ingo Molnar | 8401f77 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 750 | SCHED_FEAT_START_DEBIT * 1 | |
Ingo Molnar | d25ce4c | 2008-03-17 09:36:53 +0100 | [diff] [blame] | 751 | SCHED_FEAT_AFFINE_WAKEUPS * 1 | |
| 752 | SCHED_FEAT_CACHE_HOT_BUDDY * 1 | |
Ingo Molnar | 02e2b83 | 2008-03-19 01:37:10 +0100 | [diff] [blame] | 753 | SCHED_FEAT_SYNC_WAKEUPS * 1 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 754 | SCHED_FEAT_HRTICK * 1 | |
Peter Zijlstra | 112f53f | 2008-03-19 11:43:36 +0100 | [diff] [blame] | 755 | SCHED_FEAT_DOUBLE_TICK * 0 | |
| 756 | SCHED_FEAT_NORMALIZED_SLEEPER * 1; |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 757 | |
| 758 | #define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x) |
| 759 | |
| 760 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 761 | * Number of tasks to iterate in a single balance run. |
| 762 | * Limited because this is done with IRQs disabled. |
| 763 | */ |
| 764 | const_debug unsigned int sysctl_sched_nr_migrate = 32; |
| 765 | |
| 766 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 767 | * period over which we measure -rt task cpu usage in us. |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 768 | * default: 1s |
| 769 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 770 | unsigned int sysctl_sched_rt_period = 1000000; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 771 | |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 772 | static __read_mostly int scheduler_running; |
| 773 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 774 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 775 | * part of the period that we allow rt tasks to run in us. |
| 776 | * default: 0.95s |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 777 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 778 | int sysctl_sched_rt_runtime = 950000; |
| 779 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 780 | static inline u64 global_rt_period(void) |
| 781 | { |
| 782 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; |
| 783 | } |
| 784 | |
| 785 | static inline u64 global_rt_runtime(void) |
| 786 | { |
| 787 | if (sysctl_sched_rt_period < 0) |
| 788 | return RUNTIME_INF; |
| 789 | |
| 790 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; |
| 791 | } |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 792 | |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 793 | static const unsigned long long time_sync_thresh = 100000; |
| 794 | |
| 795 | static DEFINE_PER_CPU(unsigned long long, time_offset); |
| 796 | static DEFINE_PER_CPU(unsigned long long, prev_cpu_time); |
| 797 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 798 | /* |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 799 | * Global lock which we take every now and then to synchronize |
| 800 | * the CPUs time. This method is not warp-safe, but it's good |
| 801 | * enough to synchronize slowly diverging time sources and thus |
| 802 | * it's good enough for tracing: |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 803 | */ |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 804 | static DEFINE_SPINLOCK(time_sync_lock); |
| 805 | static unsigned long long prev_global_time; |
| 806 | |
| 807 | static unsigned long long __sync_cpu_clock(cycles_t time, int cpu) |
| 808 | { |
| 809 | unsigned long flags; |
| 810 | |
| 811 | spin_lock_irqsave(&time_sync_lock, flags); |
| 812 | |
| 813 | if (time < prev_global_time) { |
| 814 | per_cpu(time_offset, cpu) += prev_global_time - time; |
| 815 | time = prev_global_time; |
| 816 | } else { |
| 817 | prev_global_time = time; |
| 818 | } |
| 819 | |
| 820 | spin_unlock_irqrestore(&time_sync_lock, flags); |
| 821 | |
| 822 | return time; |
| 823 | } |
| 824 | |
| 825 | static unsigned long long __cpu_clock(int cpu) |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 826 | { |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 827 | unsigned long long now; |
| 828 | unsigned long flags; |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 829 | struct rq *rq; |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 830 | |
Ingo Molnar | 8ced5f6 | 2007-12-07 19:02:47 +0100 | [diff] [blame] | 831 | /* |
| 832 | * Only call sched_clock() if the scheduler has already been |
| 833 | * initialized (some code might call cpu_clock() very early): |
| 834 | */ |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 835 | if (unlikely(!scheduler_running)) |
| 836 | return 0; |
| 837 | |
| 838 | local_irq_save(flags); |
| 839 | rq = cpu_rq(cpu); |
| 840 | update_rq_clock(rq); |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 841 | now = rq->clock; |
Ingo Molnar | 2cd4d0e | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 842 | local_irq_restore(flags); |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 843 | |
| 844 | return now; |
| 845 | } |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 846 | |
| 847 | /* |
| 848 | * For kernel-internal use: high-speed (but slightly incorrect) per-cpu |
| 849 | * clock constructed from sched_clock(): |
| 850 | */ |
| 851 | unsigned long long cpu_clock(int cpu) |
| 852 | { |
| 853 | unsigned long long prev_cpu_time, time, delta_time; |
| 854 | |
| 855 | prev_cpu_time = per_cpu(prev_cpu_time, cpu); |
| 856 | time = __cpu_clock(cpu) + per_cpu(time_offset, cpu); |
| 857 | delta_time = time-prev_cpu_time; |
| 858 | |
| 859 | if (unlikely(delta_time > time_sync_thresh)) |
| 860 | time = __sync_cpu_clock(time, cpu); |
| 861 | |
| 862 | return time; |
| 863 | } |
Paul E. McKenney | a58f6f2 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 864 | EXPORT_SYMBOL_GPL(cpu_clock); |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | #ifndef prepare_arch_switch |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 867 | # define prepare_arch_switch(next) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | #endif |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 869 | #ifndef finish_arch_switch |
| 870 | # define finish_arch_switch(prev) do { } while (0) |
| 871 | #endif |
| 872 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 873 | static inline int task_current(struct rq *rq, struct task_struct *p) |
| 874 | { |
| 875 | return rq->curr == p; |
| 876 | } |
| 877 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 878 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 879 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 880 | { |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 881 | return task_current(rq, p); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 884 | 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] | 885 | { |
| 886 | } |
| 887 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 888 | 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] | 889 | { |
Ingo Molnar | da04c03 | 2005-09-13 11:17:59 +0200 | [diff] [blame] | 890 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 891 | /* this is a valid case when another task releases the spinlock */ |
| 892 | rq->lock.owner = current; |
| 893 | #endif |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 894 | /* |
| 895 | * If we are tracking spinlock dependencies then we have to |
| 896 | * fix up the runqueue lock - which gets 'carried over' from |
| 897 | * prev into current: |
| 898 | */ |
| 899 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); |
| 900 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 901 | spin_unlock_irq(&rq->lock); |
| 902 | } |
| 903 | |
| 904 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 905 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 906 | { |
| 907 | #ifdef CONFIG_SMP |
| 908 | return p->oncpu; |
| 909 | #else |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 910 | return task_current(rq, p); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 911 | #endif |
| 912 | } |
| 913 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 914 | 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] | 915 | { |
| 916 | #ifdef CONFIG_SMP |
| 917 | /* |
| 918 | * We can optimise this out completely for !SMP, because the |
| 919 | * SMP rebalancing from interrupt is the only thing that cares |
| 920 | * here. |
| 921 | */ |
| 922 | next->oncpu = 1; |
| 923 | #endif |
| 924 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 925 | spin_unlock_irq(&rq->lock); |
| 926 | #else |
| 927 | spin_unlock(&rq->lock); |
| 928 | #endif |
| 929 | } |
| 930 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 931 | 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] | 932 | { |
| 933 | #ifdef CONFIG_SMP |
| 934 | /* |
| 935 | * After ->oncpu is cleared, the task can be moved to a different CPU. |
| 936 | * We must ensure this doesn't happen until the switch is completely |
| 937 | * finished. |
| 938 | */ |
| 939 | smp_wmb(); |
| 940 | prev->oncpu = 0; |
| 941 | #endif |
| 942 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 943 | local_irq_enable(); |
| 944 | #endif |
| 945 | } |
| 946 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
| 948 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 949 | * __task_rq_lock - lock the runqueue a given task resides on. |
| 950 | * Must be called interrupts disabled. |
| 951 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 952 | static inline struct rq *__task_rq_lock(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 953 | __acquires(rq->lock) |
| 954 | { |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 955 | for (;;) { |
| 956 | struct rq *rq = task_rq(p); |
| 957 | spin_lock(&rq->lock); |
| 958 | if (likely(rq == task_rq(p))) |
| 959 | return rq; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 960 | spin_unlock(&rq->lock); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 961 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | * task_rq_lock - lock the runqueue a given task resides on and disable |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 966 | * interrupts. Note the ordering: we can safely lookup the task_rq without |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | * explicitly disabling preemption. |
| 968 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 969 | 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] | 970 | __acquires(rq->lock) |
| 971 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 972 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 974 | for (;;) { |
| 975 | local_irq_save(*flags); |
| 976 | rq = task_rq(p); |
| 977 | spin_lock(&rq->lock); |
| 978 | if (likely(rq == task_rq(p))) |
| 979 | return rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | spin_unlock_irqrestore(&rq->lock, *flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 984 | static void __task_rq_unlock(struct rq *rq) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 985 | __releases(rq->lock) |
| 986 | { |
| 987 | spin_unlock(&rq->lock); |
| 988 | } |
| 989 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 990 | static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | __releases(rq->lock) |
| 992 | { |
| 993 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 994 | } |
| 995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | /* |
Robert P. J. Day | cc2a73b | 2006-12-10 02:20:00 -0800 | [diff] [blame] | 997 | * this_rq_lock - lock this runqueue and disable interrupts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 999 | static struct rq *this_rq_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | __acquires(rq->lock) |
| 1001 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1002 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
| 1004 | local_irq_disable(); |
| 1005 | rq = this_rq(); |
| 1006 | spin_lock(&rq->lock); |
| 1007 | |
| 1008 | return rq; |
| 1009 | } |
| 1010 | |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1011 | /* |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1012 | * We are going deep-idle (irqs are disabled): |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1013 | */ |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1014 | void sched_clock_idle_sleep_event(void) |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1015 | { |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1016 | struct rq *rq = cpu_rq(smp_processor_id()); |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1017 | |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1018 | spin_lock(&rq->lock); |
| 1019 | __update_rq_clock(rq); |
| 1020 | spin_unlock(&rq->lock); |
| 1021 | rq->clock_deep_idle_events++; |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1022 | } |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1023 | EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event); |
| 1024 | |
| 1025 | /* |
| 1026 | * We just idled delta nanoseconds (called with irqs disabled): |
| 1027 | */ |
| 1028 | void sched_clock_idle_wakeup_event(u64 delta_ns) |
| 1029 | { |
| 1030 | struct rq *rq = cpu_rq(smp_processor_id()); |
| 1031 | u64 now = sched_clock(); |
| 1032 | |
| 1033 | rq->idle_clock += delta_ns; |
| 1034 | /* |
| 1035 | * Override the previous timestamp and ignore all |
| 1036 | * sched_clock() deltas that occured while we idled, |
| 1037 | * and use the PM-provided delta_ns to advance the |
| 1038 | * rq clock: |
| 1039 | */ |
| 1040 | spin_lock(&rq->lock); |
| 1041 | rq->prev_clock_raw = now; |
| 1042 | rq->clock += delta_ns; |
| 1043 | spin_unlock(&rq->lock); |
Guillaume Chazarain | 782daee | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 1044 | touch_softlockup_watchdog(); |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1045 | } |
| 1046 | EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event); |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1047 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1048 | static void __resched_task(struct task_struct *p, int tif_bit); |
| 1049 | |
| 1050 | static inline void resched_task(struct task_struct *p) |
| 1051 | { |
| 1052 | __resched_task(p, TIF_NEED_RESCHED); |
| 1053 | } |
| 1054 | |
| 1055 | #ifdef CONFIG_SCHED_HRTICK |
| 1056 | /* |
| 1057 | * Use HR-timers to deliver accurate preemption points. |
| 1058 | * |
| 1059 | * Its all a bit involved since we cannot program an hrt while holding the |
| 1060 | * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a |
| 1061 | * reschedule event. |
| 1062 | * |
| 1063 | * When we get rescheduled we reprogram the hrtick_timer outside of the |
| 1064 | * rq->lock. |
| 1065 | */ |
| 1066 | static inline void resched_hrt(struct task_struct *p) |
| 1067 | { |
| 1068 | __resched_task(p, TIF_HRTICK_RESCHED); |
| 1069 | } |
| 1070 | |
| 1071 | static inline void resched_rq(struct rq *rq) |
| 1072 | { |
| 1073 | unsigned long flags; |
| 1074 | |
| 1075 | spin_lock_irqsave(&rq->lock, flags); |
| 1076 | resched_task(rq->curr); |
| 1077 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1078 | } |
| 1079 | |
| 1080 | enum { |
| 1081 | HRTICK_SET, /* re-programm hrtick_timer */ |
| 1082 | HRTICK_RESET, /* not a new slice */ |
| 1083 | }; |
| 1084 | |
| 1085 | /* |
| 1086 | * Use hrtick when: |
| 1087 | * - enabled by features |
| 1088 | * - hrtimer is actually high res |
| 1089 | */ |
| 1090 | static inline int hrtick_enabled(struct rq *rq) |
| 1091 | { |
| 1092 | if (!sched_feat(HRTICK)) |
| 1093 | return 0; |
| 1094 | return hrtimer_is_hres_active(&rq->hrtick_timer); |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * Called to set the hrtick timer state. |
| 1099 | * |
| 1100 | * called with rq->lock held and irqs disabled |
| 1101 | */ |
| 1102 | static void hrtick_start(struct rq *rq, u64 delay, int reset) |
| 1103 | { |
| 1104 | assert_spin_locked(&rq->lock); |
| 1105 | |
| 1106 | /* |
| 1107 | * preempt at: now + delay |
| 1108 | */ |
| 1109 | rq->hrtick_expire = |
| 1110 | ktime_add_ns(rq->hrtick_timer.base->get_time(), delay); |
| 1111 | /* |
| 1112 | * indicate we need to program the timer |
| 1113 | */ |
| 1114 | __set_bit(HRTICK_SET, &rq->hrtick_flags); |
| 1115 | if (reset) |
| 1116 | __set_bit(HRTICK_RESET, &rq->hrtick_flags); |
| 1117 | |
| 1118 | /* |
| 1119 | * New slices are called from the schedule path and don't need a |
| 1120 | * forced reschedule. |
| 1121 | */ |
| 1122 | if (reset) |
| 1123 | resched_hrt(rq->curr); |
| 1124 | } |
| 1125 | |
| 1126 | static void hrtick_clear(struct rq *rq) |
| 1127 | { |
| 1128 | if (hrtimer_active(&rq->hrtick_timer)) |
| 1129 | hrtimer_cancel(&rq->hrtick_timer); |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * Update the timer from the possible pending state. |
| 1134 | */ |
| 1135 | static void hrtick_set(struct rq *rq) |
| 1136 | { |
| 1137 | ktime_t time; |
| 1138 | int set, reset; |
| 1139 | unsigned long flags; |
| 1140 | |
| 1141 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 1142 | |
| 1143 | spin_lock_irqsave(&rq->lock, flags); |
| 1144 | set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags); |
| 1145 | reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags); |
| 1146 | time = rq->hrtick_expire; |
| 1147 | clear_thread_flag(TIF_HRTICK_RESCHED); |
| 1148 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1149 | |
| 1150 | if (set) { |
| 1151 | hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS); |
| 1152 | if (reset && !hrtimer_active(&rq->hrtick_timer)) |
| 1153 | resched_rq(rq); |
| 1154 | } else |
| 1155 | hrtick_clear(rq); |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * High-resolution timer tick. |
| 1160 | * Runs from hardirq context with interrupts disabled. |
| 1161 | */ |
| 1162 | static enum hrtimer_restart hrtick(struct hrtimer *timer) |
| 1163 | { |
| 1164 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); |
| 1165 | |
| 1166 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 1167 | |
| 1168 | spin_lock(&rq->lock); |
| 1169 | __update_rq_clock(rq); |
| 1170 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
| 1171 | spin_unlock(&rq->lock); |
| 1172 | |
| 1173 | return HRTIMER_NORESTART; |
| 1174 | } |
| 1175 | |
| 1176 | static inline void init_rq_hrtick(struct rq *rq) |
| 1177 | { |
| 1178 | rq->hrtick_flags = 0; |
| 1179 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1180 | rq->hrtick_timer.function = hrtick; |
| 1181 | rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; |
| 1182 | } |
| 1183 | |
| 1184 | void hrtick_resched(void) |
| 1185 | { |
| 1186 | struct rq *rq; |
| 1187 | unsigned long flags; |
| 1188 | |
| 1189 | if (!test_thread_flag(TIF_HRTICK_RESCHED)) |
| 1190 | return; |
| 1191 | |
| 1192 | local_irq_save(flags); |
| 1193 | rq = cpu_rq(smp_processor_id()); |
| 1194 | hrtick_set(rq); |
| 1195 | local_irq_restore(flags); |
| 1196 | } |
| 1197 | #else |
| 1198 | static inline void hrtick_clear(struct rq *rq) |
| 1199 | { |
| 1200 | } |
| 1201 | |
| 1202 | static inline void hrtick_set(struct rq *rq) |
| 1203 | { |
| 1204 | } |
| 1205 | |
| 1206 | static inline void init_rq_hrtick(struct rq *rq) |
| 1207 | { |
| 1208 | } |
| 1209 | |
| 1210 | void hrtick_resched(void) |
| 1211 | { |
| 1212 | } |
| 1213 | #endif |
| 1214 | |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1215 | /* |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1216 | * resched_task - mark a task 'to be rescheduled now'. |
| 1217 | * |
| 1218 | * On UP this means the setting of the need_resched flag, on SMP it |
| 1219 | * might also involve a cross-CPU call to trigger the scheduler on |
| 1220 | * the target CPU. |
| 1221 | */ |
| 1222 | #ifdef CONFIG_SMP |
| 1223 | |
| 1224 | #ifndef tsk_is_polling |
| 1225 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) |
| 1226 | #endif |
| 1227 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1228 | static void __resched_task(struct task_struct *p, int tif_bit) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1229 | { |
| 1230 | int cpu; |
| 1231 | |
| 1232 | assert_spin_locked(&task_rq(p)->lock); |
| 1233 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1234 | if (unlikely(test_tsk_thread_flag(p, tif_bit))) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1235 | return; |
| 1236 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1237 | set_tsk_thread_flag(p, tif_bit); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1238 | |
| 1239 | cpu = task_cpu(p); |
| 1240 | if (cpu == smp_processor_id()) |
| 1241 | return; |
| 1242 | |
| 1243 | /* NEED_RESCHED must be visible before we test polling */ |
| 1244 | smp_mb(); |
| 1245 | if (!tsk_is_polling(p)) |
| 1246 | smp_send_reschedule(cpu); |
| 1247 | } |
| 1248 | |
| 1249 | static void resched_cpu(int cpu) |
| 1250 | { |
| 1251 | struct rq *rq = cpu_rq(cpu); |
| 1252 | unsigned long flags; |
| 1253 | |
| 1254 | if (!spin_trylock_irqsave(&rq->lock, flags)) |
| 1255 | return; |
| 1256 | resched_task(cpu_curr(cpu)); |
| 1257 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1258 | } |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 1259 | |
| 1260 | #ifdef CONFIG_NO_HZ |
| 1261 | /* |
| 1262 | * When add_timer_on() enqueues a timer into the timer wheel of an |
| 1263 | * idle CPU then this timer might expire before the next timer event |
| 1264 | * which is scheduled to wake up that CPU. In case of a completely |
| 1265 | * idle system the next event might even be infinite time into the |
| 1266 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and |
| 1267 | * leaves the inner idle loop so the newly added timer is taken into |
| 1268 | * account when the CPU goes back to idle and evaluates the timer |
| 1269 | * wheel for the next timer event. |
| 1270 | */ |
| 1271 | void wake_up_idle_cpu(int cpu) |
| 1272 | { |
| 1273 | struct rq *rq = cpu_rq(cpu); |
| 1274 | |
| 1275 | if (cpu == smp_processor_id()) |
| 1276 | return; |
| 1277 | |
| 1278 | /* |
| 1279 | * This is safe, as this function is called with the timer |
| 1280 | * wheel base lock of (cpu) held. When the CPU is on the way |
| 1281 | * to idle and has not yet set rq->curr to idle then it will |
| 1282 | * be serialized on the timer wheel base lock and take the new |
| 1283 | * timer into account automatically. |
| 1284 | */ |
| 1285 | if (rq->curr != rq->idle) |
| 1286 | return; |
| 1287 | |
| 1288 | /* |
| 1289 | * We can set TIF_RESCHED on the idle task of the other CPU |
| 1290 | * lockless. The worst case is that the other CPU runs the |
| 1291 | * idle task through an additional NOOP schedule() |
| 1292 | */ |
| 1293 | set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED); |
| 1294 | |
| 1295 | /* NEED_RESCHED must be visible before we test polling */ |
| 1296 | smp_mb(); |
| 1297 | if (!tsk_is_polling(rq->idle)) |
| 1298 | smp_send_reschedule(cpu); |
| 1299 | } |
| 1300 | #endif |
| 1301 | |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1302 | #else |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1303 | static void __resched_task(struct task_struct *p, int tif_bit) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1304 | { |
| 1305 | assert_spin_locked(&task_rq(p)->lock); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1306 | set_tsk_thread_flag(p, tif_bit); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1307 | } |
| 1308 | #endif |
| 1309 | |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1310 | #if BITS_PER_LONG == 32 |
| 1311 | # define WMULT_CONST (~0UL) |
| 1312 | #else |
| 1313 | # define WMULT_CONST (1UL << 32) |
| 1314 | #endif |
| 1315 | |
| 1316 | #define WMULT_SHIFT 32 |
| 1317 | |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1318 | /* |
| 1319 | * Shift right and round: |
| 1320 | */ |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1321 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1322 | |
Peter Zijlstra | 8f1bc385 | 2008-04-19 19:45:00 +0200 | [diff] [blame^] | 1323 | /* |
| 1324 | * delta *= weight / lw |
| 1325 | */ |
Ingo Molnar | cb1c4fc | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1326 | static unsigned long |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1327 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, |
| 1328 | struct load_weight *lw) |
| 1329 | { |
| 1330 | u64 tmp; |
| 1331 | |
| 1332 | if (unlikely(!lw->inv_weight)) |
Ingo Molnar | 27d1172 | 2008-03-14 22:20:01 +0100 | [diff] [blame] | 1333 | lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1334 | |
| 1335 | tmp = (u64)delta_exec * weight; |
| 1336 | /* |
| 1337 | * Check whether we'd overflow the 64-bit multiplication: |
| 1338 | */ |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1339 | if (unlikely(tmp > WMULT_CONST)) |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1340 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1341 | WMULT_SHIFT/2); |
| 1342 | else |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1343 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1344 | |
Ingo Molnar | ecf691d | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1345 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1346 | } |
| 1347 | |
Ingo Molnar | 1091985 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1348 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1349 | { |
| 1350 | lw->weight += inc; |
Ingo Molnar | e89996a | 2008-03-14 23:48:28 +0100 | [diff] [blame] | 1351 | lw->inv_weight = 0; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1352 | } |
| 1353 | |
Ingo Molnar | 1091985 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1354 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1355 | { |
| 1356 | lw->weight -= dec; |
Ingo Molnar | e89996a | 2008-03-14 23:48:28 +0100 | [diff] [blame] | 1357 | lw->inv_weight = 0; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1358 | } |
| 1359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1361 | * To aid in avoiding the subversion of "niceness" due to uneven distribution |
| 1362 | * of tasks with abnormal "nice" values across CPUs the contribution that |
| 1363 | * each task makes to its run queue's load is weighted according to its |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 1364 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1365 | * scaled version of the new time slice allocation that they receive on time |
| 1366 | * slice expiry etc. |
| 1367 | */ |
| 1368 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1369 | #define WEIGHT_IDLEPRIO 2 |
| 1370 | #define WMULT_IDLEPRIO (1 << 31) |
| 1371 | |
| 1372 | /* |
| 1373 | * Nice levels are multiplicative, with a gentle 10% change for every |
| 1374 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to |
| 1375 | * nice 1, it will get ~10% less CPU time than another CPU-bound task |
| 1376 | * that remained on nice 0. |
| 1377 | * |
| 1378 | * The "10% effect" is relative and cumulative: from _any_ nice level, |
| 1379 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level |
Ingo Molnar | f9153ee | 2007-07-16 09:46:30 +0200 | [diff] [blame] | 1380 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. |
| 1381 | * If a task goes up by ~10% and another task goes down by ~10% then |
| 1382 | * the relative distance between them is ~25%.) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1383 | */ |
| 1384 | static const int prio_to_weight[40] = { |
Ingo Molnar | 254753d | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1385 | /* -20 */ 88761, 71755, 56483, 46273, 36291, |
| 1386 | /* -15 */ 29154, 23254, 18705, 14949, 11916, |
| 1387 | /* -10 */ 9548, 7620, 6100, 4904, 3906, |
| 1388 | /* -5 */ 3121, 2501, 1991, 1586, 1277, |
| 1389 | /* 0 */ 1024, 820, 655, 526, 423, |
| 1390 | /* 5 */ 335, 272, 215, 172, 137, |
| 1391 | /* 10 */ 110, 87, 70, 56, 45, |
| 1392 | /* 15 */ 36, 29, 23, 18, 15, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1393 | }; |
| 1394 | |
Ingo Molnar | 5714d2d | 2007-07-16 09:46:31 +0200 | [diff] [blame] | 1395 | /* |
| 1396 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. |
| 1397 | * |
| 1398 | * In cases where the weight does not change often, we can use the |
| 1399 | * precalculated inverse to speed up arithmetics by turning divisions |
| 1400 | * into multiplications: |
| 1401 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1402 | static const u32 prio_to_wmult[40] = { |
Ingo Molnar | 254753d | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1403 | /* -20 */ 48388, 59856, 76040, 92818, 118348, |
| 1404 | /* -15 */ 147320, 184698, 229616, 287308, 360437, |
| 1405 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, |
| 1406 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, |
| 1407 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, |
| 1408 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, |
| 1409 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, |
| 1410 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1411 | }; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1412 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1413 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup); |
| 1414 | |
| 1415 | /* |
| 1416 | * runqueue iterator, to support SMP load-balancing between different |
| 1417 | * scheduling classes, without having to expose their internal data |
| 1418 | * structures to the load-balancing proper: |
| 1419 | */ |
| 1420 | struct rq_iterator { |
| 1421 | void *arg; |
| 1422 | struct task_struct *(*start)(void *); |
| 1423 | struct task_struct *(*next)(void *); |
| 1424 | }; |
| 1425 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1426 | #ifdef CONFIG_SMP |
| 1427 | static unsigned long |
| 1428 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1429 | unsigned long max_load_move, struct sched_domain *sd, |
| 1430 | enum cpu_idle_type idle, int *all_pinned, |
| 1431 | int *this_best_prio, struct rq_iterator *iterator); |
| 1432 | |
| 1433 | static int |
| 1434 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1435 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 1436 | struct rq_iterator *iterator); |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1437 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1438 | |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 1439 | #ifdef CONFIG_CGROUP_CPUACCT |
| 1440 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime); |
| 1441 | #else |
| 1442 | static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} |
| 1443 | #endif |
| 1444 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1445 | static inline void inc_cpu_load(struct rq *rq, unsigned long load) |
| 1446 | { |
| 1447 | update_load_add(&rq->load, load); |
| 1448 | } |
| 1449 | |
| 1450 | static inline void dec_cpu_load(struct rq *rq, unsigned long load) |
| 1451 | { |
| 1452 | update_load_sub(&rq->load, load); |
| 1453 | } |
| 1454 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1455 | #ifdef CONFIG_SMP |
| 1456 | static unsigned long source_load(int cpu, int type); |
| 1457 | static unsigned long target_load(int cpu, int type); |
| 1458 | static unsigned long cpu_avg_load_per_task(int cpu); |
| 1459 | static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1460 | |
| 1461 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 1462 | |
| 1463 | /* |
| 1464 | * Group load balancing. |
| 1465 | * |
| 1466 | * We calculate a few balance domain wide aggregate numbers; load and weight. |
| 1467 | * Given the pictures below, and assuming each item has equal weight: |
| 1468 | * |
| 1469 | * root 1 - thread |
| 1470 | * / | \ A - group |
| 1471 | * A 1 B |
| 1472 | * /|\ / \ |
| 1473 | * C 2 D 3 4 |
| 1474 | * | | |
| 1475 | * 5 6 |
| 1476 | * |
| 1477 | * load: |
| 1478 | * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd, |
| 1479 | * which equals 1/9-th of the total load. |
| 1480 | * |
| 1481 | * shares: |
| 1482 | * The weight of this group on the selected cpus. |
| 1483 | * |
| 1484 | * rq_weight: |
| 1485 | * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while |
| 1486 | * B would get 2. |
| 1487 | * |
| 1488 | * task_weight: |
| 1489 | * Part of the rq_weight contributed by tasks; all groups except B would |
| 1490 | * get 1, B gets 2. |
| 1491 | */ |
| 1492 | |
| 1493 | static inline struct aggregate_struct * |
| 1494 | aggregate(struct task_group *tg, struct sched_domain *sd) |
| 1495 | { |
| 1496 | return &tg->cfs_rq[sd->first_cpu]->aggregate; |
| 1497 | } |
| 1498 | |
| 1499 | typedef void (*aggregate_func)(struct task_group *, struct sched_domain *); |
| 1500 | |
| 1501 | /* |
| 1502 | * Iterate the full tree, calling @down when first entering a node and @up when |
| 1503 | * leaving it for the final time. |
| 1504 | */ |
| 1505 | static |
| 1506 | void aggregate_walk_tree(aggregate_func down, aggregate_func up, |
| 1507 | struct sched_domain *sd) |
| 1508 | { |
| 1509 | struct task_group *parent, *child; |
| 1510 | |
| 1511 | rcu_read_lock(); |
| 1512 | parent = &root_task_group; |
| 1513 | down: |
| 1514 | (*down)(parent, sd); |
| 1515 | list_for_each_entry_rcu(child, &parent->children, siblings) { |
| 1516 | parent = child; |
| 1517 | goto down; |
| 1518 | |
| 1519 | up: |
| 1520 | continue; |
| 1521 | } |
| 1522 | (*up)(parent, sd); |
| 1523 | |
| 1524 | child = parent; |
| 1525 | parent = parent->parent; |
| 1526 | if (parent) |
| 1527 | goto up; |
| 1528 | rcu_read_unlock(); |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * Calculate the aggregate runqueue weight. |
| 1533 | */ |
| 1534 | static |
| 1535 | void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd) |
| 1536 | { |
| 1537 | unsigned long rq_weight = 0; |
| 1538 | unsigned long task_weight = 0; |
| 1539 | int i; |
| 1540 | |
| 1541 | for_each_cpu_mask(i, sd->span) { |
| 1542 | rq_weight += tg->cfs_rq[i]->load.weight; |
| 1543 | task_weight += tg->cfs_rq[i]->task_weight; |
| 1544 | } |
| 1545 | |
| 1546 | aggregate(tg, sd)->rq_weight = rq_weight; |
| 1547 | aggregate(tg, sd)->task_weight = task_weight; |
| 1548 | } |
| 1549 | |
| 1550 | /* |
| 1551 | * Redistribute tg->shares amongst all tg->cfs_rq[]s. |
| 1552 | */ |
| 1553 | static void __aggregate_redistribute_shares(struct task_group *tg) |
| 1554 | { |
| 1555 | int i, max_cpu = smp_processor_id(); |
| 1556 | unsigned long rq_weight = 0; |
| 1557 | unsigned long shares, max_shares = 0, shares_rem = tg->shares; |
| 1558 | |
| 1559 | for_each_possible_cpu(i) |
| 1560 | rq_weight += tg->cfs_rq[i]->load.weight; |
| 1561 | |
| 1562 | for_each_possible_cpu(i) { |
| 1563 | /* |
| 1564 | * divide shares proportional to the rq_weights. |
| 1565 | */ |
| 1566 | shares = tg->shares * tg->cfs_rq[i]->load.weight; |
| 1567 | shares /= rq_weight + 1; |
| 1568 | |
| 1569 | tg->cfs_rq[i]->shares = shares; |
| 1570 | |
| 1571 | if (shares > max_shares) { |
| 1572 | max_shares = shares; |
| 1573 | max_cpu = i; |
| 1574 | } |
| 1575 | shares_rem -= shares; |
| 1576 | } |
| 1577 | |
| 1578 | /* |
| 1579 | * Ensure it all adds up to tg->shares; we can loose a few |
| 1580 | * due to rounding down when computing the per-cpu shares. |
| 1581 | */ |
| 1582 | if (shares_rem) |
| 1583 | tg->cfs_rq[max_cpu]->shares += shares_rem; |
| 1584 | } |
| 1585 | |
| 1586 | /* |
| 1587 | * Compute the weight of this group on the given cpus. |
| 1588 | */ |
| 1589 | static |
| 1590 | void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd) |
| 1591 | { |
| 1592 | unsigned long shares = 0; |
| 1593 | int i; |
| 1594 | |
| 1595 | again: |
| 1596 | for_each_cpu_mask(i, sd->span) |
| 1597 | shares += tg->cfs_rq[i]->shares; |
| 1598 | |
| 1599 | /* |
| 1600 | * When the span doesn't have any shares assigned, but does have |
| 1601 | * tasks to run do a machine wide rebalance (should be rare). |
| 1602 | */ |
| 1603 | if (unlikely(!shares && aggregate(tg, sd)->rq_weight)) { |
| 1604 | __aggregate_redistribute_shares(tg); |
| 1605 | goto again; |
| 1606 | } |
| 1607 | |
| 1608 | aggregate(tg, sd)->shares = shares; |
| 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | * Compute the load fraction assigned to this group, relies on the aggregate |
| 1613 | * weight and this group's parent's load, i.e. top-down. |
| 1614 | */ |
| 1615 | static |
| 1616 | void aggregate_group_load(struct task_group *tg, struct sched_domain *sd) |
| 1617 | { |
| 1618 | unsigned long load; |
| 1619 | |
| 1620 | if (!tg->parent) { |
| 1621 | int i; |
| 1622 | |
| 1623 | load = 0; |
| 1624 | for_each_cpu_mask(i, sd->span) |
| 1625 | load += cpu_rq(i)->load.weight; |
| 1626 | |
| 1627 | } else { |
| 1628 | load = aggregate(tg->parent, sd)->load; |
| 1629 | |
| 1630 | /* |
| 1631 | * shares is our weight in the parent's rq so |
| 1632 | * shares/parent->rq_weight gives our fraction of the load |
| 1633 | */ |
| 1634 | load *= aggregate(tg, sd)->shares; |
| 1635 | load /= aggregate(tg->parent, sd)->rq_weight + 1; |
| 1636 | } |
| 1637 | |
| 1638 | aggregate(tg, sd)->load = load; |
| 1639 | } |
| 1640 | |
| 1641 | static void __set_se_shares(struct sched_entity *se, unsigned long shares); |
| 1642 | |
| 1643 | /* |
| 1644 | * Calculate and set the cpu's group shares. |
| 1645 | */ |
| 1646 | static void |
| 1647 | __update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd, |
| 1648 | int tcpu) |
| 1649 | { |
| 1650 | int boost = 0; |
| 1651 | unsigned long shares; |
| 1652 | unsigned long rq_weight; |
| 1653 | |
| 1654 | if (!tg->se[tcpu]) |
| 1655 | return; |
| 1656 | |
| 1657 | rq_weight = tg->cfs_rq[tcpu]->load.weight; |
| 1658 | |
| 1659 | /* |
| 1660 | * If there are currently no tasks on the cpu pretend there is one of |
| 1661 | * average load so that when a new task gets to run here it will not |
| 1662 | * get delayed by group starvation. |
| 1663 | */ |
| 1664 | if (!rq_weight) { |
| 1665 | boost = 1; |
| 1666 | rq_weight = NICE_0_LOAD; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * \Sum shares * rq_weight |
| 1671 | * shares = ----------------------- |
| 1672 | * \Sum rq_weight |
| 1673 | * |
| 1674 | */ |
| 1675 | shares = aggregate(tg, sd)->shares * rq_weight; |
| 1676 | shares /= aggregate(tg, sd)->rq_weight + 1; |
| 1677 | |
| 1678 | /* |
| 1679 | * record the actual number of shares, not the boosted amount. |
| 1680 | */ |
| 1681 | tg->cfs_rq[tcpu]->shares = boost ? 0 : shares; |
| 1682 | |
| 1683 | if (shares < MIN_SHARES) |
| 1684 | shares = MIN_SHARES; |
| 1685 | |
| 1686 | __set_se_shares(tg->se[tcpu], shares); |
| 1687 | } |
| 1688 | |
| 1689 | /* |
| 1690 | * Re-adjust the weights on the cpu the task came from and on the cpu the |
| 1691 | * task went to. |
| 1692 | */ |
| 1693 | static void |
| 1694 | __move_group_shares(struct task_group *tg, struct sched_domain *sd, |
| 1695 | int scpu, int dcpu) |
| 1696 | { |
| 1697 | unsigned long shares; |
| 1698 | |
| 1699 | shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares; |
| 1700 | |
| 1701 | __update_group_shares_cpu(tg, sd, scpu); |
| 1702 | __update_group_shares_cpu(tg, sd, dcpu); |
| 1703 | |
| 1704 | /* |
| 1705 | * ensure we never loose shares due to rounding errors in the |
| 1706 | * above redistribution. |
| 1707 | */ |
| 1708 | shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares; |
| 1709 | if (shares) |
| 1710 | tg->cfs_rq[dcpu]->shares += shares; |
| 1711 | } |
| 1712 | |
| 1713 | /* |
| 1714 | * Because changing a group's shares changes the weight of the super-group |
| 1715 | * we need to walk up the tree and change all shares until we hit the root. |
| 1716 | */ |
| 1717 | static void |
| 1718 | move_group_shares(struct task_group *tg, struct sched_domain *sd, |
| 1719 | int scpu, int dcpu) |
| 1720 | { |
| 1721 | while (tg) { |
| 1722 | __move_group_shares(tg, sd, scpu, dcpu); |
| 1723 | tg = tg->parent; |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | static |
| 1728 | void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd) |
| 1729 | { |
| 1730 | unsigned long shares = aggregate(tg, sd)->shares; |
| 1731 | int i; |
| 1732 | |
| 1733 | for_each_cpu_mask(i, sd->span) { |
| 1734 | struct rq *rq = cpu_rq(i); |
| 1735 | unsigned long flags; |
| 1736 | |
| 1737 | spin_lock_irqsave(&rq->lock, flags); |
| 1738 | __update_group_shares_cpu(tg, sd, i); |
| 1739 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1740 | } |
| 1741 | |
| 1742 | aggregate_group_shares(tg, sd); |
| 1743 | |
| 1744 | /* |
| 1745 | * ensure we never loose shares due to rounding errors in the |
| 1746 | * above redistribution. |
| 1747 | */ |
| 1748 | shares -= aggregate(tg, sd)->shares; |
| 1749 | if (shares) { |
| 1750 | tg->cfs_rq[sd->first_cpu]->shares += shares; |
| 1751 | aggregate(tg, sd)->shares += shares; |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | /* |
| 1756 | * Calculate the accumulative weight and recursive load of each task group |
| 1757 | * while walking down the tree. |
| 1758 | */ |
| 1759 | static |
| 1760 | void aggregate_get_down(struct task_group *tg, struct sched_domain *sd) |
| 1761 | { |
| 1762 | aggregate_group_weight(tg, sd); |
| 1763 | aggregate_group_shares(tg, sd); |
| 1764 | aggregate_group_load(tg, sd); |
| 1765 | } |
| 1766 | |
| 1767 | /* |
| 1768 | * Rebalance the cpu shares while walking back up the tree. |
| 1769 | */ |
| 1770 | static |
| 1771 | void aggregate_get_up(struct task_group *tg, struct sched_domain *sd) |
| 1772 | { |
| 1773 | aggregate_group_set_shares(tg, sd); |
| 1774 | } |
| 1775 | |
| 1776 | static DEFINE_PER_CPU(spinlock_t, aggregate_lock); |
| 1777 | |
| 1778 | static void __init init_aggregate(void) |
| 1779 | { |
| 1780 | int i; |
| 1781 | |
| 1782 | for_each_possible_cpu(i) |
| 1783 | spin_lock_init(&per_cpu(aggregate_lock, i)); |
| 1784 | } |
| 1785 | |
| 1786 | static int get_aggregate(struct sched_domain *sd) |
| 1787 | { |
| 1788 | if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu))) |
| 1789 | return 0; |
| 1790 | |
| 1791 | aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd); |
| 1792 | return 1; |
| 1793 | } |
| 1794 | |
| 1795 | static void put_aggregate(struct sched_domain *sd) |
| 1796 | { |
| 1797 | spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu)); |
| 1798 | } |
| 1799 | |
| 1800 | static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares) |
| 1801 | { |
| 1802 | cfs_rq->shares = shares; |
| 1803 | } |
| 1804 | |
| 1805 | #else |
| 1806 | |
| 1807 | static inline void init_aggregate(void) |
| 1808 | { |
| 1809 | } |
| 1810 | |
| 1811 | static inline int get_aggregate(struct sched_domain *sd) |
| 1812 | { |
| 1813 | return 0; |
| 1814 | } |
| 1815 | |
| 1816 | static inline void put_aggregate(struct sched_domain *sd) |
| 1817 | { |
| 1818 | } |
| 1819 | #endif |
| 1820 | |
| 1821 | #else /* CONFIG_SMP */ |
| 1822 | |
| 1823 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 1824 | static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares) |
| 1825 | { |
| 1826 | } |
| 1827 | #endif |
| 1828 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1829 | #endif /* CONFIG_SMP */ |
| 1830 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1831 | #include "sched_stats.h" |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1832 | #include "sched_idletask.c" |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1833 | #include "sched_fair.c" |
| 1834 | #include "sched_rt.c" |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1835 | #ifdef CONFIG_SCHED_DEBUG |
| 1836 | # include "sched_debug.c" |
| 1837 | #endif |
| 1838 | |
| 1839 | #define sched_class_highest (&rt_sched_class) |
| 1840 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1841 | static void inc_nr_running(struct rq *rq) |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1842 | { |
| 1843 | rq->nr_running++; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1844 | } |
| 1845 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1846 | static void dec_nr_running(struct rq *rq) |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1847 | { |
| 1848 | rq->nr_running--; |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1849 | } |
| 1850 | |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1851 | static void set_load_weight(struct task_struct *p) |
| 1852 | { |
| 1853 | if (task_has_rt_policy(p)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1854 | p->se.load.weight = prio_to_weight[0] * 2; |
| 1855 | p->se.load.inv_weight = prio_to_wmult[0] >> 1; |
| 1856 | return; |
| 1857 | } |
| 1858 | |
| 1859 | /* |
| 1860 | * SCHED_IDLE tasks get minimal weight: |
| 1861 | */ |
| 1862 | if (p->policy == SCHED_IDLE) { |
| 1863 | p->se.load.weight = WEIGHT_IDLEPRIO; |
| 1864 | p->se.load.inv_weight = WMULT_IDLEPRIO; |
| 1865 | return; |
| 1866 | } |
| 1867 | |
| 1868 | p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO]; |
| 1869 | p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO]; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1872 | static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup) |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1873 | { |
| 1874 | sched_info_queued(p); |
Ingo Molnar | fd390f6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1875 | p->sched_class->enqueue_task(rq, p, wakeup); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1876 | p->se.on_rq = 1; |
| 1877 | } |
| 1878 | |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1879 | static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1880 | { |
Ingo Molnar | f02231e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1881 | p->sched_class->dequeue_task(rq, p, sleep); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1882 | p->se.on_rq = 0; |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1886 | * __normal_prio - return the priority that is based on the static prio |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1887 | */ |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1888 | static inline int __normal_prio(struct task_struct *p) |
| 1889 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1890 | return p->static_prio; |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1894 | * Calculate the expected normal priority: i.e. priority |
| 1895 | * without taking RT-inheritance into account. Might be |
| 1896 | * boosted by interactivity modifiers. Changes upon fork, |
| 1897 | * setprio syscalls, and whenever the interactivity |
| 1898 | * estimator recalculates. |
| 1899 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1900 | static inline int normal_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1901 | { |
| 1902 | int prio; |
| 1903 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1904 | if (task_has_rt_policy(p)) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1905 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
| 1906 | else |
| 1907 | prio = __normal_prio(p); |
| 1908 | return prio; |
| 1909 | } |
| 1910 | |
| 1911 | /* |
| 1912 | * Calculate the current priority, i.e. the priority |
| 1913 | * taken into account by the scheduler. This value might |
| 1914 | * be boosted by RT tasks, or might be boosted by |
| 1915 | * interactivity modifiers. Will be RT if the task got |
| 1916 | * RT-boosted. If not then it returns p->normal_prio. |
| 1917 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1918 | static int effective_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1919 | { |
| 1920 | p->normal_prio = normal_prio(p); |
| 1921 | /* |
| 1922 | * If we are RT tasks or we were boosted to RT priority, |
| 1923 | * keep the priority unchanged. Otherwise, update priority |
| 1924 | * to the normal priority: |
| 1925 | */ |
| 1926 | if (!rt_prio(p->prio)) |
| 1927 | return p->normal_prio; |
| 1928 | return p->prio; |
| 1929 | } |
| 1930 | |
| 1931 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1932 | * activate_task - move a task to the runqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1934 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 1936 | if (task_contributes_to_load(p)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1937 | rq->nr_uninterruptible--; |
| 1938 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1939 | enqueue_task(rq, p, wakeup); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1940 | inc_nr_running(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | * deactivate_task - remove a task from the runqueue. |
| 1945 | */ |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1946 | static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 1948 | if (task_contributes_to_load(p)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1949 | rq->nr_uninterruptible++; |
| 1950 | |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1951 | dequeue_task(rq, p, sleep); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1952 | dec_nr_running(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | /** |
| 1956 | * task_curr - is this task currently executing on a CPU? |
| 1957 | * @p: the task in question. |
| 1958 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1959 | inline int task_curr(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | { |
| 1961 | return cpu_curr(task_cpu(p)) == p; |
| 1962 | } |
| 1963 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1964 | /* Used instead of source_load when we know the type == 0 */ |
| 1965 | unsigned long weighted_cpuload(const int cpu) |
| 1966 | { |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1967 | return cpu_rq(cpu)->load.weight; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) |
| 1971 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1972 | set_task_rq(p, cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1973 | #ifdef CONFIG_SMP |
Dmitry Adamushko | ce96b5a | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 1974 | /* |
| 1975 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be |
| 1976 | * successfuly executed on another CPU. We must ensure that updates of |
| 1977 | * per-task data have been completed by this moment. |
| 1978 | */ |
| 1979 | smp_wmb(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1980 | task_thread_info(p)->cpu = cpu; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1981 | #endif |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1984 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
| 1985 | const struct sched_class *prev_class, |
| 1986 | int oldprio, int running) |
| 1987 | { |
| 1988 | if (prev_class != p->sched_class) { |
| 1989 | if (prev_class->switched_from) |
| 1990 | prev_class->switched_from(rq, p, running); |
| 1991 | p->sched_class->switched_to(rq, p, running); |
| 1992 | } else |
| 1993 | p->sched_class->prio_changed(rq, p, oldprio, running); |
| 1994 | } |
| 1995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | #ifdef CONFIG_SMP |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1997 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1998 | /* |
| 1999 | * Is this task likely cache-hot: |
| 2000 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2001 | static int |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2002 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) |
| 2003 | { |
| 2004 | s64 delta; |
| 2005 | |
Ingo Molnar | f540a60 | 2008-03-15 17:10:34 +0100 | [diff] [blame] | 2006 | /* |
| 2007 | * Buddy candidates are cache hot: |
| 2008 | */ |
Ingo Molnar | d25ce4c | 2008-03-17 09:36:53 +0100 | [diff] [blame] | 2009 | if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next)) |
Ingo Molnar | f540a60 | 2008-03-15 17:10:34 +0100 | [diff] [blame] | 2010 | return 1; |
| 2011 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2012 | if (p->sched_class != &fair_sched_class) |
| 2013 | return 0; |
| 2014 | |
Ingo Molnar | 6bc1665 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2015 | if (sysctl_sched_migration_cost == -1) |
| 2016 | return 1; |
| 2017 | if (sysctl_sched_migration_cost == 0) |
| 2018 | return 0; |
| 2019 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2020 | delta = now - p->se.exec_start; |
| 2021 | |
| 2022 | return delta < (s64)sysctl_sched_migration_cost; |
| 2023 | } |
| 2024 | |
| 2025 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2026 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2027 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2028 | int old_cpu = task_cpu(p); |
| 2029 | struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu); |
Srivatsa Vaddagiri | 2830cf8 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2030 | struct cfs_rq *old_cfsrq = task_cfs_rq(p), |
| 2031 | *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu); |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 2032 | u64 clock_offset; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2033 | |
| 2034 | clock_offset = old_rq->clock - new_rq->clock; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2035 | |
| 2036 | #ifdef CONFIG_SCHEDSTATS |
| 2037 | if (p->se.wait_start) |
| 2038 | p->se.wait_start -= clock_offset; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2039 | if (p->se.sleep_start) |
| 2040 | p->se.sleep_start -= clock_offset; |
| 2041 | if (p->se.block_start) |
| 2042 | p->se.block_start -= clock_offset; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2043 | if (old_cpu != new_cpu) { |
| 2044 | schedstat_inc(p, se.nr_migrations); |
| 2045 | if (task_hot(p, old_rq->clock, NULL)) |
| 2046 | schedstat_inc(p, se.nr_forced2_migrations); |
| 2047 | } |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2048 | #endif |
Srivatsa Vaddagiri | 2830cf8 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2049 | p->se.vruntime -= old_cfsrq->min_vruntime - |
| 2050 | new_cfsrq->min_vruntime; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2051 | |
| 2052 | __set_task_cpu(p, new_cpu); |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2053 | } |
| 2054 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2055 | struct migration_req { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2058 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | int dest_cpu; |
| 2060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | struct completion done; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2062 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | |
| 2064 | /* |
| 2065 | * The task's runqueue lock must be held. |
| 2066 | * Returns true if you have to wait for migration thread. |
| 2067 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2068 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2069 | 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] | 2070 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2071 | struct rq *rq = task_rq(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | |
| 2073 | /* |
| 2074 | * If the task is not on a runqueue (and not running), then |
| 2075 | * it is sufficient to simply update the task's cpu field. |
| 2076 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2077 | if (!p->se.on_rq && !task_running(rq, p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | set_task_cpu(p, dest_cpu); |
| 2079 | return 0; |
| 2080 | } |
| 2081 | |
| 2082 | init_completion(&req->done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | req->task = p; |
| 2084 | req->dest_cpu = dest_cpu; |
| 2085 | list_add(&req->list, &rq->migration_queue); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | return 1; |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * wait_task_inactive - wait for a thread to unschedule. |
| 2092 | * |
| 2093 | * The caller must ensure that the task *will* unschedule sometime soon, |
| 2094 | * else this function might spin for a *long* time. This function can't |
| 2095 | * be called with interrupts off, or it may introduce deadlock with |
| 2096 | * smp_call_function() if an IPI is sent by the same process we are |
| 2097 | * waiting to become inactive. |
| 2098 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2099 | void wait_task_inactive(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | { |
| 2101 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2102 | int running, on_rq; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2103 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2105 | for (;;) { |
| 2106 | /* |
| 2107 | * We do the initial early heuristics without holding |
| 2108 | * any task-queue locks at all. We'll only try to get |
| 2109 | * the runqueue lock when things look like they will |
| 2110 | * work out! |
| 2111 | */ |
| 2112 | rq = task_rq(p); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 2113 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2114 | /* |
| 2115 | * If the task is actively running on another CPU |
| 2116 | * still, just relax and busy-wait without holding |
| 2117 | * any locks. |
| 2118 | * |
| 2119 | * NOTE! Since we don't hold any locks, it's not |
| 2120 | * even sure that "rq" stays as the right runqueue! |
| 2121 | * But we don't care, since "task_running()" will |
| 2122 | * return false if the runqueue has changed and p |
| 2123 | * is actually now running somewhere else! |
| 2124 | */ |
| 2125 | while (task_running(rq, p)) |
| 2126 | cpu_relax(); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 2127 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2128 | /* |
| 2129 | * Ok, time to look more closely! We need the rq |
| 2130 | * lock now, to be *sure*. If we're wrong, we'll |
| 2131 | * just go back and repeat. |
| 2132 | */ |
| 2133 | rq = task_rq_lock(p, &flags); |
| 2134 | running = task_running(rq, p); |
| 2135 | on_rq = p->se.on_rq; |
| 2136 | task_rq_unlock(rq, &flags); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 2137 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2138 | /* |
| 2139 | * Was it really running after all now that we |
| 2140 | * checked with the proper locks actually held? |
| 2141 | * |
| 2142 | * Oops. Go back and try again.. |
| 2143 | */ |
| 2144 | if (unlikely(running)) { |
| 2145 | cpu_relax(); |
| 2146 | continue; |
| 2147 | } |
| 2148 | |
| 2149 | /* |
| 2150 | * It's not enough that it's not actively running, |
| 2151 | * it must be off the runqueue _entirely_, and not |
| 2152 | * preempted! |
| 2153 | * |
| 2154 | * So if it wa still runnable (but just not actively |
| 2155 | * running right now), it's preempted, and we should |
| 2156 | * yield - it could be a while. |
| 2157 | */ |
| 2158 | if (unlikely(on_rq)) { |
| 2159 | schedule_timeout_uninterruptible(1); |
| 2160 | continue; |
| 2161 | } |
| 2162 | |
| 2163 | /* |
| 2164 | * Ahh, all good. It wasn't running, and it wasn't |
| 2165 | * runnable, which means that it will never become |
| 2166 | * running in the future either. We're all done! |
| 2167 | */ |
| 2168 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | /*** |
| 2173 | * kick_process - kick a running thread to enter/exit the kernel |
| 2174 | * @p: the to-be-kicked thread |
| 2175 | * |
| 2176 | * Cause a process which is running on another CPU to enter |
| 2177 | * kernel-mode, without any delay. (to get signals handled.) |
| 2178 | * |
| 2179 | * NOTE: this function doesnt have to take the runqueue lock, |
| 2180 | * because all it wants to ensure is that the remote task enters |
| 2181 | * the kernel. If the IPI races and the task has been migrated |
| 2182 | * to another CPU then no harm is done and the purpose has been |
| 2183 | * achieved as well. |
| 2184 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2185 | void kick_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | { |
| 2187 | int cpu; |
| 2188 | |
| 2189 | preempt_disable(); |
| 2190 | cpu = task_cpu(p); |
| 2191 | if ((cpu != smp_processor_id()) && task_curr(p)) |
| 2192 | smp_send_reschedule(cpu); |
| 2193 | preempt_enable(); |
| 2194 | } |
| 2195 | |
| 2196 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2197 | * Return a low guess at the load of a migration-source cpu weighted |
| 2198 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | * |
| 2200 | * We want to under-estimate the load of migration sources, to |
| 2201 | * balance conservatively. |
| 2202 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2203 | static unsigned long source_load(int cpu, int type) |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 2204 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2205 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2206 | unsigned long total = weighted_cpuload(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2207 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2208 | if (type == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2209 | return total; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2210 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2211 | return min(rq->cpu_load[type-1], total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2215 | * Return a high guess at the load of a migration-target cpu weighted |
| 2216 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2218 | static unsigned long target_load(int cpu, int type) |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 2219 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2220 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2221 | unsigned long total = weighted_cpuload(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2222 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2223 | if (type == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2224 | return total; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2225 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2226 | return max(rq->cpu_load[type-1], total); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | /* |
| 2230 | * Return the average load per task on the cpu's run queue |
| 2231 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2232 | static unsigned long cpu_avg_load_per_task(int cpu) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2233 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2234 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2235 | unsigned long total = weighted_cpuload(cpu); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2236 | unsigned long n = rq->nr_running; |
| 2237 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2238 | return n ? total / n : SCHED_LOAD_SCALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | } |
| 2240 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2241 | /* |
| 2242 | * find_idlest_group finds and returns the least busy CPU group within the |
| 2243 | * domain. |
| 2244 | */ |
| 2245 | static struct sched_group * |
| 2246 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) |
| 2247 | { |
| 2248 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; |
| 2249 | unsigned long min_load = ULONG_MAX, this_load = 0; |
| 2250 | int load_idx = sd->forkexec_idx; |
| 2251 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
| 2252 | |
| 2253 | do { |
| 2254 | unsigned long load, avg_load; |
| 2255 | int local_group; |
| 2256 | int i; |
| 2257 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 2258 | /* Skip over this group if it has no CPUs allowed */ |
| 2259 | if (!cpus_intersects(group->cpumask, p->cpus_allowed)) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2260 | continue; |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 2261 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2262 | local_group = cpu_isset(this_cpu, group->cpumask); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2263 | |
| 2264 | /* Tally up the load of all CPUs in the group */ |
| 2265 | avg_load = 0; |
| 2266 | |
| 2267 | for_each_cpu_mask(i, group->cpumask) { |
| 2268 | /* Bias balancing toward cpus of our domain */ |
| 2269 | if (local_group) |
| 2270 | load = source_load(i, load_idx); |
| 2271 | else |
| 2272 | load = target_load(i, load_idx); |
| 2273 | |
| 2274 | avg_load += load; |
| 2275 | } |
| 2276 | |
| 2277 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2278 | avg_load = sg_div_cpu_power(group, |
| 2279 | avg_load * SCHED_LOAD_SCALE); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2280 | |
| 2281 | if (local_group) { |
| 2282 | this_load = avg_load; |
| 2283 | this = group; |
| 2284 | } else if (avg_load < min_load) { |
| 2285 | min_load = avg_load; |
| 2286 | idlest = group; |
| 2287 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2288 | } while (group = group->next, group != sd->groups); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2289 | |
| 2290 | if (!idlest || 100*this_load < imbalance*min_load) |
| 2291 | return NULL; |
| 2292 | return idlest; |
| 2293 | } |
| 2294 | |
| 2295 | /* |
Satoru Takeuchi | 0feaece | 2006-10-03 01:14:10 -0700 | [diff] [blame] | 2296 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2297 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 2298 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2299 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu, |
| 2300 | cpumask_t *tmp) |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2301 | { |
| 2302 | unsigned long load, min_load = ULONG_MAX; |
| 2303 | int idlest = -1; |
| 2304 | int i; |
| 2305 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 2306 | /* Traverse only the allowed CPUs */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2307 | cpus_and(*tmp, group->cpumask, p->cpus_allowed); |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 2308 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2309 | for_each_cpu_mask(i, *tmp) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2310 | load = weighted_cpuload(i); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2311 | |
| 2312 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 2313 | min_load = load; |
| 2314 | idlest = i; |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | return idlest; |
| 2319 | } |
| 2320 | |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2321 | /* |
| 2322 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 2323 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 2324 | * SD_BALANCE_EXEC. |
| 2325 | * |
| 2326 | * Balance, ie. select the least loaded group. |
| 2327 | * |
| 2328 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 2329 | * |
| 2330 | * preempt must be disabled. |
| 2331 | */ |
| 2332 | static int sched_balance_self(int cpu, int flag) |
| 2333 | { |
| 2334 | struct task_struct *t = current; |
| 2335 | struct sched_domain *tmp, *sd = NULL; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2336 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2337 | for_each_domain(cpu, tmp) { |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2338 | /* |
| 2339 | * If power savings logic is enabled for a domain, stop there. |
| 2340 | */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2341 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) |
| 2342 | break; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2343 | if (tmp->flags & flag) |
| 2344 | sd = tmp; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2345 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2346 | |
| 2347 | while (sd) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2348 | cpumask_t span, tmpmask; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2349 | struct sched_group *group; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2350 | int new_cpu, weight; |
| 2351 | |
| 2352 | if (!(sd->flags & flag)) { |
| 2353 | sd = sd->child; |
| 2354 | continue; |
| 2355 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2356 | |
| 2357 | span = sd->span; |
| 2358 | group = find_idlest_group(sd, t, cpu); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2359 | if (!group) { |
| 2360 | sd = sd->child; |
| 2361 | continue; |
| 2362 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2363 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2364 | new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2365 | if (new_cpu == -1 || new_cpu == cpu) { |
| 2366 | /* Now try balancing at a lower domain level of cpu */ |
| 2367 | sd = sd->child; |
| 2368 | continue; |
| 2369 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2370 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2371 | /* Now try balancing at a lower domain level of new_cpu */ |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2372 | cpu = new_cpu; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2373 | sd = NULL; |
| 2374 | weight = cpus_weight(span); |
| 2375 | for_each_domain(cpu, tmp) { |
| 2376 | if (weight <= cpus_weight(tmp->span)) |
| 2377 | break; |
| 2378 | if (tmp->flags & flag) |
| 2379 | sd = tmp; |
| 2380 | } |
| 2381 | /* while loop will break here if sd == NULL */ |
| 2382 | } |
| 2383 | |
| 2384 | return cpu; |
| 2385 | } |
| 2386 | |
| 2387 | #endif /* CONFIG_SMP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | /*** |
| 2390 | * try_to_wake_up - wake up a thread |
| 2391 | * @p: the to-be-woken-up thread |
| 2392 | * @state: the mask of task states that can be woken |
| 2393 | * @sync: do a synchronous wakeup? |
| 2394 | * |
| 2395 | * Put it on the run-queue if it's not already there. The "current" |
| 2396 | * thread is always on the run-queue (except when the actual |
| 2397 | * re-schedule is in progress), and as such you're allowed to do |
| 2398 | * the simpler "current->state = TASK_RUNNING" to mark yourself |
| 2399 | * runnable without the overhead of this. |
| 2400 | * |
| 2401 | * returns failure only if the task is already active. |
| 2402 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2403 | 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] | 2404 | { |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2405 | int cpu, orig_cpu, this_cpu, success = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | unsigned long flags; |
| 2407 | long old_state; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2408 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | |
Ingo Molnar | b85d066 | 2008-03-16 20:03:22 +0100 | [diff] [blame] | 2410 | if (!sched_feat(SYNC_WAKEUPS)) |
| 2411 | sync = 0; |
| 2412 | |
Linus Torvalds | 04e2f17 | 2008-02-23 18:05:03 -0800 | [diff] [blame] | 2413 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | rq = task_rq_lock(p, &flags); |
| 2415 | old_state = p->state; |
| 2416 | if (!(old_state & state)) |
| 2417 | goto out; |
| 2418 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2419 | if (p->se.on_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | goto out_running; |
| 2421 | |
| 2422 | cpu = task_cpu(p); |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2423 | orig_cpu = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | this_cpu = smp_processor_id(); |
| 2425 | |
| 2426 | #ifdef CONFIG_SMP |
| 2427 | if (unlikely(task_running(rq, p))) |
| 2428 | goto out_activate; |
| 2429 | |
Dmitry Adamushko | 5d2f5a6 | 2008-01-25 21:08:21 +0100 | [diff] [blame] | 2430 | cpu = p->sched_class->select_task_rq(p, sync); |
| 2431 | if (cpu != orig_cpu) { |
| 2432 | set_task_cpu(p, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | task_rq_unlock(rq, &flags); |
| 2434 | /* might preempt at this point */ |
| 2435 | rq = task_rq_lock(p, &flags); |
| 2436 | old_state = p->state; |
| 2437 | if (!(old_state & state)) |
| 2438 | goto out; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2439 | if (p->se.on_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | goto out_running; |
| 2441 | |
| 2442 | this_cpu = smp_processor_id(); |
| 2443 | cpu = task_cpu(p); |
| 2444 | } |
| 2445 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2446 | #ifdef CONFIG_SCHEDSTATS |
| 2447 | schedstat_inc(rq, ttwu_count); |
| 2448 | if (cpu == this_cpu) |
| 2449 | schedstat_inc(rq, ttwu_local); |
| 2450 | else { |
| 2451 | struct sched_domain *sd; |
| 2452 | for_each_domain(this_cpu, sd) { |
| 2453 | if (cpu_isset(cpu, sd->span)) { |
| 2454 | schedstat_inc(sd, ttwu_wake_remote); |
| 2455 | break; |
| 2456 | } |
| 2457 | } |
| 2458 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2459 | #endif |
| 2460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | out_activate: |
| 2462 | #endif /* CONFIG_SMP */ |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2463 | schedstat_inc(p, se.nr_wakeups); |
| 2464 | if (sync) |
| 2465 | schedstat_inc(p, se.nr_wakeups_sync); |
| 2466 | if (orig_cpu != cpu) |
| 2467 | schedstat_inc(p, se.nr_wakeups_migrate); |
| 2468 | if (cpu == this_cpu) |
| 2469 | schedstat_inc(p, se.nr_wakeups_local); |
| 2470 | else |
| 2471 | schedstat_inc(p, se.nr_wakeups_remote); |
Ingo Molnar | 2daa357 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 2472 | update_rq_clock(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2473 | activate_task(rq, p, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | success = 1; |
| 2475 | |
| 2476 | out_running: |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 2477 | check_preempt_curr(rq, p); |
| 2478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | p->state = TASK_RUNNING; |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2480 | #ifdef CONFIG_SMP |
| 2481 | if (p->sched_class->task_wake_up) |
| 2482 | p->sched_class->task_wake_up(rq, p); |
| 2483 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | out: |
| 2485 | task_rq_unlock(rq, &flags); |
| 2486 | |
| 2487 | return success; |
| 2488 | } |
| 2489 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2490 | int wake_up_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 2492 | return try_to_wake_up(p, TASK_ALL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | EXPORT_SYMBOL(wake_up_process); |
| 2495 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2496 | int wake_up_state(struct task_struct *p, unsigned int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | { |
| 2498 | return try_to_wake_up(p, state, 0); |
| 2499 | } |
| 2500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | /* |
| 2502 | * Perform scheduler related setup for a newly forked process p. |
| 2503 | * p is forked by current. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2504 | * |
| 2505 | * __sched_fork() is basic setup used by init_idle() too: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2507 | static void __sched_fork(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2509 | p->se.exec_start = 0; |
| 2510 | p->se.sum_exec_runtime = 0; |
Ingo Molnar | f6cf891 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 2511 | p->se.prev_sum_exec_runtime = 0; |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 2512 | p->se.last_wakeup = 0; |
| 2513 | p->se.avg_overlap = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2514 | |
| 2515 | #ifdef CONFIG_SCHEDSTATS |
| 2516 | p->se.wait_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2517 | p->se.sum_sleep_runtime = 0; |
| 2518 | p->se.sleep_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2519 | p->se.block_start = 0; |
| 2520 | p->se.sleep_max = 0; |
| 2521 | p->se.block_max = 0; |
| 2522 | p->se.exec_max = 0; |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 2523 | p->se.slice_max = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2524 | p->se.wait_max = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2525 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2526 | |
Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 2527 | INIT_LIST_HEAD(&p->rt.run_list); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2528 | p->se.on_rq = 0; |
Peter Zijlstra | 4a55bd5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 2529 | INIT_LIST_HEAD(&p->se.group_node); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2530 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2531 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 2532 | INIT_HLIST_HEAD(&p->preempt_notifiers); |
| 2533 | #endif |
| 2534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | /* |
| 2536 | * We mark the process as running here, but have not actually |
| 2537 | * inserted it onto the runqueue yet. This guarantees that |
| 2538 | * nobody will actually run it, and a signal or other external |
| 2539 | * event cannot wake it up and insert it on the runqueue either. |
| 2540 | */ |
| 2541 | p->state = TASK_RUNNING; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | /* |
| 2545 | * fork()/clone()-time setup: |
| 2546 | */ |
| 2547 | void sched_fork(struct task_struct *p, int clone_flags) |
| 2548 | { |
| 2549 | int cpu = get_cpu(); |
| 2550 | |
| 2551 | __sched_fork(p); |
| 2552 | |
| 2553 | #ifdef CONFIG_SMP |
| 2554 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); |
| 2555 | #endif |
Ingo Molnar | 02e4bac2 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 2556 | set_task_cpu(p, cpu); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2557 | |
| 2558 | /* |
| 2559 | * Make sure we do not leak PI boosting priority to the child: |
| 2560 | */ |
| 2561 | p->prio = current->normal_prio; |
Hiroshi Shimamoto | 2ddbf95 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 2562 | if (!rt_prio(p->prio)) |
| 2563 | p->sched_class = &fair_sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2564 | |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 2565 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2566 | if (likely(sched_info_on())) |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 2567 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | #endif |
Chen, Kenneth W | d6077cb | 2006-02-14 13:53:10 -0800 | [diff] [blame] | 2569 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2570 | p->oncpu = 0; |
| 2571 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | #ifdef CONFIG_PREEMPT |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2573 | /* Want to start with kernel preemption disabled. */ |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 2574 | task_thread_info(p)->preempt_count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2576 | put_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | /* |
| 2580 | * wake_up_new_task - wake up a newly created task for the first time. |
| 2581 | * |
| 2582 | * This function will do some initial scheduler statistics housekeeping |
| 2583 | * that must be done for every newly created context, then puts the task |
| 2584 | * on the runqueue and wakes it. |
| 2585 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2586 | void wake_up_new_task(struct task_struct *p, unsigned long clone_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | { |
| 2588 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2589 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | |
| 2591 | rq = task_rq_lock(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | BUG_ON(p->state != TASK_RUNNING); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 2593 | update_rq_clock(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | |
| 2595 | p->prio = effective_prio(p); |
| 2596 | |
Srivatsa Vaddagiri | b9dca1e | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2597 | if (!p->sched_class->task_new || !current->se.on_rq) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2598 | activate_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2601 | * Let the scheduling class do new task startup |
| 2602 | * management (if any): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | */ |
Ingo Molnar | ee0827d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2604 | p->sched_class->task_new(rq, p); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 2605 | inc_nr_running(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2607 | check_preempt_curr(rq, p); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2608 | #ifdef CONFIG_SMP |
| 2609 | if (p->sched_class->task_wake_up) |
| 2610 | p->sched_class->task_wake_up(rq, p); |
| 2611 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2612 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | } |
| 2614 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2615 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 2616 | |
| 2617 | /** |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2618 | * preempt_notifier_register - tell me when current is being being preempted & rescheduled |
| 2619 | * @notifier: notifier struct to register |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2620 | */ |
| 2621 | void preempt_notifier_register(struct preempt_notifier *notifier) |
| 2622 | { |
| 2623 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); |
| 2624 | } |
| 2625 | EXPORT_SYMBOL_GPL(preempt_notifier_register); |
| 2626 | |
| 2627 | /** |
| 2628 | * preempt_notifier_unregister - no longer interested in preemption notifications |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2629 | * @notifier: notifier struct to unregister |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2630 | * |
| 2631 | * This is safe to call from within a preemption notifier. |
| 2632 | */ |
| 2633 | void preempt_notifier_unregister(struct preempt_notifier *notifier) |
| 2634 | { |
| 2635 | hlist_del(¬ifier->link); |
| 2636 | } |
| 2637 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); |
| 2638 | |
| 2639 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 2640 | { |
| 2641 | struct preempt_notifier *notifier; |
| 2642 | struct hlist_node *node; |
| 2643 | |
| 2644 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) |
| 2645 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
| 2646 | } |
| 2647 | |
| 2648 | static void |
| 2649 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 2650 | struct task_struct *next) |
| 2651 | { |
| 2652 | struct preempt_notifier *notifier; |
| 2653 | struct hlist_node *node; |
| 2654 | |
| 2655 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) |
| 2656 | notifier->ops->sched_out(notifier, next); |
| 2657 | } |
| 2658 | |
| 2659 | #else |
| 2660 | |
| 2661 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 2662 | { |
| 2663 | } |
| 2664 | |
| 2665 | static void |
| 2666 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 2667 | struct task_struct *next) |
| 2668 | { |
| 2669 | } |
| 2670 | |
| 2671 | #endif |
| 2672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | /** |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2674 | * prepare_task_switch - prepare to switch tasks |
| 2675 | * @rq: the runqueue preparing to switch |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2676 | * @prev: the current task that is being switched out |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2677 | * @next: the task we are going to switch to. |
| 2678 | * |
| 2679 | * This is called with the rq lock held and interrupts off. It must |
| 2680 | * be paired with a subsequent finish_task_switch after the context |
| 2681 | * switch. |
| 2682 | * |
| 2683 | * prepare_task_switch sets up locking and calls architecture specific |
| 2684 | * hooks. |
| 2685 | */ |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2686 | static inline void |
| 2687 | prepare_task_switch(struct rq *rq, struct task_struct *prev, |
| 2688 | struct task_struct *next) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2689 | { |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2690 | fire_sched_out_preempt_notifiers(prev, next); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2691 | prepare_lock_switch(rq, next); |
| 2692 | prepare_arch_switch(next); |
| 2693 | } |
| 2694 | |
| 2695 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | * finish_task_switch - clean up after a task-switch |
Jeff Garzik | 344baba | 2005-09-07 01:15:17 -0400 | [diff] [blame] | 2697 | * @rq: runqueue associated with task-switch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | * @prev: the thread we just switched away from. |
| 2699 | * |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2700 | * finish_task_switch must be called after the context switch, paired |
| 2701 | * with a prepare_task_switch call before the context switch. |
| 2702 | * finish_task_switch will reconcile locking set up by prepare_task_switch, |
| 2703 | * and do any other architecture-specific cleanup actions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | * |
| 2705 | * Note that we may have delayed dropping an mm in context_switch(). If |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 2706 | * so, we finish that here outside of the runqueue lock. (Doing it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | * with the lock held can cause deadlocks; see schedule() for |
| 2708 | * details.) |
| 2709 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2710 | static void finish_task_switch(struct rq *rq, struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | __releases(rq->lock) |
| 2712 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | struct mm_struct *mm = rq->prev_mm; |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 2714 | long prev_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | |
| 2716 | rq->prev_mm = NULL; |
| 2717 | |
| 2718 | /* |
| 2719 | * A task struct has one reference for the use as "current". |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2720 | * 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] | 2721 | * schedule one last time. The schedule call will never return, and |
| 2722 | * the scheduled task must drop that reference. |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2723 | * The test for TASK_DEAD must occur while the runqueue locks are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | * still held, otherwise prev could be scheduled on another cpu, die |
| 2725 | * there before we look at prev->state, and then the reference would |
| 2726 | * be dropped twice. |
| 2727 | * Manfred Spraul <manfred@colorfullife.com> |
| 2728 | */ |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 2729 | prev_state = prev->state; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2730 | finish_arch_switch(prev); |
| 2731 | finish_lock_switch(rq, prev); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2732 | #ifdef CONFIG_SMP |
| 2733 | if (current->sched_class->post_schedule) |
| 2734 | current->sched_class->post_schedule(rq); |
| 2735 | #endif |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2736 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2737 | fire_sched_in_preempt_notifiers(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | if (mm) |
| 2739 | mmdrop(mm); |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2740 | if (unlikely(prev_state == TASK_DEAD)) { |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2741 | /* |
| 2742 | * Remove function-return probe instances associated with this |
| 2743 | * task and put them back on the free list. |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2744 | */ |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2745 | kprobe_flush_task(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | put_task_struct(prev); |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2747 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | /** |
| 2751 | * schedule_tail - first thing a freshly forked thread must call. |
| 2752 | * @prev: the thread we just switched away from. |
| 2753 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2754 | asmlinkage void schedule_tail(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | __releases(rq->lock) |
| 2756 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2757 | struct rq *rq = this_rq(); |
| 2758 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2759 | finish_task_switch(rq, prev); |
| 2760 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW |
| 2761 | /* In this case, finish_task_switch does not reenable preemption */ |
| 2762 | preempt_enable(); |
| 2763 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | if (current->set_child_tid) |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2765 | put_user(task_pid_vnr(current), current->set_child_tid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | /* |
| 2769 | * context_switch - switch to the new MM and the new |
| 2770 | * thread's register state. |
| 2771 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2772 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2773 | context_switch(struct rq *rq, struct task_struct *prev, |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2774 | struct task_struct *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2776 | struct mm_struct *mm, *oldmm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2778 | prepare_task_switch(rq, prev, next); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2779 | mm = next->mm; |
| 2780 | oldmm = prev->active_mm; |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 2781 | /* |
| 2782 | * For paravirt, this is coupled with an exit in switch_to to |
| 2783 | * combine the page table reload and the switch backend into |
| 2784 | * one hypercall. |
| 2785 | */ |
| 2786 | arch_enter_lazy_cpu_mode(); |
| 2787 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2788 | if (unlikely(!mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | next->active_mm = oldmm; |
| 2790 | atomic_inc(&oldmm->mm_count); |
| 2791 | enter_lazy_tlb(oldmm, next); |
| 2792 | } else |
| 2793 | switch_mm(oldmm, mm, next); |
| 2794 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2795 | if (unlikely(!prev->mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | prev->active_mm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | rq->prev_mm = oldmm; |
| 2798 | } |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 2799 | /* |
| 2800 | * Since the runqueue lock will be released by the next |
| 2801 | * task (which is an invalid locking op but in the case |
| 2802 | * of the scheduler it's an obvious special-case), so we |
| 2803 | * do an early lockdep release here: |
| 2804 | */ |
| 2805 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 2806 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 2807 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | |
| 2809 | /* Here we just switch the register state and the stack. */ |
| 2810 | switch_to(prev, next, prev); |
| 2811 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2812 | barrier(); |
| 2813 | /* |
| 2814 | * this_rq must be evaluated again because prev may have moved |
| 2815 | * CPUs since it called schedule(), thus the 'rq' on its stack |
| 2816 | * frame will be invalid. |
| 2817 | */ |
| 2818 | finish_task_switch(this_rq(), prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | /* |
| 2822 | * nr_running, nr_uninterruptible and nr_context_switches: |
| 2823 | * |
| 2824 | * externally visible scheduler statistics: current number of runnable |
| 2825 | * threads, current number of uninterruptible-sleeping threads, total |
| 2826 | * number of context switches performed since bootup. |
| 2827 | */ |
| 2828 | unsigned long nr_running(void) |
| 2829 | { |
| 2830 | unsigned long i, sum = 0; |
| 2831 | |
| 2832 | for_each_online_cpu(i) |
| 2833 | sum += cpu_rq(i)->nr_running; |
| 2834 | |
| 2835 | return sum; |
| 2836 | } |
| 2837 | |
| 2838 | unsigned long nr_uninterruptible(void) |
| 2839 | { |
| 2840 | unsigned long i, sum = 0; |
| 2841 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2842 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | sum += cpu_rq(i)->nr_uninterruptible; |
| 2844 | |
| 2845 | /* |
| 2846 | * Since we read the counters lockless, it might be slightly |
| 2847 | * inaccurate. Do not allow it to go below zero though: |
| 2848 | */ |
| 2849 | if (unlikely((long)sum < 0)) |
| 2850 | sum = 0; |
| 2851 | |
| 2852 | return sum; |
| 2853 | } |
| 2854 | |
| 2855 | unsigned long long nr_context_switches(void) |
| 2856 | { |
Steven Rostedt | cc94abf | 2006-06-27 02:54:31 -0700 | [diff] [blame] | 2857 | int i; |
| 2858 | unsigned long long sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2860 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | sum += cpu_rq(i)->nr_switches; |
| 2862 | |
| 2863 | return sum; |
| 2864 | } |
| 2865 | |
| 2866 | unsigned long nr_iowait(void) |
| 2867 | { |
| 2868 | unsigned long i, sum = 0; |
| 2869 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2870 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
| 2872 | |
| 2873 | return sum; |
| 2874 | } |
| 2875 | |
Jack Steiner | db1b1fe | 2006-03-31 02:31:21 -0800 | [diff] [blame] | 2876 | unsigned long nr_active(void) |
| 2877 | { |
| 2878 | unsigned long i, running = 0, uninterruptible = 0; |
| 2879 | |
| 2880 | for_each_online_cpu(i) { |
| 2881 | running += cpu_rq(i)->nr_running; |
| 2882 | uninterruptible += cpu_rq(i)->nr_uninterruptible; |
| 2883 | } |
| 2884 | |
| 2885 | if (unlikely((long)uninterruptible < 0)) |
| 2886 | uninterruptible = 0; |
| 2887 | |
| 2888 | return running + uninterruptible; |
| 2889 | } |
| 2890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2892 | * Update rq->cpu_load[] statistics. This function is usually called every |
| 2893 | * scheduler tick (TICK_NSEC). |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2894 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2895 | static void update_cpu_load(struct rq *this_rq) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2896 | { |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 2897 | unsigned long this_load = this_rq->load.weight; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2898 | int i, scale; |
| 2899 | |
| 2900 | this_rq->nr_load_updates++; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2901 | |
| 2902 | /* Update our load: */ |
| 2903 | for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { |
| 2904 | unsigned long old_load, new_load; |
| 2905 | |
| 2906 | /* scale is effectively 1 << i now, and >> i divides by scale */ |
| 2907 | |
| 2908 | old_load = this_rq->cpu_load[i]; |
| 2909 | new_load = this_load; |
Ingo Molnar | a25707f | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 2910 | /* |
| 2911 | * Round up the averaging division if load is increasing. This |
| 2912 | * prevents us from getting stuck on 9 if the load is 10, for |
| 2913 | * example. |
| 2914 | */ |
| 2915 | if (new_load > old_load) |
| 2916 | new_load += scale-1; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2917 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; |
| 2918 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2921 | #ifdef CONFIG_SMP |
| 2922 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2923 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | * double_rq_lock - safely lock two runqueues |
| 2925 | * |
| 2926 | * Note this does not disable interrupts like task_rq_lock, |
| 2927 | * you need to do so manually before calling. |
| 2928 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2929 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | __acquires(rq1->lock) |
| 2931 | __acquires(rq2->lock) |
| 2932 | { |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 2933 | BUG_ON(!irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | if (rq1 == rq2) { |
| 2935 | spin_lock(&rq1->lock); |
| 2936 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 2937 | } else { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2938 | if (rq1 < rq2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | spin_lock(&rq1->lock); |
| 2940 | spin_lock(&rq2->lock); |
| 2941 | } else { |
| 2942 | spin_lock(&rq2->lock); |
| 2943 | spin_lock(&rq1->lock); |
| 2944 | } |
| 2945 | } |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 2946 | update_rq_clock(rq1); |
| 2947 | update_rq_clock(rq2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | /* |
| 2951 | * double_rq_unlock - safely unlock two runqueues |
| 2952 | * |
| 2953 | * Note this does not restore interrupts like task_rq_unlock, |
| 2954 | * you need to do so manually after calling. |
| 2955 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2956 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | __releases(rq1->lock) |
| 2958 | __releases(rq2->lock) |
| 2959 | { |
| 2960 | spin_unlock(&rq1->lock); |
| 2961 | if (rq1 != rq2) |
| 2962 | spin_unlock(&rq2->lock); |
| 2963 | else |
| 2964 | __release(rq2->lock); |
| 2965 | } |
| 2966 | |
| 2967 | /* |
| 2968 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. |
| 2969 | */ |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2970 | static int double_lock_balance(struct rq *this_rq, struct rq *busiest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | __releases(this_rq->lock) |
| 2972 | __acquires(busiest->lock) |
| 2973 | __acquires(this_rq->lock) |
| 2974 | { |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2975 | int ret = 0; |
| 2976 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 2977 | if (unlikely(!irqs_disabled())) { |
| 2978 | /* printk() doesn't work good under rq->lock */ |
| 2979 | spin_unlock(&this_rq->lock); |
| 2980 | BUG_ON(1); |
| 2981 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | if (unlikely(!spin_trylock(&busiest->lock))) { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2983 | if (busiest < this_rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | spin_unlock(&this_rq->lock); |
| 2985 | spin_lock(&busiest->lock); |
| 2986 | spin_lock(&this_rq->lock); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2987 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | } else |
| 2989 | spin_lock(&busiest->lock); |
| 2990 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2991 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | * If dest_cpu is allowed for this process, migrate the task to it. |
| 2996 | * This is accomplished by forcing the cpu_allowed mask to only |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 2997 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | * the cpu_allowed mask is restored. |
| 2999 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3000 | static void sched_migrate_task(struct task_struct *p, int dest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3002 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3004 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | |
| 3006 | rq = task_rq_lock(p, &flags); |
| 3007 | if (!cpu_isset(dest_cpu, p->cpus_allowed) |
| 3008 | || unlikely(cpu_is_offline(dest_cpu))) |
| 3009 | goto out; |
| 3010 | |
| 3011 | /* force the process onto the specified CPU */ |
| 3012 | if (migrate_task(p, dest_cpu, &req)) { |
| 3013 | /* Need to wait for migration thread (might exit: take ref). */ |
| 3014 | struct task_struct *mt = rq->migration_thread; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | get_task_struct(mt); |
| 3017 | task_rq_unlock(rq, &flags); |
| 3018 | wake_up_process(mt); |
| 3019 | put_task_struct(mt); |
| 3020 | wait_for_completion(&req.done); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | return; |
| 3023 | } |
| 3024 | out: |
| 3025 | task_rq_unlock(rq, &flags); |
| 3026 | } |
| 3027 | |
| 3028 | /* |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 3029 | * sched_exec - execve() is a valuable balancing opportunity, because at |
| 3030 | * this point the task has the smallest effective memory and cache footprint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | */ |
| 3032 | void sched_exec(void) |
| 3033 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | int new_cpu, this_cpu = get_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 3035 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | put_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 3037 | if (new_cpu != this_cpu) |
| 3038 | sched_migrate_task(current, new_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | /* |
| 3042 | * pull_task - move a task from a remote runqueue to the local runqueue. |
| 3043 | * Both runqueues must be locked. |
| 3044 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3045 | static void pull_task(struct rq *src_rq, struct task_struct *p, |
| 3046 | struct rq *this_rq, int this_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 3048 | deactivate_task(src_rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | set_task_cpu(p, this_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3050 | activate_task(this_rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | /* |
| 3052 | * Note that idle threads have a prio of MAX_PRIO, for this test |
| 3053 | * to be always true for them. |
| 3054 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3055 | check_preempt_curr(this_rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | /* |
| 3059 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 3060 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3061 | static |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3062 | 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] | 3063 | struct sched_domain *sd, enum cpu_idle_type idle, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 3064 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | { |
| 3066 | /* |
| 3067 | * We do not migrate tasks that are: |
| 3068 | * 1) running (obviously), or |
| 3069 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 3070 | * 3) are cache-hot on their current CPU. |
| 3071 | */ |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3072 | if (!cpu_isset(this_cpu, p->cpus_allowed)) { |
| 3073 | schedstat_inc(p, se.nr_failed_migrations_affine); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3075 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3076 | *all_pinned = 0; |
| 3077 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3078 | if (task_running(rq, p)) { |
| 3079 | schedstat_inc(p, se.nr_failed_migrations_running); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3080 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3081 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3083 | /* |
| 3084 | * Aggressive migration if: |
| 3085 | * 1) task is cache cold, or |
| 3086 | * 2) too many balance attempts have failed. |
| 3087 | */ |
| 3088 | |
Ingo Molnar | 6bc1665 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3089 | if (!task_hot(p, rq->clock, sd) || |
| 3090 | sd->nr_balance_failed > sd->cache_nice_tries) { |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3091 | #ifdef CONFIG_SCHEDSTATS |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3092 | if (task_hot(p, rq->clock, sd)) { |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3093 | schedstat_inc(sd, lb_hot_gained[idle]); |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3094 | schedstat_inc(p, se.nr_forced_migrations); |
| 3095 | } |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3096 | #endif |
| 3097 | return 1; |
| 3098 | } |
| 3099 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3100 | if (task_hot(p, rq->clock, sd)) { |
| 3101 | schedstat_inc(p, se.nr_failed_migrations_hot); |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3102 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | return 1; |
| 3105 | } |
| 3106 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3107 | static unsigned long |
| 3108 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 3109 | unsigned long max_load_move, struct sched_domain *sd, |
| 3110 | enum cpu_idle_type idle, int *all_pinned, |
| 3111 | int *this_best_prio, struct rq_iterator *iterator) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3112 | { |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 3113 | int loops = 0, pulled = 0, pinned = 0, skip_for_load; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3114 | struct task_struct *p; |
| 3115 | long rem_load_move = max_load_move; |
| 3116 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3117 | if (max_load_move == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3118 | goto out; |
| 3119 | |
| 3120 | pinned = 1; |
| 3121 | |
| 3122 | /* |
| 3123 | * Start the load-balancing iterator: |
| 3124 | */ |
| 3125 | p = iterator->start(iterator->arg); |
| 3126 | next: |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 3127 | if (!p || loops++ > sysctl_sched_nr_migrate) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3128 | goto out; |
| 3129 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 3130 | * To help distribute high priority tasks across CPUs we don't |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3131 | * skip a task if it will be the highest priority task (i.e. smallest |
| 3132 | * prio value) on its new queue regardless of its load weight |
| 3133 | */ |
| 3134 | skip_for_load = (p->se.load.weight >> 1) > rem_load_move + |
| 3135 | SCHED_LOAD_SCALE_FUZZ; |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3136 | if ((skip_for_load && p->prio >= *this_best_prio) || |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3137 | !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3138 | p = iterator->next(iterator->arg); |
| 3139 | goto next; |
| 3140 | } |
| 3141 | |
| 3142 | pull_task(busiest, p, this_rq, this_cpu); |
| 3143 | pulled++; |
| 3144 | rem_load_move -= p->se.load.weight; |
| 3145 | |
| 3146 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 3147 | * We only want to steal up to the prescribed amount of weighted load. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3148 | */ |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3149 | if (rem_load_move > 0) { |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3150 | if (p->prio < *this_best_prio) |
| 3151 | *this_best_prio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3152 | p = iterator->next(iterator->arg); |
| 3153 | goto next; |
| 3154 | } |
| 3155 | out: |
| 3156 | /* |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3157 | * Right now, this is one of only two places pull_task() is called, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3158 | * so we can safely collect pull_task() stats here rather than |
| 3159 | * inside pull_task(). |
| 3160 | */ |
| 3161 | schedstat_add(sd, lb_gained[idle], pulled); |
| 3162 | |
| 3163 | if (all_pinned) |
| 3164 | *all_pinned = pinned; |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3165 | |
| 3166 | return max_load_move - rem_load_move; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3167 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | /* |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3170 | * move_tasks tries to move up to max_load_move weighted load from busiest to |
| 3171 | * this_rq, as part of a balancing operation within domain "sd". |
| 3172 | * Returns 1 if successful and 0 otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | * |
| 3174 | * Called with both runqueues locked. |
| 3175 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3176 | static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3177 | unsigned long max_load_move, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3178 | struct sched_domain *sd, enum cpu_idle_type idle, |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3179 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3181 | const struct sched_class *class = sched_class_highest; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3182 | unsigned long total_load_moved = 0; |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3183 | int this_best_prio = this_rq->curr->prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3185 | do { |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3186 | total_load_moved += |
| 3187 | class->load_balance(this_rq, this_cpu, busiest, |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3188 | max_load_move - total_load_moved, |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3189 | sd, idle, all_pinned, &this_best_prio); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3190 | class = class->next; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3191 | } while (class && max_load_move > total_load_moved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3193 | return total_load_moved > 0; |
| 3194 | } |
| 3195 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3196 | static int |
| 3197 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 3198 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 3199 | struct rq_iterator *iterator) |
| 3200 | { |
| 3201 | struct task_struct *p = iterator->start(iterator->arg); |
| 3202 | int pinned = 0; |
| 3203 | |
| 3204 | while (p) { |
| 3205 | if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
| 3206 | pull_task(busiest, p, this_rq, this_cpu); |
| 3207 | /* |
| 3208 | * Right now, this is only the second place pull_task() |
| 3209 | * is called, so we can safely collect pull_task() |
| 3210 | * stats here rather than inside pull_task(). |
| 3211 | */ |
| 3212 | schedstat_inc(sd, lb_gained[idle]); |
| 3213 | |
| 3214 | return 1; |
| 3215 | } |
| 3216 | p = iterator->next(iterator->arg); |
| 3217 | } |
| 3218 | |
| 3219 | return 0; |
| 3220 | } |
| 3221 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3222 | /* |
| 3223 | * move_one_task tries to move exactly one task from busiest to this_rq, as |
| 3224 | * part of active balancing operations within "domain". |
| 3225 | * Returns 1 if successful and 0 otherwise. |
| 3226 | * |
| 3227 | * Called with both runqueues locked. |
| 3228 | */ |
| 3229 | static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 3230 | struct sched_domain *sd, enum cpu_idle_type idle) |
| 3231 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3232 | const struct sched_class *class; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3233 | |
| 3234 | for (class = sched_class_highest; class; class = class->next) |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3235 | if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle)) |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3236 | return 1; |
| 3237 | |
| 3238 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | /* |
| 3242 | * find_busiest_group finds and returns the busiest CPU group within the |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3243 | * domain. It calculates and returns the amount of weighted load which |
| 3244 | * should be moved to restore balance via the imbalance parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | */ |
| 3246 | static struct sched_group * |
| 3247 | find_busiest_group(struct sched_domain *sd, int this_cpu, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3248 | unsigned long *imbalance, enum cpu_idle_type idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3249 | int *sd_idle, const cpumask_t *cpus, int *balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | { |
| 3251 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; |
| 3252 | 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] | 3253 | unsigned long max_pull; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3254 | unsigned long busiest_load_per_task, busiest_nr_running; |
| 3255 | unsigned long this_load_per_task, this_nr_running; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3256 | int load_idx, group_imb = 0; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3257 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 3258 | int power_savings_balance = 1; |
| 3259 | unsigned long leader_nr_running = 0, min_load_per_task = 0; |
| 3260 | unsigned long min_nr_running = ULONG_MAX; |
| 3261 | struct sched_group *group_min = NULL, *group_leader = NULL; |
| 3262 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | |
| 3264 | max_load = this_load = total_load = total_pwr = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3265 | busiest_load_per_task = busiest_nr_running = 0; |
| 3266 | this_load_per_task = this_nr_running = 0; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3267 | if (idle == CPU_NOT_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 3268 | load_idx = sd->busy_idx; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3269 | else if (idle == CPU_NEWLY_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 3270 | load_idx = sd->newidle_idx; |
| 3271 | else |
| 3272 | load_idx = sd->idle_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | |
| 3274 | do { |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3275 | unsigned long load, group_capacity, max_cpu_load, min_cpu_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | int local_group; |
| 3277 | int i; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3278 | int __group_imb = 0; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3279 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3280 | unsigned long sum_nr_running, sum_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | |
| 3282 | local_group = cpu_isset(this_cpu, group->cpumask); |
| 3283 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3284 | if (local_group) |
| 3285 | balance_cpu = first_cpu(group->cpumask); |
| 3286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | /* Tally up the load of all CPUs in the group */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3288 | sum_weighted_load = sum_nr_running = avg_load = 0; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3289 | max_cpu_load = 0; |
| 3290 | min_cpu_load = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | |
| 3292 | for_each_cpu_mask(i, group->cpumask) { |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3293 | struct rq *rq; |
| 3294 | |
| 3295 | if (!cpu_isset(i, *cpus)) |
| 3296 | continue; |
| 3297 | |
| 3298 | rq = cpu_rq(i); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3299 | |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3300 | if (*sd_idle && rq->nr_running) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3301 | *sd_idle = 0; |
| 3302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | /* Bias balancing toward cpus of our domain */ |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3304 | if (local_group) { |
| 3305 | if (idle_cpu(i) && !first_idle_cpu) { |
| 3306 | first_idle_cpu = 1; |
| 3307 | balance_cpu = i; |
| 3308 | } |
| 3309 | |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 3310 | load = target_load(i, load_idx); |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3311 | } else { |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 3312 | load = source_load(i, load_idx); |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3313 | if (load > max_cpu_load) |
| 3314 | max_cpu_load = load; |
| 3315 | if (min_cpu_load > load) |
| 3316 | min_cpu_load = load; |
| 3317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | |
| 3319 | avg_load += load; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3320 | sum_nr_running += rq->nr_running; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3321 | sum_weighted_load += weighted_cpuload(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | } |
| 3323 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3324 | /* |
| 3325 | * First idle cpu or the first cpu(busiest) in this sched group |
| 3326 | * is eligible for doing load balancing at this and above |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3327 | * domains. In the newly idle case, we will allow all the cpu's |
| 3328 | * to do the newly idle load balance. |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3329 | */ |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3330 | if (idle != CPU_NEWLY_IDLE && local_group && |
| 3331 | balance_cpu != this_cpu && balance) { |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3332 | *balance = 0; |
| 3333 | goto ret; |
| 3334 | } |
| 3335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | total_load += avg_load; |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3337 | total_pwr += group->__cpu_power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | |
| 3339 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3340 | avg_load = sg_div_cpu_power(group, |
| 3341 | avg_load * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3343 | if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE) |
| 3344 | __group_imb = 1; |
| 3345 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3346 | group_capacity = group->__cpu_power / SCHED_LOAD_SCALE; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | if (local_group) { |
| 3349 | this_load = avg_load; |
| 3350 | this = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3351 | this_nr_running = sum_nr_running; |
| 3352 | this_load_per_task = sum_weighted_load; |
| 3353 | } else if (avg_load > max_load && |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3354 | (sum_nr_running > group_capacity || __group_imb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3355 | max_load = avg_load; |
| 3356 | busiest = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3357 | busiest_nr_running = sum_nr_running; |
| 3358 | busiest_load_per_task = sum_weighted_load; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3359 | group_imb = __group_imb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3361 | |
| 3362 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 3363 | /* |
| 3364 | * Busy processors will not participate in power savings |
| 3365 | * balance. |
| 3366 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3367 | if (idle == CPU_NOT_IDLE || |
| 3368 | !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
| 3369 | goto group_next; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3370 | |
| 3371 | /* |
| 3372 | * If the local group is idle or completely loaded |
| 3373 | * no need to do power savings balance at this domain |
| 3374 | */ |
| 3375 | if (local_group && (this_nr_running >= group_capacity || |
| 3376 | !this_nr_running)) |
| 3377 | power_savings_balance = 0; |
| 3378 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3379 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3380 | * If a group is already running at full capacity or idle, |
| 3381 | * don't include that group in power savings calculations |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3382 | */ |
| 3383 | if (!power_savings_balance || sum_nr_running >= group_capacity |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3384 | || !sum_nr_running) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3385 | goto group_next; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3386 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3387 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3388 | * Calculate the group which has the least non-idle load. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3389 | * This is the group from where we need to pick up the load |
| 3390 | * for saving power |
| 3391 | */ |
| 3392 | if ((sum_nr_running < min_nr_running) || |
| 3393 | (sum_nr_running == min_nr_running && |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3394 | first_cpu(group->cpumask) < |
| 3395 | first_cpu(group_min->cpumask))) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3396 | group_min = group; |
| 3397 | min_nr_running = sum_nr_running; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3398 | min_load_per_task = sum_weighted_load / |
| 3399 | sum_nr_running; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3400 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3401 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3402 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3403 | * Calculate the group which is almost near its |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3404 | * capacity but still has some space to pick up some load |
| 3405 | * from other group and save more power |
| 3406 | */ |
| 3407 | if (sum_nr_running <= group_capacity - 1) { |
| 3408 | if (sum_nr_running > leader_nr_running || |
| 3409 | (sum_nr_running == leader_nr_running && |
| 3410 | first_cpu(group->cpumask) > |
| 3411 | first_cpu(group_leader->cpumask))) { |
| 3412 | group_leader = group; |
| 3413 | leader_nr_running = sum_nr_running; |
| 3414 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3415 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3416 | group_next: |
| 3417 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | group = group->next; |
| 3419 | } while (group != sd->groups); |
| 3420 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3421 | if (!busiest || this_load >= max_load || busiest_nr_running == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | goto out_balanced; |
| 3423 | |
| 3424 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; |
| 3425 | |
| 3426 | if (this_load >= avg_load || |
| 3427 | 100*max_load <= sd->imbalance_pct*this_load) |
| 3428 | goto out_balanced; |
| 3429 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3430 | busiest_load_per_task /= busiest_nr_running; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3431 | if (group_imb) |
| 3432 | busiest_load_per_task = min(busiest_load_per_task, avg_load); |
| 3433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3434 | /* |
| 3435 | * We're trying to get all the cpus to the average_load, so we don't |
| 3436 | * want to push ourselves above the average load, nor do we wish to |
| 3437 | * reduce the max loaded cpu below the average load, as either of these |
| 3438 | * actions would just result in more rebalancing later, and ping-pong |
| 3439 | * tasks around. Thus we look for the minimum possible imbalance. |
| 3440 | * Negative imbalances (*we* are more loaded than anyone else) will |
| 3441 | * be counted as no imbalance for these purposes -- we can't fix that |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3442 | * by pulling tasks to us. Be careful of negative numbers as they'll |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | * appear as very large values with unsigned longs. |
| 3444 | */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3445 | if (max_load <= busiest_load_per_task) |
| 3446 | goto out_balanced; |
| 3447 | |
| 3448 | /* |
| 3449 | * In the presence of smp nice balancing, certain scenarios can have |
| 3450 | * max load less than avg load(as we skip the groups at or below |
| 3451 | * its cpu_power, while calculating max_load..) |
| 3452 | */ |
| 3453 | if (max_load < avg_load) { |
| 3454 | *imbalance = 0; |
| 3455 | goto small_imbalance; |
| 3456 | } |
Siddha, Suresh B | 0c117f1 | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3457 | |
| 3458 | /* 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] | 3459 | 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] | 3460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | /* How much load to actually move to equalise the imbalance */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3462 | *imbalance = min(max_pull * busiest->__cpu_power, |
| 3463 | (avg_load - this_load) * this->__cpu_power) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | / SCHED_LOAD_SCALE; |
| 3465 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3466 | /* |
| 3467 | * if *imbalance is less than the average load per runnable task |
| 3468 | * there is no gaurantee that any tasks will be moved so we'll have |
| 3469 | * a think about bumping its value to force at least one task to be |
| 3470 | * moved |
| 3471 | */ |
Suresh Siddha | 7fd0d2d | 2007-09-05 14:32:48 +0200 | [diff] [blame] | 3472 | if (*imbalance < busiest_load_per_task) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3473 | unsigned long tmp, pwr_now, pwr_move; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3474 | unsigned int imbn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3476 | small_imbalance: |
| 3477 | pwr_move = pwr_now = 0; |
| 3478 | imbn = 2; |
| 3479 | if (this_nr_running) { |
| 3480 | this_load_per_task /= this_nr_running; |
| 3481 | if (busiest_load_per_task > this_load_per_task) |
| 3482 | imbn = 1; |
| 3483 | } else |
| 3484 | this_load_per_task = SCHED_LOAD_SCALE; |
| 3485 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3486 | if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >= |
| 3487 | busiest_load_per_task * imbn) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3488 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | return busiest; |
| 3490 | } |
| 3491 | |
| 3492 | /* |
| 3493 | * OK, we don't have enough imbalance to justify moving tasks, |
| 3494 | * however we may be able to increase total CPU power used by |
| 3495 | * moving them. |
| 3496 | */ |
| 3497 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3498 | pwr_now += busiest->__cpu_power * |
| 3499 | min(busiest_load_per_task, max_load); |
| 3500 | pwr_now += this->__cpu_power * |
| 3501 | min(this_load_per_task, this_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3502 | pwr_now /= SCHED_LOAD_SCALE; |
| 3503 | |
| 3504 | /* Amount of load we'd subtract */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3505 | tmp = sg_div_cpu_power(busiest, |
| 3506 | busiest_load_per_task * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | if (max_load > tmp) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3508 | pwr_move += busiest->__cpu_power * |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3509 | min(busiest_load_per_task, max_load - tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | |
| 3511 | /* Amount of load we'd add */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3512 | if (max_load * busiest->__cpu_power < |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 3513 | busiest_load_per_task * SCHED_LOAD_SCALE) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3514 | tmp = sg_div_cpu_power(this, |
| 3515 | max_load * busiest->__cpu_power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | else |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3517 | tmp = sg_div_cpu_power(this, |
| 3518 | busiest_load_per_task * SCHED_LOAD_SCALE); |
| 3519 | pwr_move += this->__cpu_power * |
| 3520 | min(this_load_per_task, this_load + tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | pwr_move /= SCHED_LOAD_SCALE; |
| 3522 | |
| 3523 | /* Move if we gain throughput */ |
Suresh Siddha | 7fd0d2d | 2007-09-05 14:32:48 +0200 | [diff] [blame] | 3524 | if (pwr_move > pwr_now) |
| 3525 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | } |
| 3527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | return busiest; |
| 3529 | |
| 3530 | out_balanced: |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3531 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3532 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3533 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3535 | if (this == group_leader && group_leader != group_min) { |
| 3536 | *imbalance = min_load_per_task; |
| 3537 | return group_min; |
| 3538 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3539 | #endif |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3540 | ret: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | *imbalance = 0; |
| 3542 | return NULL; |
| 3543 | } |
| 3544 | |
| 3545 | /* |
| 3546 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 3547 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3548 | static struct rq * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3549 | find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3550 | unsigned long imbalance, const cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3551 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3552 | struct rq *busiest = NULL, *rq; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3553 | unsigned long max_load = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | int i; |
| 3555 | |
| 3556 | for_each_cpu_mask(i, group->cpumask) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3557 | unsigned long wl; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3558 | |
| 3559 | if (!cpu_isset(i, *cpus)) |
| 3560 | continue; |
| 3561 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3562 | rq = cpu_rq(i); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3563 | wl = weighted_cpuload(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3565 | if (rq->nr_running == 1 && wl > imbalance) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3566 | continue; |
| 3567 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3568 | if (wl > max_load) { |
| 3569 | max_load = wl; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3570 | busiest = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | } |
| 3572 | } |
| 3573 | |
| 3574 | return busiest; |
| 3575 | } |
| 3576 | |
| 3577 | /* |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 3578 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 3579 | * so long as it is large enough. |
| 3580 | */ |
| 3581 | #define MAX_PINNED_INTERVAL 512 |
| 3582 | |
| 3583 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3585 | * tasks if there is an imbalance. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3586 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3587 | static int load_balance(int this_cpu, struct rq *this_rq, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3588 | struct sched_domain *sd, enum cpu_idle_type idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3589 | int *balance, cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | { |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3591 | int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | struct sched_group *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3593 | unsigned long imbalance; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3594 | struct rq *busiest; |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3595 | unsigned long flags; |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 3596 | int unlock_aggregate; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3597 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3598 | cpus_setall(*cpus); |
| 3599 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 3600 | unlock_aggregate = get_aggregate(sd); |
| 3601 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3602 | /* |
| 3603 | * When power savings policy is enabled for the parent domain, idle |
| 3604 | * sibling can pick up load irrespective of busy siblings. In this case, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3605 | * let the state of idle sibling percolate up as CPU_IDLE, instead of |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3606 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3607 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3608 | if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3609 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3610 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3612 | schedstat_inc(sd, lb_count[idle]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3614 | redo: |
| 3615 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3616 | cpus, balance); |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3617 | |
Chen, Kenneth W | 0606671 | 2006-12-10 02:20:35 -0800 | [diff] [blame] | 3618 | if (*balance == 0) |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3619 | goto out_balanced; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | if (!group) { |
| 3622 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 3623 | goto out_balanced; |
| 3624 | } |
| 3625 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3626 | busiest = find_busiest_queue(group, idle, imbalance, cpus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | if (!busiest) { |
| 3628 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 3629 | goto out_balanced; |
| 3630 | } |
| 3631 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3632 | BUG_ON(busiest == this_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | |
| 3634 | schedstat_add(sd, lb_imbalance[idle], imbalance); |
| 3635 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3636 | ld_moved = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | if (busiest->nr_running > 1) { |
| 3638 | /* |
| 3639 | * Attempt to move tasks. If find_busiest_group has found |
| 3640 | * an imbalance but busiest->nr_running <= 1, the group is |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3641 | * still unbalanced. ld_moved simply stays zero, so it is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | * correctly treated as an imbalance. |
| 3643 | */ |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3644 | local_irq_save(flags); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 3645 | double_rq_lock(this_rq, busiest); |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3646 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3647 | imbalance, sd, idle, &all_pinned); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 3648 | double_rq_unlock(this_rq, busiest); |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3649 | local_irq_restore(flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3650 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3651 | /* |
| 3652 | * some other cpu did the load balance for us. |
| 3653 | */ |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3654 | if (ld_moved && this_cpu != smp_processor_id()) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3655 | resched_cpu(this_cpu); |
| 3656 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3657 | /* All tasks on this runqueue were pinned by CPU affinity */ |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3658 | if (unlikely(all_pinned)) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3659 | cpu_clear(cpu_of(busiest), *cpus); |
| 3660 | if (!cpus_empty(*cpus)) |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3661 | goto redo; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3662 | goto out_balanced; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3663 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3665 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3666 | if (!ld_moved) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | schedstat_inc(sd, lb_failed[idle]); |
| 3668 | sd->nr_balance_failed++; |
| 3669 | |
| 3670 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3672 | spin_lock_irqsave(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3673 | |
| 3674 | /* don't kick the migration_thread, if the curr |
| 3675 | * task on busiest cpu can't be moved to this_cpu |
| 3676 | */ |
| 3677 | if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3678 | spin_unlock_irqrestore(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3679 | all_pinned = 1; |
| 3680 | goto out_one_pinned; |
| 3681 | } |
| 3682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | if (!busiest->active_balance) { |
| 3684 | busiest->active_balance = 1; |
| 3685 | busiest->push_cpu = this_cpu; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3686 | active_balance = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3687 | } |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3688 | spin_unlock_irqrestore(&busiest->lock, flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3689 | if (active_balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3690 | wake_up_process(busiest->migration_thread); |
| 3691 | |
| 3692 | /* |
| 3693 | * We've kicked active balancing, reset the failure |
| 3694 | * counter. |
| 3695 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3696 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3698 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3699 | sd->nr_balance_failed = 0; |
| 3700 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3701 | if (likely(!active_balance)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | /* We were unbalanced, so reset the balancing interval */ |
| 3703 | sd->balance_interval = sd->min_interval; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3704 | } else { |
| 3705 | /* |
| 3706 | * If we've begun active balancing, start to back off. This |
| 3707 | * case may not be covered by the all_pinned logic if there |
| 3708 | * is only 1 task on the busy runqueue (because we don't call |
| 3709 | * move_tasks). |
| 3710 | */ |
| 3711 | if (sd->balance_interval < sd->max_interval) |
| 3712 | sd->balance_interval *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | } |
| 3714 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3715 | if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3716 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 3717 | ld_moved = -1; |
| 3718 | |
| 3719 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | |
| 3721 | out_balanced: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3722 | schedstat_inc(sd, lb_balanced[idle]); |
| 3723 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3724 | sd->nr_balance_failed = 0; |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3725 | |
| 3726 | out_one_pinned: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3727 | /* tune up the balancing interval */ |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 3728 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
| 3729 | (sd->balance_interval < sd->max_interval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | sd->balance_interval *= 2; |
| 3731 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3732 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3733 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 3734 | ld_moved = -1; |
| 3735 | else |
| 3736 | ld_moved = 0; |
| 3737 | out: |
| 3738 | if (unlock_aggregate) |
| 3739 | put_aggregate(sd); |
| 3740 | return ld_moved; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | /* |
| 3744 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3745 | * tasks if there is an imbalance. |
| 3746 | * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3747 | * 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] | 3748 | * this_rq is locked. |
| 3749 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3750 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3751 | load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd, |
| 3752 | cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | { |
| 3754 | struct sched_group *group; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3755 | struct rq *busiest = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | unsigned long imbalance; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3757 | int ld_moved = 0; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3758 | int sd_idle = 0; |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3759 | int all_pinned = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3760 | |
| 3761 | cpus_setall(*cpus); |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3762 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3763 | /* |
| 3764 | * When power savings policy is enabled for the parent domain, idle |
| 3765 | * sibling can pick up load irrespective of busy siblings. In this case, |
| 3766 | * let the state of idle sibling percolate up as IDLE, instead of |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3767 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3768 | */ |
| 3769 | if (sd->flags & SD_SHARE_CPUPOWER && |
| 3770 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3771 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3773 | schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3774 | redo: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3775 | group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3776 | &sd_idle, cpus, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3777 | if (!group) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3778 | schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3779 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | } |
| 3781 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3782 | busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus); |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3783 | if (!busiest) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3784 | schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3785 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | } |
| 3787 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3788 | BUG_ON(busiest == this_rq); |
| 3789 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3790 | schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3791 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3792 | ld_moved = 0; |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3793 | if (busiest->nr_running > 1) { |
| 3794 | /* Attempt to move tasks */ |
| 3795 | double_lock_balance(this_rq, busiest); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3796 | /* this_rq->clock is already updated */ |
| 3797 | update_rq_clock(busiest); |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3798 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3799 | imbalance, sd, CPU_NEWLY_IDLE, |
| 3800 | &all_pinned); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3801 | spin_unlock(&busiest->lock); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3802 | |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3803 | if (unlikely(all_pinned)) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3804 | cpu_clear(cpu_of(busiest), *cpus); |
| 3805 | if (!cpus_empty(*cpus)) |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3806 | goto redo; |
| 3807 | } |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3808 | } |
| 3809 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3810 | if (!ld_moved) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3811 | schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3812 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 3813 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3814 | return -1; |
| 3815 | } else |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3816 | sd->nr_balance_failed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3817 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3818 | return ld_moved; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3819 | |
| 3820 | out_balanced: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3821 | schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3822 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3823 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3824 | return -1; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3825 | sd->nr_balance_failed = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3826 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3827 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | /* |
| 3831 | * idle_balance is called by schedule() if this_cpu is about to become |
| 3832 | * idle. Attempts to pull tasks from other CPUs. |
| 3833 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3834 | static void idle_balance(int this_cpu, struct rq *this_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3835 | { |
| 3836 | struct sched_domain *sd; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3837 | int pulled_task = -1; |
| 3838 | unsigned long next_balance = jiffies + HZ; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3839 | cpumask_t tmpmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3840 | |
| 3841 | for_each_domain(this_cpu, sd) { |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 3842 | unsigned long interval; |
| 3843 | |
| 3844 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 3845 | continue; |
| 3846 | |
| 3847 | if (sd->flags & SD_BALANCE_NEWIDLE) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3848 | /* If we've pulled tasks over stop searching: */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3849 | pulled_task = load_balance_newidle(this_cpu, this_rq, |
| 3850 | sd, &tmpmask); |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 3851 | |
| 3852 | interval = msecs_to_jiffies(sd->balance_interval); |
| 3853 | if (time_after(next_balance, sd->last_balance + interval)) |
| 3854 | next_balance = sd->last_balance + interval; |
| 3855 | if (pulled_task) |
| 3856 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3857 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3858 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 3859 | /* |
| 3860 | * We are going idle. next_balance may be set based on |
| 3861 | * a busy processor. So reset next_balance. |
| 3862 | */ |
| 3863 | this_rq->next_balance = next_balance; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | /* |
| 3868 | * active_load_balance is run by migration threads. It pushes running tasks |
| 3869 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be |
| 3870 | * running on each physical CPU where possible, and avoids physical / |
| 3871 | * logical imbalances. |
| 3872 | * |
| 3873 | * Called with busiest_rq locked. |
| 3874 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3875 | static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3876 | { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3877 | int target_cpu = busiest_rq->push_cpu; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3878 | struct sched_domain *sd; |
| 3879 | struct rq *target_rq; |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3880 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3881 | /* Is there any task to move? */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3882 | if (busiest_rq->nr_running <= 1) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3883 | return; |
| 3884 | |
| 3885 | target_rq = cpu_rq(target_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | |
| 3887 | /* |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3888 | * This condition is "impossible", if it occurs |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3889 | * we need to fix it. Originally reported by |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3890 | * Bjorn Helgaas on a 128-cpu setup. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3892 | BUG_ON(busiest_rq == target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3894 | /* move a task from busiest_rq to target_rq */ |
| 3895 | double_lock_balance(busiest_rq, target_rq); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3896 | update_rq_clock(busiest_rq); |
| 3897 | update_rq_clock(target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3899 | /* Search for an sd spanning us and the target CPU. */ |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3900 | for_each_domain(target_cpu, sd) { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3901 | if ((sd->flags & SD_LOAD_BALANCE) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3902 | cpu_isset(busiest_cpu, sd->span)) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3903 | break; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3904 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3905 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3906 | if (likely(sd)) { |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3907 | schedstat_inc(sd, alb_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3909 | if (move_one_task(target_rq, target_cpu, busiest_rq, |
| 3910 | sd, CPU_IDLE)) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3911 | schedstat_inc(sd, alb_pushed); |
| 3912 | else |
| 3913 | schedstat_inc(sd, alb_failed); |
| 3914 | } |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3915 | spin_unlock(&target_rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3918 | #ifdef CONFIG_NO_HZ |
| 3919 | static struct { |
| 3920 | atomic_t load_balancer; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3921 | cpumask_t cpu_mask; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3922 | } nohz ____cacheline_aligned = { |
| 3923 | .load_balancer = ATOMIC_INIT(-1), |
| 3924 | .cpu_mask = CPU_MASK_NONE, |
| 3925 | }; |
| 3926 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3927 | /* |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3928 | * This routine will try to nominate the ilb (idle load balancing) |
| 3929 | * owner among the cpus whose ticks are stopped. ilb owner will do the idle |
| 3930 | * load balancing on behalf of all those cpus. If all the cpus in the system |
| 3931 | * go into this tickless mode, then there will be no ilb owner (as there is |
| 3932 | * no need for one) and all the cpus will sleep till the next wakeup event |
| 3933 | * arrives... |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3934 | * |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3935 | * For the ilb owner, tick is not stopped. And this tick will be used |
| 3936 | * for idle load balancing. ilb owner will still be part of |
| 3937 | * nohz.cpu_mask.. |
| 3938 | * |
| 3939 | * While stopping the tick, this cpu will become the ilb owner if there |
| 3940 | * is no other owner. And will be the owner till that cpu becomes busy |
| 3941 | * or if all cpus in the system stop their ticks at which point |
| 3942 | * there is no need for ilb owner. |
| 3943 | * |
| 3944 | * When the ilb owner becomes busy, it nominates another owner, during the |
| 3945 | * next busy scheduler_tick() |
| 3946 | */ |
| 3947 | int select_nohz_load_balancer(int stop_tick) |
| 3948 | { |
| 3949 | int cpu = smp_processor_id(); |
| 3950 | |
| 3951 | if (stop_tick) { |
| 3952 | cpu_set(cpu, nohz.cpu_mask); |
| 3953 | cpu_rq(cpu)->in_nohz_recently = 1; |
| 3954 | |
| 3955 | /* |
| 3956 | * If we are going offline and still the leader, give up! |
| 3957 | */ |
| 3958 | if (cpu_is_offline(cpu) && |
| 3959 | atomic_read(&nohz.load_balancer) == cpu) { |
| 3960 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 3961 | BUG(); |
| 3962 | return 0; |
| 3963 | } |
| 3964 | |
| 3965 | /* time for ilb owner also to sleep */ |
| 3966 | if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 3967 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 3968 | atomic_set(&nohz.load_balancer, -1); |
| 3969 | return 0; |
| 3970 | } |
| 3971 | |
| 3972 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 3973 | /* make me the ilb owner */ |
| 3974 | if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) |
| 3975 | return 1; |
| 3976 | } else if (atomic_read(&nohz.load_balancer) == cpu) |
| 3977 | return 1; |
| 3978 | } else { |
| 3979 | if (!cpu_isset(cpu, nohz.cpu_mask)) |
| 3980 | return 0; |
| 3981 | |
| 3982 | cpu_clear(cpu, nohz.cpu_mask); |
| 3983 | |
| 3984 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 3985 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 3986 | BUG(); |
| 3987 | } |
| 3988 | return 0; |
| 3989 | } |
| 3990 | #endif |
| 3991 | |
| 3992 | static DEFINE_SPINLOCK(balancing); |
| 3993 | |
| 3994 | /* |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3995 | * It checks each scheduling domain to see if it is due to be balanced, |
| 3996 | * and initiates a balancing operation if so. |
| 3997 | * |
| 3998 | * Balancing parameters are set up in arch_init_sched_domains. |
| 3999 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4000 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4001 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4002 | int balance = 1; |
| 4003 | struct rq *rq = cpu_rq(cpu); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4004 | unsigned long interval; |
| 4005 | struct sched_domain *sd; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4006 | /* Earliest time when we have to do rebalance again */ |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 4007 | unsigned long next_balance = jiffies + 60*HZ; |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 4008 | int update_next_balance = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 4009 | cpumask_t tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4011 | for_each_domain(cpu, sd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 4013 | continue; |
| 4014 | |
| 4015 | interval = sd->balance_interval; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 4016 | if (idle != CPU_IDLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | interval *= sd->busy_factor; |
| 4018 | |
| 4019 | /* scale ms to jiffies */ |
| 4020 | interval = msecs_to_jiffies(interval); |
| 4021 | if (unlikely(!interval)) |
| 4022 | interval = 1; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4023 | if (interval > HZ*NR_CPUS/10) |
| 4024 | interval = HZ*NR_CPUS/10; |
| 4025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 4027 | if (sd->flags & SD_SERIALIZE) { |
| 4028 | if (!spin_trylock(&balancing)) |
| 4029 | goto out; |
| 4030 | } |
| 4031 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 4032 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 4033 | if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) { |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 4034 | /* |
| 4035 | * We've pulled tasks over so either we're no |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 4036 | * longer idle, or one of our SMT siblings is |
| 4037 | * not idle. |
| 4038 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 4039 | idle = CPU_NOT_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | } |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 4041 | sd->last_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | } |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 4043 | if (sd->flags & SD_SERIALIZE) |
| 4044 | spin_unlock(&balancing); |
| 4045 | out: |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 4046 | if (time_after(next_balance, sd->last_balance + interval)) { |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 4047 | next_balance = sd->last_balance + interval; |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 4048 | update_next_balance = 1; |
| 4049 | } |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 4050 | |
| 4051 | /* |
| 4052 | * Stop the load balance at this level. There is another |
| 4053 | * CPU in our sched group which is doing load balancing more |
| 4054 | * actively. |
| 4055 | */ |
| 4056 | if (!balance) |
| 4057 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4058 | } |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 4059 | |
| 4060 | /* |
| 4061 | * next_balance will be updated only when there is a need. |
| 4062 | * When the cpu is attached to null domain for ex, it will not be |
| 4063 | * updated. |
| 4064 | */ |
| 4065 | if (likely(update_next_balance)) |
| 4066 | rq->next_balance = next_balance; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | /* |
| 4070 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
| 4071 | * In CONFIG_NO_HZ case, the idle load balance owner will do the |
| 4072 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 4073 | */ |
| 4074 | static void run_rebalance_domains(struct softirq_action *h) |
| 4075 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4076 | int this_cpu = smp_processor_id(); |
| 4077 | struct rq *this_rq = cpu_rq(this_cpu); |
| 4078 | enum cpu_idle_type idle = this_rq->idle_at_tick ? |
| 4079 | CPU_IDLE : CPU_NOT_IDLE; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4080 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4081 | rebalance_domains(this_cpu, idle); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4082 | |
| 4083 | #ifdef CONFIG_NO_HZ |
| 4084 | /* |
| 4085 | * If this cpu is the owner for idle load balancing, then do the |
| 4086 | * balancing on behalf of the other idle cpus whose ticks are |
| 4087 | * stopped. |
| 4088 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4089 | if (this_rq->idle_at_tick && |
| 4090 | atomic_read(&nohz.load_balancer) == this_cpu) { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4091 | cpumask_t cpus = nohz.cpu_mask; |
| 4092 | struct rq *rq; |
| 4093 | int balance_cpu; |
| 4094 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4095 | cpu_clear(this_cpu, cpus); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4096 | for_each_cpu_mask(balance_cpu, cpus) { |
| 4097 | /* |
| 4098 | * If this cpu gets work to do, stop the load balancing |
| 4099 | * work being done for other cpus. Next load |
| 4100 | * balancing owner will pick it up. |
| 4101 | */ |
| 4102 | if (need_resched()) |
| 4103 | break; |
| 4104 | |
Oleg Nesterov | de0cf89 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 4105 | rebalance_domains(balance_cpu, CPU_IDLE); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4106 | |
| 4107 | rq = cpu_rq(balance_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4108 | if (time_after(this_rq->next_balance, rq->next_balance)) |
| 4109 | this_rq->next_balance = rq->next_balance; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4110 | } |
| 4111 | } |
| 4112 | #endif |
| 4113 | } |
| 4114 | |
| 4115 | /* |
| 4116 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. |
| 4117 | * |
| 4118 | * In case of CONFIG_NO_HZ, this is the place where we nominate a new |
| 4119 | * idle load balancing owner or decide to stop the periodic load balancing, |
| 4120 | * if the whole system is idle. |
| 4121 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4122 | static inline void trigger_load_balance(struct rq *rq, int cpu) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4123 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4124 | #ifdef CONFIG_NO_HZ |
| 4125 | /* |
| 4126 | * If we were in the nohz mode recently and busy at the current |
| 4127 | * scheduler tick, then check if we need to nominate new idle |
| 4128 | * load balancer. |
| 4129 | */ |
| 4130 | if (rq->in_nohz_recently && !rq->idle_at_tick) { |
| 4131 | rq->in_nohz_recently = 0; |
| 4132 | |
| 4133 | if (atomic_read(&nohz.load_balancer) == cpu) { |
| 4134 | cpu_clear(cpu, nohz.cpu_mask); |
| 4135 | atomic_set(&nohz.load_balancer, -1); |
| 4136 | } |
| 4137 | |
| 4138 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 4139 | /* |
| 4140 | * simple selection for now: Nominate the |
| 4141 | * first cpu in the nohz list to be the next |
| 4142 | * ilb owner. |
| 4143 | * |
| 4144 | * TBD: Traverse the sched domains and nominate |
| 4145 | * the nearest cpu in the nohz.cpu_mask. |
| 4146 | */ |
| 4147 | int ilb = first_cpu(nohz.cpu_mask); |
| 4148 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 4149 | if (ilb < nr_cpu_ids) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4150 | resched_cpu(ilb); |
| 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | /* |
| 4155 | * If this cpu is idle and doing idle load balancing for all the |
| 4156 | * cpus with ticks stopped, is it time for that to stop? |
| 4157 | */ |
| 4158 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && |
| 4159 | cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 4160 | resched_cpu(cpu); |
| 4161 | return; |
| 4162 | } |
| 4163 | |
| 4164 | /* |
| 4165 | * If this cpu is idle and the idle load balancing is done by |
| 4166 | * someone else, then no need raise the SCHED_SOFTIRQ |
| 4167 | */ |
| 4168 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && |
| 4169 | cpu_isset(cpu, nohz.cpu_mask)) |
| 4170 | return; |
| 4171 | #endif |
| 4172 | if (time_after_eq(jiffies, rq->next_balance)) |
| 4173 | raise_softirq(SCHED_SOFTIRQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4175 | |
| 4176 | #else /* CONFIG_SMP */ |
| 4177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | /* |
| 4179 | * on UP we do not need to balance between CPUs: |
| 4180 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4181 | static inline void idle_balance(int cpu, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | { |
| 4183 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | #endif |
| 4186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
| 4188 | |
| 4189 | EXPORT_PER_CPU_SYMBOL(kstat); |
| 4190 | |
| 4191 | /* |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4192 | * Return p->sum_exec_runtime plus any more ns on the sched_clock |
| 4193 | * that have not yet been banked in case the task is currently running. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | */ |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4195 | unsigned long long task_sched_runtime(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | unsigned long flags; |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4198 | u64 ns, delta_exec; |
| 4199 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4200 | |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4201 | rq = task_rq_lock(p, &flags); |
| 4202 | ns = p->se.sum_exec_runtime; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 4203 | if (task_current(rq, p)) { |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4204 | update_rq_clock(rq); |
| 4205 | delta_exec = rq->clock - p->se.exec_start; |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4206 | if ((s64)delta_exec > 0) |
| 4207 | ns += delta_exec; |
| 4208 | } |
| 4209 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | return ns; |
| 4212 | } |
| 4213 | |
| 4214 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4215 | * Account user cpu time to a process. |
| 4216 | * @p: the process that the cpu time gets accounted to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | * @cputime: the cpu time spent in user space since the last update |
| 4218 | */ |
| 4219 | void account_user_time(struct task_struct *p, cputime_t cputime) |
| 4220 | { |
| 4221 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 4222 | cputime64_t tmp; |
| 4223 | |
| 4224 | p->utime = cputime_add(p->utime, cputime); |
| 4225 | |
| 4226 | /* Add user time to cpustat. */ |
| 4227 | tmp = cputime_to_cputime64(cputime); |
| 4228 | if (TASK_NICE(p) > 0) |
| 4229 | cpustat->nice = cputime64_add(cpustat->nice, tmp); |
| 4230 | else |
| 4231 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 4232 | } |
| 4233 | |
| 4234 | /* |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 4235 | * Account guest cpu time to a process. |
| 4236 | * @p: the process that the cpu time gets accounted to |
| 4237 | * @cputime: the cpu time spent in virtual machine since the last update |
| 4238 | */ |
Adrian Bunk | f7402e0 | 2007-10-29 21:18:10 +0100 | [diff] [blame] | 4239 | static void account_guest_time(struct task_struct *p, cputime_t cputime) |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 4240 | { |
| 4241 | cputime64_t tmp; |
| 4242 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 4243 | |
| 4244 | tmp = cputime_to_cputime64(cputime); |
| 4245 | |
| 4246 | p->utime = cputime_add(p->utime, cputime); |
| 4247 | p->gtime = cputime_add(p->gtime, cputime); |
| 4248 | |
| 4249 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 4250 | cpustat->guest = cputime64_add(cpustat->guest, tmp); |
| 4251 | } |
| 4252 | |
| 4253 | /* |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 4254 | * Account scaled user cpu time to a process. |
| 4255 | * @p: the process that the cpu time gets accounted to |
| 4256 | * @cputime: the cpu time spent in user space since the last update |
| 4257 | */ |
| 4258 | void account_user_time_scaled(struct task_struct *p, cputime_t cputime) |
| 4259 | { |
| 4260 | p->utimescaled = cputime_add(p->utimescaled, cputime); |
| 4261 | } |
| 4262 | |
| 4263 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | * Account system cpu time to a process. |
| 4265 | * @p: the process that the cpu time gets accounted to |
| 4266 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 4267 | * @cputime: the cpu time spent in kernel space since the last update |
| 4268 | */ |
| 4269 | void account_system_time(struct task_struct *p, int hardirq_offset, |
| 4270 | cputime_t cputime) |
| 4271 | { |
| 4272 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4273 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | cputime64_t tmp; |
| 4275 | |
Christian Borntraeger | 9778385 | 2007-11-15 20:57:39 +0100 | [diff] [blame] | 4276 | if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) |
| 4277 | return account_guest_time(p, cputime); |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 4278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | p->stime = cputime_add(p->stime, cputime); |
| 4280 | |
| 4281 | /* Add system time to cpustat. */ |
| 4282 | tmp = cputime_to_cputime64(cputime); |
| 4283 | if (hardirq_count() - hardirq_offset) |
| 4284 | cpustat->irq = cputime64_add(cpustat->irq, tmp); |
| 4285 | else if (softirq_count()) |
| 4286 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 4287 | else if (p != rq->idle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | cpustat->system = cputime64_add(cpustat->system, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 4289 | else if (atomic_read(&rq->nr_iowait) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 4291 | else |
| 4292 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 4293 | /* Account for system time used */ |
| 4294 | acct_update_integrals(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | /* |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 4298 | * Account scaled system cpu time to a process. |
| 4299 | * @p: the process that the cpu time gets accounted to |
| 4300 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 4301 | * @cputime: the cpu time spent in kernel space since the last update |
| 4302 | */ |
| 4303 | void account_system_time_scaled(struct task_struct *p, cputime_t cputime) |
| 4304 | { |
| 4305 | p->stimescaled = cputime_add(p->stimescaled, cputime); |
| 4306 | } |
| 4307 | |
| 4308 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | * Account for involuntary wait time. |
| 4310 | * @p: the process from which the cpu time has been stolen |
| 4311 | * @steal: the cpu time spent in involuntary wait |
| 4312 | */ |
| 4313 | void account_steal_time(struct task_struct *p, cputime_t steal) |
| 4314 | { |
| 4315 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 4316 | cputime64_t tmp = cputime_to_cputime64(steal); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4317 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | |
| 4319 | if (p == rq->idle) { |
| 4320 | p->stime = cputime_add(p->stime, steal); |
| 4321 | if (atomic_read(&rq->nr_iowait) > 0) |
| 4322 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 4323 | else |
| 4324 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 4325 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | cpustat->steal = cputime64_add(cpustat->steal, tmp); |
| 4327 | } |
| 4328 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4329 | /* |
| 4330 | * This function gets called by the timer code, with HZ frequency. |
| 4331 | * We call it with interrupts disabled. |
| 4332 | * |
| 4333 | * It also gets called by the fork code, when changing the parent's |
| 4334 | * timeslices. |
| 4335 | */ |
| 4336 | void scheduler_tick(void) |
| 4337 | { |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4338 | int cpu = smp_processor_id(); |
| 4339 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4340 | struct task_struct *curr = rq->curr; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 4341 | u64 next_tick = rq->tick_timestamp + TICK_NSEC; |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4342 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4343 | spin_lock(&rq->lock); |
Ingo Molnar | 546fe3c | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 4344 | __update_rq_clock(rq); |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 4345 | /* |
| 4346 | * Let rq->clock advance by at least TICK_NSEC: |
| 4347 | */ |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 4348 | if (unlikely(rq->clock < next_tick)) { |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 4349 | rq->clock = next_tick; |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 4350 | rq->clock_underflows++; |
| 4351 | } |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 4352 | rq->tick_timestamp = rq->clock; |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 4353 | update_last_tick_seen(rq); |
Ingo Molnar | f1a438d | 2007-08-09 11:16:45 +0200 | [diff] [blame] | 4354 | update_cpu_load(rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4355 | curr->sched_class->task_tick(rq, curr, 0); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4356 | spin_unlock(&rq->lock); |
| 4357 | |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 4358 | #ifdef CONFIG_SMP |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4359 | rq->idle_at_tick = idle_cpu(cpu); |
| 4360 | trigger_load_balance(rq, cpu); |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 4361 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | } |
| 4363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) |
| 4365 | |
Srinivasa Ds | 4362758 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 4366 | void __kprobes add_preempt_count(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | { |
| 4368 | /* |
| 4369 | * Underflow? |
| 4370 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 4371 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
| 4372 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | preempt_count() += val; |
| 4374 | /* |
| 4375 | * Spinlock count overflowing soon? |
| 4376 | */ |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 4377 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
| 4378 | PREEMPT_MASK - 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | } |
| 4380 | EXPORT_SYMBOL(add_preempt_count); |
| 4381 | |
Srinivasa Ds | 4362758 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 4382 | void __kprobes sub_preempt_count(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | { |
| 4384 | /* |
| 4385 | * Underflow? |
| 4386 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 4387 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
| 4388 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | /* |
| 4390 | * Is the spinlock portion underflowing? |
| 4391 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 4392 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
| 4393 | !(preempt_count() & PREEMPT_MASK))) |
| 4394 | return; |
| 4395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | preempt_count() -= val; |
| 4397 | } |
| 4398 | EXPORT_SYMBOL(sub_preempt_count); |
| 4399 | |
| 4400 | #endif |
| 4401 | |
| 4402 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4403 | * Print scheduling while atomic bug: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4405 | static noinline void __schedule_bug(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | { |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 4407 | struct pt_regs *regs = get_irq_regs(); |
| 4408 | |
| 4409 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
| 4410 | prev->comm, prev->pid, preempt_count()); |
| 4411 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4412 | debug_show_held_locks(prev); |
| 4413 | if (irqs_disabled()) |
| 4414 | print_irqtrace_events(prev); |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 4415 | |
| 4416 | if (regs) |
| 4417 | show_regs(regs); |
| 4418 | else |
| 4419 | dump_stack(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4422 | /* |
| 4423 | * Various schedule()-time debugging checks and statistics: |
| 4424 | */ |
| 4425 | static inline void schedule_debug(struct task_struct *prev) |
| 4426 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4428 | * Test if we are atomic. Since do_exit() needs to call into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | * schedule() atomically, we ignore that path for now. |
| 4430 | * Otherwise, whine if we are scheduling when we should not be. |
| 4431 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4432 | if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state)) |
| 4433 | __schedule_bug(prev); |
| 4434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
| 4436 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4437 | schedstat_inc(this_rq(), sched_count); |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 4438 | #ifdef CONFIG_SCHEDSTATS |
| 4439 | if (unlikely(prev->lock_depth >= 0)) { |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4440 | schedstat_inc(this_rq(), bkl_count); |
| 4441 | schedstat_inc(prev, sched_info.bkl_count); |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 4442 | } |
| 4443 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | /* |
| 4447 | * Pick up the highest-prio task: |
| 4448 | */ |
| 4449 | static inline struct task_struct * |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4450 | pick_next_task(struct rq *rq, struct task_struct *prev) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4451 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4452 | const struct sched_class *class; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4453 | struct task_struct *p; |
| 4454 | |
| 4455 | /* |
| 4456 | * Optimization: we know that if all tasks are in |
| 4457 | * the fair class we can call that function directly: |
| 4458 | */ |
| 4459 | if (likely(rq->nr_running == rq->cfs.nr_running)) { |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 4460 | p = fair_sched_class.pick_next_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4461 | if (likely(p)) |
| 4462 | return p; |
| 4463 | } |
| 4464 | |
| 4465 | class = sched_class_highest; |
| 4466 | for ( ; ; ) { |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 4467 | p = class->pick_next_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4468 | if (p) |
| 4469 | return p; |
| 4470 | /* |
| 4471 | * Will never be NULL as the idle class always |
| 4472 | * returns a non-NULL p: |
| 4473 | */ |
| 4474 | class = class->next; |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | /* |
| 4479 | * schedule() is the main scheduler function. |
| 4480 | */ |
| 4481 | asmlinkage void __sched schedule(void) |
| 4482 | { |
| 4483 | struct task_struct *prev, *next; |
Harvey Harrison | 67ca7bd | 2008-02-15 09:56:36 -0800 | [diff] [blame] | 4484 | unsigned long *switch_count; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4485 | struct rq *rq; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4486 | int cpu; |
| 4487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | need_resched: |
| 4489 | preempt_disable(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4490 | cpu = smp_processor_id(); |
| 4491 | rq = cpu_rq(cpu); |
| 4492 | rcu_qsctr_inc(cpu); |
| 4493 | prev = rq->curr; |
| 4494 | switch_count = &prev->nivcsw; |
| 4495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | release_kernel_lock(prev); |
| 4497 | need_resched_nonpreemptible: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4499 | schedule_debug(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4501 | hrtick_clear(rq); |
| 4502 | |
Ingo Molnar | 1e81995 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4503 | /* |
| 4504 | * Do the rq-clock update outside the rq lock: |
| 4505 | */ |
| 4506 | local_irq_disable(); |
Ingo Molnar | c1b3da3 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4507 | __update_rq_clock(rq); |
Ingo Molnar | 1e81995 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4508 | spin_lock(&rq->lock); |
| 4509 | clear_tsk_need_resched(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4511 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
| 4512 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && |
Roel Kluin | 23e3c3c | 2008-03-13 17:41:59 +0100 | [diff] [blame] | 4513 | signal_pending(prev))) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4514 | prev->state = TASK_RUNNING; |
| 4515 | } else { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4516 | deactivate_task(rq, prev, 1); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4517 | } |
| 4518 | switch_count = &prev->nvcsw; |
| 4519 | } |
| 4520 | |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4521 | #ifdef CONFIG_SMP |
| 4522 | if (prev->sched_class->pre_schedule) |
| 4523 | prev->sched_class->pre_schedule(rq, prev); |
| 4524 | #endif |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 4525 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4526 | if (unlikely(!rq->nr_running)) |
| 4527 | idle_balance(cpu, rq); |
| 4528 | |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4529 | prev->sched_class->put_prev_task(rq, prev); |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4530 | next = pick_next_task(rq, prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | |
| 4532 | sched_info_switch(prev, next); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | if (likely(prev != next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4535 | rq->nr_switches++; |
| 4536 | rq->curr = next; |
| 4537 | ++*switch_count; |
| 4538 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4539 | context_switch(rq, prev, next); /* unlocks the rq */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4540 | /* |
| 4541 | * the context switch might have flipped the stack from under |
| 4542 | * us, hence refresh the local variables. |
| 4543 | */ |
| 4544 | cpu = smp_processor_id(); |
| 4545 | rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | } else |
| 4547 | spin_unlock_irq(&rq->lock); |
| 4548 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4549 | hrtick_set(rq); |
| 4550 | |
| 4551 | if (unlikely(reacquire_kernel_lock(current) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | goto need_resched_nonpreemptible; |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | preempt_enable_no_resched(); |
| 4555 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 4556 | goto need_resched; |
| 4557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | EXPORT_SYMBOL(schedule); |
| 4559 | |
| 4560 | #ifdef CONFIG_PREEMPT |
| 4561 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4562 | * this is the entry point to schedule() from in-kernel preemption |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4563 | * off of preempt_enable. Kernel preemptions off return from interrupt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4564 | * occur there and call schedule directly. |
| 4565 | */ |
| 4566 | asmlinkage void __sched preempt_schedule(void) |
| 4567 | { |
| 4568 | struct thread_info *ti = current_thread_info(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | struct task_struct *task = current; |
| 4570 | int saved_lock_depth; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 4571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | /* |
| 4573 | * If there is a non-zero preempt_count or interrupts are disabled, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4574 | * we do not want to preempt the current task. Just return.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | */ |
Nick Piggin | beed33a | 2006-10-11 01:21:52 -0700 | [diff] [blame] | 4576 | if (likely(ti->preempt_count || irqs_disabled())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | return; |
| 4578 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4579 | do { |
| 4580 | add_preempt_count(PREEMPT_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4582 | /* |
| 4583 | * We keep the big kernel semaphore locked, but we |
| 4584 | * clear ->lock_depth so that schedule() doesnt |
| 4585 | * auto-release the semaphore: |
| 4586 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4587 | saved_lock_depth = task->lock_depth; |
| 4588 | task->lock_depth = -1; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4589 | schedule(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4590 | task->lock_depth = saved_lock_depth; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4591 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4592 | |
| 4593 | /* |
| 4594 | * Check again in case we missed a preemption opportunity |
| 4595 | * between schedule and now. |
| 4596 | */ |
| 4597 | barrier(); |
| 4598 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | EXPORT_SYMBOL(preempt_schedule); |
| 4601 | |
| 4602 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4603 | * this is the entry point to schedule() from kernel preemption |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | * off of irq context. |
| 4605 | * Note, that this is called and return with irqs disabled. This will |
| 4606 | * protect us against recursive calling from irq. |
| 4607 | */ |
| 4608 | asmlinkage void __sched preempt_schedule_irq(void) |
| 4609 | { |
| 4610 | struct thread_info *ti = current_thread_info(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | struct task_struct *task = current; |
| 4612 | int saved_lock_depth; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 4613 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4614 | /* Catch callers which need to be fixed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
| 4616 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4617 | do { |
| 4618 | add_preempt_count(PREEMPT_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4620 | /* |
| 4621 | * We keep the big kernel semaphore locked, but we |
| 4622 | * clear ->lock_depth so that schedule() doesnt |
| 4623 | * auto-release the semaphore: |
| 4624 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4625 | saved_lock_depth = task->lock_depth; |
| 4626 | task->lock_depth = -1; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4627 | local_irq_enable(); |
| 4628 | schedule(); |
| 4629 | local_irq_disable(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4630 | task->lock_depth = saved_lock_depth; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4631 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4632 | |
| 4633 | /* |
| 4634 | * Check again in case we missed a preemption opportunity |
| 4635 | * between schedule and now. |
| 4636 | */ |
| 4637 | barrier(); |
| 4638 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | #endif /* CONFIG_PREEMPT */ |
| 4642 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4643 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, |
| 4644 | void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4646 | return try_to_wake_up(curr->private, mode, sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | EXPORT_SYMBOL(default_wake_function); |
| 4649 | |
| 4650 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4651 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just |
| 4652 | * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | * number) then we wake all the non-exclusive tasks and one exclusive task. |
| 4654 | * |
| 4655 | * There are circumstances in which we can try to wake a task which has already |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4656 | * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | * zero in this (rare) case, and we handle it by continuing to scan the queue. |
| 4658 | */ |
| 4659 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, |
| 4660 | int nr_exclusive, int sync, void *key) |
| 4661 | { |
Matthias Kaehlcke | 2e45874 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 4662 | wait_queue_t *curr, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | |
Matthias Kaehlcke | 2e45874 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 4664 | list_for_each_entry_safe(curr, next, &q->task_list, task_list) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4665 | unsigned flags = curr->flags; |
| 4666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | if (curr->func(curr, mode, sync, key) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4668 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | break; |
| 4670 | } |
| 4671 | } |
| 4672 | |
| 4673 | /** |
| 4674 | * __wake_up - wake up threads blocked on a waitqueue. |
| 4675 | * @q: the waitqueue |
| 4676 | * @mode: which threads |
| 4677 | * @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] | 4678 | * @key: is directly passed to the wakeup function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4680 | void __wake_up(wait_queue_head_t *q, unsigned int mode, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4681 | int nr_exclusive, void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | { |
| 4683 | unsigned long flags; |
| 4684 | |
| 4685 | spin_lock_irqsave(&q->lock, flags); |
| 4686 | __wake_up_common(q, mode, nr_exclusive, 0, key); |
| 4687 | spin_unlock_irqrestore(&q->lock, flags); |
| 4688 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | EXPORT_SYMBOL(__wake_up); |
| 4690 | |
| 4691 | /* |
| 4692 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. |
| 4693 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4694 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | { |
| 4696 | __wake_up_common(q, mode, 1, 0, NULL); |
| 4697 | } |
| 4698 | |
| 4699 | /** |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 4700 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | * @q: the waitqueue |
| 4702 | * @mode: which threads |
| 4703 | * @nr_exclusive: how many wake-one or wake-many threads to wake up |
| 4704 | * |
| 4705 | * The sync wakeup differs that the waker knows that it will schedule |
| 4706 | * away soon, so while the target thread will be woken up, it will not |
| 4707 | * be migrated to another CPU - ie. the two threads are 'synchronized' |
| 4708 | * with each other. This can prevent needless bouncing between CPUs. |
| 4709 | * |
| 4710 | * On UP it can prevent extra preemption. |
| 4711 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4712 | void |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4713 | __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] | 4714 | { |
| 4715 | unsigned long flags; |
| 4716 | int sync = 1; |
| 4717 | |
| 4718 | if (unlikely(!q)) |
| 4719 | return; |
| 4720 | |
| 4721 | if (unlikely(!nr_exclusive)) |
| 4722 | sync = 0; |
| 4723 | |
| 4724 | spin_lock_irqsave(&q->lock, flags); |
| 4725 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); |
| 4726 | spin_unlock_irqrestore(&q->lock, flags); |
| 4727 | } |
| 4728 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ |
| 4729 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4730 | void complete(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | { |
| 4732 | unsigned long flags; |
| 4733 | |
| 4734 | spin_lock_irqsave(&x->wait.lock, flags); |
| 4735 | x->done++; |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 4736 | __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 4738 | } |
| 4739 | EXPORT_SYMBOL(complete); |
| 4740 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4741 | void complete_all(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | { |
| 4743 | unsigned long flags; |
| 4744 | |
| 4745 | spin_lock_irqsave(&x->wait.lock, flags); |
| 4746 | x->done += UINT_MAX/2; |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 4747 | __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 4749 | } |
| 4750 | EXPORT_SYMBOL(complete_all); |
| 4751 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4752 | static inline long __sched |
| 4753 | do_wait_for_common(struct completion *x, long timeout, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | if (!x->done) { |
| 4756 | DECLARE_WAITQUEUE(wait, current); |
| 4757 | |
| 4758 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 4759 | __add_wait_queue_tail(&x->wait, &wait); |
| 4760 | do { |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 4761 | if ((state == TASK_INTERRUPTIBLE && |
| 4762 | signal_pending(current)) || |
| 4763 | (state == TASK_KILLABLE && |
| 4764 | fatal_signal_pending(current))) { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4765 | __remove_wait_queue(&x->wait, &wait); |
| 4766 | return -ERESTARTSYS; |
| 4767 | } |
| 4768 | __set_current_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | spin_unlock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4770 | timeout = schedule_timeout(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | spin_lock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4772 | if (!timeout) { |
| 4773 | __remove_wait_queue(&x->wait, &wait); |
| 4774 | return timeout; |
| 4775 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | } while (!x->done); |
| 4777 | __remove_wait_queue(&x->wait, &wait); |
| 4778 | } |
| 4779 | x->done--; |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4780 | return timeout; |
| 4781 | } |
| 4782 | |
| 4783 | static long __sched |
| 4784 | wait_for_common(struct completion *x, long timeout, int state) |
| 4785 | { |
| 4786 | might_sleep(); |
| 4787 | |
| 4788 | spin_lock_irq(&x->wait.lock); |
| 4789 | timeout = do_wait_for_common(x, timeout, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | spin_unlock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4791 | return timeout; |
| 4792 | } |
| 4793 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4794 | void __sched wait_for_completion(struct completion *x) |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4795 | { |
| 4796 | wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | } |
| 4798 | EXPORT_SYMBOL(wait_for_completion); |
| 4799 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4800 | unsigned long __sched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) |
| 4802 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4803 | return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | } |
| 4805 | EXPORT_SYMBOL(wait_for_completion_timeout); |
| 4806 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4807 | int __sched wait_for_completion_interruptible(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | { |
Andi Kleen | 51e9799 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 4809 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE); |
| 4810 | if (t == -ERESTARTSYS) |
| 4811 | return t; |
| 4812 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | } |
| 4814 | EXPORT_SYMBOL(wait_for_completion_interruptible); |
| 4815 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4816 | unsigned long __sched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | wait_for_completion_interruptible_timeout(struct completion *x, |
| 4818 | unsigned long timeout) |
| 4819 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4820 | return wait_for_common(x, timeout, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | } |
| 4822 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); |
| 4823 | |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 4824 | int __sched wait_for_completion_killable(struct completion *x) |
| 4825 | { |
| 4826 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE); |
| 4827 | if (t == -ERESTARTSYS) |
| 4828 | return t; |
| 4829 | return 0; |
| 4830 | } |
| 4831 | EXPORT_SYMBOL(wait_for_completion_killable); |
| 4832 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4833 | static long __sched |
| 4834 | sleep_on_common(wait_queue_head_t *q, int state, long timeout) |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4835 | { |
| 4836 | unsigned long flags; |
| 4837 | wait_queue_t wait; |
| 4838 | |
| 4839 | init_waitqueue_entry(&wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4841 | __set_current_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4843 | spin_lock_irqsave(&q->lock, flags); |
| 4844 | __add_wait_queue(q, &wait); |
| 4845 | spin_unlock(&q->lock); |
| 4846 | timeout = schedule_timeout(timeout); |
| 4847 | spin_lock_irq(&q->lock); |
| 4848 | __remove_wait_queue(q, &wait); |
| 4849 | spin_unlock_irqrestore(&q->lock, flags); |
| 4850 | |
| 4851 | return timeout; |
| 4852 | } |
| 4853 | |
| 4854 | void __sched interruptible_sleep_on(wait_queue_head_t *q) |
| 4855 | { |
| 4856 | sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | EXPORT_SYMBOL(interruptible_sleep_on); |
| 4859 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4860 | long __sched |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4861 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4862 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4863 | return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
| 4866 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4867 | void __sched sleep_on(wait_queue_head_t *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4869 | sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | EXPORT_SYMBOL(sleep_on); |
| 4872 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4873 | long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4875 | return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | EXPORT_SYMBOL(sleep_on_timeout); |
| 4878 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4879 | #ifdef CONFIG_RT_MUTEXES |
| 4880 | |
| 4881 | /* |
| 4882 | * rt_mutex_setprio - set the current priority of a task |
| 4883 | * @p: task |
| 4884 | * @prio: prio value (kernel-internal form) |
| 4885 | * |
| 4886 | * This function changes the 'effective' priority of a task. It does |
| 4887 | * not touch ->normal_prio like __setscheduler(). |
| 4888 | * |
| 4889 | * Used by the rt_mutex code to implement priority inheritance logic. |
| 4890 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4891 | void rt_mutex_setprio(struct task_struct *p, int prio) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4892 | { |
| 4893 | unsigned long flags; |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4894 | int oldprio, on_rq, running; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4895 | struct rq *rq; |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4896 | const struct sched_class *prev_class = p->sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4897 | |
| 4898 | BUG_ON(prio < 0 || prio > MAX_PRIO); |
| 4899 | |
| 4900 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4901 | update_rq_clock(rq); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4902 | |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4903 | oldprio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4904 | on_rq = p->se.on_rq; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 4905 | running = task_current(rq, p); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4906 | if (on_rq) |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4907 | dequeue_task(rq, p, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4908 | if (running) |
| 4909 | p->sched_class->put_prev_task(rq, p); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4910 | |
| 4911 | if (rt_prio(prio)) |
| 4912 | p->sched_class = &rt_sched_class; |
| 4913 | else |
| 4914 | p->sched_class = &fair_sched_class; |
| 4915 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4916 | p->prio = prio; |
| 4917 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4918 | if (running) |
| 4919 | p->sched_class->set_curr_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4920 | if (on_rq) { |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4921 | enqueue_task(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4922 | |
| 4923 | check_class_changed(rq, p, prev_class, oldprio, running); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4924 | } |
| 4925 | task_rq_unlock(rq, &flags); |
| 4926 | } |
| 4927 | |
| 4928 | #endif |
| 4929 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4930 | void set_user_nice(struct task_struct *p, long nice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4932 | int old_prio, delta, on_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4934 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | |
| 4936 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) |
| 4937 | return; |
| 4938 | /* |
| 4939 | * We have to be careful, if called from sys_setpriority(), |
| 4940 | * the task might be in the middle of scheduling on another CPU. |
| 4941 | */ |
| 4942 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4943 | update_rq_clock(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | /* |
| 4945 | * The RT priorities are set via sched_setscheduler(), but we still |
| 4946 | * allow the 'normal' nice value to be set - but as expected |
| 4947 | * it wont have any effect on scheduling until the task is |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4948 | * SCHED_FIFO/SCHED_RR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | */ |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4950 | if (task_has_rt_policy(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | p->static_prio = NICE_TO_PRIO(nice); |
| 4952 | goto out_unlock; |
| 4953 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4954 | on_rq = p->se.on_rq; |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 4955 | if (on_rq) |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4956 | dequeue_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | p->static_prio = NICE_TO_PRIO(nice); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 4959 | set_load_weight(p); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4960 | old_prio = p->prio; |
| 4961 | p->prio = effective_prio(p); |
| 4962 | delta = p->prio - old_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4963 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4964 | if (on_rq) { |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4965 | enqueue_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | /* |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4967 | * If the task increased its priority or is running and |
| 4968 | * lowered its priority, then reschedule its CPU: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | */ |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4970 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | resched_task(rq->curr); |
| 4972 | } |
| 4973 | out_unlock: |
| 4974 | task_rq_unlock(rq, &flags); |
| 4975 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | EXPORT_SYMBOL(set_user_nice); |
| 4977 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4978 | /* |
| 4979 | * can_nice - check if a task can reduce its nice value |
| 4980 | * @p: task |
| 4981 | * @nice: nice value |
| 4982 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4983 | int can_nice(const struct task_struct *p, const int nice) |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4984 | { |
Matt Mackall | 024f474 | 2005-08-18 11:24:19 -0700 | [diff] [blame] | 4985 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
| 4986 | int nice_rlim = 20 - nice; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4987 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4988 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
| 4989 | capable(CAP_SYS_NICE)); |
| 4990 | } |
| 4991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | #ifdef __ARCH_WANT_SYS_NICE |
| 4993 | |
| 4994 | /* |
| 4995 | * sys_nice - change the priority of the current process. |
| 4996 | * @increment: priority increment |
| 4997 | * |
| 4998 | * sys_setpriority is a more generic, but much slower function that |
| 4999 | * does similar things. |
| 5000 | */ |
| 5001 | asmlinkage long sys_nice(int increment) |
| 5002 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5003 | long nice, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5004 | |
| 5005 | /* |
| 5006 | * Setpriority might change our priority at the same moment. |
| 5007 | * We don't have to worry. Conceptually one call occurs first |
| 5008 | * and we have a single winner. |
| 5009 | */ |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 5010 | if (increment < -40) |
| 5011 | increment = -40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | if (increment > 40) |
| 5013 | increment = 40; |
| 5014 | |
| 5015 | nice = PRIO_TO_NICE(current->static_prio) + increment; |
| 5016 | if (nice < -20) |
| 5017 | nice = -20; |
| 5018 | if (nice > 19) |
| 5019 | nice = 19; |
| 5020 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 5021 | if (increment < 0 && !can_nice(current, nice)) |
| 5022 | return -EPERM; |
| 5023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5024 | retval = security_task_setnice(current, nice); |
| 5025 | if (retval) |
| 5026 | return retval; |
| 5027 | |
| 5028 | set_user_nice(current, nice); |
| 5029 | return 0; |
| 5030 | } |
| 5031 | |
| 5032 | #endif |
| 5033 | |
| 5034 | /** |
| 5035 | * task_prio - return the priority value of a given task. |
| 5036 | * @p: the task in question. |
| 5037 | * |
| 5038 | * This is the priority value as seen by users in /proc. |
| 5039 | * RT tasks are offset by -200. Normal tasks are centered |
| 5040 | * around 0, value goes from -16 to +15. |
| 5041 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5042 | int task_prio(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | { |
| 5044 | return p->prio - MAX_RT_PRIO; |
| 5045 | } |
| 5046 | |
| 5047 | /** |
| 5048 | * task_nice - return the nice value of a given task. |
| 5049 | * @p: the task in question. |
| 5050 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5051 | int task_nice(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5052 | { |
| 5053 | return TASK_NICE(p); |
| 5054 | } |
Pavel Roskin | 150d8be | 2008-03-05 16:56:37 -0500 | [diff] [blame] | 5055 | EXPORT_SYMBOL(task_nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | |
| 5057 | /** |
| 5058 | * idle_cpu - is a given cpu idle currently? |
| 5059 | * @cpu: the processor in question. |
| 5060 | */ |
| 5061 | int idle_cpu(int cpu) |
| 5062 | { |
| 5063 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; |
| 5064 | } |
| 5065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | /** |
| 5067 | * idle_task - return the idle task for a given cpu. |
| 5068 | * @cpu: the processor in question. |
| 5069 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5070 | struct task_struct *idle_task(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | { |
| 5072 | return cpu_rq(cpu)->idle; |
| 5073 | } |
| 5074 | |
| 5075 | /** |
| 5076 | * find_process_by_pid - find a process with a matching PID value. |
| 5077 | * @pid: the pid in question. |
| 5078 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5079 | static struct task_struct *find_process_by_pid(pid_t pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | { |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 5081 | return pid ? find_task_by_vpid(pid) : current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | /* Actually do priority change: must hold rq lock. */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5085 | static void |
| 5086 | __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5088 | BUG_ON(p->se.on_rq); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | p->policy = policy; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5091 | switch (p->policy) { |
| 5092 | case SCHED_NORMAL: |
| 5093 | case SCHED_BATCH: |
| 5094 | case SCHED_IDLE: |
| 5095 | p->sched_class = &fair_sched_class; |
| 5096 | break; |
| 5097 | case SCHED_FIFO: |
| 5098 | case SCHED_RR: |
| 5099 | p->sched_class = &rt_sched_class; |
| 5100 | break; |
| 5101 | } |
| 5102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | p->rt_priority = prio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5104 | p->normal_prio = normal_prio(p); |
| 5105 | /* we are holding p->pi_lock already */ |
| 5106 | p->prio = rt_mutex_getprio(p); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 5107 | set_load_weight(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | } |
| 5109 | |
| 5110 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 5111 | * 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] | 5112 | * @p: the task in question. |
| 5113 | * @policy: new policy. |
| 5114 | * @param: structure containing the new RT priority. |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 5115 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 5116 | * NOTE that the task may be already dead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5118 | int sched_setscheduler(struct task_struct *p, int policy, |
| 5119 | struct sched_param *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5121 | int retval, oldprio, oldpolicy = -1, on_rq, running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | unsigned long flags; |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5123 | const struct sched_class *prev_class = p->sched_class; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5124 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | |
Steven Rostedt | 66e5393 | 2006-06-27 02:54:44 -0700 | [diff] [blame] | 5126 | /* may grab non-irq protected spin_locks */ |
| 5127 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | recheck: |
| 5129 | /* double check policy once rq lock held */ |
| 5130 | if (policy < 0) |
| 5131 | policy = oldpolicy = p->policy; |
| 5132 | else if (policy != SCHED_FIFO && policy != SCHED_RR && |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5133 | policy != SCHED_NORMAL && policy != SCHED_BATCH && |
| 5134 | policy != SCHED_IDLE) |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5135 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | /* |
| 5137 | * Valid priorities for SCHED_FIFO and SCHED_RR are |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5138 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
| 5139 | * SCHED_BATCH and SCHED_IDLE is 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | */ |
| 5141 | if (param->sched_priority < 0 || |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5142 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
Steven Rostedt | d46523e | 2005-07-25 16:28:39 -0400 | [diff] [blame] | 5143 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | return -EINVAL; |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5145 | if (rt_policy(policy) != (param->sched_priority != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | return -EINVAL; |
| 5147 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 5148 | /* |
| 5149 | * Allow unprivileged RT tasks to decrease priority: |
| 5150 | */ |
| 5151 | if (!capable(CAP_SYS_NICE)) { |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5152 | if (rt_policy(policy)) { |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 5153 | unsigned long rlim_rtprio; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 5154 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 5155 | if (!lock_task_sighand(p, &flags)) |
| 5156 | return -ESRCH; |
| 5157 | rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur; |
| 5158 | unlock_task_sighand(p, &flags); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 5159 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 5160 | /* can't set/change the rt policy */ |
| 5161 | if (policy != p->policy && !rlim_rtprio) |
| 5162 | return -EPERM; |
| 5163 | |
| 5164 | /* can't increase priority */ |
| 5165 | if (param->sched_priority > p->rt_priority && |
| 5166 | param->sched_priority > rlim_rtprio) |
| 5167 | return -EPERM; |
| 5168 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5169 | /* |
| 5170 | * Like positive nice levels, dont allow tasks to |
| 5171 | * move out of SCHED_IDLE either: |
| 5172 | */ |
| 5173 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) |
| 5174 | return -EPERM; |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 5175 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 5176 | /* can't change other user's priorities */ |
| 5177 | if ((current->euid != p->euid) && |
| 5178 | (current->euid != p->uid)) |
| 5179 | return -EPERM; |
| 5180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 5182 | #ifdef CONFIG_RT_GROUP_SCHED |
| 5183 | /* |
| 5184 | * Do not allow realtime tasks into groups that have no runtime |
| 5185 | * assigned. |
| 5186 | */ |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 5187 | if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0) |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 5188 | return -EPERM; |
| 5189 | #endif |
| 5190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | retval = security_task_setscheduler(p, policy, param); |
| 5192 | if (retval) |
| 5193 | return retval; |
| 5194 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5195 | * make sure no PI-waiters arrive (or leave) while we are |
| 5196 | * changing the priority of the task: |
| 5197 | */ |
| 5198 | spin_lock_irqsave(&p->pi_lock, flags); |
| 5199 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | * To be able to change p->policy safely, the apropriate |
| 5201 | * runqueue lock must be held. |
| 5202 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5203 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | /* recheck policy now with rq lock held */ |
| 5205 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
| 5206 | policy = oldpolicy = -1; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5207 | __task_rq_unlock(rq); |
| 5208 | spin_unlock_irqrestore(&p->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | goto recheck; |
| 5210 | } |
Ingo Molnar | 2daa357 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5211 | update_rq_clock(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5212 | on_rq = p->se.on_rq; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 5213 | running = task_current(rq, p); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 5214 | if (on_rq) |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5215 | deactivate_task(rq, p, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 5216 | if (running) |
| 5217 | p->sched_class->put_prev_task(rq, p); |
Dmitry Adamushko | f6b53205 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 | oldprio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5220 | __setscheduler(rq, p, policy, param->sched_priority); |
Dmitry Adamushko | f6b53205 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5221 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 5222 | if (running) |
| 5223 | p->sched_class->set_curr_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5224 | if (on_rq) { |
| 5225 | activate_task(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5226 | |
| 5227 | check_class_changed(rq, p, prev_class, oldprio, running); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5229 | __task_rq_unlock(rq); |
| 5230 | spin_unlock_irqrestore(&p->pi_lock, flags); |
| 5231 | |
Thomas Gleixner | 95e02ca | 2006-06-27 02:55:02 -0700 | [diff] [blame] | 5232 | rt_mutex_adjust_pi(p); |
| 5233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | return 0; |
| 5235 | } |
| 5236 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
| 5237 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5238 | static int |
| 5239 | 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] | 5240 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | struct sched_param lparam; |
| 5242 | struct task_struct *p; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5243 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | |
| 5245 | if (!param || pid < 0) |
| 5246 | return -EINVAL; |
| 5247 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) |
| 5248 | return -EFAULT; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 5249 | |
| 5250 | rcu_read_lock(); |
| 5251 | retval = -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | p = find_process_by_pid(pid); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 5253 | if (p != NULL) |
| 5254 | retval = sched_setscheduler(p, policy, &lparam); |
| 5255 | rcu_read_unlock(); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | return retval; |
| 5258 | } |
| 5259 | |
| 5260 | /** |
| 5261 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority |
| 5262 | * @pid: the pid in question. |
| 5263 | * @policy: new policy. |
| 5264 | * @param: structure containing the new RT priority. |
| 5265 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5266 | asmlinkage long |
| 5267 | sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | { |
Jason Baron | c21761f | 2006-01-18 17:43:03 -0800 | [diff] [blame] | 5269 | /* negative values for policy are not valid */ |
| 5270 | if (policy < 0) |
| 5271 | return -EINVAL; |
| 5272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | return do_sched_setscheduler(pid, policy, param); |
| 5274 | } |
| 5275 | |
| 5276 | /** |
| 5277 | * sys_sched_setparam - set/change the RT priority of a thread |
| 5278 | * @pid: the pid in question. |
| 5279 | * @param: structure containing the new RT priority. |
| 5280 | */ |
| 5281 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) |
| 5282 | { |
| 5283 | return do_sched_setscheduler(pid, -1, param); |
| 5284 | } |
| 5285 | |
| 5286 | /** |
| 5287 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread |
| 5288 | * @pid: the pid in question. |
| 5289 | */ |
| 5290 | asmlinkage long sys_sched_getscheduler(pid_t pid) |
| 5291 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5292 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5293 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | |
| 5295 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5296 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | |
| 5298 | retval = -ESRCH; |
| 5299 | read_lock(&tasklist_lock); |
| 5300 | p = find_process_by_pid(pid); |
| 5301 | if (p) { |
| 5302 | retval = security_task_getscheduler(p); |
| 5303 | if (!retval) |
| 5304 | retval = p->policy; |
| 5305 | } |
| 5306 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | return retval; |
| 5308 | } |
| 5309 | |
| 5310 | /** |
| 5311 | * sys_sched_getscheduler - get the RT priority of a thread |
| 5312 | * @pid: the pid in question. |
| 5313 | * @param: structure containing the RT priority. |
| 5314 | */ |
| 5315 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) |
| 5316 | { |
| 5317 | struct sched_param lp; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5318 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5319 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | |
| 5321 | if (!param || pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5322 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | |
| 5324 | read_lock(&tasklist_lock); |
| 5325 | p = find_process_by_pid(pid); |
| 5326 | retval = -ESRCH; |
| 5327 | if (!p) |
| 5328 | goto out_unlock; |
| 5329 | |
| 5330 | retval = security_task_getscheduler(p); |
| 5331 | if (retval) |
| 5332 | goto out_unlock; |
| 5333 | |
| 5334 | lp.sched_priority = p->rt_priority; |
| 5335 | read_unlock(&tasklist_lock); |
| 5336 | |
| 5337 | /* |
| 5338 | * This one might sleep, we cannot do it with a spinlock held ... |
| 5339 | */ |
| 5340 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; |
| 5341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | return retval; |
| 5343 | |
| 5344 | out_unlock: |
| 5345 | read_unlock(&tasklist_lock); |
| 5346 | return retval; |
| 5347 | } |
| 5348 | |
Mike Travis | b53e921 | 2008-04-04 18:11:08 -0700 | [diff] [blame] | 5349 | long sched_setaffinity(pid_t pid, const cpumask_t *in_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 | cpumask_t cpus_allowed; |
Mike Travis | b53e921 | 2008-04-04 18:11:08 -0700 | [diff] [blame] | 5352 | cpumask_t new_mask = *in_mask; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5353 | struct task_struct *p; |
| 5354 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5356 | get_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | read_lock(&tasklist_lock); |
| 5358 | |
| 5359 | p = find_process_by_pid(pid); |
| 5360 | if (!p) { |
| 5361 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5362 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | return -ESRCH; |
| 5364 | } |
| 5365 | |
| 5366 | /* |
| 5367 | * It is not safe to call set_cpus_allowed with the |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5368 | * tasklist_lock held. We will bump the task_struct's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | * usage count and then drop tasklist_lock. |
| 5370 | */ |
| 5371 | get_task_struct(p); |
| 5372 | read_unlock(&tasklist_lock); |
| 5373 | |
| 5374 | retval = -EPERM; |
| 5375 | if ((current->euid != p->euid) && (current->euid != p->uid) && |
| 5376 | !capable(CAP_SYS_NICE)) |
| 5377 | goto out_unlock; |
| 5378 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 5379 | retval = security_task_setscheduler(p, 0, NULL); |
| 5380 | if (retval) |
| 5381 | goto out_unlock; |
| 5382 | |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 5383 | cpuset_cpus_allowed(p, &cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | cpus_and(new_mask, new_mask, cpus_allowed); |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 5385 | again: |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 5386 | retval = set_cpus_allowed_ptr(p, &new_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 5388 | if (!retval) { |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 5389 | cpuset_cpus_allowed(p, &cpus_allowed); |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 5390 | if (!cpus_subset(new_mask, cpus_allowed)) { |
| 5391 | /* |
| 5392 | * We must have raced with a concurrent cpuset |
| 5393 | * update. Just reset the cpus_allowed to the |
| 5394 | * cpuset's cpus_allowed |
| 5395 | */ |
| 5396 | new_mask = cpus_allowed; |
| 5397 | goto again; |
| 5398 | } |
| 5399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | out_unlock: |
| 5401 | put_task_struct(p); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5402 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5403 | return retval; |
| 5404 | } |
| 5405 | |
| 5406 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, |
| 5407 | cpumask_t *new_mask) |
| 5408 | { |
| 5409 | if (len < sizeof(cpumask_t)) { |
| 5410 | memset(new_mask, 0, sizeof(cpumask_t)); |
| 5411 | } else if (len > sizeof(cpumask_t)) { |
| 5412 | len = sizeof(cpumask_t); |
| 5413 | } |
| 5414 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
| 5415 | } |
| 5416 | |
| 5417 | /** |
| 5418 | * sys_sched_setaffinity - set the cpu affinity of a process |
| 5419 | * @pid: pid of the process |
| 5420 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 5421 | * @user_mask_ptr: user-space pointer to the new cpu mask |
| 5422 | */ |
| 5423 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, |
| 5424 | unsigned long __user *user_mask_ptr) |
| 5425 | { |
| 5426 | cpumask_t new_mask; |
| 5427 | int retval; |
| 5428 | |
| 5429 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); |
| 5430 | if (retval) |
| 5431 | return retval; |
| 5432 | |
Mike Travis | b53e921 | 2008-04-04 18:11:08 -0700 | [diff] [blame] | 5433 | return sched_setaffinity(pid, &new_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | } |
| 5435 | |
| 5436 | /* |
| 5437 | * Represents all cpu's present in the system |
| 5438 | * In systems capable of hotplug, this map could dynamically grow |
| 5439 | * as new cpu's are detected in the system via any platform specific |
| 5440 | * method, such as ACPI for e.g. |
| 5441 | */ |
| 5442 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 5443 | cpumask_t cpu_present_map __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | EXPORT_SYMBOL(cpu_present_map); |
| 5445 | |
| 5446 | #ifndef CONFIG_SMP |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 5447 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 5448 | EXPORT_SYMBOL(cpu_online_map); |
| 5449 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 5450 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 5451 | EXPORT_SYMBOL(cpu_possible_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | #endif |
| 5453 | |
| 5454 | long sched_getaffinity(pid_t pid, cpumask_t *mask) |
| 5455 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5456 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5459 | get_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | read_lock(&tasklist_lock); |
| 5461 | |
| 5462 | retval = -ESRCH; |
| 5463 | p = find_process_by_pid(pid); |
| 5464 | if (!p) |
| 5465 | goto out_unlock; |
| 5466 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 5467 | retval = security_task_getscheduler(p); |
| 5468 | if (retval) |
| 5469 | goto out_unlock; |
| 5470 | |
Jack Steiner | 2f7016d | 2006-02-01 03:05:18 -0800 | [diff] [blame] | 5471 | cpus_and(*mask, p->cpus_allowed, cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | |
| 5473 | out_unlock: |
| 5474 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5475 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | |
Ulrich Drepper | 9531b62 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5477 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | /** |
| 5481 | * sys_sched_getaffinity - get the cpu affinity of a process |
| 5482 | * @pid: pid of the process |
| 5483 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 5484 | * @user_mask_ptr: user-space pointer to hold the current cpu mask |
| 5485 | */ |
| 5486 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, |
| 5487 | unsigned long __user *user_mask_ptr) |
| 5488 | { |
| 5489 | int ret; |
| 5490 | cpumask_t mask; |
| 5491 | |
| 5492 | if (len < sizeof(cpumask_t)) |
| 5493 | return -EINVAL; |
| 5494 | |
| 5495 | ret = sched_getaffinity(pid, &mask); |
| 5496 | if (ret < 0) |
| 5497 | return ret; |
| 5498 | |
| 5499 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) |
| 5500 | return -EFAULT; |
| 5501 | |
| 5502 | return sizeof(cpumask_t); |
| 5503 | } |
| 5504 | |
| 5505 | /** |
| 5506 | * sys_sched_yield - yield the current processor to other threads. |
| 5507 | * |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5508 | * This function yields the current CPU to other tasks. If there are no |
| 5509 | * other threads running on this CPU then this function will return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | */ |
| 5511 | asmlinkage long sys_sched_yield(void) |
| 5512 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5513 | struct rq *rq = this_rq_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 5515 | schedstat_inc(rq, yld_count); |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5516 | current->sched_class->yield_task(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5517 | |
| 5518 | /* |
| 5519 | * Since we are going to call schedule() anyway, there's |
| 5520 | * no need to preempt or enable interrupts: |
| 5521 | */ |
| 5522 | __release(rq->lock); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 5523 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | _raw_spin_unlock(&rq->lock); |
| 5525 | preempt_enable_no_resched(); |
| 5526 | |
| 5527 | schedule(); |
| 5528 | |
| 5529 | return 0; |
| 5530 | } |
| 5531 | |
Andrew Morton | e7b3840 | 2006-06-30 01:56:00 -0700 | [diff] [blame] | 5532 | static void __cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | { |
Ingo Molnar | 8e0a43d | 2006-06-23 02:05:23 -0700 | [diff] [blame] | 5534 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 5535 | __might_sleep(__FILE__, __LINE__); |
| 5536 | #endif |
Ingo Molnar | 5bbcfd9 | 2005-07-07 17:57:04 -0700 | [diff] [blame] | 5537 | /* |
| 5538 | * The BKS might be reacquired before we have dropped |
| 5539 | * PREEMPT_ACTIVE, which could trigger a second |
| 5540 | * cond_resched() call. |
| 5541 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5542 | do { |
| 5543 | add_preempt_count(PREEMPT_ACTIVE); |
| 5544 | schedule(); |
| 5545 | sub_preempt_count(PREEMPT_ACTIVE); |
| 5546 | } while (need_resched()); |
| 5547 | } |
| 5548 | |
Herbert Xu | 02b67cc3 | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 5549 | #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY) |
| 5550 | int __sched _cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | { |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 5552 | if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) && |
| 5553 | system_state == SYSTEM_RUNNING) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | __cond_resched(); |
| 5555 | return 1; |
| 5556 | } |
| 5557 | return 0; |
| 5558 | } |
Herbert Xu | 02b67cc3 | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 5559 | EXPORT_SYMBOL(_cond_resched); |
| 5560 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | |
| 5562 | /* |
| 5563 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, |
| 5564 | * call schedule, and on return reacquire the lock. |
| 5565 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5566 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | * operations here to prevent schedule() from being called twice (once via |
| 5568 | * spin_unlock(), once by hand). |
| 5569 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5570 | int cond_resched_lock(spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | { |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5572 | int resched = need_resched() && system_state == SYSTEM_RUNNING; |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5573 | int ret = 0; |
| 5574 | |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5575 | if (spin_needbreak(lock) || resched) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | spin_unlock(lock); |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5577 | if (resched && need_resched()) |
| 5578 | __cond_resched(); |
| 5579 | else |
| 5580 | cpu_relax(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5581 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5582 | spin_lock(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | } |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5584 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | EXPORT_SYMBOL(cond_resched_lock); |
| 5587 | |
| 5588 | int __sched cond_resched_softirq(void) |
| 5589 | { |
| 5590 | BUG_ON(!in_softirq()); |
| 5591 | |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 5592 | if (need_resched() && system_state == SYSTEM_RUNNING) { |
Thomas Gleixner | 98d82567 | 2007-05-23 13:58:18 -0700 | [diff] [blame] | 5593 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | __cond_resched(); |
| 5595 | local_bh_disable(); |
| 5596 | return 1; |
| 5597 | } |
| 5598 | return 0; |
| 5599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | EXPORT_SYMBOL(cond_resched_softirq); |
| 5601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | /** |
| 5603 | * yield - yield the current processor to other threads. |
| 5604 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 5605 | * This is a shortcut for kernel-space yielding - it marks the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | * thread runnable and calls sys_sched_yield(). |
| 5607 | */ |
| 5608 | void __sched yield(void) |
| 5609 | { |
| 5610 | set_current_state(TASK_RUNNING); |
| 5611 | sys_sched_yield(); |
| 5612 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | EXPORT_SYMBOL(yield); |
| 5614 | |
| 5615 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5616 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | * that process accounting knows that this is a task in IO wait state. |
| 5618 | * |
| 5619 | * But don't do that if it is a deliberate, throttling IO wait (this task |
| 5620 | * has set its backing_dev_info: the queue against which it should throttle) |
| 5621 | */ |
| 5622 | void __sched io_schedule(void) |
| 5623 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5624 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5626 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | atomic_inc(&rq->nr_iowait); |
| 5628 | schedule(); |
| 5629 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5630 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5632 | EXPORT_SYMBOL(io_schedule); |
| 5633 | |
| 5634 | long __sched io_schedule_timeout(long timeout) |
| 5635 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5636 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | long ret; |
| 5638 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5639 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | atomic_inc(&rq->nr_iowait); |
| 5641 | ret = schedule_timeout(timeout); |
| 5642 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5643 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | return ret; |
| 5645 | } |
| 5646 | |
| 5647 | /** |
| 5648 | * sys_sched_get_priority_max - return maximum RT priority. |
| 5649 | * @policy: scheduling class. |
| 5650 | * |
| 5651 | * this syscall returns the maximum rt_priority that can be used |
| 5652 | * by a given scheduling class. |
| 5653 | */ |
| 5654 | asmlinkage long sys_sched_get_priority_max(int policy) |
| 5655 | { |
| 5656 | int ret = -EINVAL; |
| 5657 | |
| 5658 | switch (policy) { |
| 5659 | case SCHED_FIFO: |
| 5660 | case SCHED_RR: |
| 5661 | ret = MAX_USER_RT_PRIO-1; |
| 5662 | break; |
| 5663 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5664 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5665 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | ret = 0; |
| 5667 | break; |
| 5668 | } |
| 5669 | return ret; |
| 5670 | } |
| 5671 | |
| 5672 | /** |
| 5673 | * sys_sched_get_priority_min - return minimum RT priority. |
| 5674 | * @policy: scheduling class. |
| 5675 | * |
| 5676 | * this syscall returns the minimum rt_priority that can be used |
| 5677 | * by a given scheduling class. |
| 5678 | */ |
| 5679 | asmlinkage long sys_sched_get_priority_min(int policy) |
| 5680 | { |
| 5681 | int ret = -EINVAL; |
| 5682 | |
| 5683 | switch (policy) { |
| 5684 | case SCHED_FIFO: |
| 5685 | case SCHED_RR: |
| 5686 | ret = 1; |
| 5687 | break; |
| 5688 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5689 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5690 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | ret = 0; |
| 5692 | } |
| 5693 | return ret; |
| 5694 | } |
| 5695 | |
| 5696 | /** |
| 5697 | * sys_sched_rr_get_interval - return the default timeslice of a process. |
| 5698 | * @pid: pid of the process. |
| 5699 | * @interval: userspace pointer to the timeslice value. |
| 5700 | * |
| 5701 | * this syscall writes the default timeslice value of a given process |
| 5702 | * into the user-space timespec buffer. A value of '0' means infinity. |
| 5703 | */ |
| 5704 | asmlinkage |
| 5705 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) |
| 5706 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5707 | struct task_struct *p; |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5708 | unsigned int time_slice; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5709 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | struct timespec t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | |
| 5712 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5713 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | |
| 5715 | retval = -ESRCH; |
| 5716 | read_lock(&tasklist_lock); |
| 5717 | p = find_process_by_pid(pid); |
| 5718 | if (!p) |
| 5719 | goto out_unlock; |
| 5720 | |
| 5721 | retval = security_task_getscheduler(p); |
| 5722 | if (retval) |
| 5723 | goto out_unlock; |
| 5724 | |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5725 | /* |
| 5726 | * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER |
| 5727 | * tasks that are on an otherwise idle runqueue: |
| 5728 | */ |
| 5729 | time_slice = 0; |
| 5730 | if (p->policy == SCHED_RR) { |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5731 | time_slice = DEF_TIMESLICE; |
Miao Xie | 1868f95 | 2008-03-07 09:35:06 +0800 | [diff] [blame] | 5732 | } else if (p->policy != SCHED_FIFO) { |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5733 | struct sched_entity *se = &p->se; |
| 5734 | unsigned long flags; |
| 5735 | struct rq *rq; |
| 5736 | |
| 5737 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5738 | if (rq->cfs.load.weight) |
| 5739 | time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5740 | task_rq_unlock(rq, &flags); |
| 5741 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | read_unlock(&tasklist_lock); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5743 | jiffies_to_timespec(time_slice, &t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | return retval; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | out_unlock: |
| 5748 | read_unlock(&tasklist_lock); |
| 5749 | return retval; |
| 5750 | } |
| 5751 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5752 | static const char stat_nam[] = "RSDTtZX"; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5753 | |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5754 | void sched_show_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | unsigned long free = 0; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5757 | unsigned state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | state = p->state ? __ffs(p->state) + 1 : 0; |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5760 | printk(KERN_INFO "%-13.13s %c", p->comm, |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5761 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5762 | #if BITS_PER_LONG == 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5763 | if (state == TASK_RUNNING) |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5764 | printk(KERN_CONT " running "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | else |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5766 | printk(KERN_CONT " %08lx ", thread_saved_pc(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | #else |
| 5768 | if (state == TASK_RUNNING) |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5769 | printk(KERN_CONT " running task "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | else |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5771 | printk(KERN_CONT " %016lx ", thread_saved_pc(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5772 | #endif |
| 5773 | #ifdef CONFIG_DEBUG_STACK_USAGE |
| 5774 | { |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 5775 | unsigned long *n = end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | while (!*n) |
| 5777 | n++; |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 5778 | free = (unsigned long)n - (unsigned long)end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | } |
| 5780 | #endif |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5781 | printk(KERN_CONT "%5lu %5d %6d\n", free, |
Roland McGrath | fcfd50a | 2008-01-09 00:03:23 -0800 | [diff] [blame] | 5782 | task_pid_nr(p), task_pid_nr(p->real_parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5783 | |
Nick Piggin | 5fb5e6d | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 5784 | show_stack(p, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5785 | } |
| 5786 | |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 5787 | void show_state_filter(unsigned long state_filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5789 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5791 | #if BITS_PER_LONG == 32 |
| 5792 | printk(KERN_INFO |
| 5793 | " task PC stack pid father\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | #else |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5795 | printk(KERN_INFO |
| 5796 | " task PC stack pid father\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | #endif |
| 5798 | read_lock(&tasklist_lock); |
| 5799 | do_each_thread(g, p) { |
| 5800 | /* |
| 5801 | * reset the NMI-timeout, listing all files on a slow |
| 5802 | * console might take alot of time: |
| 5803 | */ |
| 5804 | touch_nmi_watchdog(); |
Ingo Molnar | 39bc89f | 2007-04-25 20:50:03 -0700 | [diff] [blame] | 5805 | if (!state_filter || (p->state & state_filter)) |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5806 | sched_show_task(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | } while_each_thread(g, p); |
| 5808 | |
Jeremy Fitzhardinge | 04c9167 | 2007-05-08 00:28:05 -0700 | [diff] [blame] | 5809 | touch_all_softlockup_watchdogs(); |
| 5810 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5811 | #ifdef CONFIG_SCHED_DEBUG |
| 5812 | sysrq_sched_debug_show(); |
| 5813 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | read_unlock(&tasklist_lock); |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 5815 | /* |
| 5816 | * Only show locks if all tasks are dumped: |
| 5817 | */ |
| 5818 | if (state_filter == -1) |
| 5819 | debug_show_all_locks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | } |
| 5821 | |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5822 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) |
| 5823 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5824 | idle->sched_class = &idle_sched_class; |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5825 | } |
| 5826 | |
Ingo Molnar | f340c0d | 2005-06-28 16:40:42 +0200 | [diff] [blame] | 5827 | /** |
| 5828 | * init_idle - set up an idle thread for a given CPU |
| 5829 | * @idle: task in question |
| 5830 | * @cpu: cpu the idle task belongs to |
| 5831 | * |
| 5832 | * NOTE: this function does not set the idle thread's NEED_RESCHED |
| 5833 | * flag, to make booting more robust. |
| 5834 | */ |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 5835 | void __cpuinit init_idle(struct task_struct *idle, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5837 | struct rq *rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | unsigned long flags; |
| 5839 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5840 | __sched_fork(idle); |
| 5841 | idle->se.exec_start = sched_clock(); |
| 5842 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5843 | idle->prio = idle->normal_prio = MAX_PRIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5844 | idle->cpus_allowed = cpumask_of_cpu(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5845 | __set_task_cpu(idle, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | |
| 5847 | spin_lock_irqsave(&rq->lock, flags); |
| 5848 | rq->curr = rq->idle = idle; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 5849 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
| 5850 | idle->oncpu = 1; |
| 5851 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5853 | |
| 5854 | /* Set the preempt count _outside_ the spinlocks! */ |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 5855 | task_thread_info(idle)->preempt_count = 0; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 5856 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5857 | /* |
| 5858 | * The idle tasks have their own, simple scheduling class: |
| 5859 | */ |
| 5860 | idle->sched_class = &idle_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5861 | } |
| 5862 | |
| 5863 | /* |
| 5864 | * In a system that switches off the HZ timer nohz_cpu_mask |
| 5865 | * indicates which cpus entered this state. This is used |
| 5866 | * in the rcu update to wait only for active cpus. For system |
| 5867 | * which do not switch off the HZ timer nohz_cpu_mask should |
| 5868 | * always be CPU_MASK_NONE. |
| 5869 | */ |
| 5870 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; |
| 5871 | |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 5872 | /* |
| 5873 | * Increase the granularity value when there are more CPUs, |
| 5874 | * because with more CPUs the 'effective latency' as visible |
| 5875 | * to users decreases. But the relationship is not linear, |
| 5876 | * so pick a second-best guess by going with the log2 of the |
| 5877 | * number of CPUs. |
| 5878 | * |
| 5879 | * This idea comes from the SD scheduler of Con Kolivas: |
| 5880 | */ |
| 5881 | static inline void sched_init_granularity(void) |
| 5882 | { |
| 5883 | unsigned int factor = 1 + ilog2(num_online_cpus()); |
| 5884 | const unsigned long limit = 200000000; |
| 5885 | |
| 5886 | sysctl_sched_min_granularity *= factor; |
| 5887 | if (sysctl_sched_min_granularity > limit) |
| 5888 | sysctl_sched_min_granularity = limit; |
| 5889 | |
| 5890 | sysctl_sched_latency *= factor; |
| 5891 | if (sysctl_sched_latency > limit) |
| 5892 | sysctl_sched_latency = limit; |
| 5893 | |
| 5894 | sysctl_sched_wakeup_granularity *= factor; |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 5895 | } |
| 5896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | #ifdef CONFIG_SMP |
| 5898 | /* |
| 5899 | * This is how migration works: |
| 5900 | * |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5901 | * 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] | 5902 | * runqueue and wake up that CPU's migration thread. |
| 5903 | * 2) we down() the locked semaphore => thread blocks. |
| 5904 | * 3) migration thread wakes up (implicitly it forces the migrated |
| 5905 | * thread off the CPU) |
| 5906 | * 4) it gets the migration request and checks whether the migrated |
| 5907 | * task is still in the wrong runqueue. |
| 5908 | * 5) if it's in the wrong runqueue then the migration thread removes |
| 5909 | * it and puts it into the right queue. |
| 5910 | * 6) migration thread up()s the semaphore. |
| 5911 | * 7) we wake up and the migration is done. |
| 5912 | */ |
| 5913 | |
| 5914 | /* |
| 5915 | * Change a given task's CPU affinity. Migrate the thread to a |
| 5916 | * proper CPU and schedule it away if the CPU it's executing on |
| 5917 | * is removed from the allowed bitmask. |
| 5918 | * |
| 5919 | * NOTE: the caller must have a valid reference to the task, the |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5920 | * task must not exit() & deallocate itself prematurely. The |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | * call is not atomic; no spinlocks may be held. |
| 5922 | */ |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5923 | int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5925 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5927 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5928 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | |
| 5930 | rq = task_rq_lock(p, &flags); |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5931 | if (!cpus_intersects(*new_mask, cpu_online_map)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | ret = -EINVAL; |
| 5933 | goto out; |
| 5934 | } |
| 5935 | |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 5936 | if (p->sched_class->set_cpus_allowed) |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5937 | p->sched_class->set_cpus_allowed(p, new_mask); |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 5938 | else { |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5939 | p->cpus_allowed = *new_mask; |
| 5940 | p->rt.nr_cpus_allowed = cpus_weight(*new_mask); |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 5941 | } |
| 5942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | /* Can the task run on the task's current CPU? If so, we're done */ |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5944 | if (cpu_isset(task_cpu(p), *new_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | goto out; |
| 5946 | |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5947 | if (migrate_task(p, any_online_cpu(*new_mask), &req)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | /* Need help from migration thread: drop lock and wait. */ |
| 5949 | task_rq_unlock(rq, &flags); |
| 5950 | wake_up_process(rq->migration_thread); |
| 5951 | wait_for_completion(&req.done); |
| 5952 | tlb_migrate_finish(p->mm); |
| 5953 | return 0; |
| 5954 | } |
| 5955 | out: |
| 5956 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | return ret; |
| 5959 | } |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5960 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | |
| 5962 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5963 | * Move (not current) task off this cpu, onto dest cpu. We're doing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | * this because either it can't run here any more (set_cpus_allowed() |
| 5965 | * away from this CPU, or CPU going down), or because we're |
| 5966 | * attempting to rebalance this task on exec (sched_exec). |
| 5967 | * |
| 5968 | * So we race with normal scheduler movements, but that's OK, as long |
| 5969 | * as the task is no longer on this CPU. |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5970 | * |
| 5971 | * Returns non-zero if task was successfully migrated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | */ |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5973 | 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] | 5974 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5975 | struct rq *rq_dest, *rq_src; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5976 | int ret = 0, on_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | |
| 5978 | if (unlikely(cpu_is_offline(dest_cpu))) |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5979 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | |
| 5981 | rq_src = cpu_rq(src_cpu); |
| 5982 | rq_dest = cpu_rq(dest_cpu); |
| 5983 | |
| 5984 | double_rq_lock(rq_src, rq_dest); |
| 5985 | /* Already moved. */ |
| 5986 | if (task_cpu(p) != src_cpu) |
| 5987 | goto out; |
| 5988 | /* Affinity changed (again). */ |
| 5989 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) |
| 5990 | goto out; |
| 5991 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5992 | on_rq = p->se.on_rq; |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5993 | if (on_rq) |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5994 | deactivate_task(rq_src, p, 0); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | set_task_cpu(p, dest_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5997 | if (on_rq) { |
| 5998 | activate_task(rq_dest, p, 0); |
| 5999 | check_preempt_curr(rq_dest, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | } |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 6001 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | out: |
| 6003 | double_rq_unlock(rq_src, rq_dest); |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 6004 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6005 | } |
| 6006 | |
| 6007 | /* |
| 6008 | * migration_thread - this is a highprio system thread that performs |
| 6009 | * thread migration by bumping thread off CPU then 'pushing' onto |
| 6010 | * another runqueue. |
| 6011 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 6012 | static int migration_thread(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6013 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | int cpu = (long)data; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6015 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | |
| 6017 | rq = cpu_rq(cpu); |
| 6018 | BUG_ON(rq->migration_thread != current); |
| 6019 | |
| 6020 | set_current_state(TASK_INTERRUPTIBLE); |
| 6021 | while (!kthread_should_stop()) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6022 | struct migration_req *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6023 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | spin_lock_irq(&rq->lock); |
| 6026 | |
| 6027 | if (cpu_is_offline(cpu)) { |
| 6028 | spin_unlock_irq(&rq->lock); |
| 6029 | goto wait_to_die; |
| 6030 | } |
| 6031 | |
| 6032 | if (rq->active_balance) { |
| 6033 | active_load_balance(rq, cpu); |
| 6034 | rq->active_balance = 0; |
| 6035 | } |
| 6036 | |
| 6037 | head = &rq->migration_queue; |
| 6038 | |
| 6039 | if (list_empty(head)) { |
| 6040 | spin_unlock_irq(&rq->lock); |
| 6041 | schedule(); |
| 6042 | set_current_state(TASK_INTERRUPTIBLE); |
| 6043 | continue; |
| 6044 | } |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6045 | req = list_entry(head->next, struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6046 | list_del_init(head->next); |
| 6047 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 6048 | spin_unlock(&rq->lock); |
| 6049 | __migrate_task(req->task, cpu, req->dest_cpu); |
| 6050 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | |
| 6052 | complete(&req->done); |
| 6053 | } |
| 6054 | __set_current_state(TASK_RUNNING); |
| 6055 | return 0; |
| 6056 | |
| 6057 | wait_to_die: |
| 6058 | /* Wait for kthread_stop */ |
| 6059 | set_current_state(TASK_INTERRUPTIBLE); |
| 6060 | while (!kthread_should_stop()) { |
| 6061 | schedule(); |
| 6062 | set_current_state(TASK_INTERRUPTIBLE); |
| 6063 | } |
| 6064 | __set_current_state(TASK_RUNNING); |
| 6065 | return 0; |
| 6066 | } |
| 6067 | |
| 6068 | #ifdef CONFIG_HOTPLUG_CPU |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6069 | |
| 6070 | static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu) |
| 6071 | { |
| 6072 | int ret; |
| 6073 | |
| 6074 | local_irq_disable(); |
| 6075 | ret = __migrate_task(p, src_cpu, dest_cpu); |
| 6076 | local_irq_enable(); |
| 6077 | return ret; |
| 6078 | } |
| 6079 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 6080 | /* |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 6081 | * Figure out where task on dead CPU should go, use force if necessary. |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 6082 | * NOTE: interrupts should be disabled by the caller |
| 6083 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6084 | 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] | 6085 | { |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 6086 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6087 | cpumask_t mask; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6088 | struct rq *rq; |
| 6089 | int dest_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6090 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6091 | do { |
| 6092 | /* On same node? */ |
| 6093 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); |
| 6094 | cpus_and(mask, mask, p->cpus_allowed); |
| 6095 | dest_cpu = any_online_cpu(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6097 | /* On any allowed CPU? */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6098 | if (dest_cpu >= nr_cpu_ids) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6099 | dest_cpu = any_online_cpu(p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6101 | /* No more Mr. Nice Guy. */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6102 | if (dest_cpu >= nr_cpu_ids) { |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 6103 | cpumask_t cpus_allowed; |
| 6104 | |
| 6105 | cpuset_cpus_allowed_locked(p, &cpus_allowed); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6106 | /* |
| 6107 | * Try to stay on the same cpuset, where the |
| 6108 | * current cpuset may be a subset of all cpus. |
| 6109 | * The cpuset_cpus_allowed_locked() variant of |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6110 | * cpuset_cpus_allowed() will not block. It must be |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6111 | * called within calls to cpuset_lock/cpuset_unlock. |
| 6112 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6113 | rq = task_rq_lock(p, &flags); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6114 | p->cpus_allowed = cpus_allowed; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6115 | dest_cpu = any_online_cpu(p->cpus_allowed); |
| 6116 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6118 | /* |
| 6119 | * Don't tell them about moving exiting tasks or |
| 6120 | * kernel threads (both mm NULL), since they never |
| 6121 | * leave kernel. |
| 6122 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6123 | if (p->mm && printk_ratelimit()) { |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6124 | printk(KERN_INFO "process %d (%s) no " |
| 6125 | "longer affine to cpu%d\n", |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6126 | task_pid_nr(p), p->comm, dead_cpu); |
| 6127 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6128 | } |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6129 | } while (!__migrate_task_irq(p, dead_cpu, dest_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | } |
| 6131 | |
| 6132 | /* |
| 6133 | * While a dead CPU has no uninterruptible tasks queued at this point, |
| 6134 | * it might still have a nonzero ->nr_uninterruptible counter, because |
| 6135 | * for performance reasons the counter is not stricly tracking tasks to |
| 6136 | * their home CPUs. So we just add the counter to another CPU's counter, |
| 6137 | * to keep the global sum constant after CPU-down: |
| 6138 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6139 | static void migrate_nr_uninterruptible(struct rq *rq_src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6140 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6141 | struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | unsigned long flags; |
| 6143 | |
| 6144 | local_irq_save(flags); |
| 6145 | double_rq_lock(rq_src, rq_dest); |
| 6146 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; |
| 6147 | rq_src->nr_uninterruptible = 0; |
| 6148 | double_rq_unlock(rq_src, rq_dest); |
| 6149 | local_irq_restore(flags); |
| 6150 | } |
| 6151 | |
| 6152 | /* Run through task list and migrate tasks from the dead cpu. */ |
| 6153 | static void migrate_live_tasks(int src_cpu) |
| 6154 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6155 | struct task_struct *p, *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6157 | read_lock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6159 | do_each_thread(t, p) { |
| 6160 | if (p == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6161 | continue; |
| 6162 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6163 | if (task_cpu(p) == src_cpu) |
| 6164 | move_task_off_dead_cpu(src_cpu, p); |
| 6165 | } while_each_thread(t, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6167 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | } |
| 6169 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6170 | /* |
| 6171 | * Schedules idle task to be the next runnable task on current CPU. |
Dmitry Adamushko | 94bc9a7 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 6172 | * It does so by boosting its priority to highest possible. |
| 6173 | * Used by CPU offline code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | */ |
| 6175 | void sched_idle_next(void) |
| 6176 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6177 | int this_cpu = smp_processor_id(); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6178 | struct rq *rq = cpu_rq(this_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | struct task_struct *p = rq->idle; |
| 6180 | unsigned long flags; |
| 6181 | |
| 6182 | /* cpu has to be offline */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6183 | BUG_ON(cpu_online(this_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6184 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6185 | /* |
| 6186 | * Strictly not necessary since rest of the CPUs are stopped by now |
| 6187 | * and interrupts disabled on the current cpu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6188 | */ |
| 6189 | spin_lock_irqsave(&rq->lock, flags); |
| 6190 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6191 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6192 | |
Dmitry Adamushko | 94bc9a7 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 6193 | update_rq_clock(rq); |
| 6194 | activate_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | |
| 6196 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6197 | } |
| 6198 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6199 | /* |
| 6200 | * 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] | 6201 | * offline. |
| 6202 | */ |
| 6203 | void idle_task_exit(void) |
| 6204 | { |
| 6205 | struct mm_struct *mm = current->active_mm; |
| 6206 | |
| 6207 | BUG_ON(cpu_online(smp_processor_id())); |
| 6208 | |
| 6209 | if (mm != &init_mm) |
| 6210 | switch_mm(mm, &init_mm, current); |
| 6211 | mmdrop(mm); |
| 6212 | } |
| 6213 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 6214 | /* called under rq->lock with disabled interrupts */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 6215 | static void migrate_dead(unsigned int dead_cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6217 | struct rq *rq = cpu_rq(dead_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | |
| 6219 | /* Must be exiting, otherwise would be on tasklist. */ |
Eugene Teo | 270f722 | 2007-10-18 23:40:38 -0700 | [diff] [blame] | 6220 | BUG_ON(!p->exit_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6221 | |
| 6222 | /* Cannot have done final schedule yet: would have vanished. */ |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 6223 | BUG_ON(p->state == TASK_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6225 | get_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | |
| 6227 | /* |
| 6228 | * Drop lock around migration; if someone else moves it, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6229 | * that's OK. No task can be added to this CPU, so iteration is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6230 | * fine. |
| 6231 | */ |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6232 | spin_unlock_irq(&rq->lock); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6233 | move_task_off_dead_cpu(dead_cpu, p); |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6234 | spin_lock_irq(&rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6236 | put_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | } |
| 6238 | |
| 6239 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ |
| 6240 | static void migrate_dead_tasks(unsigned int dead_cpu) |
| 6241 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6242 | struct rq *rq = cpu_rq(dead_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6243 | struct task_struct *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6244 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6245 | for ( ; ; ) { |
| 6246 | if (!rq->nr_running) |
| 6247 | break; |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 6248 | update_rq_clock(rq); |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 6249 | next = pick_next_task(rq, rq->curr); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6250 | if (!next) |
| 6251 | break; |
| 6252 | migrate_dead(dead_cpu, next); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | } |
| 6255 | } |
| 6256 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 6257 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6258 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
| 6259 | |
| 6260 | static struct ctl_table sd_ctl_dir[] = { |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6261 | { |
| 6262 | .procname = "sched_domain", |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6263 | .mode = 0555, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6264 | }, |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 6265 | {0, }, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6266 | }; |
| 6267 | |
| 6268 | static struct ctl_table sd_ctl_root[] = { |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6269 | { |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6270 | .ctl_name = CTL_KERN, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6271 | .procname = "kernel", |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6272 | .mode = 0555, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6273 | .child = sd_ctl_dir, |
| 6274 | }, |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 6275 | {0, }, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6276 | }; |
| 6277 | |
| 6278 | static struct ctl_table *sd_alloc_ctl_entry(int n) |
| 6279 | { |
| 6280 | struct ctl_table *entry = |
Milton Miller | 5cf9f06 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6281 | kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6282 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6283 | return entry; |
| 6284 | } |
| 6285 | |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6286 | static void sd_free_ctl_entry(struct ctl_table **tablep) |
| 6287 | { |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 6288 | struct ctl_table *entry; |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6289 | |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 6290 | /* |
| 6291 | * In the intermediate directories, both the child directory and |
| 6292 | * procname are dynamically allocated and could fail but the mode |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6293 | * will always be set. In the lowest directory the names are |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 6294 | * static strings and all have proc handlers. |
| 6295 | */ |
| 6296 | for (entry = *tablep; entry->mode; entry++) { |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6297 | if (entry->child) |
| 6298 | sd_free_ctl_entry(&entry->child); |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 6299 | if (entry->proc_handler == NULL) |
| 6300 | kfree(entry->procname); |
| 6301 | } |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6302 | |
| 6303 | kfree(*tablep); |
| 6304 | *tablep = NULL; |
| 6305 | } |
| 6306 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6307 | static void |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6308 | set_table_entry(struct ctl_table *entry, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6309 | const char *procname, void *data, int maxlen, |
| 6310 | mode_t mode, proc_handler *proc_handler) |
| 6311 | { |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6312 | entry->procname = procname; |
| 6313 | entry->data = data; |
| 6314 | entry->maxlen = maxlen; |
| 6315 | entry->mode = mode; |
| 6316 | entry->proc_handler = proc_handler; |
| 6317 | } |
| 6318 | |
| 6319 | static struct ctl_table * |
| 6320 | sd_alloc_ctl_domain_table(struct sched_domain *sd) |
| 6321 | { |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6322 | struct ctl_table *table = sd_alloc_ctl_entry(12); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6323 | |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6324 | if (table == NULL) |
| 6325 | return NULL; |
| 6326 | |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6327 | set_table_entry(&table[0], "min_interval", &sd->min_interval, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6328 | sizeof(long), 0644, proc_doulongvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6329 | set_table_entry(&table[1], "max_interval", &sd->max_interval, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6330 | sizeof(long), 0644, proc_doulongvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6331 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6332 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6333 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6334 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6335 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6336 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6337 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6338 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6339 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6340 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6341 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6342 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6343 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6344 | sizeof(int), 0644, proc_dointvec_minmax); |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6345 | set_table_entry(&table[9], "cache_nice_tries", |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6346 | &sd->cache_nice_tries, |
| 6347 | sizeof(int), 0644, proc_dointvec_minmax); |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6348 | set_table_entry(&table[10], "flags", &sd->flags, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6349 | sizeof(int), 0644, proc_dointvec_minmax); |
Milton Miller | 6323469f | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6350 | /* &table[11] is terminator */ |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6351 | |
| 6352 | return table; |
| 6353 | } |
| 6354 | |
Ingo Molnar | 9a4e715 | 2007-11-28 15:52:56 +0100 | [diff] [blame] | 6355 | static ctl_table *sd_alloc_ctl_cpu_table(int cpu) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6356 | { |
| 6357 | struct ctl_table *entry, *table; |
| 6358 | struct sched_domain *sd; |
| 6359 | int domain_num = 0, i; |
| 6360 | char buf[32]; |
| 6361 | |
| 6362 | for_each_domain(cpu, sd) |
| 6363 | domain_num++; |
| 6364 | entry = table = sd_alloc_ctl_entry(domain_num + 1); |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6365 | if (table == NULL) |
| 6366 | return NULL; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6367 | |
| 6368 | i = 0; |
| 6369 | for_each_domain(cpu, sd) { |
| 6370 | snprintf(buf, 32, "domain%d", i); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6371 | entry->procname = kstrdup(buf, GFP_KERNEL); |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6372 | entry->mode = 0555; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6373 | entry->child = sd_alloc_ctl_domain_table(sd); |
| 6374 | entry++; |
| 6375 | i++; |
| 6376 | } |
| 6377 | return table; |
| 6378 | } |
| 6379 | |
| 6380 | static struct ctl_table_header *sd_sysctl_header; |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6381 | static void register_sched_domain_sysctl(void) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6382 | { |
| 6383 | int i, cpu_num = num_online_cpus(); |
| 6384 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); |
| 6385 | char buf[32]; |
| 6386 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6387 | WARN_ON(sd_ctl_dir[0].child); |
| 6388 | sd_ctl_dir[0].child = entry; |
| 6389 | |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6390 | if (entry == NULL) |
| 6391 | return; |
| 6392 | |
Milton Miller | 97b6ea7 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6393 | for_each_online_cpu(i) { |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6394 | snprintf(buf, 32, "cpu%d", i); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6395 | entry->procname = kstrdup(buf, GFP_KERNEL); |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6396 | entry->mode = 0555; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6397 | entry->child = sd_alloc_ctl_cpu_table(i); |
Milton Miller | 97b6ea7 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6398 | entry++; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6399 | } |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6400 | |
| 6401 | WARN_ON(sd_sysctl_header); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6402 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); |
| 6403 | } |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6404 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6405 | /* may be called multiple times per register */ |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6406 | static void unregister_sched_domain_sysctl(void) |
| 6407 | { |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6408 | if (sd_sysctl_header) |
| 6409 | unregister_sysctl_table(sd_sysctl_header); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6410 | sd_sysctl_header = NULL; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6411 | if (sd_ctl_dir[0].child) |
| 6412 | sd_free_ctl_entry(&sd_ctl_dir[0].child); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6413 | } |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6414 | #else |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6415 | static void register_sched_domain_sysctl(void) |
| 6416 | { |
| 6417 | } |
| 6418 | static void unregister_sched_domain_sysctl(void) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6419 | { |
| 6420 | } |
| 6421 | #endif |
| 6422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | /* |
| 6424 | * migration_call - callback that gets triggered when a CPU is added. |
| 6425 | * Here we can start up the necessary migration thread for the new CPU. |
| 6426 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6427 | static int __cpuinit |
| 6428 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6429 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | struct task_struct *p; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6431 | int cpu = (long)hcpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6433 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | |
| 6435 | switch (action) { |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 6436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6438 | case CPU_UP_PREPARE_FROZEN: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6439 | p = kthread_create(migration_thread, hcpu, "migration/%d", cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6440 | if (IS_ERR(p)) |
| 6441 | return NOTIFY_BAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6442 | kthread_bind(p, cpu); |
| 6443 | /* Must be high prio: stop_machine expects to yield to it. */ |
| 6444 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6445 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | task_rq_unlock(rq, &flags); |
| 6447 | cpu_rq(cpu)->migration_thread = p; |
| 6448 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6451 | case CPU_ONLINE_FROZEN: |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 6452 | /* Strictly unnecessary, as first user will wake it. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | wake_up_process(cpu_rq(cpu)->migration_thread); |
Gregory Haskins | 1f94ef5 | 2008-03-10 16:52:41 -0400 | [diff] [blame] | 6454 | |
| 6455 | /* Update our root-domain */ |
| 6456 | rq = cpu_rq(cpu); |
| 6457 | spin_lock_irqsave(&rq->lock, flags); |
| 6458 | if (rq->rd) { |
| 6459 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); |
| 6460 | cpu_set(cpu, rq->rd->online); |
| 6461 | } |
| 6462 | spin_unlock_irqrestore(&rq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | #ifdef CONFIG_HOTPLUG_CPU |
| 6466 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6467 | case CPU_UP_CANCELED_FROZEN: |
Heiko Carstens | fc75cdf | 2006-06-25 05:49:10 -0700 | [diff] [blame] | 6468 | if (!cpu_rq(cpu)->migration_thread) |
| 6469 | break; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6470 | /* Unbind it from offline cpu so it can run. Fall thru. */ |
Heiko Carstens | a4c4af7 | 2005-11-07 00:58:38 -0800 | [diff] [blame] | 6471 | kthread_bind(cpu_rq(cpu)->migration_thread, |
| 6472 | any_online_cpu(cpu_online_map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6473 | kthread_stop(cpu_rq(cpu)->migration_thread); |
| 6474 | cpu_rq(cpu)->migration_thread = NULL; |
| 6475 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6478 | case CPU_DEAD_FROZEN: |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6479 | cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6480 | migrate_live_tasks(cpu); |
| 6481 | rq = cpu_rq(cpu); |
| 6482 | kthread_stop(rq->migration_thread); |
| 6483 | rq->migration_thread = NULL; |
| 6484 | /* Idle task back to normal (off runqueue, low prio) */ |
Oleg Nesterov | d2da272 | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6485 | spin_lock_irq(&rq->lock); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 6486 | update_rq_clock(rq); |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 6487 | deactivate_task(rq, rq->idle, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6488 | rq->idle->static_prio = MAX_PRIO; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6489 | __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); |
| 6490 | rq->idle->sched_class = &idle_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | migrate_dead_tasks(cpu); |
Oleg Nesterov | d2da272 | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6492 | spin_unlock_irq(&rq->lock); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6493 | cpuset_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | migrate_nr_uninterruptible(rq); |
| 6495 | BUG_ON(rq->nr_running != 0); |
| 6496 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6497 | /* |
| 6498 | * No need to migrate the tasks: it was best-effort if |
| 6499 | * they didn't take sched_hotcpu_mutex. Just wake up |
| 6500 | * the requestors. |
| 6501 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | spin_lock_irq(&rq->lock); |
| 6503 | while (!list_empty(&rq->migration_queue)) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6504 | struct migration_req *req; |
| 6505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | req = list_entry(rq->migration_queue.next, |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6507 | struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6508 | list_del_init(&req->list); |
| 6509 | complete(&req->done); |
| 6510 | } |
| 6511 | spin_unlock_irq(&rq->lock); |
| 6512 | break; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6513 | |
Gregory Haskins | 08f503b | 2008-03-10 17:59:11 -0400 | [diff] [blame] | 6514 | case CPU_DYING: |
| 6515 | case CPU_DYING_FROZEN: |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6516 | /* Update our root-domain */ |
| 6517 | rq = cpu_rq(cpu); |
| 6518 | spin_lock_irqsave(&rq->lock, flags); |
| 6519 | if (rq->rd) { |
| 6520 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); |
| 6521 | cpu_clear(cpu, rq->rd->online); |
| 6522 | } |
| 6523 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6524 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6525 | #endif |
| 6526 | } |
| 6527 | return NOTIFY_OK; |
| 6528 | } |
| 6529 | |
| 6530 | /* Register at highest priority so that task migration (migrate_all_tasks) |
| 6531 | * happens before everything else. |
| 6532 | */ |
Chandra Seetharaman | 26c2143 | 2006-06-27 02:54:10 -0700 | [diff] [blame] | 6533 | static struct notifier_block __cpuinitdata migration_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6534 | .notifier_call = migration_call, |
| 6535 | .priority = 10 |
| 6536 | }; |
| 6537 | |
Adrian Bunk | e6fe664 | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 6538 | void __init migration_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | { |
| 6540 | void *cpu = (void *)(long)smp_processor_id(); |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 6541 | int err; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6542 | |
| 6543 | /* Start one for the boot CPU: */ |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 6544 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
| 6545 | BUG_ON(err == NOTIFY_BAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6546 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
| 6547 | register_cpu_notifier(&migration_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | } |
| 6549 | #endif |
| 6550 | |
| 6551 | #ifdef CONFIG_SMP |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 6552 | |
Ingo Molnar | 3e9830d | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 6553 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6554 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6555 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, |
| 6556 | cpumask_t *groupmask) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6557 | { |
| 6558 | struct sched_group *group = sd->groups; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6559 | char str[256]; |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6560 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6561 | cpulist_scnprintf(str, sizeof(str), sd->span); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6562 | cpus_clear(*groupmask); |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6563 | |
| 6564 | printk(KERN_DEBUG "%*s domain %d: ", level, "", level); |
| 6565 | |
| 6566 | if (!(sd->flags & SD_LOAD_BALANCE)) { |
| 6567 | printk("does not load-balance\n"); |
| 6568 | if (sd->parent) |
| 6569 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" |
| 6570 | " has parent"); |
| 6571 | return -1; |
| 6572 | } |
| 6573 | |
| 6574 | printk(KERN_CONT "span %s\n", str); |
| 6575 | |
| 6576 | if (!cpu_isset(cpu, sd->span)) { |
| 6577 | printk(KERN_ERR "ERROR: domain->span does not contain " |
| 6578 | "CPU%d\n", cpu); |
| 6579 | } |
| 6580 | if (!cpu_isset(cpu, group->cpumask)) { |
| 6581 | printk(KERN_ERR "ERROR: domain->groups does not contain" |
| 6582 | " CPU%d\n", cpu); |
| 6583 | } |
| 6584 | |
| 6585 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); |
| 6586 | do { |
| 6587 | if (!group) { |
| 6588 | printk("\n"); |
| 6589 | printk(KERN_ERR "ERROR: group is NULL\n"); |
| 6590 | break; |
| 6591 | } |
| 6592 | |
| 6593 | if (!group->__cpu_power) { |
| 6594 | printk(KERN_CONT "\n"); |
| 6595 | printk(KERN_ERR "ERROR: domain->cpu_power not " |
| 6596 | "set\n"); |
| 6597 | break; |
| 6598 | } |
| 6599 | |
| 6600 | if (!cpus_weight(group->cpumask)) { |
| 6601 | printk(KERN_CONT "\n"); |
| 6602 | printk(KERN_ERR "ERROR: empty group\n"); |
| 6603 | break; |
| 6604 | } |
| 6605 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6606 | if (cpus_intersects(*groupmask, group->cpumask)) { |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6607 | printk(KERN_CONT "\n"); |
| 6608 | printk(KERN_ERR "ERROR: repeated CPUs\n"); |
| 6609 | break; |
| 6610 | } |
| 6611 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6612 | cpus_or(*groupmask, *groupmask, group->cpumask); |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6613 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6614 | cpulist_scnprintf(str, sizeof(str), group->cpumask); |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6615 | printk(KERN_CONT " %s", str); |
| 6616 | |
| 6617 | group = group->next; |
| 6618 | } while (group != sd->groups); |
| 6619 | printk(KERN_CONT "\n"); |
| 6620 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6621 | if (!cpus_equal(sd->span, *groupmask)) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6622 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
| 6623 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6624 | if (sd->parent && !cpus_subset(*groupmask, sd->parent->span)) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6625 | printk(KERN_ERR "ERROR: parent span is not a superset " |
| 6626 | "of domain->span\n"); |
| 6627 | return 0; |
| 6628 | } |
| 6629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
| 6631 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6632 | cpumask_t *groupmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | int level = 0; |
| 6634 | |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 6635 | if (!sd) { |
| 6636 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); |
| 6637 | return; |
| 6638 | } |
| 6639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6640 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
| 6641 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6642 | groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL); |
| 6643 | if (!groupmask) { |
| 6644 | printk(KERN_DEBUG "Cannot load-balance (out of memory)\n"); |
| 6645 | return; |
| 6646 | } |
| 6647 | |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6648 | for (;;) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6649 | if (sched_domain_debug_one(sd, cpu, level, groupmask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | level++; |
| 6652 | sd = sd->parent; |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 6653 | if (!sd) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6654 | break; |
| 6655 | } |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6656 | kfree(groupmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6657 | } |
| 6658 | #else |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6659 | # define sched_domain_debug(sd, cpu) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | #endif |
| 6661 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6662 | static int sd_degenerate(struct sched_domain *sd) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6663 | { |
| 6664 | if (cpus_weight(sd->span) == 1) |
| 6665 | return 1; |
| 6666 | |
| 6667 | /* Following flags need at least 2 groups */ |
| 6668 | if (sd->flags & (SD_LOAD_BALANCE | |
| 6669 | SD_BALANCE_NEWIDLE | |
| 6670 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6671 | SD_BALANCE_EXEC | |
| 6672 | SD_SHARE_CPUPOWER | |
| 6673 | SD_SHARE_PKG_RESOURCES)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6674 | if (sd->groups != sd->groups->next) |
| 6675 | return 0; |
| 6676 | } |
| 6677 | |
| 6678 | /* Following flags don't use groups */ |
| 6679 | if (sd->flags & (SD_WAKE_IDLE | |
| 6680 | SD_WAKE_AFFINE | |
| 6681 | SD_WAKE_BALANCE)) |
| 6682 | return 0; |
| 6683 | |
| 6684 | return 1; |
| 6685 | } |
| 6686 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6687 | static int |
| 6688 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6689 | { |
| 6690 | unsigned long cflags = sd->flags, pflags = parent->flags; |
| 6691 | |
| 6692 | if (sd_degenerate(parent)) |
| 6693 | return 1; |
| 6694 | |
| 6695 | if (!cpus_equal(sd->span, parent->span)) |
| 6696 | return 0; |
| 6697 | |
| 6698 | /* Does parent contain flags not in child? */ |
| 6699 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ |
| 6700 | if (cflags & SD_WAKE_AFFINE) |
| 6701 | pflags &= ~SD_WAKE_BALANCE; |
| 6702 | /* Flags needing groups don't count if only 1 group in parent */ |
| 6703 | if (parent->groups == parent->groups->next) { |
| 6704 | pflags &= ~(SD_LOAD_BALANCE | |
| 6705 | SD_BALANCE_NEWIDLE | |
| 6706 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6707 | SD_BALANCE_EXEC | |
| 6708 | SD_SHARE_CPUPOWER | |
| 6709 | SD_SHARE_PKG_RESOURCES); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6710 | } |
| 6711 | if (~cflags & pflags) |
| 6712 | return 0; |
| 6713 | |
| 6714 | return 1; |
| 6715 | } |
| 6716 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6717 | static void rq_attach_root(struct rq *rq, struct root_domain *rd) |
| 6718 | { |
| 6719 | unsigned long flags; |
| 6720 | const struct sched_class *class; |
| 6721 | |
| 6722 | spin_lock_irqsave(&rq->lock, flags); |
| 6723 | |
| 6724 | if (rq->rd) { |
| 6725 | struct root_domain *old_rd = rq->rd; |
| 6726 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6727 | for (class = sched_class_highest; class; class = class->next) { |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6728 | if (class->leave_domain) |
| 6729 | class->leave_domain(rq); |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6730 | } |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6731 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6732 | cpu_clear(rq->cpu, old_rd->span); |
| 6733 | cpu_clear(rq->cpu, old_rd->online); |
| 6734 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6735 | if (atomic_dec_and_test(&old_rd->refcount)) |
| 6736 | kfree(old_rd); |
| 6737 | } |
| 6738 | |
| 6739 | atomic_inc(&rd->refcount); |
| 6740 | rq->rd = rd; |
| 6741 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6742 | cpu_set(rq->cpu, rd->span); |
Gregory Haskins | 1f94ef5 | 2008-03-10 16:52:41 -0400 | [diff] [blame] | 6743 | if (cpu_isset(rq->cpu, cpu_online_map)) |
| 6744 | cpu_set(rq->cpu, rd->online); |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6745 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6746 | for (class = sched_class_highest; class; class = class->next) { |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6747 | if (class->join_domain) |
| 6748 | class->join_domain(rq); |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6749 | } |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6750 | |
| 6751 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6752 | } |
| 6753 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6754 | static void init_rootdomain(struct root_domain *rd) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6755 | { |
| 6756 | memset(rd, 0, sizeof(*rd)); |
| 6757 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6758 | cpus_clear(rd->span); |
| 6759 | cpus_clear(rd->online); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6760 | } |
| 6761 | |
| 6762 | static void init_defrootdomain(void) |
| 6763 | { |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6764 | init_rootdomain(&def_root_domain); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6765 | atomic_set(&def_root_domain.refcount, 1); |
| 6766 | } |
| 6767 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6768 | static struct root_domain *alloc_rootdomain(void) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6769 | { |
| 6770 | struct root_domain *rd; |
| 6771 | |
| 6772 | rd = kmalloc(sizeof(*rd), GFP_KERNEL); |
| 6773 | if (!rd) |
| 6774 | return NULL; |
| 6775 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6776 | init_rootdomain(rd); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6777 | |
| 6778 | return rd; |
| 6779 | } |
| 6780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | /* |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6782 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | * hold the hotplug lock. |
| 6784 | */ |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6785 | static void |
| 6786 | cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6787 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6788 | struct rq *rq = cpu_rq(cpu); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6789 | struct sched_domain *tmp; |
| 6790 | |
| 6791 | /* Remove the sched domains which do not contribute to scheduling. */ |
| 6792 | for (tmp = sd; tmp; tmp = tmp->parent) { |
| 6793 | struct sched_domain *parent = tmp->parent; |
| 6794 | if (!parent) |
| 6795 | break; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6796 | if (sd_parent_degenerate(tmp, parent)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6797 | tmp->parent = parent->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6798 | if (parent->parent) |
| 6799 | parent->parent->child = tmp; |
| 6800 | } |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6801 | } |
| 6802 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6803 | if (sd && sd_degenerate(sd)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6804 | sd = sd->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6805 | if (sd) |
| 6806 | sd->child = NULL; |
| 6807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | |
| 6809 | sched_domain_debug(sd, cpu); |
| 6810 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6811 | rq_attach_root(rq, rd); |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 6812 | rcu_assign_pointer(rq->sd, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | } |
| 6814 | |
| 6815 | /* cpus with isolated domains */ |
Tim Chen | 67af63a | 2006-12-22 01:07:50 -0800 | [diff] [blame] | 6816 | static cpumask_t cpu_isolated_map = CPU_MASK_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | |
| 6818 | /* Setup the mask of cpus configured for isolated domains */ |
| 6819 | static int __init isolated_cpu_setup(char *str) |
| 6820 | { |
| 6821 | int ints[NR_CPUS], i; |
| 6822 | |
| 6823 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 6824 | cpus_clear(cpu_isolated_map); |
| 6825 | for (i = 1; i <= ints[0]; i++) |
| 6826 | if (ints[i] < NR_CPUS) |
| 6827 | cpu_set(ints[i], cpu_isolated_map); |
| 6828 | return 1; |
| 6829 | } |
| 6830 | |
Ingo Molnar | 8927f49 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 6831 | __setup("isolcpus=", isolated_cpu_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | |
| 6833 | /* |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6834 | * init_sched_build_groups takes the cpumask we wish to span, and a pointer |
| 6835 | * to a function which identifies what group(along with sched group) a CPU |
| 6836 | * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS |
| 6837 | * (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] | 6838 | * |
| 6839 | * init_sched_build_groups will build a circular linked list of the groups |
| 6840 | * covered by the given span, and will set each group's ->cpumask correctly, |
| 6841 | * and ->cpu_power to 0. |
| 6842 | */ |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6843 | static void |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6844 | init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map, |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6845 | int (*group_fn)(int cpu, const cpumask_t *cpu_map, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6846 | struct sched_group **sg, |
| 6847 | cpumask_t *tmpmask), |
| 6848 | cpumask_t *covered, cpumask_t *tmpmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6849 | { |
| 6850 | struct sched_group *first = NULL, *last = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6851 | int i; |
| 6852 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6853 | cpus_clear(*covered); |
| 6854 | |
| 6855 | for_each_cpu_mask(i, *span) { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6856 | struct sched_group *sg; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6857 | int group = group_fn(i, cpu_map, &sg, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6858 | int j; |
| 6859 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6860 | if (cpu_isset(i, *covered)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | continue; |
| 6862 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6863 | cpus_clear(sg->cpumask); |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6864 | sg->__cpu_power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6866 | for_each_cpu_mask(j, *span) { |
| 6867 | if (group_fn(j, cpu_map, NULL, tmpmask) != group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | continue; |
| 6869 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6870 | cpu_set(j, *covered); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | cpu_set(j, sg->cpumask); |
| 6872 | } |
| 6873 | if (!first) |
| 6874 | first = sg; |
| 6875 | if (last) |
| 6876 | last->next = sg; |
| 6877 | last = sg; |
| 6878 | } |
| 6879 | last->next = first; |
| 6880 | } |
| 6881 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6882 | #define SD_NODES_PER_DOMAIN 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6883 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6884 | #ifdef CONFIG_NUMA |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 6885 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6886 | /** |
| 6887 | * find_next_best_node - find the next node to include in a sched_domain |
| 6888 | * @node: node whose sched_domain we're building |
| 6889 | * @used_nodes: nodes already in the sched_domain |
| 6890 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6891 | * Find the next node to include in a given scheduling domain. Simply |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6892 | * finds the closest node not already in the @used_nodes map. |
| 6893 | * |
| 6894 | * Should use nodemask_t. |
| 6895 | */ |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6896 | static int find_next_best_node(int node, nodemask_t *used_nodes) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6897 | { |
| 6898 | int i, n, val, min_val, best_node = 0; |
| 6899 | |
| 6900 | min_val = INT_MAX; |
| 6901 | |
| 6902 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6903 | /* Start at @node */ |
| 6904 | n = (node + i) % MAX_NUMNODES; |
| 6905 | |
| 6906 | if (!nr_cpus_node(n)) |
| 6907 | continue; |
| 6908 | |
| 6909 | /* Skip already used nodes */ |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6910 | if (node_isset(n, *used_nodes)) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6911 | continue; |
| 6912 | |
| 6913 | /* Simple min distance search */ |
| 6914 | val = node_distance(node, n); |
| 6915 | |
| 6916 | if (val < min_val) { |
| 6917 | min_val = val; |
| 6918 | best_node = n; |
| 6919 | } |
| 6920 | } |
| 6921 | |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6922 | node_set(best_node, *used_nodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6923 | return best_node; |
| 6924 | } |
| 6925 | |
| 6926 | /** |
| 6927 | * sched_domain_node_span - get a cpumask for a node's sched_domain |
| 6928 | * @node: node whose cpumask we're constructing |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6929 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6930 | * Given a node, construct a good cpumask for its sched_domain to span. It |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6931 | * should be one that prevents unnecessary balancing, but also spreads tasks |
| 6932 | * out optimally. |
| 6933 | */ |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 6934 | static void sched_domain_node_span(int node, cpumask_t *span) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6935 | { |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6936 | nodemask_t used_nodes; |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6937 | node_to_cpumask_ptr(nodemask, node); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6938 | int i; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6939 | |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 6940 | cpus_clear(*span); |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6941 | nodes_clear(used_nodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6942 | |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 6943 | cpus_or(*span, *span, *nodemask); |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6944 | node_set(node, used_nodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6945 | |
| 6946 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6947 | int next_node = find_next_best_node(node, &used_nodes); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6948 | |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6949 | node_to_cpumask_ptr_next(nodemask, next_node); |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 6950 | cpus_or(*span, *span, *nodemask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6951 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6952 | } |
| 6953 | #endif |
| 6954 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6955 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6956 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6957 | /* |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6958 | * SMT sched-domains: |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6959 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | #ifdef CONFIG_SCHED_SMT |
| 6961 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6962 | static DEFINE_PER_CPU(struct sched_group, sched_group_cpus); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6963 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6964 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6965 | cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 6966 | cpumask_t *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6967 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6968 | if (sg) |
| 6969 | *sg = &per_cpu(sched_group_cpus, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | return cpu; |
| 6971 | } |
| 6972 | #endif |
| 6973 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6974 | /* |
| 6975 | * multi-core sched-domains: |
| 6976 | */ |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6977 | #ifdef CONFIG_SCHED_MC |
| 6978 | static DEFINE_PER_CPU(struct sched_domain, core_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6979 | static DEFINE_PER_CPU(struct sched_group, sched_group_core); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6980 | #endif |
| 6981 | |
| 6982 | #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6983 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6984 | cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 6985 | cpumask_t *mask) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6986 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6987 | int group; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6988 | |
| 6989 | *mask = per_cpu(cpu_sibling_map, cpu); |
| 6990 | cpus_and(*mask, *mask, *cpu_map); |
| 6991 | group = first_cpu(*mask); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6992 | if (sg) |
| 6993 | *sg = &per_cpu(sched_group_core, group); |
| 6994 | return group; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6995 | } |
| 6996 | #elif defined(CONFIG_SCHED_MC) |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6997 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6998 | cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 6999 | cpumask_t *unused) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7000 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7001 | if (sg) |
| 7002 | *sg = &per_cpu(sched_group_core, cpu); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7003 | return cpu; |
| 7004 | } |
| 7005 | #endif |
| 7006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7008 | static DEFINE_PER_CPU(struct sched_group, sched_group_phys); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7009 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7010 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7011 | cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 7012 | cpumask_t *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7013 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7014 | int group; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7015 | #ifdef CONFIG_SCHED_MC |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7016 | *mask = cpu_coregroup_map(cpu); |
| 7017 | cpus_and(*mask, *mask, *cpu_map); |
| 7018 | group = first_cpu(*mask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7019 | #elif defined(CONFIG_SCHED_SMT) |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7020 | *mask = per_cpu(cpu_sibling_map, cpu); |
| 7021 | cpus_and(*mask, *mask, *cpu_map); |
| 7022 | group = first_cpu(*mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | #else |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7024 | group = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7025 | #endif |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7026 | if (sg) |
| 7027 | *sg = &per_cpu(sched_group_phys, group); |
| 7028 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7029 | } |
| 7030 | |
| 7031 | #ifdef CONFIG_NUMA |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7032 | /* |
| 7033 | * The init_sched_build_groups can't handle what we want to do with node |
| 7034 | * groups, so roll our own. Now each node has its own list of groups which |
| 7035 | * gets dynamically allocated. |
| 7036 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7037 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7038 | static struct sched_group ***sched_group_nodes_bycpu; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7039 | |
| 7040 | static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7041 | static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7042 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7043 | static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7044 | struct sched_group **sg, cpumask_t *nodemask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7045 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7046 | int group; |
| 7047 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7048 | *nodemask = node_to_cpumask(cpu_to_node(cpu)); |
| 7049 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 7050 | group = first_cpu(*nodemask); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7051 | |
| 7052 | if (sg) |
| 7053 | *sg = &per_cpu(sched_group_allnodes, group); |
| 7054 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7055 | } |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7056 | |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7057 | static void init_numa_sched_groups_power(struct sched_group *group_head) |
| 7058 | { |
| 7059 | struct sched_group *sg = group_head; |
| 7060 | int j; |
| 7061 | |
| 7062 | if (!sg) |
| 7063 | return; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7064 | do { |
| 7065 | for_each_cpu_mask(j, sg->cpumask) { |
| 7066 | struct sched_domain *sd; |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7067 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7068 | sd = &per_cpu(phys_domains, j); |
| 7069 | if (j != first_cpu(sd->groups->cpumask)) { |
| 7070 | /* |
| 7071 | * Only add "power" once for each |
| 7072 | * physical package. |
| 7073 | */ |
| 7074 | continue; |
| 7075 | } |
| 7076 | |
| 7077 | sg_inc_cpu_power(sg, sd->groups->__cpu_power); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7078 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7079 | sg = sg->next; |
| 7080 | } while (sg != group_head); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7081 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7082 | #endif |
| 7083 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7084 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7085 | /* Free memory allocated for various sched_group structures */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7086 | static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask) |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7087 | { |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7088 | int cpu, i; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7089 | |
| 7090 | for_each_cpu_mask(cpu, *cpu_map) { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7091 | struct sched_group **sched_group_nodes |
| 7092 | = sched_group_nodes_bycpu[cpu]; |
| 7093 | |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7094 | if (!sched_group_nodes) |
| 7095 | continue; |
| 7096 | |
| 7097 | for (i = 0; i < MAX_NUMNODES; i++) { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7098 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; |
| 7099 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7100 | *nodemask = node_to_cpumask(i); |
| 7101 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 7102 | if (cpus_empty(*nodemask)) |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7103 | continue; |
| 7104 | |
| 7105 | if (sg == NULL) |
| 7106 | continue; |
| 7107 | sg = sg->next; |
| 7108 | next_sg: |
| 7109 | oldsg = sg; |
| 7110 | sg = sg->next; |
| 7111 | kfree(oldsg); |
| 7112 | if (oldsg != sched_group_nodes[i]) |
| 7113 | goto next_sg; |
| 7114 | } |
| 7115 | kfree(sched_group_nodes); |
| 7116 | sched_group_nodes_bycpu[cpu] = NULL; |
| 7117 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7118 | } |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7119 | #else |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7120 | static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask) |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7121 | { |
| 7122 | } |
| 7123 | #endif |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7125 | /* |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7126 | * Initialize sched groups cpu_power. |
| 7127 | * |
| 7128 | * cpu_power indicates the capacity of sched group, which is used while |
| 7129 | * distributing the load between different sched groups in a sched domain. |
| 7130 | * Typically cpu_power for all the groups in a sched domain will be same unless |
| 7131 | * there are asymmetries in the topology. If there are asymmetries, group |
| 7132 | * having more cpu_power will pickup more load compared to the group having |
| 7133 | * less cpu_power. |
| 7134 | * |
| 7135 | * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents |
| 7136 | * the maximum number of tasks a group can handle in the presence of other idle |
| 7137 | * or lightly loaded groups in the same sched domain. |
| 7138 | */ |
| 7139 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) |
| 7140 | { |
| 7141 | struct sched_domain *child; |
| 7142 | struct sched_group *group; |
| 7143 | |
| 7144 | WARN_ON(!sd || !sd->groups); |
| 7145 | |
| 7146 | if (cpu != first_cpu(sd->groups->cpumask)) |
| 7147 | return; |
| 7148 | |
| 7149 | child = sd->child; |
| 7150 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7151 | sd->groups->__cpu_power = 0; |
| 7152 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7153 | /* |
| 7154 | * For perf policy, if the groups in child domain share resources |
| 7155 | * (for example cores sharing some portions of the cache hierarchy |
| 7156 | * or SMT), then set this domain groups cpu_power such that each group |
| 7157 | * can handle only one task, when there are other idle groups in the |
| 7158 | * same sched domain. |
| 7159 | */ |
| 7160 | if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) && |
| 7161 | (child->flags & |
| 7162 | (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7163 | sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7164 | return; |
| 7165 | } |
| 7166 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7167 | /* |
| 7168 | * add cpu_power of each child group to this groups cpu_power |
| 7169 | */ |
| 7170 | group = child->groups; |
| 7171 | do { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7172 | sg_inc_cpu_power(sd->groups, group->__cpu_power); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7173 | group = group->next; |
| 7174 | } while (group != child->groups); |
| 7175 | } |
| 7176 | |
| 7177 | /* |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7178 | * Initializers for schedule domains |
| 7179 | * Non-inlined to reduce accumulated stack pressure in build_sched_domains() |
| 7180 | */ |
| 7181 | |
| 7182 | #define SD_INIT(sd, type) sd_init_##type(sd) |
| 7183 | #define SD_INIT_FUNC(type) \ |
| 7184 | static noinline void sd_init_##type(struct sched_domain *sd) \ |
| 7185 | { \ |
| 7186 | memset(sd, 0, sizeof(*sd)); \ |
| 7187 | *sd = SD_##type##_INIT; \ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7188 | sd->level = SD_LV_##type; \ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7189 | } |
| 7190 | |
| 7191 | SD_INIT_FUNC(CPU) |
| 7192 | #ifdef CONFIG_NUMA |
| 7193 | SD_INIT_FUNC(ALLNODES) |
| 7194 | SD_INIT_FUNC(NODE) |
| 7195 | #endif |
| 7196 | #ifdef CONFIG_SCHED_SMT |
| 7197 | SD_INIT_FUNC(SIBLING) |
| 7198 | #endif |
| 7199 | #ifdef CONFIG_SCHED_MC |
| 7200 | SD_INIT_FUNC(MC) |
| 7201 | #endif |
| 7202 | |
| 7203 | /* |
| 7204 | * To minimize stack usage kmalloc room for cpumasks and share the |
| 7205 | * space as the usage in build_sched_domains() dictates. Used only |
| 7206 | * if the amount of space is significant. |
| 7207 | */ |
| 7208 | struct allmasks { |
| 7209 | cpumask_t tmpmask; /* make this one first */ |
| 7210 | union { |
| 7211 | cpumask_t nodemask; |
| 7212 | cpumask_t this_sibling_map; |
| 7213 | cpumask_t this_core_map; |
| 7214 | }; |
| 7215 | cpumask_t send_covered; |
| 7216 | |
| 7217 | #ifdef CONFIG_NUMA |
| 7218 | cpumask_t domainspan; |
| 7219 | cpumask_t covered; |
| 7220 | cpumask_t notcovered; |
| 7221 | #endif |
| 7222 | }; |
| 7223 | |
| 7224 | #if NR_CPUS > 128 |
| 7225 | #define SCHED_CPUMASK_ALLOC 1 |
| 7226 | #define SCHED_CPUMASK_FREE(v) kfree(v) |
| 7227 | #define SCHED_CPUMASK_DECLARE(v) struct allmasks *v |
| 7228 | #else |
| 7229 | #define SCHED_CPUMASK_ALLOC 0 |
| 7230 | #define SCHED_CPUMASK_FREE(v) |
| 7231 | #define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v |
| 7232 | #endif |
| 7233 | |
| 7234 | #define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \ |
| 7235 | ((unsigned long)(a) + offsetof(struct allmasks, v)) |
| 7236 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7237 | static int default_relax_domain_level = -1; |
| 7238 | |
| 7239 | static int __init setup_relax_domain_level(char *str) |
| 7240 | { |
| 7241 | default_relax_domain_level = simple_strtoul(str, NULL, 0); |
| 7242 | return 1; |
| 7243 | } |
| 7244 | __setup("relax_domain_level=", setup_relax_domain_level); |
| 7245 | |
| 7246 | static void set_domain_attribute(struct sched_domain *sd, |
| 7247 | struct sched_domain_attr *attr) |
| 7248 | { |
| 7249 | int request; |
| 7250 | |
| 7251 | if (!attr || attr->relax_domain_level < 0) { |
| 7252 | if (default_relax_domain_level < 0) |
| 7253 | return; |
| 7254 | else |
| 7255 | request = default_relax_domain_level; |
| 7256 | } else |
| 7257 | request = attr->relax_domain_level; |
| 7258 | if (request < sd->level) { |
| 7259 | /* turn off idle balance on this domain */ |
| 7260 | sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE); |
| 7261 | } else { |
| 7262 | /* turn on idle balance on this domain */ |
| 7263 | sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE); |
| 7264 | } |
| 7265 | } |
| 7266 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7267 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7268 | * Build sched domains for a given set of cpus and attach the sched domains |
| 7269 | * to the individual cpus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7270 | */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7271 | static int __build_sched_domains(const cpumask_t *cpu_map, |
| 7272 | struct sched_domain_attr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7273 | { |
| 7274 | int i; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7275 | struct root_domain *rd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7276 | SCHED_CPUMASK_DECLARE(allmasks); |
| 7277 | cpumask_t *tmpmask; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7278 | #ifdef CONFIG_NUMA |
| 7279 | struct sched_group **sched_group_nodes = NULL; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7280 | int sd_allnodes = 0; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7281 | |
| 7282 | /* |
| 7283 | * Allocate the per-node list of sched groups |
| 7284 | */ |
Milton Miller | 5cf9f06 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 7285 | sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *), |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7286 | GFP_KERNEL); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7287 | if (!sched_group_nodes) { |
| 7288 | printk(KERN_WARNING "Can not alloc sched group node list\n"); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7289 | return -ENOMEM; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7290 | } |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7291 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7292 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 7293 | rd = alloc_rootdomain(); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7294 | if (!rd) { |
| 7295 | printk(KERN_WARNING "Cannot alloc root domain\n"); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7296 | #ifdef CONFIG_NUMA |
| 7297 | kfree(sched_group_nodes); |
| 7298 | #endif |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7299 | return -ENOMEM; |
| 7300 | } |
| 7301 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7302 | #if SCHED_CPUMASK_ALLOC |
| 7303 | /* get space for all scratch cpumask variables */ |
| 7304 | allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL); |
| 7305 | if (!allmasks) { |
| 7306 | printk(KERN_WARNING "Cannot alloc cpumask array\n"); |
| 7307 | kfree(rd); |
| 7308 | #ifdef CONFIG_NUMA |
| 7309 | kfree(sched_group_nodes); |
| 7310 | #endif |
| 7311 | return -ENOMEM; |
| 7312 | } |
| 7313 | #endif |
| 7314 | tmpmask = (cpumask_t *)allmasks; |
| 7315 | |
| 7316 | |
| 7317 | #ifdef CONFIG_NUMA |
| 7318 | sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; |
| 7319 | #endif |
| 7320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7321 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7322 | * Set up domains for cpus specified by the cpu_map. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7323 | */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7324 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7325 | struct sched_domain *sd = NULL, *p; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7326 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7327 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7328 | *nodemask = node_to_cpumask(cpu_to_node(i)); |
| 7329 | cpus_and(*nodemask, *nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7330 | |
| 7331 | #ifdef CONFIG_NUMA |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7332 | if (cpus_weight(*cpu_map) > |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7333 | SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7334 | sd = &per_cpu(allnodes_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7335 | SD_INIT(sd, ALLNODES); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7336 | set_domain_attribute(sd, attr); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7337 | sd->span = *cpu_map; |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7338 | sd->first_cpu = first_cpu(sd->span); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7339 | cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7340 | p = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7341 | sd_allnodes = 1; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7342 | } else |
| 7343 | p = NULL; |
| 7344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7345 | sd = &per_cpu(node_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7346 | SD_INIT(sd, NODE); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7347 | set_domain_attribute(sd, attr); |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 7348 | sched_domain_node_span(cpu_to_node(i), &sd->span); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7349 | sd->first_cpu = first_cpu(sd->span); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7350 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 7351 | if (p) |
| 7352 | p->child = sd; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7353 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7354 | #endif |
| 7355 | |
| 7356 | p = sd; |
| 7357 | sd = &per_cpu(phys_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7358 | SD_INIT(sd, CPU); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7359 | set_domain_attribute(sd, attr); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7360 | sd->span = *nodemask; |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7361 | sd->first_cpu = first_cpu(sd->span); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7362 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 7363 | if (p) |
| 7364 | p->child = sd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7365 | cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7366 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7367 | #ifdef CONFIG_SCHED_MC |
| 7368 | p = sd; |
| 7369 | sd = &per_cpu(core_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7370 | SD_INIT(sd, MC); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7371 | set_domain_attribute(sd, attr); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7372 | sd->span = cpu_coregroup_map(i); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7373 | sd->first_cpu = first_cpu(sd->span); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7374 | cpus_and(sd->span, sd->span, *cpu_map); |
| 7375 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 7376 | p->child = sd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7377 | cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7378 | #endif |
| 7379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7380 | #ifdef CONFIG_SCHED_SMT |
| 7381 | p = sd; |
| 7382 | sd = &per_cpu(cpu_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7383 | SD_INIT(sd, SIBLING); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7384 | set_domain_attribute(sd, attr); |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 7385 | sd->span = per_cpu(cpu_sibling_map, i); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7386 | sd->first_cpu = first_cpu(sd->span); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7387 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7388 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 7389 | p->child = sd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7390 | cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7391 | #endif |
| 7392 | } |
| 7393 | |
| 7394 | #ifdef CONFIG_SCHED_SMT |
| 7395 | /* Set up CPU (sibling) groups */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7396 | for_each_cpu_mask(i, *cpu_map) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7397 | SCHED_CPUMASK_VAR(this_sibling_map, allmasks); |
| 7398 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
| 7399 | |
| 7400 | *this_sibling_map = per_cpu(cpu_sibling_map, i); |
| 7401 | cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map); |
| 7402 | if (i != first_cpu(*this_sibling_map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7403 | continue; |
| 7404 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7405 | init_sched_build_groups(this_sibling_map, cpu_map, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7406 | &cpu_to_cpu_group, |
| 7407 | send_covered, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7408 | } |
| 7409 | #endif |
| 7410 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7411 | #ifdef CONFIG_SCHED_MC |
| 7412 | /* Set up multi-core groups */ |
| 7413 | for_each_cpu_mask(i, *cpu_map) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7414 | SCHED_CPUMASK_VAR(this_core_map, allmasks); |
| 7415 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
| 7416 | |
| 7417 | *this_core_map = cpu_coregroup_map(i); |
| 7418 | cpus_and(*this_core_map, *this_core_map, *cpu_map); |
| 7419 | if (i != first_cpu(*this_core_map)) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7420 | continue; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7421 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7422 | init_sched_build_groups(this_core_map, cpu_map, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7423 | &cpu_to_core_group, |
| 7424 | send_covered, tmpmask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7425 | } |
| 7426 | #endif |
| 7427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7428 | /* Set up physical groups */ |
| 7429 | for (i = 0; i < MAX_NUMNODES; i++) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7430 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
| 7431 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7432 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7433 | *nodemask = node_to_cpumask(i); |
| 7434 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 7435 | if (cpus_empty(*nodemask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7436 | continue; |
| 7437 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7438 | init_sched_build_groups(nodemask, cpu_map, |
| 7439 | &cpu_to_phys_group, |
| 7440 | send_covered, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7441 | } |
| 7442 | |
| 7443 | #ifdef CONFIG_NUMA |
| 7444 | /* Set up node groups */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7445 | if (sd_allnodes) { |
| 7446 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
| 7447 | |
| 7448 | init_sched_build_groups(cpu_map, cpu_map, |
| 7449 | &cpu_to_allnodes_group, |
| 7450 | send_covered, tmpmask); |
| 7451 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7452 | |
| 7453 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 7454 | /* Set up node groups */ |
| 7455 | struct sched_group *sg, *prev; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7456 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
| 7457 | SCHED_CPUMASK_VAR(domainspan, allmasks); |
| 7458 | SCHED_CPUMASK_VAR(covered, allmasks); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7459 | int j; |
| 7460 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7461 | *nodemask = node_to_cpumask(i); |
| 7462 | cpus_clear(*covered); |
| 7463 | |
| 7464 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 7465 | if (cpus_empty(*nodemask)) { |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7466 | sched_group_nodes[i] = NULL; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7467 | continue; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7468 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7469 | |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 7470 | sched_domain_node_span(i, domainspan); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7471 | cpus_and(*domainspan, *domainspan, *cpu_map); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7472 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 7473 | sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7474 | if (!sg) { |
| 7475 | printk(KERN_WARNING "Can not alloc domain group for " |
| 7476 | "node %d\n", i); |
| 7477 | goto error; |
| 7478 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7479 | sched_group_nodes[i] = sg; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7480 | for_each_cpu_mask(j, *nodemask) { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7481 | struct sched_domain *sd; |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 7482 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7483 | sd = &per_cpu(node_domains, j); |
| 7484 | sd->groups = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7485 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7486 | sg->__cpu_power = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7487 | sg->cpumask = *nodemask; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7488 | sg->next = sg; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7489 | cpus_or(*covered, *covered, *nodemask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7490 | prev = sg; |
| 7491 | |
| 7492 | for (j = 0; j < MAX_NUMNODES; j++) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7493 | SCHED_CPUMASK_VAR(notcovered, allmasks); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7494 | int n = (i + j) % MAX_NUMNODES; |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7495 | node_to_cpumask_ptr(pnodemask, n); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7496 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7497 | cpus_complement(*notcovered, *covered); |
| 7498 | cpus_and(*tmpmask, *notcovered, *cpu_map); |
| 7499 | cpus_and(*tmpmask, *tmpmask, *domainspan); |
| 7500 | if (cpus_empty(*tmpmask)) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7501 | break; |
| 7502 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7503 | cpus_and(*tmpmask, *tmpmask, *pnodemask); |
| 7504 | if (cpus_empty(*tmpmask)) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7505 | continue; |
| 7506 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 7507 | sg = kmalloc_node(sizeof(struct sched_group), |
| 7508 | GFP_KERNEL, i); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7509 | if (!sg) { |
| 7510 | printk(KERN_WARNING |
| 7511 | "Can not alloc domain group for node %d\n", j); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7512 | goto error; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7513 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7514 | sg->__cpu_power = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7515 | sg->cpumask = *tmpmask; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7516 | sg->next = prev->next; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7517 | cpus_or(*covered, *covered, *tmpmask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7518 | prev->next = sg; |
| 7519 | prev = sg; |
| 7520 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7522 | #endif |
| 7523 | |
| 7524 | /* Calculate CPU power for physical packages and nodes */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7525 | #ifdef CONFIG_SCHED_SMT |
| 7526 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7527 | struct sched_domain *sd = &per_cpu(cpu_domains, i); |
| 7528 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7529 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7530 | } |
| 7531 | #endif |
| 7532 | #ifdef CONFIG_SCHED_MC |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7533 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7534 | struct sched_domain *sd = &per_cpu(core_domains, i); |
| 7535 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7536 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7537 | } |
| 7538 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7539 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7540 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7541 | struct sched_domain *sd = &per_cpu(phys_domains, i); |
| 7542 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7543 | init_sched_groups_power(i, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7544 | } |
| 7545 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7546 | #ifdef CONFIG_NUMA |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7547 | for (i = 0; i < MAX_NUMNODES; i++) |
| 7548 | init_numa_sched_groups_power(sched_group_nodes[i]); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7549 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7550 | if (sd_allnodes) { |
| 7551 | struct sched_group *sg; |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 7552 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7553 | cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg, |
| 7554 | tmpmask); |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 7555 | init_numa_sched_groups_power(sg); |
| 7556 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7557 | #endif |
| 7558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7559 | /* Attach the domains */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7560 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7561 | struct sched_domain *sd; |
| 7562 | #ifdef CONFIG_SCHED_SMT |
| 7563 | sd = &per_cpu(cpu_domains, i); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7564 | #elif defined(CONFIG_SCHED_MC) |
| 7565 | sd = &per_cpu(core_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7566 | #else |
| 7567 | sd = &per_cpu(phys_domains, i); |
| 7568 | #endif |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7569 | cpu_attach_domain(sd, rd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7570 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7571 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7572 | SCHED_CPUMASK_FREE((void *)allmasks); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7573 | return 0; |
| 7574 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7575 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7576 | error: |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7577 | free_sched_groups(cpu_map, tmpmask); |
| 7578 | SCHED_CPUMASK_FREE((void *)allmasks); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7579 | return -ENOMEM; |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7580 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7581 | } |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7582 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7583 | static int build_sched_domains(const cpumask_t *cpu_map) |
| 7584 | { |
| 7585 | return __build_sched_domains(cpu_map, NULL); |
| 7586 | } |
| 7587 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7588 | static cpumask_t *doms_cur; /* current sched domains */ |
| 7589 | static int ndoms_cur; /* number of sched domains in 'doms_cur' */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7590 | static struct sched_domain_attr *dattr_cur; /* attribues of custom domains |
| 7591 | in 'doms_cur' */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7592 | |
| 7593 | /* |
| 7594 | * Special case: If a kmalloc of a doms_cur partition (array of |
| 7595 | * cpumask_t) fails, then fallback to a single sched domain, |
| 7596 | * as determined by the single cpumask_t fallback_doms. |
| 7597 | */ |
| 7598 | static cpumask_t fallback_doms; |
| 7599 | |
Heiko Carstens | 22e52b0 | 2008-03-12 18:31:59 +0100 | [diff] [blame] | 7600 | void __attribute__((weak)) arch_update_cpu_topology(void) |
| 7601 | { |
| 7602 | } |
| 7603 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7604 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7605 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7606 | * For now this just excludes isolated cpus, but could be used to |
| 7607 | * exclude other special cases in the future. |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7608 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7609 | static int arch_init_sched_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7610 | { |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7611 | int err; |
| 7612 | |
Heiko Carstens | 22e52b0 | 2008-03-12 18:31:59 +0100 | [diff] [blame] | 7613 | arch_update_cpu_topology(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7614 | ndoms_cur = 1; |
| 7615 | doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL); |
| 7616 | if (!doms_cur) |
| 7617 | doms_cur = &fallback_doms; |
| 7618 | cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7619 | dattr_cur = NULL; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7620 | err = build_sched_domains(doms_cur); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 7621 | register_sched_domain_sysctl(); |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7622 | |
| 7623 | return err; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7624 | } |
| 7625 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7626 | static void arch_destroy_sched_domains(const cpumask_t *cpu_map, |
| 7627 | cpumask_t *tmpmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7628 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7629 | free_sched_groups(cpu_map, tmpmask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7630 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7631 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7632 | /* |
| 7633 | * Detach sched domains from a group of cpus specified in cpu_map |
| 7634 | * These cpus will now be attached to the NULL domain |
| 7635 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 7636 | static void detach_destroy_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7637 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7638 | cpumask_t tmpmask; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7639 | int i; |
| 7640 | |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 7641 | unregister_sched_domain_sysctl(); |
| 7642 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7643 | for_each_cpu_mask(i, *cpu_map) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7644 | cpu_attach_domain(NULL, &def_root_domain, i); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7645 | synchronize_sched(); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7646 | arch_destroy_sched_domains(cpu_map, &tmpmask); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7647 | } |
| 7648 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7649 | /* handle null as "default" */ |
| 7650 | static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, |
| 7651 | struct sched_domain_attr *new, int idx_new) |
| 7652 | { |
| 7653 | struct sched_domain_attr tmp; |
| 7654 | |
| 7655 | /* fast path */ |
| 7656 | if (!new && !cur) |
| 7657 | return 1; |
| 7658 | |
| 7659 | tmp = SD_ATTR_INIT; |
| 7660 | return !memcmp(cur ? (cur + idx_cur) : &tmp, |
| 7661 | new ? (new + idx_new) : &tmp, |
| 7662 | sizeof(struct sched_domain_attr)); |
| 7663 | } |
| 7664 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7665 | /* |
| 7666 | * Partition sched domains as specified by the 'ndoms_new' |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7667 | * cpumasks in the array doms_new[] of cpumasks. This compares |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7668 | * doms_new[] to the current sched domain partitioning, doms_cur[]. |
| 7669 | * It destroys each deleted domain and builds each new domain. |
| 7670 | * |
| 7671 | * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'. |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7672 | * The masks don't intersect (don't overlap.) We should setup one |
| 7673 | * sched domain for each mask. CPUs not in any of the cpumasks will |
| 7674 | * not be load balanced. If the same cpumask appears both in the |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7675 | * current 'doms_cur' domains and in the new 'doms_new', we can leave |
| 7676 | * it as it is. |
| 7677 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7678 | * The passed in 'doms_new' should be kmalloc'd. This routine takes |
| 7679 | * ownership of it and will kfree it when done with it. If the caller |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7680 | * failed the kmalloc call, then it can pass in doms_new == NULL, |
| 7681 | * and partition_sched_domains() will fallback to the single partition |
| 7682 | * 'fallback_doms'. |
| 7683 | * |
| 7684 | * Call with hotplug lock held |
| 7685 | */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7686 | void partition_sched_domains(int ndoms_new, cpumask_t *doms_new, |
| 7687 | struct sched_domain_attr *dattr_new) |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7688 | { |
| 7689 | int i, j; |
| 7690 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7691 | lock_doms_cur(); |
| 7692 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7693 | /* always unregister in case we don't destroy any domains */ |
| 7694 | unregister_sched_domain_sysctl(); |
| 7695 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7696 | if (doms_new == NULL) { |
| 7697 | ndoms_new = 1; |
| 7698 | doms_new = &fallback_doms; |
| 7699 | cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7700 | dattr_new = NULL; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7701 | } |
| 7702 | |
| 7703 | /* Destroy deleted domains */ |
| 7704 | for (i = 0; i < ndoms_cur; i++) { |
| 7705 | for (j = 0; j < ndoms_new; j++) { |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7706 | if (cpus_equal(doms_cur[i], doms_new[j]) |
| 7707 | && dattrs_equal(dattr_cur, i, dattr_new, j)) |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7708 | goto match1; |
| 7709 | } |
| 7710 | /* no match - a current sched domain not in new doms_new[] */ |
| 7711 | detach_destroy_domains(doms_cur + i); |
| 7712 | match1: |
| 7713 | ; |
| 7714 | } |
| 7715 | |
| 7716 | /* Build new domains */ |
| 7717 | for (i = 0; i < ndoms_new; i++) { |
| 7718 | for (j = 0; j < ndoms_cur; j++) { |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7719 | if (cpus_equal(doms_new[i], doms_cur[j]) |
| 7720 | && dattrs_equal(dattr_new, i, dattr_cur, j)) |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7721 | goto match2; |
| 7722 | } |
| 7723 | /* no match - add a new doms_new */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7724 | __build_sched_domains(doms_new + i, |
| 7725 | dattr_new ? dattr_new + i : NULL); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7726 | match2: |
| 7727 | ; |
| 7728 | } |
| 7729 | |
| 7730 | /* Remember the new sched domains */ |
| 7731 | if (doms_cur != &fallback_doms) |
| 7732 | kfree(doms_cur); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7733 | kfree(dattr_cur); /* kfree(NULL) is safe */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7734 | doms_cur = doms_new; |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7735 | dattr_cur = dattr_new; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7736 | ndoms_cur = ndoms_new; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7737 | |
| 7738 | register_sched_domain_sysctl(); |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7739 | |
| 7740 | unlock_doms_cur(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7741 | } |
| 7742 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7743 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Heiko Carstens | 9aefd0a | 2008-03-12 18:31:58 +0100 | [diff] [blame] | 7744 | int arch_reinit_sched_domains(void) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7745 | { |
| 7746 | int err; |
| 7747 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7748 | get_online_cpus(); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7749 | detach_destroy_domains(&cpu_online_map); |
| 7750 | err = arch_init_sched_domains(&cpu_online_map); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7751 | put_online_cpus(); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7752 | |
| 7753 | return err; |
| 7754 | } |
| 7755 | |
| 7756 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) |
| 7757 | { |
| 7758 | int ret; |
| 7759 | |
| 7760 | if (buf[0] != '0' && buf[0] != '1') |
| 7761 | return -EINVAL; |
| 7762 | |
| 7763 | if (smt) |
| 7764 | sched_smt_power_savings = (buf[0] == '1'); |
| 7765 | else |
| 7766 | sched_mc_power_savings = (buf[0] == '1'); |
| 7767 | |
| 7768 | ret = arch_reinit_sched_domains(); |
| 7769 | |
| 7770 | return ret ? ret : count; |
| 7771 | } |
| 7772 | |
Adrian Bunk | 6707de00 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 7773 | #ifdef CONFIG_SCHED_MC |
| 7774 | static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page) |
| 7775 | { |
| 7776 | return sprintf(page, "%u\n", sched_mc_power_savings); |
| 7777 | } |
| 7778 | static ssize_t sched_mc_power_savings_store(struct sys_device *dev, |
| 7779 | const char *buf, size_t count) |
| 7780 | { |
| 7781 | return sched_power_savings_store(buf, count, 0); |
| 7782 | } |
| 7783 | static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show, |
| 7784 | sched_mc_power_savings_store); |
| 7785 | #endif |
| 7786 | |
| 7787 | #ifdef CONFIG_SCHED_SMT |
| 7788 | static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page) |
| 7789 | { |
| 7790 | return sprintf(page, "%u\n", sched_smt_power_savings); |
| 7791 | } |
| 7792 | static ssize_t sched_smt_power_savings_store(struct sys_device *dev, |
| 7793 | const char *buf, size_t count) |
| 7794 | { |
| 7795 | return sched_power_savings_store(buf, count, 1); |
| 7796 | } |
| 7797 | static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show, |
| 7798 | sched_smt_power_savings_store); |
| 7799 | #endif |
| 7800 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7801 | int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) |
| 7802 | { |
| 7803 | int err = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7804 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7805 | #ifdef CONFIG_SCHED_SMT |
| 7806 | if (smt_capable()) |
| 7807 | err = sysfs_create_file(&cls->kset.kobj, |
| 7808 | &attr_sched_smt_power_savings.attr); |
| 7809 | #endif |
| 7810 | #ifdef CONFIG_SCHED_MC |
| 7811 | if (!err && mc_capable()) |
| 7812 | err = sysfs_create_file(&cls->kset.kobj, |
| 7813 | &attr_sched_mc_power_savings.attr); |
| 7814 | #endif |
| 7815 | return err; |
| 7816 | } |
| 7817 | #endif |
| 7818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7819 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7820 | * Force a reinitialization of the sched domains hierarchy. The domains |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7821 | * and groups cannot be updated in place without racing with the balancing |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 7822 | * code, so we temporarily attach all running cpus to the NULL domain |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7823 | * which will prevent rebalancing while the sched domains are recalculated. |
| 7824 | */ |
| 7825 | static int update_sched_domains(struct notifier_block *nfb, |
| 7826 | unsigned long action, void *hcpu) |
| 7827 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7828 | switch (action) { |
| 7829 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7830 | case CPU_UP_PREPARE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7831 | case CPU_DOWN_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7832 | case CPU_DOWN_PREPARE_FROZEN: |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7833 | detach_destroy_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7834 | return NOTIFY_OK; |
| 7835 | |
| 7836 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7837 | case CPU_UP_CANCELED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7838 | case CPU_DOWN_FAILED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7839 | case CPU_DOWN_FAILED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7840 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7841 | case CPU_ONLINE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7842 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7843 | case CPU_DEAD_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7844 | /* |
| 7845 | * Fall through and re-initialise the domains. |
| 7846 | */ |
| 7847 | break; |
| 7848 | default: |
| 7849 | return NOTIFY_DONE; |
| 7850 | } |
| 7851 | |
| 7852 | /* The hotplug lock is already held by cpu_up/cpu_down */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7853 | arch_init_sched_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7854 | |
| 7855 | return NOTIFY_OK; |
| 7856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7857 | |
| 7858 | void __init sched_init_smp(void) |
| 7859 | { |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7860 | cpumask_t non_isolated_cpus; |
| 7861 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7862 | #if defined(CONFIG_NUMA) |
| 7863 | sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **), |
| 7864 | GFP_KERNEL); |
| 7865 | BUG_ON(sched_group_nodes_bycpu == NULL); |
| 7866 | #endif |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7867 | get_online_cpus(); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7868 | arch_init_sched_domains(&cpu_online_map); |
Nathan Lynch | e5e5673 | 2007-01-10 23:15:28 -0800 | [diff] [blame] | 7869 | cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7870 | if (cpus_empty(non_isolated_cpus)) |
| 7871 | cpu_set(smp_processor_id(), non_isolated_cpus); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7872 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7873 | /* XXX: Theoretical race here - CPU may be hotplugged now */ |
| 7874 | hotcpu_notifier(update_sched_domains, 0); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7875 | |
| 7876 | /* Move init over to a non-isolated CPU */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7877 | if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0) |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7878 | BUG(); |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 7879 | sched_init_granularity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7880 | } |
| 7881 | #else |
| 7882 | void __init sched_init_smp(void) |
| 7883 | { |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7884 | #if defined(CONFIG_NUMA) |
| 7885 | sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **), |
| 7886 | GFP_KERNEL); |
| 7887 | BUG_ON(sched_group_nodes_bycpu == NULL); |
| 7888 | #endif |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 7889 | sched_init_granularity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7890 | } |
| 7891 | #endif /* CONFIG_SMP */ |
| 7892 | |
| 7893 | int in_sched_functions(unsigned long addr) |
| 7894 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7895 | return in_lock_functions(addr) || |
| 7896 | (addr >= (unsigned long)__sched_text_start |
| 7897 | && addr < (unsigned long)__sched_text_end); |
| 7898 | } |
| 7899 | |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 7900 | static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7901 | { |
| 7902 | cfs_rq->tasks_timeline = RB_ROOT; |
Peter Zijlstra | 4a55bd5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7903 | INIT_LIST_HEAD(&cfs_rq->tasks); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7904 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7905 | cfs_rq->rq = rq; |
| 7906 | #endif |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 7907 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7908 | } |
| 7909 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7910 | static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) |
| 7911 | { |
| 7912 | struct rt_prio_array *array; |
| 7913 | int i; |
| 7914 | |
| 7915 | array = &rt_rq->active; |
| 7916 | for (i = 0; i < MAX_RT_PRIO; i++) { |
| 7917 | INIT_LIST_HEAD(array->queue + i); |
| 7918 | __clear_bit(i, array->bitmap); |
| 7919 | } |
| 7920 | /* delimiter for bitsearch: */ |
| 7921 | __set_bit(MAX_RT_PRIO, array->bitmap); |
| 7922 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7923 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 7924 | rt_rq->highest_prio = MAX_RT_PRIO; |
| 7925 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7926 | #ifdef CONFIG_SMP |
| 7927 | rt_rq->rt_nr_migratory = 0; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7928 | rt_rq->overloaded = 0; |
| 7929 | #endif |
| 7930 | |
| 7931 | rt_rq->rt_time = 0; |
| 7932 | rt_rq->rt_throttled = 0; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 7933 | rt_rq->rt_runtime = 0; |
| 7934 | spin_lock_init(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7935 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7936 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7937 | rt_rq->rt_nr_boosted = 0; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7938 | rt_rq->rq = rq; |
| 7939 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7940 | } |
| 7941 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7942 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7943 | static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
| 7944 | struct sched_entity *se, int cpu, int add, |
| 7945 | struct sched_entity *parent) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7946 | { |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7947 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7948 | tg->cfs_rq[cpu] = cfs_rq; |
| 7949 | init_cfs_rq(cfs_rq, rq); |
| 7950 | cfs_rq->tg = tg; |
| 7951 | if (add) |
| 7952 | list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list); |
| 7953 | |
| 7954 | tg->se[cpu] = se; |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7955 | /* se could be NULL for init_task_group */ |
| 7956 | if (!se) |
| 7957 | return; |
| 7958 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7959 | if (!parent) |
| 7960 | se->cfs_rq = &rq->cfs; |
| 7961 | else |
| 7962 | se->cfs_rq = parent->my_q; |
| 7963 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7964 | se->my_q = cfs_rq; |
| 7965 | se->load.weight = tg->shares; |
| 7966 | se->load.inv_weight = div64_64(1ULL<<32, se->load.weight); |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7967 | se->parent = parent; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7968 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7969 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7970 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7971 | #ifdef CONFIG_RT_GROUP_SCHED |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7972 | static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, |
| 7973 | struct sched_rt_entity *rt_se, int cpu, int add, |
| 7974 | struct sched_rt_entity *parent) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7975 | { |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7976 | struct rq *rq = cpu_rq(cpu); |
| 7977 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7978 | tg->rt_rq[cpu] = rt_rq; |
| 7979 | init_rt_rq(rt_rq, rq); |
| 7980 | rt_rq->tg = tg; |
| 7981 | rt_rq->rt_se = rt_se; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 7982 | rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7983 | if (add) |
| 7984 | list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list); |
| 7985 | |
| 7986 | tg->rt_se[cpu] = rt_se; |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7987 | if (!rt_se) |
| 7988 | return; |
| 7989 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7990 | if (!parent) |
| 7991 | rt_se->rt_rq = &rq->rt; |
| 7992 | else |
| 7993 | rt_se->rt_rq = parent->my_q; |
| 7994 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7995 | rt_se->rt_rq = &rq->rt; |
| 7996 | rt_se->my_q = rt_rq; |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7997 | rt_se->parent = parent; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7998 | INIT_LIST_HEAD(&rt_se->run_list); |
| 7999 | } |
| 8000 | #endif |
| 8001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8002 | void __init sched_init(void) |
| 8003 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8004 | int i, j; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8005 | unsigned long alloc_size = 0, ptr; |
| 8006 | |
| 8007 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8008 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); |
| 8009 | #endif |
| 8010 | #ifdef CONFIG_RT_GROUP_SCHED |
| 8011 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); |
| 8012 | #endif |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8013 | #ifdef CONFIG_USER_SCHED |
| 8014 | alloc_size *= 2; |
| 8015 | #endif |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8016 | /* |
| 8017 | * As sched_init() is called before page_alloc is setup, |
| 8018 | * we use alloc_bootmem(). |
| 8019 | */ |
| 8020 | if (alloc_size) { |
| 8021 | ptr = (unsigned long)alloc_bootmem_low(alloc_size); |
| 8022 | |
| 8023 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8024 | init_task_group.se = (struct sched_entity **)ptr; |
| 8025 | ptr += nr_cpu_ids * sizeof(void **); |
| 8026 | |
| 8027 | init_task_group.cfs_rq = (struct cfs_rq **)ptr; |
| 8028 | ptr += nr_cpu_ids * sizeof(void **); |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8029 | |
| 8030 | #ifdef CONFIG_USER_SCHED |
| 8031 | root_task_group.se = (struct sched_entity **)ptr; |
| 8032 | ptr += nr_cpu_ids * sizeof(void **); |
| 8033 | |
| 8034 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; |
| 8035 | ptr += nr_cpu_ids * sizeof(void **); |
| 8036 | #endif |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8037 | #endif |
| 8038 | #ifdef CONFIG_RT_GROUP_SCHED |
| 8039 | init_task_group.rt_se = (struct sched_rt_entity **)ptr; |
| 8040 | ptr += nr_cpu_ids * sizeof(void **); |
| 8041 | |
| 8042 | init_task_group.rt_rq = (struct rt_rq **)ptr; |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8043 | ptr += nr_cpu_ids * sizeof(void **); |
| 8044 | |
| 8045 | #ifdef CONFIG_USER_SCHED |
| 8046 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; |
| 8047 | ptr += nr_cpu_ids * sizeof(void **); |
| 8048 | |
| 8049 | root_task_group.rt_rq = (struct rt_rq **)ptr; |
| 8050 | ptr += nr_cpu_ids * sizeof(void **); |
| 8051 | #endif |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8052 | #endif |
| 8053 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8054 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 8055 | #ifdef CONFIG_SMP |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8056 | init_aggregate(); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 8057 | init_defrootdomain(); |
| 8058 | #endif |
| 8059 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8060 | init_rt_bandwidth(&def_rt_bandwidth, |
| 8061 | global_rt_period(), global_rt_runtime()); |
| 8062 | |
| 8063 | #ifdef CONFIG_RT_GROUP_SCHED |
| 8064 | init_rt_bandwidth(&init_task_group.rt_bandwidth, |
| 8065 | global_rt_period(), global_rt_runtime()); |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8066 | #ifdef CONFIG_USER_SCHED |
| 8067 | init_rt_bandwidth(&root_task_group.rt_bandwidth, |
| 8068 | global_rt_period(), RUNTIME_INF); |
| 8069 | #endif |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8070 | #endif |
| 8071 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8072 | #ifdef CONFIG_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8073 | list_add(&init_task_group.list, &task_groups); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8074 | INIT_LIST_HEAD(&init_task_group.children); |
| 8075 | |
| 8076 | #ifdef CONFIG_USER_SCHED |
| 8077 | INIT_LIST_HEAD(&root_task_group.children); |
| 8078 | init_task_group.parent = &root_task_group; |
| 8079 | list_add(&init_task_group.siblings, &root_task_group.children); |
| 8080 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8081 | #endif |
| 8082 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 8083 | for_each_possible_cpu(i) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 8084 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8085 | |
| 8086 | rq = cpu_rq(i); |
| 8087 | spin_lock_init(&rq->lock); |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 8088 | lockdep_set_class(&rq->lock, &rq->rq_lock_key); |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 8089 | rq->nr_running = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8090 | rq->clock = 1; |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8091 | update_last_tick_seen(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8092 | init_cfs_rq(&rq->cfs, rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 8093 | init_rt_rq(&rq->rt, rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8094 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8095 | init_task_group.shares = init_task_group_load; |
| 8096 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8097 | #ifdef CONFIG_CGROUP_SCHED |
| 8098 | /* |
| 8099 | * How much cpu bandwidth does init_task_group get? |
| 8100 | * |
| 8101 | * In case of task-groups formed thr' the cgroup filesystem, it |
| 8102 | * gets 100% of the cpu resources in the system. This overall |
| 8103 | * system cpu resource is divided among the tasks of |
| 8104 | * init_task_group and its child task-groups in a fair manner, |
| 8105 | * based on each entity's (task or task-group's) weight |
| 8106 | * (se->load.weight). |
| 8107 | * |
| 8108 | * In other words, if init_task_group has 10 tasks of weight |
| 8109 | * 1024) and two child groups A0 and A1 (of weight 1024 each), |
| 8110 | * then A0's share of the cpu resource is: |
| 8111 | * |
| 8112 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% |
| 8113 | * |
| 8114 | * We achieve this by letting init_task_group's tasks sit |
| 8115 | * directly in rq->cfs (i.e init_task_group->se[] = NULL). |
| 8116 | */ |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8117 | init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8118 | #elif defined CONFIG_USER_SCHED |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8119 | root_task_group.shares = NICE_0_LOAD; |
| 8120 | init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8121 | /* |
| 8122 | * In case of task-groups formed thr' the user id of tasks, |
| 8123 | * init_task_group represents tasks belonging to root user. |
| 8124 | * Hence it forms a sibling of all subsequent groups formed. |
| 8125 | * In this case, init_task_group gets only a fraction of overall |
| 8126 | * system cpu resource, based on the weight assigned to root |
| 8127 | * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished |
| 8128 | * by letting tasks of init_task_group sit in a separate cfs_rq |
| 8129 | * (init_cfs_rq) and having one entity represent this group of |
| 8130 | * tasks in rq->cfs (i.e init_task_group->se[] != NULL). |
| 8131 | */ |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8132 | init_tg_cfs_entry(&init_task_group, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8133 | &per_cpu(init_cfs_rq, i), |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8134 | &per_cpu(init_sched_entity, i), i, 1, |
| 8135 | root_task_group.se[i]); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8136 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8137 | #endif |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8138 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 8139 | |
| 8140 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8141 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8142 | INIT_LIST_HEAD(&rq->leaf_rt_rq_list); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8143 | #ifdef CONFIG_CGROUP_SCHED |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8144 | init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8145 | #elif defined CONFIG_USER_SCHED |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8146 | init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL); |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8147 | init_tg_rt_entry(&init_task_group, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8148 | &per_cpu(init_rt_rq, i), |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8149 | &per_cpu(init_sched_rt_entity, i), i, 1, |
| 8150 | root_task_group.rt_se[i]); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8151 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8152 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8153 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8154 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) |
| 8155 | rq->cpu_load[j] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8156 | #ifdef CONFIG_SMP |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 8157 | rq->sd = NULL; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 8158 | rq->rd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8159 | rq->active_balance = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8160 | rq->next_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8161 | rq->push_cpu = 0; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 8162 | rq->cpu = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8163 | rq->migration_thread = NULL; |
| 8164 | INIT_LIST_HEAD(&rq->migration_queue); |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 8165 | rq_attach_root(rq, &def_root_domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8166 | #endif |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 8167 | init_rq_hrtick(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8168 | atomic_set(&rq->nr_iowait, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8169 | } |
| 8170 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 8171 | set_load_weight(&init_task); |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 8172 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 8173 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 8174 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); |
| 8175 | #endif |
| 8176 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 8177 | #ifdef CONFIG_SMP |
| 8178 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL); |
| 8179 | #endif |
| 8180 | |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 8181 | #ifdef CONFIG_RT_MUTEXES |
| 8182 | plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); |
| 8183 | #endif |
| 8184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8185 | /* |
| 8186 | * The boot idle thread does lazy MMU switching as well: |
| 8187 | */ |
| 8188 | atomic_inc(&init_mm.mm_count); |
| 8189 | enter_lazy_tlb(&init_mm, current); |
| 8190 | |
| 8191 | /* |
| 8192 | * Make us the idle thread. Technically, schedule() should not be |
| 8193 | * called from this thread, however somewhere below it might be, |
| 8194 | * but because we are the idle thread, we just pick up running again |
| 8195 | * when this runqueue becomes "idle". |
| 8196 | */ |
| 8197 | init_idle(current, smp_processor_id()); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8198 | /* |
| 8199 | * During early bootup we pretend to be a normal task: |
| 8200 | */ |
| 8201 | current->sched_class = &fair_sched_class; |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 8202 | |
| 8203 | scheduler_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8204 | } |
| 8205 | |
| 8206 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 8207 | void __might_sleep(char *file, int line) |
| 8208 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 8209 | #ifdef in_atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8210 | static unsigned long prev_jiffy; /* ratelimiting */ |
| 8211 | |
| 8212 | if ((in_atomic() || irqs_disabled()) && |
| 8213 | system_state == SYSTEM_RUNNING && !oops_in_progress) { |
| 8214 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 8215 | return; |
| 8216 | prev_jiffy = jiffies; |
Ingo Molnar | 91368d7 | 2006-03-23 03:00:54 -0800 | [diff] [blame] | 8217 | printk(KERN_ERR "BUG: sleeping function called from invalid" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8218 | " context at %s:%d\n", file, line); |
| 8219 | printk("in_atomic():%d, irqs_disabled():%d\n", |
| 8220 | in_atomic(), irqs_disabled()); |
Peter Zijlstra | a4c410f | 2006-12-06 20:37:21 -0800 | [diff] [blame] | 8221 | debug_show_held_locks(current); |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 8222 | if (irqs_disabled()) |
| 8223 | print_irqtrace_events(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8224 | dump_stack(); |
| 8225 | } |
| 8226 | #endif |
| 8227 | } |
| 8228 | EXPORT_SYMBOL(__might_sleep); |
| 8229 | #endif |
| 8230 | |
| 8231 | #ifdef CONFIG_MAGIC_SYSRQ |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 8232 | static void normalize_task(struct rq *rq, struct task_struct *p) |
| 8233 | { |
| 8234 | int on_rq; |
| 8235 | update_rq_clock(rq); |
| 8236 | on_rq = p->se.on_rq; |
| 8237 | if (on_rq) |
| 8238 | deactivate_task(rq, p, 0); |
| 8239 | __setscheduler(rq, p, SCHED_NORMAL, 0); |
| 8240 | if (on_rq) { |
| 8241 | activate_task(rq, p, 0); |
| 8242 | resched_task(rq->curr); |
| 8243 | } |
| 8244 | } |
| 8245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8246 | void normalize_rt_tasks(void) |
| 8247 | { |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 8248 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8249 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 8250 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8251 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8252 | read_lock_irqsave(&tasklist_lock, flags); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 8253 | do_each_thread(g, p) { |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 8254 | /* |
| 8255 | * Only normalize user tasks: |
| 8256 | */ |
| 8257 | if (!p->mm) |
| 8258 | continue; |
| 8259 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8260 | p->se.exec_start = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 8261 | #ifdef CONFIG_SCHEDSTATS |
| 8262 | p->se.wait_start = 0; |
| 8263 | p->se.sleep_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8264 | p->se.block_start = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 8265 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8266 | task_rq(p)->clock = 0; |
| 8267 | |
| 8268 | if (!rt_task(p)) { |
| 8269 | /* |
| 8270 | * Renice negative nice level userspace |
| 8271 | * tasks back to 0: |
| 8272 | */ |
| 8273 | if (TASK_NICE(p) < 0 && p->mm) |
| 8274 | set_user_nice(p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8275 | continue; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8277 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8278 | spin_lock(&p->pi_lock); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 8279 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8280 | |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 8281 | normalize_task(rq, p); |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 8282 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 8283 | __task_rq_unlock(rq); |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8284 | spin_unlock(&p->pi_lock); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 8285 | } while_each_thread(g, p); |
| 8286 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8287 | read_unlock_irqrestore(&tasklist_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8288 | } |
| 8289 | |
| 8290 | #endif /* CONFIG_MAGIC_SYSRQ */ |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 8291 | |
| 8292 | #ifdef CONFIG_IA64 |
| 8293 | /* |
| 8294 | * These functions are only useful for the IA64 MCA handling. |
| 8295 | * |
| 8296 | * They can only be called when the whole system has been |
| 8297 | * stopped - every CPU needs to be quiescent, and no scheduling |
| 8298 | * activity can take place. Using them for anything else would |
| 8299 | * be a serious bug, and as a result, they aren't even visible |
| 8300 | * under any other configuration. |
| 8301 | */ |
| 8302 | |
| 8303 | /** |
| 8304 | * curr_task - return the current task for a given cpu. |
| 8305 | * @cpu: the processor in question. |
| 8306 | * |
| 8307 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 8308 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 8309 | struct task_struct *curr_task(int cpu) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 8310 | { |
| 8311 | return cpu_curr(cpu); |
| 8312 | } |
| 8313 | |
| 8314 | /** |
| 8315 | * set_curr_task - set the current task for a given cpu. |
| 8316 | * @cpu: the processor in question. |
| 8317 | * @p: the task pointer to set. |
| 8318 | * |
| 8319 | * Description: This function must only be used when non-maskable interrupts |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8320 | * are serviced on a separate stack. It allows the architecture to switch the |
| 8321 | * notion of the current task on a cpu in a non-blocking manner. This function |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 8322 | * must be called with all CPU's synchronized, and interrupts disabled, the |
| 8323 | * and caller must save the original value of the current task (see |
| 8324 | * curr_task() above) and restore that value before reenabling interrupts and |
| 8325 | * re-starting the system. |
| 8326 | * |
| 8327 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 8328 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 8329 | void set_curr_task(int cpu, struct task_struct *p) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 8330 | { |
| 8331 | cpu_curr(cpu) = p; |
| 8332 | } |
| 8333 | |
| 8334 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8335 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8336 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8337 | static void free_fair_sched_group(struct task_group *tg) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8338 | { |
| 8339 | int i; |
| 8340 | |
| 8341 | for_each_possible_cpu(i) { |
| 8342 | if (tg->cfs_rq) |
| 8343 | kfree(tg->cfs_rq[i]); |
| 8344 | if (tg->se) |
| 8345 | kfree(tg->se[i]); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8346 | } |
| 8347 | |
| 8348 | kfree(tg->cfs_rq); |
| 8349 | kfree(tg->se); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8350 | } |
| 8351 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8352 | static |
| 8353 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8354 | { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8355 | struct cfs_rq *cfs_rq; |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8356 | struct sched_entity *se, *parent_se; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8357 | struct rq *rq; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8358 | int i; |
| 8359 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8360 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8361 | if (!tg->cfs_rq) |
| 8362 | goto err; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8363 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8364 | if (!tg->se) |
| 8365 | goto err; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8366 | |
| 8367 | tg->shares = NICE_0_LOAD; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8368 | |
| 8369 | for_each_possible_cpu(i) { |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8370 | rq = cpu_rq(i); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8371 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8372 | cfs_rq = kmalloc_node(sizeof(struct cfs_rq), |
| 8373 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8374 | if (!cfs_rq) |
| 8375 | goto err; |
| 8376 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8377 | se = kmalloc_node(sizeof(struct sched_entity), |
| 8378 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8379 | if (!se) |
| 8380 | goto err; |
| 8381 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8382 | parent_se = parent ? parent->se[i] : NULL; |
| 8383 | init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8384 | } |
| 8385 | |
| 8386 | return 1; |
| 8387 | |
| 8388 | err: |
| 8389 | return 0; |
| 8390 | } |
| 8391 | |
| 8392 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) |
| 8393 | { |
| 8394 | list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list, |
| 8395 | &cpu_rq(cpu)->leaf_cfs_rq_list); |
| 8396 | } |
| 8397 | |
| 8398 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 8399 | { |
| 8400 | list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list); |
| 8401 | } |
| 8402 | #else |
| 8403 | static inline void free_fair_sched_group(struct task_group *tg) |
| 8404 | { |
| 8405 | } |
| 8406 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8407 | static inline |
| 8408 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8409 | { |
| 8410 | return 1; |
| 8411 | } |
| 8412 | |
| 8413 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) |
| 8414 | { |
| 8415 | } |
| 8416 | |
| 8417 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 8418 | { |
| 8419 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8420 | #endif |
| 8421 | |
| 8422 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8423 | static void free_rt_sched_group(struct task_group *tg) |
| 8424 | { |
| 8425 | int i; |
| 8426 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8427 | destroy_rt_bandwidth(&tg->rt_bandwidth); |
| 8428 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8429 | for_each_possible_cpu(i) { |
| 8430 | if (tg->rt_rq) |
| 8431 | kfree(tg->rt_rq[i]); |
| 8432 | if (tg->rt_se) |
| 8433 | kfree(tg->rt_se[i]); |
| 8434 | } |
| 8435 | |
| 8436 | kfree(tg->rt_rq); |
| 8437 | kfree(tg->rt_se); |
| 8438 | } |
| 8439 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8440 | static |
| 8441 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8442 | { |
| 8443 | struct rt_rq *rt_rq; |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8444 | struct sched_rt_entity *rt_se, *parent_se; |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8445 | struct rq *rq; |
| 8446 | int i; |
| 8447 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8448 | tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8449 | if (!tg->rt_rq) |
| 8450 | goto err; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8451 | tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8452 | if (!tg->rt_se) |
| 8453 | goto err; |
| 8454 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8455 | init_rt_bandwidth(&tg->rt_bandwidth, |
| 8456 | ktime_to_ns(def_rt_bandwidth.rt_period), 0); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8457 | |
| 8458 | for_each_possible_cpu(i) { |
| 8459 | rq = cpu_rq(i); |
| 8460 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8461 | rt_rq = kmalloc_node(sizeof(struct rt_rq), |
| 8462 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
| 8463 | if (!rt_rq) |
| 8464 | goto err; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8465 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8466 | rt_se = kmalloc_node(sizeof(struct sched_rt_entity), |
| 8467 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
| 8468 | if (!rt_se) |
| 8469 | goto err; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8470 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8471 | parent_se = parent ? parent->rt_se[i] : NULL; |
| 8472 | init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8473 | } |
| 8474 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8475 | return 1; |
| 8476 | |
| 8477 | err: |
| 8478 | return 0; |
| 8479 | } |
| 8480 | |
| 8481 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) |
| 8482 | { |
| 8483 | list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list, |
| 8484 | &cpu_rq(cpu)->leaf_rt_rq_list); |
| 8485 | } |
| 8486 | |
| 8487 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) |
| 8488 | { |
| 8489 | list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list); |
| 8490 | } |
| 8491 | #else |
| 8492 | static inline void free_rt_sched_group(struct task_group *tg) |
| 8493 | { |
| 8494 | } |
| 8495 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8496 | static inline |
| 8497 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8498 | { |
| 8499 | return 1; |
| 8500 | } |
| 8501 | |
| 8502 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) |
| 8503 | { |
| 8504 | } |
| 8505 | |
| 8506 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) |
| 8507 | { |
| 8508 | } |
| 8509 | #endif |
| 8510 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8511 | #ifdef CONFIG_GROUP_SCHED |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8512 | static void free_sched_group(struct task_group *tg) |
| 8513 | { |
| 8514 | free_fair_sched_group(tg); |
| 8515 | free_rt_sched_group(tg); |
| 8516 | kfree(tg); |
| 8517 | } |
| 8518 | |
| 8519 | /* allocate runqueue etc for a new task group */ |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8520 | struct task_group *sched_create_group(struct task_group *parent) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8521 | { |
| 8522 | struct task_group *tg; |
| 8523 | unsigned long flags; |
| 8524 | int i; |
| 8525 | |
| 8526 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); |
| 8527 | if (!tg) |
| 8528 | return ERR_PTR(-ENOMEM); |
| 8529 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8530 | if (!alloc_fair_sched_group(tg, parent)) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8531 | goto err; |
| 8532 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8533 | if (!alloc_rt_sched_group(tg, parent)) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8534 | goto err; |
| 8535 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8536 | spin_lock_irqsave(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8537 | for_each_possible_cpu(i) { |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8538 | register_fair_sched_group(tg, i); |
| 8539 | register_rt_sched_group(tg, i); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8540 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8541 | list_add_rcu(&tg->list, &task_groups); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8542 | |
| 8543 | WARN_ON(!parent); /* root should already exist */ |
| 8544 | |
| 8545 | tg->parent = parent; |
| 8546 | list_add_rcu(&tg->siblings, &parent->children); |
| 8547 | INIT_LIST_HEAD(&tg->children); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8548 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8549 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8550 | return tg; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8551 | |
| 8552 | err: |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8553 | free_sched_group(tg); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8554 | return ERR_PTR(-ENOMEM); |
| 8555 | } |
| 8556 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8557 | /* rcu callback to free various structures associated with a task group */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8558 | static void free_sched_group_rcu(struct rcu_head *rhp) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8559 | { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8560 | /* now it should be safe to free those cfs_rqs */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8561 | free_sched_group(container_of(rhp, struct task_group, rcu)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8562 | } |
| 8563 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8564 | /* Destroy runqueue etc associated with a task group */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8565 | void sched_destroy_group(struct task_group *tg) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8566 | { |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8567 | unsigned long flags; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8568 | int i; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8569 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8570 | spin_lock_irqsave(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8571 | for_each_possible_cpu(i) { |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8572 | unregister_fair_sched_group(tg, i); |
| 8573 | unregister_rt_sched_group(tg, i); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8574 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8575 | list_del_rcu(&tg->list); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8576 | list_del_rcu(&tg->siblings); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8577 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8578 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8579 | /* wait for possible concurrent references to cfs_rqs complete */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8580 | call_rcu(&tg->rcu, free_sched_group_rcu); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8581 | } |
| 8582 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8583 | /* change task's runqueue when it moves between groups. |
Ingo Molnar | 3a25201 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 8584 | * The caller of this function should have put the task in its new group |
| 8585 | * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to |
| 8586 | * reflect its new group. |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8587 | */ |
| 8588 | void sched_move_task(struct task_struct *tsk) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8589 | { |
| 8590 | int on_rq, running; |
| 8591 | unsigned long flags; |
| 8592 | struct rq *rq; |
| 8593 | |
| 8594 | rq = task_rq_lock(tsk, &flags); |
| 8595 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8596 | update_rq_clock(rq); |
| 8597 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 8598 | running = task_current(rq, tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8599 | on_rq = tsk->se.on_rq; |
| 8600 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 8601 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8602 | dequeue_task(rq, tsk, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 8603 | if (unlikely(running)) |
| 8604 | tsk->sched_class->put_prev_task(rq, tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8605 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8606 | set_task_rq(tsk, task_cpu(tsk)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8607 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 8608 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8609 | if (tsk->sched_class->moved_group) |
| 8610 | tsk->sched_class->moved_group(tsk); |
| 8611 | #endif |
| 8612 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 8613 | if (unlikely(running)) |
| 8614 | tsk->sched_class->set_curr_task(rq); |
| 8615 | if (on_rq) |
Dmitry Adamushko | 7074bad | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8616 | enqueue_task(rq, tsk, 0); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8617 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8618 | task_rq_unlock(rq, &flags); |
| 8619 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8620 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8621 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8622 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8623 | static void __set_se_shares(struct sched_entity *se, unsigned long shares) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8624 | { |
| 8625 | struct cfs_rq *cfs_rq = se->cfs_rq; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8626 | int on_rq; |
| 8627 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8628 | on_rq = se->on_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8629 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8630 | dequeue_entity(cfs_rq, se, 0); |
| 8631 | |
| 8632 | se->load.weight = shares; |
| 8633 | se->load.inv_weight = div64_64((1ULL<<32), shares); |
| 8634 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8635 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8636 | enqueue_entity(cfs_rq, se, 0); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8637 | } |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8638 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8639 | static void set_se_shares(struct sched_entity *se, unsigned long shares) |
| 8640 | { |
| 8641 | struct cfs_rq *cfs_rq = se->cfs_rq; |
| 8642 | struct rq *rq = cfs_rq->rq; |
| 8643 | unsigned long flags; |
| 8644 | |
| 8645 | spin_lock_irqsave(&rq->lock, flags); |
| 8646 | __set_se_shares(se, shares); |
| 8647 | spin_unlock_irqrestore(&rq->lock, flags); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8648 | } |
| 8649 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8650 | static DEFINE_MUTEX(shares_mutex); |
| 8651 | |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8652 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8653 | { |
| 8654 | int i; |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8655 | unsigned long flags; |
Ingo Molnar | c61935f | 2008-01-22 11:24:58 +0100 | [diff] [blame] | 8656 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8657 | /* |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8658 | * We can't change the weight of the root cgroup. |
| 8659 | */ |
| 8660 | if (!tg->se[0]) |
| 8661 | return -EINVAL; |
| 8662 | |
| 8663 | /* |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8664 | * A weight of 0 or 1 can cause arithmetics problems. |
| 8665 | * (The default weight is 1024 - so there's no practical |
| 8666 | * limitation from this.) |
| 8667 | */ |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8668 | if (shares < MIN_SHARES) |
| 8669 | shares = MIN_SHARES; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8670 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8671 | mutex_lock(&shares_mutex); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8672 | if (tg->shares == shares) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8673 | goto done; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8674 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8675 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8676 | for_each_possible_cpu(i) |
| 8677 | unregister_fair_sched_group(tg, i); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8678 | list_del_rcu(&tg->siblings); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8679 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 8680 | |
| 8681 | /* wait for any ongoing reference to this group to finish */ |
| 8682 | synchronize_sched(); |
| 8683 | |
| 8684 | /* |
| 8685 | * Now we are free to modify the group's share on each cpu |
| 8686 | * w/o tripping rebalance_share or load_balance_fair. |
| 8687 | */ |
| 8688 | tg->shares = shares; |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8689 | for_each_possible_cpu(i) { |
| 8690 | /* |
| 8691 | * force a rebalance |
| 8692 | */ |
| 8693 | cfs_rq_set_shares(tg->cfs_rq[i], 0); |
| 8694 | set_se_shares(tg->se[i], shares/nr_cpu_ids); |
| 8695 | } |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 8696 | |
| 8697 | /* |
| 8698 | * Enable load balance activity on this group, by inserting it back on |
| 8699 | * each cpu's rq->leaf_cfs_rq_list. |
| 8700 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8701 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8702 | for_each_possible_cpu(i) |
| 8703 | register_fair_sched_group(tg, i); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8704 | list_add_rcu(&tg->siblings, &tg->parent->children); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8705 | spin_unlock_irqrestore(&task_group_lock, flags); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8706 | done: |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8707 | mutex_unlock(&shares_mutex); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8708 | return 0; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8709 | } |
| 8710 | |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8711 | unsigned long sched_group_shares(struct task_group *tg) |
| 8712 | { |
| 8713 | return tg->shares; |
| 8714 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8715 | #endif |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8716 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8717 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8718 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8719 | * Ensure that the real time constraints are schedulable. |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8720 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8721 | static DEFINE_MUTEX(rt_constraints_mutex); |
| 8722 | |
| 8723 | static unsigned long to_ratio(u64 period, u64 runtime) |
| 8724 | { |
| 8725 | if (runtime == RUNTIME_INF) |
| 8726 | return 1ULL << 16; |
| 8727 | |
Peter Zijlstra | 2692a24 | 2008-02-27 12:00:46 +0100 | [diff] [blame] | 8728 | return div64_64(runtime << 16, period); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8729 | } |
| 8730 | |
Peter Zijlstra | b40b2e8 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8731 | #ifdef CONFIG_CGROUP_SCHED |
| 8732 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
| 8733 | { |
| 8734 | struct task_group *tgi, *parent = tg->parent; |
| 8735 | unsigned long total = 0; |
| 8736 | |
| 8737 | if (!parent) { |
| 8738 | if (global_rt_period() < period) |
| 8739 | return 0; |
| 8740 | |
| 8741 | return to_ratio(period, runtime) < |
| 8742 | to_ratio(global_rt_period(), global_rt_runtime()); |
| 8743 | } |
| 8744 | |
| 8745 | if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period) |
| 8746 | return 0; |
| 8747 | |
| 8748 | rcu_read_lock(); |
| 8749 | list_for_each_entry_rcu(tgi, &parent->children, siblings) { |
| 8750 | if (tgi == tg) |
| 8751 | continue; |
| 8752 | |
| 8753 | total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period), |
| 8754 | tgi->rt_bandwidth.rt_runtime); |
| 8755 | } |
| 8756 | rcu_read_unlock(); |
| 8757 | |
| 8758 | return total + to_ratio(period, runtime) < |
| 8759 | to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period), |
| 8760 | parent->rt_bandwidth.rt_runtime); |
| 8761 | } |
| 8762 | #elif defined CONFIG_USER_SCHED |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8763 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8764 | { |
| 8765 | struct task_group *tgi; |
| 8766 | unsigned long total = 0; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8767 | unsigned long global_ratio = |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8768 | to_ratio(global_rt_period(), global_rt_runtime()); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8769 | |
| 8770 | rcu_read_lock(); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8771 | list_for_each_entry_rcu(tgi, &task_groups, list) { |
| 8772 | if (tgi == tg) |
| 8773 | continue; |
| 8774 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8775 | total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period), |
| 8776 | tgi->rt_bandwidth.rt_runtime); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8777 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8778 | rcu_read_unlock(); |
| 8779 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8780 | return total + to_ratio(period, runtime) < global_ratio; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8781 | } |
Peter Zijlstra | b40b2e8 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8782 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8783 | |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8784 | /* Must be called with tasklist_lock held */ |
| 8785 | static inline int tg_has_rt_tasks(struct task_group *tg) |
| 8786 | { |
| 8787 | struct task_struct *g, *p; |
| 8788 | do_each_thread(g, p) { |
| 8789 | if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg) |
| 8790 | return 1; |
| 8791 | } while_each_thread(g, p); |
| 8792 | return 0; |
| 8793 | } |
| 8794 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8795 | static int tg_set_bandwidth(struct task_group *tg, |
| 8796 | u64 rt_period, u64 rt_runtime) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8797 | { |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8798 | int i, err = 0; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8799 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8800 | mutex_lock(&rt_constraints_mutex); |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8801 | read_lock(&tasklist_lock); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8802 | if (rt_runtime == 0 && tg_has_rt_tasks(tg)) { |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8803 | err = -EBUSY; |
| 8804 | goto unlock; |
| 8805 | } |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8806 | if (!__rt_schedulable(tg, rt_period, rt_runtime)) { |
| 8807 | err = -EINVAL; |
| 8808 | goto unlock; |
| 8809 | } |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8810 | |
| 8811 | spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock); |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8812 | tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period); |
| 8813 | tg->rt_bandwidth.rt_runtime = rt_runtime; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8814 | |
| 8815 | for_each_possible_cpu(i) { |
| 8816 | struct rt_rq *rt_rq = tg->rt_rq[i]; |
| 8817 | |
| 8818 | spin_lock(&rt_rq->rt_runtime_lock); |
| 8819 | rt_rq->rt_runtime = rt_runtime; |
| 8820 | spin_unlock(&rt_rq->rt_runtime_lock); |
| 8821 | } |
| 8822 | spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8823 | unlock: |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8824 | read_unlock(&tasklist_lock); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8825 | mutex_unlock(&rt_constraints_mutex); |
| 8826 | |
| 8827 | return err; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8828 | } |
| 8829 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8830 | int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) |
| 8831 | { |
| 8832 | u64 rt_runtime, rt_period; |
| 8833 | |
| 8834 | rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period); |
| 8835 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; |
| 8836 | if (rt_runtime_us < 0) |
| 8837 | rt_runtime = RUNTIME_INF; |
| 8838 | |
| 8839 | return tg_set_bandwidth(tg, rt_period, rt_runtime); |
| 8840 | } |
| 8841 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8842 | long sched_group_rt_runtime(struct task_group *tg) |
| 8843 | { |
| 8844 | u64 rt_runtime_us; |
| 8845 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8846 | if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF) |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8847 | return -1; |
| 8848 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8849 | rt_runtime_us = tg->rt_bandwidth.rt_runtime; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8850 | do_div(rt_runtime_us, NSEC_PER_USEC); |
| 8851 | return rt_runtime_us; |
| 8852 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8853 | |
| 8854 | int sched_group_set_rt_period(struct task_group *tg, long rt_period_us) |
| 8855 | { |
| 8856 | u64 rt_runtime, rt_period; |
| 8857 | |
| 8858 | rt_period = (u64)rt_period_us * NSEC_PER_USEC; |
| 8859 | rt_runtime = tg->rt_bandwidth.rt_runtime; |
| 8860 | |
| 8861 | return tg_set_bandwidth(tg, rt_period, rt_runtime); |
| 8862 | } |
| 8863 | |
| 8864 | long sched_group_rt_period(struct task_group *tg) |
| 8865 | { |
| 8866 | u64 rt_period_us; |
| 8867 | |
| 8868 | rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period); |
| 8869 | do_div(rt_period_us, NSEC_PER_USEC); |
| 8870 | return rt_period_us; |
| 8871 | } |
| 8872 | |
| 8873 | static int sched_rt_global_constraints(void) |
| 8874 | { |
| 8875 | int ret = 0; |
| 8876 | |
| 8877 | mutex_lock(&rt_constraints_mutex); |
| 8878 | if (!__rt_schedulable(NULL, 1, 0)) |
| 8879 | ret = -EINVAL; |
| 8880 | mutex_unlock(&rt_constraints_mutex); |
| 8881 | |
| 8882 | return ret; |
| 8883 | } |
| 8884 | #else |
| 8885 | static int sched_rt_global_constraints(void) |
| 8886 | { |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8887 | unsigned long flags; |
| 8888 | int i; |
| 8889 | |
| 8890 | spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); |
| 8891 | for_each_possible_cpu(i) { |
| 8892 | struct rt_rq *rt_rq = &cpu_rq(i)->rt; |
| 8893 | |
| 8894 | spin_lock(&rt_rq->rt_runtime_lock); |
| 8895 | rt_rq->rt_runtime = global_rt_runtime(); |
| 8896 | spin_unlock(&rt_rq->rt_runtime_lock); |
| 8897 | } |
| 8898 | spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags); |
| 8899 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8900 | return 0; |
| 8901 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8902 | #endif |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8903 | |
| 8904 | int sched_rt_handler(struct ctl_table *table, int write, |
| 8905 | struct file *filp, void __user *buffer, size_t *lenp, |
| 8906 | loff_t *ppos) |
| 8907 | { |
| 8908 | int ret; |
| 8909 | int old_period, old_runtime; |
| 8910 | static DEFINE_MUTEX(mutex); |
| 8911 | |
| 8912 | mutex_lock(&mutex); |
| 8913 | old_period = sysctl_sched_rt_period; |
| 8914 | old_runtime = sysctl_sched_rt_runtime; |
| 8915 | |
| 8916 | ret = proc_dointvec(table, write, filp, buffer, lenp, ppos); |
| 8917 | |
| 8918 | if (!ret && write) { |
| 8919 | ret = sched_rt_global_constraints(); |
| 8920 | if (ret) { |
| 8921 | sysctl_sched_rt_period = old_period; |
| 8922 | sysctl_sched_rt_runtime = old_runtime; |
| 8923 | } else { |
| 8924 | def_rt_bandwidth.rt_runtime = global_rt_runtime(); |
| 8925 | def_rt_bandwidth.rt_period = |
| 8926 | ns_to_ktime(global_rt_period()); |
| 8927 | } |
| 8928 | } |
| 8929 | mutex_unlock(&mutex); |
| 8930 | |
| 8931 | return ret; |
| 8932 | } |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8933 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8934 | #ifdef CONFIG_CGROUP_SCHED |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8935 | |
| 8936 | /* return corresponding task_group object of a cgroup */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8937 | static inline struct task_group *cgroup_tg(struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8938 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8939 | return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id), |
| 8940 | struct task_group, css); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8941 | } |
| 8942 | |
| 8943 | static struct cgroup_subsys_state * |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8944 | cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8945 | { |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8946 | struct task_group *tg, *parent; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8947 | |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8948 | if (!cgrp->parent) { |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8949 | /* This is early initialization for the top cgroup */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8950 | init_task_group.css.cgroup = cgrp; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8951 | return &init_task_group.css; |
| 8952 | } |
| 8953 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8954 | parent = cgroup_tg(cgrp->parent); |
| 8955 | tg = sched_create_group(parent); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8956 | if (IS_ERR(tg)) |
| 8957 | return ERR_PTR(-ENOMEM); |
| 8958 | |
| 8959 | /* Bind the cgroup to task_group object we just created */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8960 | tg->css.cgroup = cgrp; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8961 | |
| 8962 | return &tg->css; |
| 8963 | } |
| 8964 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8965 | static void |
| 8966 | cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8967 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8968 | struct task_group *tg = cgroup_tg(cgrp); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8969 | |
| 8970 | sched_destroy_group(tg); |
| 8971 | } |
| 8972 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8973 | static int |
| 8974 | cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
| 8975 | struct task_struct *tsk) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8976 | { |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8977 | #ifdef CONFIG_RT_GROUP_SCHED |
| 8978 | /* Don't accept realtime tasks when there is no way for them to run */ |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8979 | if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0) |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8980 | return -EINVAL; |
| 8981 | #else |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8982 | /* We don't support RT-tasks being in separate groups */ |
| 8983 | if (tsk->sched_class != &fair_sched_class) |
| 8984 | return -EINVAL; |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8985 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8986 | |
| 8987 | return 0; |
| 8988 | } |
| 8989 | |
| 8990 | static void |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8991 | cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8992 | struct cgroup *old_cont, struct task_struct *tsk) |
| 8993 | { |
| 8994 | sched_move_task(tsk); |
| 8995 | } |
| 8996 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8997 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8998 | static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype, |
| 8999 | u64 shareval) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9000 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9001 | return sched_group_set_shares(cgroup_tg(cgrp), shareval); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9002 | } |
| 9003 | |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9004 | static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9005 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9006 | struct task_group *tg = cgroup_tg(cgrp); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9007 | |
| 9008 | return (u64) tg->shares; |
| 9009 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9010 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9011 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9012 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 9013 | static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft, |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9014 | struct file *file, |
| 9015 | const char __user *userbuf, |
| 9016 | size_t nbytes, loff_t *unused_ppos) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9017 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9018 | char buffer[64]; |
| 9019 | int retval = 0; |
| 9020 | s64 val; |
| 9021 | char *end; |
| 9022 | |
| 9023 | if (!nbytes) |
| 9024 | return -EINVAL; |
| 9025 | if (nbytes >= sizeof(buffer)) |
| 9026 | return -E2BIG; |
| 9027 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 9028 | return -EFAULT; |
| 9029 | |
| 9030 | buffer[nbytes] = 0; /* nul-terminate */ |
| 9031 | |
| 9032 | /* strip newline if necessary */ |
| 9033 | if (nbytes && (buffer[nbytes-1] == '\n')) |
| 9034 | buffer[nbytes-1] = 0; |
| 9035 | val = simple_strtoll(buffer, &end, 0); |
| 9036 | if (*end) |
| 9037 | return -EINVAL; |
| 9038 | |
| 9039 | /* Pass to subsystem */ |
| 9040 | retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val); |
| 9041 | if (!retval) |
| 9042 | retval = nbytes; |
| 9043 | return retval; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9044 | } |
| 9045 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9046 | static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft, |
| 9047 | struct file *file, |
| 9048 | char __user *buf, size_t nbytes, |
| 9049 | loff_t *ppos) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9050 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9051 | char tmp[64]; |
| 9052 | long val = sched_group_rt_runtime(cgroup_tg(cgrp)); |
| 9053 | int len = sprintf(tmp, "%ld\n", val); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9054 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9055 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9056 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 9057 | |
| 9058 | static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype, |
| 9059 | u64 rt_period_us) |
| 9060 | { |
| 9061 | return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us); |
| 9062 | } |
| 9063 | |
| 9064 | static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft) |
| 9065 | { |
| 9066 | return sched_group_rt_period(cgroup_tg(cgrp)); |
| 9067 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9068 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9069 | |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 9070 | static struct cftype cpu_files[] = { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9071 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 9072 | { |
| 9073 | .name = "shares", |
| 9074 | .read_uint = cpu_shares_read_uint, |
| 9075 | .write_uint = cpu_shares_write_uint, |
| 9076 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9077 | #endif |
| 9078 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9079 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9080 | .name = "rt_runtime_us", |
| 9081 | .read = cpu_rt_runtime_read, |
| 9082 | .write = cpu_rt_runtime_write, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9083 | }, |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 9084 | { |
| 9085 | .name = "rt_period_us", |
| 9086 | .read_uint = cpu_rt_period_read_uint, |
| 9087 | .write_uint = cpu_rt_period_write_uint, |
| 9088 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9089 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9090 | }; |
| 9091 | |
| 9092 | static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont) |
| 9093 | { |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 9094 | return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files)); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9095 | } |
| 9096 | |
| 9097 | struct cgroup_subsys cpu_cgroup_subsys = { |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 9098 | .name = "cpu", |
| 9099 | .create = cpu_cgroup_create, |
| 9100 | .destroy = cpu_cgroup_destroy, |
| 9101 | .can_attach = cpu_cgroup_can_attach, |
| 9102 | .attach = cpu_cgroup_attach, |
| 9103 | .populate = cpu_cgroup_populate, |
| 9104 | .subsys_id = cpu_cgroup_subsys_id, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9105 | .early_init = 1, |
| 9106 | }; |
| 9107 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9108 | #endif /* CONFIG_CGROUP_SCHED */ |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9109 | |
| 9110 | #ifdef CONFIG_CGROUP_CPUACCT |
| 9111 | |
| 9112 | /* |
| 9113 | * CPU accounting code for task groups. |
| 9114 | * |
| 9115 | * Based on the work by Paul Menage (menage@google.com) and Balbir Singh |
| 9116 | * (balbir@in.ibm.com). |
| 9117 | */ |
| 9118 | |
| 9119 | /* track cpu usage of a group of tasks */ |
| 9120 | struct cpuacct { |
| 9121 | struct cgroup_subsys_state css; |
| 9122 | /* cpuusage holds pointer to a u64-type object on every cpu */ |
| 9123 | u64 *cpuusage; |
| 9124 | }; |
| 9125 | |
| 9126 | struct cgroup_subsys cpuacct_subsys; |
| 9127 | |
| 9128 | /* return cpu accounting group corresponding to this container */ |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9129 | static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9130 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9131 | return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id), |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9132 | struct cpuacct, css); |
| 9133 | } |
| 9134 | |
| 9135 | /* return cpu accounting group to which this task belongs */ |
| 9136 | static inline struct cpuacct *task_ca(struct task_struct *tsk) |
| 9137 | { |
| 9138 | return container_of(task_subsys_state(tsk, cpuacct_subsys_id), |
| 9139 | struct cpuacct, css); |
| 9140 | } |
| 9141 | |
| 9142 | /* create a new cpu accounting group */ |
| 9143 | static struct cgroup_subsys_state *cpuacct_create( |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9144 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9145 | { |
| 9146 | struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL); |
| 9147 | |
| 9148 | if (!ca) |
| 9149 | return ERR_PTR(-ENOMEM); |
| 9150 | |
| 9151 | ca->cpuusage = alloc_percpu(u64); |
| 9152 | if (!ca->cpuusage) { |
| 9153 | kfree(ca); |
| 9154 | return ERR_PTR(-ENOMEM); |
| 9155 | } |
| 9156 | |
| 9157 | return &ca->css; |
| 9158 | } |
| 9159 | |
| 9160 | /* destroy an existing cpu accounting group */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 9161 | static void |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9162 | cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9163 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9164 | struct cpuacct *ca = cgroup_ca(cgrp); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9165 | |
| 9166 | free_percpu(ca->cpuusage); |
| 9167 | kfree(ca); |
| 9168 | } |
| 9169 | |
| 9170 | /* return total cpu usage (in nanoseconds) of a group */ |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9171 | static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9172 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9173 | struct cpuacct *ca = cgroup_ca(cgrp); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9174 | u64 totalcpuusage = 0; |
| 9175 | int i; |
| 9176 | |
| 9177 | for_each_possible_cpu(i) { |
| 9178 | u64 *cpuusage = percpu_ptr(ca->cpuusage, i); |
| 9179 | |
| 9180 | /* |
| 9181 | * Take rq->lock to make 64-bit addition safe on 32-bit |
| 9182 | * platforms. |
| 9183 | */ |
| 9184 | spin_lock_irq(&cpu_rq(i)->lock); |
| 9185 | totalcpuusage += *cpuusage; |
| 9186 | spin_unlock_irq(&cpu_rq(i)->lock); |
| 9187 | } |
| 9188 | |
| 9189 | return totalcpuusage; |
| 9190 | } |
| 9191 | |
Dhaval Giani | 0297b80 | 2008-02-29 10:02:44 +0530 | [diff] [blame] | 9192 | static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype, |
| 9193 | u64 reset) |
| 9194 | { |
| 9195 | struct cpuacct *ca = cgroup_ca(cgrp); |
| 9196 | int err = 0; |
| 9197 | int i; |
| 9198 | |
| 9199 | if (reset) { |
| 9200 | err = -EINVAL; |
| 9201 | goto out; |
| 9202 | } |
| 9203 | |
| 9204 | for_each_possible_cpu(i) { |
| 9205 | u64 *cpuusage = percpu_ptr(ca->cpuusage, i); |
| 9206 | |
| 9207 | spin_lock_irq(&cpu_rq(i)->lock); |
| 9208 | *cpuusage = 0; |
| 9209 | spin_unlock_irq(&cpu_rq(i)->lock); |
| 9210 | } |
| 9211 | out: |
| 9212 | return err; |
| 9213 | } |
| 9214 | |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9215 | static struct cftype files[] = { |
| 9216 | { |
| 9217 | .name = "usage", |
| 9218 | .read_uint = cpuusage_read, |
Dhaval Giani | 0297b80 | 2008-02-29 10:02:44 +0530 | [diff] [blame] | 9219 | .write_uint = cpuusage_write, |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9220 | }, |
| 9221 | }; |
| 9222 | |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9223 | static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9224 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9225 | return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files)); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9226 | } |
| 9227 | |
| 9228 | /* |
| 9229 | * charge this task's execution time to its accounting group. |
| 9230 | * |
| 9231 | * called with rq->lock held. |
| 9232 | */ |
| 9233 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime) |
| 9234 | { |
| 9235 | struct cpuacct *ca; |
| 9236 | |
| 9237 | if (!cpuacct_subsys.active) |
| 9238 | return; |
| 9239 | |
| 9240 | ca = task_ca(tsk); |
| 9241 | if (ca) { |
| 9242 | u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk)); |
| 9243 | |
| 9244 | *cpuusage += cputime; |
| 9245 | } |
| 9246 | } |
| 9247 | |
| 9248 | struct cgroup_subsys cpuacct_subsys = { |
| 9249 | .name = "cpuacct", |
| 9250 | .create = cpuacct_create, |
| 9251 | .destroy = cpuacct_destroy, |
| 9252 | .populate = cpuacct_populate, |
| 9253 | .subsys_id = cpuacct_subsys_id, |
| 9254 | }; |
| 9255 | #endif /* CONFIG_CGROUP_CPUACCT */ |