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> |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 71 | #include <linux/debugfs.h> |
| 72 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 74 | #include <asm/tlb.h> |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 75 | #include <asm/irq_regs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | /* |
Alexey Dobriyan | b035b6d | 2007-02-10 01:45:10 -0800 | [diff] [blame] | 78 | * Scheduler clock - returns current time in nanosec units. |
| 79 | * This is default implementation. |
| 80 | * Architectures and sub-architectures can override this. |
| 81 | */ |
| 82 | unsigned long long __attribute__((weak)) sched_clock(void) |
| 83 | { |
Eric Dumazet | d6322fa | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 84 | return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ); |
Alexey Dobriyan | b035b6d | 2007-02-10 01:45:10 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * Convert user-nice values [ -20 ... 0 ... 19 ] |
| 89 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], |
| 90 | * and back. |
| 91 | */ |
| 92 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) |
| 93 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) |
| 94 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) |
| 95 | |
| 96 | /* |
| 97 | * 'User priority' is the nice value converted to something we |
| 98 | * can work with better when scaling various scheduler parameters, |
| 99 | * it's a [ 0 ... 39 ] range. |
| 100 | */ |
| 101 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) |
| 102 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) |
| 103 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) |
| 104 | |
| 105 | /* |
Ingo Molnar | d7876a0 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 106 | * Helpers for converting nanosecond timing to jiffy resolution |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | */ |
Eric Dumazet | d6322fa | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 108 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 110 | #define NICE_0_LOAD SCHED_LOAD_SCALE |
| 111 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | /* |
| 114 | * These are the 'tuning knobs' of the scheduler: |
| 115 | * |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 116 | * default timeslice is 100 msecs (used only for SCHED_RR tasks). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * Timeslices get refilled after they expire. |
| 118 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | #define DEF_TIMESLICE (100 * HZ / 1000) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 120 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 121 | /* |
| 122 | * single value that denotes runtime == period, ie unlimited time. |
| 123 | */ |
| 124 | #define RUNTIME_INF ((u64)~0ULL) |
| 125 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 126 | #ifdef CONFIG_SMP |
| 127 | /* |
| 128 | * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) |
| 129 | * Since cpu_power is a 'constant', we can use a reciprocal divide. |
| 130 | */ |
| 131 | static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load) |
| 132 | { |
| 133 | return reciprocal_divide(load, sg->reciprocal_cpu_power); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Each time a sched group cpu_power is changed, |
| 138 | * we must compute its reciprocal value |
| 139 | */ |
| 140 | static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val) |
| 141 | { |
| 142 | sg->__cpu_power += val; |
| 143 | sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power); |
| 144 | } |
| 145 | #endif |
| 146 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 147 | static inline int rt_policy(int policy) |
| 148 | { |
| 149 | if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR)) |
| 150 | return 1; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static inline int task_has_rt_policy(struct task_struct *p) |
| 155 | { |
| 156 | return rt_policy(p->policy); |
| 157 | } |
| 158 | |
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 | * This is the priority-queue data structure of the RT scheduling class: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | */ |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 162 | struct rt_prio_array { |
| 163 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ |
| 164 | struct list_head queue[MAX_RT_PRIO]; |
| 165 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 167 | struct rt_bandwidth { |
Ingo Molnar | ea736ed | 2008-03-25 13:51:45 +0100 | [diff] [blame] | 168 | /* nests inside the rq lock: */ |
| 169 | spinlock_t rt_runtime_lock; |
| 170 | ktime_t rt_period; |
| 171 | u64 rt_runtime; |
| 172 | struct hrtimer rt_period_timer; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | static struct rt_bandwidth def_rt_bandwidth; |
| 176 | |
| 177 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); |
| 178 | |
| 179 | static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) |
| 180 | { |
| 181 | struct rt_bandwidth *rt_b = |
| 182 | container_of(timer, struct rt_bandwidth, rt_period_timer); |
| 183 | ktime_t now; |
| 184 | int overrun; |
| 185 | int idle = 0; |
| 186 | |
| 187 | for (;;) { |
| 188 | now = hrtimer_cb_get_time(timer); |
| 189 | overrun = hrtimer_forward(timer, now, rt_b->rt_period); |
| 190 | |
| 191 | if (!overrun) |
| 192 | break; |
| 193 | |
| 194 | idle = do_sched_rt_period_timer(rt_b, overrun); |
| 195 | } |
| 196 | |
| 197 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; |
| 198 | } |
| 199 | |
| 200 | static |
| 201 | void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) |
| 202 | { |
| 203 | rt_b->rt_period = ns_to_ktime(period); |
| 204 | rt_b->rt_runtime = runtime; |
| 205 | |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 206 | spin_lock_init(&rt_b->rt_runtime_lock); |
| 207 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 208 | hrtimer_init(&rt_b->rt_period_timer, |
| 209 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 210 | rt_b->rt_period_timer.function = sched_rt_period_timer; |
| 211 | rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; |
| 212 | } |
| 213 | |
| 214 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) |
| 215 | { |
| 216 | ktime_t now; |
| 217 | |
| 218 | if (rt_b->rt_runtime == RUNTIME_INF) |
| 219 | return; |
| 220 | |
| 221 | if (hrtimer_active(&rt_b->rt_period_timer)) |
| 222 | return; |
| 223 | |
| 224 | spin_lock(&rt_b->rt_runtime_lock); |
| 225 | for (;;) { |
| 226 | if (hrtimer_active(&rt_b->rt_period_timer)) |
| 227 | break; |
| 228 | |
| 229 | now = hrtimer_cb_get_time(&rt_b->rt_period_timer); |
| 230 | hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period); |
| 231 | hrtimer_start(&rt_b->rt_period_timer, |
| 232 | rt_b->rt_period_timer.expires, |
| 233 | HRTIMER_MODE_ABS); |
| 234 | } |
| 235 | spin_unlock(&rt_b->rt_runtime_lock); |
| 236 | } |
| 237 | |
| 238 | #ifdef CONFIG_RT_GROUP_SCHED |
| 239 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) |
| 240 | { |
| 241 | hrtimer_cancel(&rt_b->rt_period_timer); |
| 242 | } |
| 243 | #endif |
| 244 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 245 | #ifdef CONFIG_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 246 | |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 247 | #include <linux/cgroup.h> |
| 248 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 249 | struct cfs_rq; |
| 250 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 251 | static LIST_HEAD(task_groups); |
| 252 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 253 | /* task group related information */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 254 | struct task_group { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 255 | #ifdef CONFIG_CGROUP_SCHED |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 256 | struct cgroup_subsys_state css; |
| 257 | #endif |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 258 | |
| 259 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 260 | /* schedulable entities of this group on each cpu */ |
| 261 | struct sched_entity **se; |
| 262 | /* runqueue "owned" by this group on each cpu */ |
| 263 | struct cfs_rq **cfs_rq; |
| 264 | unsigned long shares; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 265 | #endif |
| 266 | |
| 267 | #ifdef CONFIG_RT_GROUP_SCHED |
| 268 | struct sched_rt_entity **rt_se; |
| 269 | struct rt_rq **rt_rq; |
| 270 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 271 | struct rt_bandwidth rt_bandwidth; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 272 | #endif |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 273 | |
Srivatsa Vaddagiri | ae8393e | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 274 | struct rcu_head rcu; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 275 | struct list_head list; |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 276 | |
| 277 | struct task_group *parent; |
| 278 | struct list_head siblings; |
| 279 | struct list_head children; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 280 | }; |
| 281 | |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 282 | #ifdef CONFIG_USER_SCHED |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * Root task group. |
| 286 | * Every UID task group (including init_task_group aka UID-0) will |
| 287 | * be a child to this group. |
| 288 | */ |
| 289 | struct task_group root_task_group; |
| 290 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 291 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 292 | /* Default task group's sched entity on each cpu */ |
| 293 | static DEFINE_PER_CPU(struct sched_entity, init_sched_entity); |
| 294 | /* Default task group's cfs_rq on each cpu */ |
| 295 | 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] | 296 | #endif |
| 297 | |
| 298 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 299 | static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity); |
| 300 | 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] | 301 | #endif |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 302 | #else |
| 303 | #define root_task_group init_task_group |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 304 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 305 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 306 | /* 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] | 307 | * a task group's cpu shares. |
| 308 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 309 | static DEFINE_SPINLOCK(task_group_lock); |
Srivatsa Vaddagiri | ec2c507 | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 310 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 311 | /* doms_cur_mutex serializes access to doms_cur[] array */ |
| 312 | static DEFINE_MUTEX(doms_cur_mutex); |
| 313 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 314 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 315 | #ifdef CONFIG_USER_SCHED |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 316 | # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD) |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 317 | #else |
Srivatsa Vaddagiri | 93f992c | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 318 | # define INIT_TASK_GROUP_LOAD NICE_0_LOAD |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 319 | #endif |
| 320 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 321 | #define MIN_SHARES 2 |
| 322 | |
Srivatsa Vaddagiri | 93f992c | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 323 | static int init_task_group_load = INIT_TASK_GROUP_LOAD; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 324 | #endif |
| 325 | |
| 326 | /* Default task group. |
| 327 | * Every task in system belong to this group at bootup. |
| 328 | */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 329 | struct task_group init_task_group; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 330 | |
| 331 | /* return group to which a task belongs */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 332 | static inline struct task_group *task_group(struct task_struct *p) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 333 | { |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 334 | struct task_group *tg; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 335 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 336 | #ifdef CONFIG_USER_SCHED |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 337 | tg = p->user->tg; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 338 | #elif defined(CONFIG_CGROUP_SCHED) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 339 | tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id), |
| 340 | struct task_group, css); |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 341 | #else |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 342 | tg = &init_task_group; |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 343 | #endif |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 344 | return tg; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* 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] | 348 | 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] | 349 | { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 350 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Dmitry Adamushko | ce96b5a | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 351 | p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; |
| 352 | p->se.parent = task_group(p)->se[cpu]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 353 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 354 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 355 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 356 | p->rt.rt_rq = task_group(p)->rt_rq[cpu]; |
| 357 | p->rt.parent = task_group(p)->rt_se[cpu]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 358 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 361 | static inline void lock_doms_cur(void) |
| 362 | { |
| 363 | mutex_lock(&doms_cur_mutex); |
| 364 | } |
| 365 | |
| 366 | static inline void unlock_doms_cur(void) |
| 367 | { |
| 368 | mutex_unlock(&doms_cur_mutex); |
| 369 | } |
| 370 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 371 | #else |
| 372 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 373 | 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] | 374 | static inline void lock_doms_cur(void) { } |
| 375 | static inline void unlock_doms_cur(void) { } |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 376 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 377 | #endif /* CONFIG_GROUP_SCHED */ |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 378 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 379 | /* CFS-related fields in a runqueue */ |
| 380 | struct cfs_rq { |
| 381 | struct load_weight load; |
| 382 | unsigned long nr_running; |
| 383 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 384 | u64 exec_clock; |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 385 | u64 min_vruntime; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 386 | |
| 387 | struct rb_root tasks_timeline; |
| 388 | struct rb_node *rb_leftmost; |
Peter Zijlstra | 4a55bd5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 389 | |
| 390 | struct list_head tasks; |
| 391 | struct list_head *balance_iterator; |
| 392 | |
| 393 | /* |
| 394 | * 'curr' points to currently running entity on this cfs_rq. |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 395 | * It is set to NULL otherwise (i.e when none are currently running). |
| 396 | */ |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 397 | struct sched_entity *curr, *next; |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 398 | |
| 399 | unsigned long nr_spread_over; |
| 400 | |
Ingo Molnar | 62160e3 | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 401 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 402 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ |
| 403 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 404 | /* |
| 405 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 406 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities |
| 407 | * (like users, containers etc.) |
| 408 | * |
| 409 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This |
| 410 | * list is used during load balance. |
| 411 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 412 | struct list_head leaf_cfs_rq_list; |
| 413 | struct task_group *tg; /* group that "owns" this runqueue */ |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 414 | |
| 415 | #ifdef CONFIG_SMP |
| 416 | unsigned long task_weight; |
| 417 | unsigned long shares; |
| 418 | /* |
| 419 | * We need space to build a sched_domain wide view of the full task |
| 420 | * group tree, in order to avoid depending on dynamic memory allocation |
| 421 | * during the load balancing we place this in the per cpu task group |
| 422 | * hierarchy. This limits the load balancing to one instance per cpu, |
| 423 | * but more should not be needed anyway. |
| 424 | */ |
| 425 | struct aggregate_struct { |
| 426 | /* |
| 427 | * load = weight(cpus) * f(tg) |
| 428 | * |
| 429 | * Where f(tg) is the recursive weight fraction assigned to |
| 430 | * this group. |
| 431 | */ |
| 432 | unsigned long load; |
| 433 | |
| 434 | /* |
| 435 | * part of the group weight distributed to this span. |
| 436 | */ |
| 437 | unsigned long shares; |
| 438 | |
| 439 | /* |
| 440 | * The sum of all runqueue weights within this span. |
| 441 | */ |
| 442 | unsigned long rq_weight; |
| 443 | |
| 444 | /* |
| 445 | * Weight contributed by tasks; this is the part we can |
| 446 | * influence by moving tasks around. |
| 447 | */ |
| 448 | unsigned long task_weight; |
| 449 | } aggregate; |
| 450 | #endif |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 451 | #endif |
| 452 | }; |
| 453 | |
| 454 | /* Real-Time classes' related field in a runqueue: */ |
| 455 | struct rt_rq { |
| 456 | struct rt_prio_array active; |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 457 | unsigned long rt_nr_running; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 458 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 459 | int highest_prio; /* highest queued rt task prio */ |
| 460 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 461 | #ifdef CONFIG_SMP |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 462 | unsigned long rt_nr_migratory; |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 463 | int overloaded; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 464 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 465 | int rt_throttled; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 466 | u64 rt_time; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 467 | u64 rt_runtime; |
Ingo Molnar | ea736ed | 2008-03-25 13:51:45 +0100 | [diff] [blame] | 468 | /* Nests inside the rq lock: */ |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 469 | spinlock_t rt_runtime_lock; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 470 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 471 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 472 | unsigned long rt_nr_boosted; |
| 473 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 474 | struct rq *rq; |
| 475 | struct list_head leaf_rt_rq_list; |
| 476 | struct task_group *tg; |
| 477 | struct sched_rt_entity *rt_se; |
| 478 | #endif |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 479 | }; |
| 480 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 481 | #ifdef CONFIG_SMP |
| 482 | |
| 483 | /* |
| 484 | * 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] | 485 | * variables. Each exclusive cpuset essentially defines an island domain by |
| 486 | * fully partitioning the member cpus from any other cpuset. Whenever a new |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 487 | * exclusive cpuset is created, we also create and attach a new root-domain |
| 488 | * object. |
| 489 | * |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 490 | */ |
| 491 | struct root_domain { |
| 492 | atomic_t refcount; |
| 493 | cpumask_t span; |
| 494 | cpumask_t online; |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 495 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 496 | /* |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 497 | * The "RT overload" flag: it gets set if a CPU has more than |
| 498 | * one runnable RT task. |
| 499 | */ |
| 500 | cpumask_t rto_mask; |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 501 | atomic_t rto_count; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 502 | }; |
| 503 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 504 | /* |
| 505 | * By default the system creates a single root-domain with all cpus as |
| 506 | * members (mimicking the global state we have today). |
| 507 | */ |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 508 | static struct root_domain def_root_domain; |
| 509 | |
| 510 | #endif |
| 511 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 512 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | * This is the main, per-CPU runqueue data structure. |
| 514 | * |
| 515 | * Locking rule: those places that want to lock multiple runqueues |
| 516 | * (such as the load balancing or the thread migration code), lock |
| 517 | * acquire operations must be ordered by ascending &runqueue. |
| 518 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 519 | struct rq { |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 520 | /* runqueue lock: */ |
| 521 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | /* |
| 524 | * nr_running and cpu_load should be in the same cacheline because |
| 525 | * remote CPUs use both these fields when doing load calculation. |
| 526 | */ |
| 527 | unsigned long nr_running; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 528 | #define CPU_LOAD_IDX_MAX 5 |
| 529 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; |
Siddha, Suresh B | bdecea3 | 2007-05-08 00:32:48 -0700 | [diff] [blame] | 530 | unsigned char idle_at_tick; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 531 | #ifdef CONFIG_NO_HZ |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 532 | unsigned long last_tick_seen; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 533 | unsigned char in_nohz_recently; |
| 534 | #endif |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 535 | /* capture load from *all* tasks on this cpu: */ |
| 536 | struct load_weight load; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 537 | unsigned long nr_load_updates; |
| 538 | u64 nr_switches; |
| 539 | |
| 540 | struct cfs_rq cfs; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 541 | struct rt_rq rt; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 542 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 543 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 544 | /* list of leaf cfs_rq on this cpu: */ |
| 545 | struct list_head leaf_cfs_rq_list; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 546 | #endif |
| 547 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 548 | struct list_head leaf_rt_rq_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * This is part of a global counter where only the total sum |
| 553 | * over all CPUs matters. A task can increase this counter on |
| 554 | * one CPU and if it got migrated afterwards it may decrease |
| 555 | * it on another CPU. Always updated under the runqueue lock: |
| 556 | */ |
| 557 | unsigned long nr_uninterruptible; |
| 558 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 559 | struct task_struct *curr, *idle; |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 560 | unsigned long next_balance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | struct mm_struct *prev_mm; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 562 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 563 | u64 clock, prev_clock_raw; |
| 564 | s64 clock_max_delta; |
| 565 | |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 566 | unsigned int clock_warps, clock_overflows, clock_underflows; |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 567 | u64 idle_clock; |
| 568 | unsigned int clock_deep_idle_events; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 569 | u64 tick_timestamp; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | atomic_t nr_iowait; |
| 572 | |
| 573 | #ifdef CONFIG_SMP |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 574 | struct root_domain *rd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | struct sched_domain *sd; |
| 576 | |
| 577 | /* For active balancing */ |
| 578 | int active_balance; |
| 579 | int push_cpu; |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 580 | /* cpu of this runqueue: */ |
| 581 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 583 | struct task_struct *migration_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | struct list_head migration_queue; |
| 585 | #endif |
| 586 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 587 | #ifdef CONFIG_SCHED_HRTICK |
| 588 | unsigned long hrtick_flags; |
| 589 | ktime_t hrtick_expire; |
| 590 | struct hrtimer hrtick_timer; |
| 591 | #endif |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | #ifdef CONFIG_SCHEDSTATS |
| 594 | /* latency stats */ |
| 595 | struct sched_info rq_sched_info; |
| 596 | |
| 597 | /* sys_sched_yield() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 598 | unsigned int yld_exp_empty; |
| 599 | unsigned int yld_act_empty; |
| 600 | unsigned int yld_both_empty; |
| 601 | unsigned int yld_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
| 603 | /* schedule() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 604 | unsigned int sched_switch; |
| 605 | unsigned int sched_count; |
| 606 | unsigned int sched_goidle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | /* try_to_wake_up() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 609 | unsigned int ttwu_count; |
| 610 | unsigned int ttwu_local; |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 611 | |
| 612 | /* BKL stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 613 | unsigned int bkl_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | #endif |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 615 | struct lock_class_key rq_lock_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | }; |
| 617 | |
Fenghua Yu | f34e3b6 | 2007-07-19 01:48:13 -0700 | [diff] [blame] | 618 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 620 | static inline void check_preempt_curr(struct rq *rq, struct task_struct *p) |
| 621 | { |
| 622 | rq->curr->sched_class->check_preempt_curr(rq, p); |
| 623 | } |
| 624 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 625 | static inline int cpu_of(struct rq *rq) |
| 626 | { |
| 627 | #ifdef CONFIG_SMP |
| 628 | return rq->cpu; |
| 629 | #else |
| 630 | return 0; |
| 631 | #endif |
| 632 | } |
| 633 | |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 634 | #ifdef CONFIG_NO_HZ |
| 635 | static inline bool nohz_on(int cpu) |
| 636 | { |
| 637 | return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE; |
| 638 | } |
| 639 | |
| 640 | static inline u64 max_skipped_ticks(struct rq *rq) |
| 641 | { |
| 642 | return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1; |
| 643 | } |
| 644 | |
| 645 | static inline void update_last_tick_seen(struct rq *rq) |
| 646 | { |
| 647 | rq->last_tick_seen = jiffies; |
| 648 | } |
| 649 | #else |
| 650 | static inline u64 max_skipped_ticks(struct rq *rq) |
| 651 | { |
| 652 | return 1; |
| 653 | } |
| 654 | |
| 655 | static inline void update_last_tick_seen(struct rq *rq) |
| 656 | { |
| 657 | } |
| 658 | #endif |
| 659 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 660 | /* |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 661 | * Update the per-runqueue clock, as finegrained as the platform can give |
| 662 | * us, but without assuming monotonicity, etc.: |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 663 | */ |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 664 | static void __update_rq_clock(struct rq *rq) |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 665 | { |
| 666 | u64 prev_raw = rq->prev_clock_raw; |
| 667 | u64 now = sched_clock(); |
| 668 | s64 delta = now - prev_raw; |
| 669 | u64 clock = rq->clock; |
| 670 | |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 671 | #ifdef CONFIG_SCHED_DEBUG |
| 672 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 673 | #endif |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 674 | /* |
| 675 | * Protect against sched_clock() occasionally going backwards: |
| 676 | */ |
| 677 | if (unlikely(delta < 0)) { |
| 678 | clock++; |
| 679 | rq->clock_warps++; |
| 680 | } else { |
| 681 | /* |
| 682 | * Catch too large forward jumps too: |
| 683 | */ |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 684 | u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC; |
| 685 | u64 max_time = rq->tick_timestamp + max_jump; |
| 686 | |
| 687 | if (unlikely(clock + delta > max_time)) { |
| 688 | if (clock < max_time) |
| 689 | clock = max_time; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 690 | else |
| 691 | clock++; |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 692 | rq->clock_overflows++; |
| 693 | } else { |
| 694 | if (unlikely(delta > rq->clock_max_delta)) |
| 695 | rq->clock_max_delta = delta; |
| 696 | clock += delta; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | rq->prev_clock_raw = now; |
| 701 | rq->clock = clock; |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 704 | static void update_rq_clock(struct rq *rq) |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 705 | { |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 706 | if (likely(smp_processor_id() == cpu_of(rq))) |
| 707 | __update_rq_clock(rq); |
| 708 | } |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 709 | |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 710 | /* |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 711 | * 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] | 712 | * See detach_destroy_domains: synchronize_sched for details. |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 713 | * |
| 714 | * The domain tree of any CPU may only be accessed from within |
| 715 | * preempt-disabled sections. |
| 716 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 717 | #define for_each_domain(cpu, __sd) \ |
| 718 | for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
| 720 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
| 721 | #define this_rq() (&__get_cpu_var(runqueues)) |
| 722 | #define task_rq(p) cpu_rq(task_cpu(p)) |
| 723 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) |
| 724 | |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 725 | /* |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 726 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: |
| 727 | */ |
| 728 | #ifdef CONFIG_SCHED_DEBUG |
| 729 | # define const_debug __read_mostly |
| 730 | #else |
| 731 | # define const_debug static const |
| 732 | #endif |
| 733 | |
| 734 | /* |
| 735 | * Debugging: various feature bits |
| 736 | */ |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 737 | |
| 738 | #define SCHED_FEAT(name, enabled) \ |
| 739 | __SCHED_FEAT_##name , |
| 740 | |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 741 | enum { |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 742 | #include "sched_features.h" |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 743 | }; |
| 744 | |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 745 | #undef SCHED_FEAT |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 746 | |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 747 | #define SCHED_FEAT(name, enabled) \ |
| 748 | (1UL << __SCHED_FEAT_##name) * enabled | |
| 749 | |
| 750 | const_debug unsigned int sysctl_sched_features = |
| 751 | #include "sched_features.h" |
| 752 | 0; |
| 753 | |
| 754 | #undef SCHED_FEAT |
| 755 | |
| 756 | #ifdef CONFIG_SCHED_DEBUG |
| 757 | #define SCHED_FEAT(name, enabled) \ |
| 758 | #name , |
| 759 | |
| 760 | __read_mostly char *sched_feat_names[] = { |
| 761 | #include "sched_features.h" |
| 762 | NULL |
| 763 | }; |
| 764 | |
| 765 | #undef SCHED_FEAT |
| 766 | |
| 767 | int sched_feat_open(struct inode *inode, struct file *filp) |
| 768 | { |
| 769 | filp->private_data = inode->i_private; |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | static ssize_t |
| 774 | sched_feat_read(struct file *filp, char __user *ubuf, |
| 775 | size_t cnt, loff_t *ppos) |
| 776 | { |
| 777 | char *buf; |
| 778 | int r = 0; |
| 779 | int len = 0; |
| 780 | int i; |
| 781 | |
| 782 | for (i = 0; sched_feat_names[i]; i++) { |
| 783 | len += strlen(sched_feat_names[i]); |
| 784 | len += 4; |
| 785 | } |
| 786 | |
| 787 | buf = kmalloc(len + 2, GFP_KERNEL); |
| 788 | if (!buf) |
| 789 | return -ENOMEM; |
| 790 | |
| 791 | for (i = 0; sched_feat_names[i]; i++) { |
| 792 | if (sysctl_sched_features & (1UL << i)) |
| 793 | r += sprintf(buf + r, "%s ", sched_feat_names[i]); |
| 794 | else |
Ingo Molnar | c24b7c5 | 2008-04-18 10:55:34 +0200 | [diff] [blame] | 795 | r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]); |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | r += sprintf(buf + r, "\n"); |
| 799 | WARN_ON(r >= len + 2); |
| 800 | |
| 801 | r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 802 | |
| 803 | kfree(buf); |
| 804 | |
| 805 | return r; |
| 806 | } |
| 807 | |
| 808 | static ssize_t |
| 809 | sched_feat_write(struct file *filp, const char __user *ubuf, |
| 810 | size_t cnt, loff_t *ppos) |
| 811 | { |
| 812 | char buf[64]; |
| 813 | char *cmp = buf; |
| 814 | int neg = 0; |
| 815 | int i; |
| 816 | |
| 817 | if (cnt > 63) |
| 818 | cnt = 63; |
| 819 | |
| 820 | if (copy_from_user(&buf, ubuf, cnt)) |
| 821 | return -EFAULT; |
| 822 | |
| 823 | buf[cnt] = 0; |
| 824 | |
Ingo Molnar | c24b7c5 | 2008-04-18 10:55:34 +0200 | [diff] [blame] | 825 | if (strncmp(buf, "NO_", 3) == 0) { |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 826 | neg = 1; |
| 827 | cmp += 3; |
| 828 | } |
| 829 | |
| 830 | for (i = 0; sched_feat_names[i]; i++) { |
| 831 | int len = strlen(sched_feat_names[i]); |
| 832 | |
| 833 | if (strncmp(cmp, sched_feat_names[i], len) == 0) { |
| 834 | if (neg) |
| 835 | sysctl_sched_features &= ~(1UL << i); |
| 836 | else |
| 837 | sysctl_sched_features |= (1UL << i); |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | if (!sched_feat_names[i]) |
| 843 | return -EINVAL; |
| 844 | |
| 845 | filp->f_pos += cnt; |
| 846 | |
| 847 | return cnt; |
| 848 | } |
| 849 | |
| 850 | static struct file_operations sched_feat_fops = { |
| 851 | .open = sched_feat_open, |
| 852 | .read = sched_feat_read, |
| 853 | .write = sched_feat_write, |
| 854 | }; |
| 855 | |
| 856 | static __init int sched_init_debug(void) |
| 857 | { |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 858 | debugfs_create_file("sched_features", 0644, NULL, NULL, |
| 859 | &sched_feat_fops); |
| 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | late_initcall(sched_init_debug); |
| 864 | |
| 865 | #endif |
| 866 | |
| 867 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 868 | |
| 869 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 870 | * Number of tasks to iterate in a single balance run. |
| 871 | * Limited because this is done with IRQs disabled. |
| 872 | */ |
| 873 | const_debug unsigned int sysctl_sched_nr_migrate = 32; |
| 874 | |
| 875 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 876 | * period over which we measure -rt task cpu usage in us. |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 877 | * default: 1s |
| 878 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 879 | unsigned int sysctl_sched_rt_period = 1000000; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 880 | |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 881 | static __read_mostly int scheduler_running; |
| 882 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 883 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 884 | * part of the period that we allow rt tasks to run in us. |
| 885 | * default: 0.95s |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 886 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 887 | int sysctl_sched_rt_runtime = 950000; |
| 888 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 889 | static inline u64 global_rt_period(void) |
| 890 | { |
| 891 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; |
| 892 | } |
| 893 | |
| 894 | static inline u64 global_rt_runtime(void) |
| 895 | { |
| 896 | if (sysctl_sched_rt_period < 0) |
| 897 | return RUNTIME_INF; |
| 898 | |
| 899 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; |
| 900 | } |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 901 | |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 902 | static const unsigned long long time_sync_thresh = 100000; |
| 903 | |
| 904 | static DEFINE_PER_CPU(unsigned long long, time_offset); |
| 905 | static DEFINE_PER_CPU(unsigned long long, prev_cpu_time); |
| 906 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 907 | /* |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 908 | * Global lock which we take every now and then to synchronize |
| 909 | * the CPUs time. This method is not warp-safe, but it's good |
| 910 | * enough to synchronize slowly diverging time sources and thus |
| 911 | * it's good enough for tracing: |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 912 | */ |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 913 | static DEFINE_SPINLOCK(time_sync_lock); |
| 914 | static unsigned long long prev_global_time; |
| 915 | |
| 916 | static unsigned long long __sync_cpu_clock(cycles_t time, int cpu) |
| 917 | { |
| 918 | unsigned long flags; |
| 919 | |
| 920 | spin_lock_irqsave(&time_sync_lock, flags); |
| 921 | |
| 922 | if (time < prev_global_time) { |
| 923 | per_cpu(time_offset, cpu) += prev_global_time - time; |
| 924 | time = prev_global_time; |
| 925 | } else { |
| 926 | prev_global_time = time; |
| 927 | } |
| 928 | |
| 929 | spin_unlock_irqrestore(&time_sync_lock, flags); |
| 930 | |
| 931 | return time; |
| 932 | } |
| 933 | |
| 934 | static unsigned long long __cpu_clock(int cpu) |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 935 | { |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 936 | unsigned long long now; |
| 937 | unsigned long flags; |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 938 | struct rq *rq; |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 939 | |
Ingo Molnar | 8ced5f6 | 2007-12-07 19:02:47 +0100 | [diff] [blame] | 940 | /* |
| 941 | * Only call sched_clock() if the scheduler has already been |
| 942 | * initialized (some code might call cpu_clock() very early): |
| 943 | */ |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 944 | if (unlikely(!scheduler_running)) |
| 945 | return 0; |
| 946 | |
| 947 | local_irq_save(flags); |
| 948 | rq = cpu_rq(cpu); |
| 949 | update_rq_clock(rq); |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 950 | now = rq->clock; |
Ingo Molnar | 2cd4d0e | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 951 | local_irq_restore(flags); |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 952 | |
| 953 | return now; |
| 954 | } |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 955 | |
| 956 | /* |
| 957 | * For kernel-internal use: high-speed (but slightly incorrect) per-cpu |
| 958 | * clock constructed from sched_clock(): |
| 959 | */ |
| 960 | unsigned long long cpu_clock(int cpu) |
| 961 | { |
| 962 | unsigned long long prev_cpu_time, time, delta_time; |
| 963 | |
| 964 | prev_cpu_time = per_cpu(prev_cpu_time, cpu); |
| 965 | time = __cpu_clock(cpu) + per_cpu(time_offset, cpu); |
| 966 | delta_time = time-prev_cpu_time; |
| 967 | |
| 968 | if (unlikely(delta_time > time_sync_thresh)) |
| 969 | time = __sync_cpu_clock(time, cpu); |
| 970 | |
| 971 | return time; |
| 972 | } |
Paul E. McKenney | a58f6f2 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 973 | EXPORT_SYMBOL_GPL(cpu_clock); |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | #ifndef prepare_arch_switch |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 976 | # define prepare_arch_switch(next) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | #endif |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 978 | #ifndef finish_arch_switch |
| 979 | # define finish_arch_switch(prev) do { } while (0) |
| 980 | #endif |
| 981 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 982 | static inline int task_current(struct rq *rq, struct task_struct *p) |
| 983 | { |
| 984 | return rq->curr == p; |
| 985 | } |
| 986 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 987 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 988 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 989 | { |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 990 | return task_current(rq, p); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 993 | 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] | 994 | { |
| 995 | } |
| 996 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 997 | 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] | 998 | { |
Ingo Molnar | da04c03 | 2005-09-13 11:17:59 +0200 | [diff] [blame] | 999 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 1000 | /* this is a valid case when another task releases the spinlock */ |
| 1001 | rq->lock.owner = current; |
| 1002 | #endif |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 1003 | /* |
| 1004 | * If we are tracking spinlock dependencies then we have to |
| 1005 | * fix up the runqueue lock - which gets 'carried over' from |
| 1006 | * prev into current: |
| 1007 | */ |
| 1008 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); |
| 1009 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1010 | spin_unlock_irq(&rq->lock); |
| 1011 | } |
| 1012 | |
| 1013 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1014 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1015 | { |
| 1016 | #ifdef CONFIG_SMP |
| 1017 | return p->oncpu; |
| 1018 | #else |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 1019 | return task_current(rq, p); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1020 | #endif |
| 1021 | } |
| 1022 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1023 | 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] | 1024 | { |
| 1025 | #ifdef CONFIG_SMP |
| 1026 | /* |
| 1027 | * We can optimise this out completely for !SMP, because the |
| 1028 | * SMP rebalancing from interrupt is the only thing that cares |
| 1029 | * here. |
| 1030 | */ |
| 1031 | next->oncpu = 1; |
| 1032 | #endif |
| 1033 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 1034 | spin_unlock_irq(&rq->lock); |
| 1035 | #else |
| 1036 | spin_unlock(&rq->lock); |
| 1037 | #endif |
| 1038 | } |
| 1039 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1040 | 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] | 1041 | { |
| 1042 | #ifdef CONFIG_SMP |
| 1043 | /* |
| 1044 | * After ->oncpu is cleared, the task can be moved to a different CPU. |
| 1045 | * We must ensure this doesn't happen until the switch is completely |
| 1046 | * finished. |
| 1047 | */ |
| 1048 | smp_wmb(); |
| 1049 | prev->oncpu = 0; |
| 1050 | #endif |
| 1051 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 1052 | local_irq_enable(); |
| 1053 | #endif |
| 1054 | } |
| 1055 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1058 | * __task_rq_lock - lock the runqueue a given task resides on. |
| 1059 | * Must be called interrupts disabled. |
| 1060 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1061 | static inline struct rq *__task_rq_lock(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1062 | __acquires(rq->lock) |
| 1063 | { |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1064 | for (;;) { |
| 1065 | struct rq *rq = task_rq(p); |
| 1066 | spin_lock(&rq->lock); |
| 1067 | if (likely(rq == task_rq(p))) |
| 1068 | return rq; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1069 | spin_unlock(&rq->lock); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1070 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | * 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] | 1075 | * interrupts. Note the ordering: we can safely lookup the task_rq without |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | * explicitly disabling preemption. |
| 1077 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1078 | 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] | 1079 | __acquires(rq->lock) |
| 1080 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1081 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1083 | for (;;) { |
| 1084 | local_irq_save(*flags); |
| 1085 | rq = task_rq(p); |
| 1086 | spin_lock(&rq->lock); |
| 1087 | if (likely(rq == task_rq(p))) |
| 1088 | return rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | spin_unlock_irqrestore(&rq->lock, *flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1093 | static void __task_rq_unlock(struct rq *rq) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1094 | __releases(rq->lock) |
| 1095 | { |
| 1096 | spin_unlock(&rq->lock); |
| 1097 | } |
| 1098 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1099 | static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | __releases(rq->lock) |
| 1101 | { |
| 1102 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 1103 | } |
| 1104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | /* |
Robert P. J. Day | cc2a73b | 2006-12-10 02:20:00 -0800 | [diff] [blame] | 1106 | * this_rq_lock - lock this runqueue and disable interrupts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1108 | static struct rq *this_rq_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | __acquires(rq->lock) |
| 1110 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1111 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
| 1113 | local_irq_disable(); |
| 1114 | rq = this_rq(); |
| 1115 | spin_lock(&rq->lock); |
| 1116 | |
| 1117 | return rq; |
| 1118 | } |
| 1119 | |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1120 | /* |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1121 | * We are going deep-idle (irqs are disabled): |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1122 | */ |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1123 | void sched_clock_idle_sleep_event(void) |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1124 | { |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1125 | struct rq *rq = cpu_rq(smp_processor_id()); |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1126 | |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1127 | spin_lock(&rq->lock); |
| 1128 | __update_rq_clock(rq); |
| 1129 | spin_unlock(&rq->lock); |
| 1130 | rq->clock_deep_idle_events++; |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1131 | } |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1132 | EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event); |
| 1133 | |
| 1134 | /* |
| 1135 | * We just idled delta nanoseconds (called with irqs disabled): |
| 1136 | */ |
| 1137 | void sched_clock_idle_wakeup_event(u64 delta_ns) |
| 1138 | { |
| 1139 | struct rq *rq = cpu_rq(smp_processor_id()); |
| 1140 | u64 now = sched_clock(); |
| 1141 | |
| 1142 | rq->idle_clock += delta_ns; |
| 1143 | /* |
| 1144 | * Override the previous timestamp and ignore all |
| 1145 | * sched_clock() deltas that occured while we idled, |
| 1146 | * and use the PM-provided delta_ns to advance the |
| 1147 | * rq clock: |
| 1148 | */ |
| 1149 | spin_lock(&rq->lock); |
| 1150 | rq->prev_clock_raw = now; |
| 1151 | rq->clock += delta_ns; |
| 1152 | spin_unlock(&rq->lock); |
Guillaume Chazarain | 782daee | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 1153 | touch_softlockup_watchdog(); |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 1154 | } |
| 1155 | EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event); |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1156 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1157 | static void __resched_task(struct task_struct *p, int tif_bit); |
| 1158 | |
| 1159 | static inline void resched_task(struct task_struct *p) |
| 1160 | { |
| 1161 | __resched_task(p, TIF_NEED_RESCHED); |
| 1162 | } |
| 1163 | |
| 1164 | #ifdef CONFIG_SCHED_HRTICK |
| 1165 | /* |
| 1166 | * Use HR-timers to deliver accurate preemption points. |
| 1167 | * |
| 1168 | * Its all a bit involved since we cannot program an hrt while holding the |
| 1169 | * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a |
| 1170 | * reschedule event. |
| 1171 | * |
| 1172 | * When we get rescheduled we reprogram the hrtick_timer outside of the |
| 1173 | * rq->lock. |
| 1174 | */ |
| 1175 | static inline void resched_hrt(struct task_struct *p) |
| 1176 | { |
| 1177 | __resched_task(p, TIF_HRTICK_RESCHED); |
| 1178 | } |
| 1179 | |
| 1180 | static inline void resched_rq(struct rq *rq) |
| 1181 | { |
| 1182 | unsigned long flags; |
| 1183 | |
| 1184 | spin_lock_irqsave(&rq->lock, flags); |
| 1185 | resched_task(rq->curr); |
| 1186 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1187 | } |
| 1188 | |
| 1189 | enum { |
| 1190 | HRTICK_SET, /* re-programm hrtick_timer */ |
| 1191 | HRTICK_RESET, /* not a new slice */ |
| 1192 | }; |
| 1193 | |
| 1194 | /* |
| 1195 | * Use hrtick when: |
| 1196 | * - enabled by features |
| 1197 | * - hrtimer is actually high res |
| 1198 | */ |
| 1199 | static inline int hrtick_enabled(struct rq *rq) |
| 1200 | { |
| 1201 | if (!sched_feat(HRTICK)) |
| 1202 | return 0; |
| 1203 | return hrtimer_is_hres_active(&rq->hrtick_timer); |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * Called to set the hrtick timer state. |
| 1208 | * |
| 1209 | * called with rq->lock held and irqs disabled |
| 1210 | */ |
| 1211 | static void hrtick_start(struct rq *rq, u64 delay, int reset) |
| 1212 | { |
| 1213 | assert_spin_locked(&rq->lock); |
| 1214 | |
| 1215 | /* |
| 1216 | * preempt at: now + delay |
| 1217 | */ |
| 1218 | rq->hrtick_expire = |
| 1219 | ktime_add_ns(rq->hrtick_timer.base->get_time(), delay); |
| 1220 | /* |
| 1221 | * indicate we need to program the timer |
| 1222 | */ |
| 1223 | __set_bit(HRTICK_SET, &rq->hrtick_flags); |
| 1224 | if (reset) |
| 1225 | __set_bit(HRTICK_RESET, &rq->hrtick_flags); |
| 1226 | |
| 1227 | /* |
| 1228 | * New slices are called from the schedule path and don't need a |
| 1229 | * forced reschedule. |
| 1230 | */ |
| 1231 | if (reset) |
| 1232 | resched_hrt(rq->curr); |
| 1233 | } |
| 1234 | |
| 1235 | static void hrtick_clear(struct rq *rq) |
| 1236 | { |
| 1237 | if (hrtimer_active(&rq->hrtick_timer)) |
| 1238 | hrtimer_cancel(&rq->hrtick_timer); |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * Update the timer from the possible pending state. |
| 1243 | */ |
| 1244 | static void hrtick_set(struct rq *rq) |
| 1245 | { |
| 1246 | ktime_t time; |
| 1247 | int set, reset; |
| 1248 | unsigned long flags; |
| 1249 | |
| 1250 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 1251 | |
| 1252 | spin_lock_irqsave(&rq->lock, flags); |
| 1253 | set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags); |
| 1254 | reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags); |
| 1255 | time = rq->hrtick_expire; |
| 1256 | clear_thread_flag(TIF_HRTICK_RESCHED); |
| 1257 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1258 | |
| 1259 | if (set) { |
| 1260 | hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS); |
| 1261 | if (reset && !hrtimer_active(&rq->hrtick_timer)) |
| 1262 | resched_rq(rq); |
| 1263 | } else |
| 1264 | hrtick_clear(rq); |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * High-resolution timer tick. |
| 1269 | * Runs from hardirq context with interrupts disabled. |
| 1270 | */ |
| 1271 | static enum hrtimer_restart hrtick(struct hrtimer *timer) |
| 1272 | { |
| 1273 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); |
| 1274 | |
| 1275 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 1276 | |
| 1277 | spin_lock(&rq->lock); |
| 1278 | __update_rq_clock(rq); |
| 1279 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
| 1280 | spin_unlock(&rq->lock); |
| 1281 | |
| 1282 | return HRTIMER_NORESTART; |
| 1283 | } |
| 1284 | |
| 1285 | static inline void init_rq_hrtick(struct rq *rq) |
| 1286 | { |
| 1287 | rq->hrtick_flags = 0; |
| 1288 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1289 | rq->hrtick_timer.function = hrtick; |
| 1290 | rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; |
| 1291 | } |
| 1292 | |
| 1293 | void hrtick_resched(void) |
| 1294 | { |
| 1295 | struct rq *rq; |
| 1296 | unsigned long flags; |
| 1297 | |
| 1298 | if (!test_thread_flag(TIF_HRTICK_RESCHED)) |
| 1299 | return; |
| 1300 | |
| 1301 | local_irq_save(flags); |
| 1302 | rq = cpu_rq(smp_processor_id()); |
| 1303 | hrtick_set(rq); |
| 1304 | local_irq_restore(flags); |
| 1305 | } |
| 1306 | #else |
| 1307 | static inline void hrtick_clear(struct rq *rq) |
| 1308 | { |
| 1309 | } |
| 1310 | |
| 1311 | static inline void hrtick_set(struct rq *rq) |
| 1312 | { |
| 1313 | } |
| 1314 | |
| 1315 | static inline void init_rq_hrtick(struct rq *rq) |
| 1316 | { |
| 1317 | } |
| 1318 | |
| 1319 | void hrtick_resched(void) |
| 1320 | { |
| 1321 | } |
| 1322 | #endif |
| 1323 | |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1324 | /* |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1325 | * resched_task - mark a task 'to be rescheduled now'. |
| 1326 | * |
| 1327 | * On UP this means the setting of the need_resched flag, on SMP it |
| 1328 | * might also involve a cross-CPU call to trigger the scheduler on |
| 1329 | * the target CPU. |
| 1330 | */ |
| 1331 | #ifdef CONFIG_SMP |
| 1332 | |
| 1333 | #ifndef tsk_is_polling |
| 1334 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) |
| 1335 | #endif |
| 1336 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1337 | static void __resched_task(struct task_struct *p, int tif_bit) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1338 | { |
| 1339 | int cpu; |
| 1340 | |
| 1341 | assert_spin_locked(&task_rq(p)->lock); |
| 1342 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1343 | if (unlikely(test_tsk_thread_flag(p, tif_bit))) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1344 | return; |
| 1345 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1346 | set_tsk_thread_flag(p, tif_bit); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1347 | |
| 1348 | cpu = task_cpu(p); |
| 1349 | if (cpu == smp_processor_id()) |
| 1350 | return; |
| 1351 | |
| 1352 | /* NEED_RESCHED must be visible before we test polling */ |
| 1353 | smp_mb(); |
| 1354 | if (!tsk_is_polling(p)) |
| 1355 | smp_send_reschedule(cpu); |
| 1356 | } |
| 1357 | |
| 1358 | static void resched_cpu(int cpu) |
| 1359 | { |
| 1360 | struct rq *rq = cpu_rq(cpu); |
| 1361 | unsigned long flags; |
| 1362 | |
| 1363 | if (!spin_trylock_irqsave(&rq->lock, flags)) |
| 1364 | return; |
| 1365 | resched_task(cpu_curr(cpu)); |
| 1366 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1367 | } |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 1368 | |
| 1369 | #ifdef CONFIG_NO_HZ |
| 1370 | /* |
| 1371 | * When add_timer_on() enqueues a timer into the timer wheel of an |
| 1372 | * idle CPU then this timer might expire before the next timer event |
| 1373 | * which is scheduled to wake up that CPU. In case of a completely |
| 1374 | * idle system the next event might even be infinite time into the |
| 1375 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and |
| 1376 | * leaves the inner idle loop so the newly added timer is taken into |
| 1377 | * account when the CPU goes back to idle and evaluates the timer |
| 1378 | * wheel for the next timer event. |
| 1379 | */ |
| 1380 | void wake_up_idle_cpu(int cpu) |
| 1381 | { |
| 1382 | struct rq *rq = cpu_rq(cpu); |
| 1383 | |
| 1384 | if (cpu == smp_processor_id()) |
| 1385 | return; |
| 1386 | |
| 1387 | /* |
| 1388 | * This is safe, as this function is called with the timer |
| 1389 | * wheel base lock of (cpu) held. When the CPU is on the way |
| 1390 | * to idle and has not yet set rq->curr to idle then it will |
| 1391 | * be serialized on the timer wheel base lock and take the new |
| 1392 | * timer into account automatically. |
| 1393 | */ |
| 1394 | if (rq->curr != rq->idle) |
| 1395 | return; |
| 1396 | |
| 1397 | /* |
| 1398 | * We can set TIF_RESCHED on the idle task of the other CPU |
| 1399 | * lockless. The worst case is that the other CPU runs the |
| 1400 | * idle task through an additional NOOP schedule() |
| 1401 | */ |
| 1402 | set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED); |
| 1403 | |
| 1404 | /* NEED_RESCHED must be visible before we test polling */ |
| 1405 | smp_mb(); |
| 1406 | if (!tsk_is_polling(rq->idle)) |
| 1407 | smp_send_reschedule(cpu); |
| 1408 | } |
| 1409 | #endif |
| 1410 | |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1411 | #else |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1412 | static void __resched_task(struct task_struct *p, int tif_bit) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1413 | { |
| 1414 | assert_spin_locked(&task_rq(p)->lock); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1415 | set_tsk_thread_flag(p, tif_bit); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1416 | } |
| 1417 | #endif |
| 1418 | |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1419 | #if BITS_PER_LONG == 32 |
| 1420 | # define WMULT_CONST (~0UL) |
| 1421 | #else |
| 1422 | # define WMULT_CONST (1UL << 32) |
| 1423 | #endif |
| 1424 | |
| 1425 | #define WMULT_SHIFT 32 |
| 1426 | |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1427 | /* |
| 1428 | * Shift right and round: |
| 1429 | */ |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1430 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1431 | |
Peter Zijlstra | 8f1bc38 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1432 | /* |
| 1433 | * delta *= weight / lw |
| 1434 | */ |
Ingo Molnar | cb1c4fc | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1435 | static unsigned long |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1436 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, |
| 1437 | struct load_weight *lw) |
| 1438 | { |
| 1439 | u64 tmp; |
| 1440 | |
| 1441 | if (unlikely(!lw->inv_weight)) |
Ingo Molnar | 27d1172 | 2008-03-14 22:20:01 +0100 | [diff] [blame] | 1442 | lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1443 | |
| 1444 | tmp = (u64)delta_exec * weight; |
| 1445 | /* |
| 1446 | * Check whether we'd overflow the 64-bit multiplication: |
| 1447 | */ |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1448 | if (unlikely(tmp > WMULT_CONST)) |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1449 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1450 | WMULT_SHIFT/2); |
| 1451 | else |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1452 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1453 | |
Ingo Molnar | ecf691d | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1454 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
Ingo Molnar | 1091985 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1457 | 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] | 1458 | { |
| 1459 | lw->weight += inc; |
Ingo Molnar | e89996a | 2008-03-14 23:48:28 +0100 | [diff] [blame] | 1460 | lw->inv_weight = 0; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
Ingo Molnar | 1091985 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1463 | 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] | 1464 | { |
| 1465 | lw->weight -= dec; |
Ingo Molnar | e89996a | 2008-03-14 23:48:28 +0100 | [diff] [blame] | 1466 | lw->inv_weight = 0; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1467 | } |
| 1468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1470 | * To aid in avoiding the subversion of "niceness" due to uneven distribution |
| 1471 | * of tasks with abnormal "nice" values across CPUs the contribution that |
| 1472 | * 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] | 1473 | * 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] | 1474 | * scaled version of the new time slice allocation that they receive on time |
| 1475 | * slice expiry etc. |
| 1476 | */ |
| 1477 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1478 | #define WEIGHT_IDLEPRIO 2 |
| 1479 | #define WMULT_IDLEPRIO (1 << 31) |
| 1480 | |
| 1481 | /* |
| 1482 | * Nice levels are multiplicative, with a gentle 10% change for every |
| 1483 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to |
| 1484 | * nice 1, it will get ~10% less CPU time than another CPU-bound task |
| 1485 | * that remained on nice 0. |
| 1486 | * |
| 1487 | * The "10% effect" is relative and cumulative: from _any_ nice level, |
| 1488 | * 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] | 1489 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. |
| 1490 | * If a task goes up by ~10% and another task goes down by ~10% then |
| 1491 | * the relative distance between them is ~25%.) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1492 | */ |
| 1493 | static const int prio_to_weight[40] = { |
Ingo Molnar | 254753d | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1494 | /* -20 */ 88761, 71755, 56483, 46273, 36291, |
| 1495 | /* -15 */ 29154, 23254, 18705, 14949, 11916, |
| 1496 | /* -10 */ 9548, 7620, 6100, 4904, 3906, |
| 1497 | /* -5 */ 3121, 2501, 1991, 1586, 1277, |
| 1498 | /* 0 */ 1024, 820, 655, 526, 423, |
| 1499 | /* 5 */ 335, 272, 215, 172, 137, |
| 1500 | /* 10 */ 110, 87, 70, 56, 45, |
| 1501 | /* 15 */ 36, 29, 23, 18, 15, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1502 | }; |
| 1503 | |
Ingo Molnar | 5714d2d | 2007-07-16 09:46:31 +0200 | [diff] [blame] | 1504 | /* |
| 1505 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. |
| 1506 | * |
| 1507 | * In cases where the weight does not change often, we can use the |
| 1508 | * precalculated inverse to speed up arithmetics by turning divisions |
| 1509 | * into multiplications: |
| 1510 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1511 | static const u32 prio_to_wmult[40] = { |
Ingo Molnar | 254753d | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1512 | /* -20 */ 48388, 59856, 76040, 92818, 118348, |
| 1513 | /* -15 */ 147320, 184698, 229616, 287308, 360437, |
| 1514 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, |
| 1515 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, |
| 1516 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, |
| 1517 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, |
| 1518 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, |
| 1519 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1520 | }; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1521 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1522 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup); |
| 1523 | |
| 1524 | /* |
| 1525 | * runqueue iterator, to support SMP load-balancing between different |
| 1526 | * scheduling classes, without having to expose their internal data |
| 1527 | * structures to the load-balancing proper: |
| 1528 | */ |
| 1529 | struct rq_iterator { |
| 1530 | void *arg; |
| 1531 | struct task_struct *(*start)(void *); |
| 1532 | struct task_struct *(*next)(void *); |
| 1533 | }; |
| 1534 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1535 | #ifdef CONFIG_SMP |
| 1536 | static unsigned long |
| 1537 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1538 | unsigned long max_load_move, struct sched_domain *sd, |
| 1539 | enum cpu_idle_type idle, int *all_pinned, |
| 1540 | int *this_best_prio, struct rq_iterator *iterator); |
| 1541 | |
| 1542 | static int |
| 1543 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1544 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 1545 | struct rq_iterator *iterator); |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1546 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1547 | |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 1548 | #ifdef CONFIG_CGROUP_CPUACCT |
| 1549 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime); |
| 1550 | #else |
| 1551 | static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} |
| 1552 | #endif |
| 1553 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1554 | static inline void inc_cpu_load(struct rq *rq, unsigned long load) |
| 1555 | { |
| 1556 | update_load_add(&rq->load, load); |
| 1557 | } |
| 1558 | |
| 1559 | static inline void dec_cpu_load(struct rq *rq, unsigned long load) |
| 1560 | { |
| 1561 | update_load_sub(&rq->load, load); |
| 1562 | } |
| 1563 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1564 | #ifdef CONFIG_SMP |
| 1565 | static unsigned long source_load(int cpu, int type); |
| 1566 | static unsigned long target_load(int cpu, int type); |
| 1567 | static unsigned long cpu_avg_load_per_task(int cpu); |
| 1568 | 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] | 1569 | |
| 1570 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 1571 | |
| 1572 | /* |
| 1573 | * Group load balancing. |
| 1574 | * |
| 1575 | * We calculate a few balance domain wide aggregate numbers; load and weight. |
| 1576 | * Given the pictures below, and assuming each item has equal weight: |
| 1577 | * |
| 1578 | * root 1 - thread |
| 1579 | * / | \ A - group |
| 1580 | * A 1 B |
| 1581 | * /|\ / \ |
| 1582 | * C 2 D 3 4 |
| 1583 | * | | |
| 1584 | * 5 6 |
| 1585 | * |
| 1586 | * load: |
| 1587 | * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd, |
| 1588 | * which equals 1/9-th of the total load. |
| 1589 | * |
| 1590 | * shares: |
| 1591 | * The weight of this group on the selected cpus. |
| 1592 | * |
| 1593 | * rq_weight: |
| 1594 | * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while |
| 1595 | * B would get 2. |
| 1596 | * |
| 1597 | * task_weight: |
| 1598 | * Part of the rq_weight contributed by tasks; all groups except B would |
| 1599 | * get 1, B gets 2. |
| 1600 | */ |
| 1601 | |
| 1602 | static inline struct aggregate_struct * |
| 1603 | aggregate(struct task_group *tg, struct sched_domain *sd) |
| 1604 | { |
| 1605 | return &tg->cfs_rq[sd->first_cpu]->aggregate; |
| 1606 | } |
| 1607 | |
| 1608 | typedef void (*aggregate_func)(struct task_group *, struct sched_domain *); |
| 1609 | |
| 1610 | /* |
| 1611 | * Iterate the full tree, calling @down when first entering a node and @up when |
| 1612 | * leaving it for the final time. |
| 1613 | */ |
| 1614 | static |
| 1615 | void aggregate_walk_tree(aggregate_func down, aggregate_func up, |
| 1616 | struct sched_domain *sd) |
| 1617 | { |
| 1618 | struct task_group *parent, *child; |
| 1619 | |
| 1620 | rcu_read_lock(); |
| 1621 | parent = &root_task_group; |
| 1622 | down: |
| 1623 | (*down)(parent, sd); |
| 1624 | list_for_each_entry_rcu(child, &parent->children, siblings) { |
| 1625 | parent = child; |
| 1626 | goto down; |
| 1627 | |
| 1628 | up: |
| 1629 | continue; |
| 1630 | } |
| 1631 | (*up)(parent, sd); |
| 1632 | |
| 1633 | child = parent; |
| 1634 | parent = parent->parent; |
| 1635 | if (parent) |
| 1636 | goto up; |
| 1637 | rcu_read_unlock(); |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Calculate the aggregate runqueue weight. |
| 1642 | */ |
| 1643 | static |
| 1644 | void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd) |
| 1645 | { |
| 1646 | unsigned long rq_weight = 0; |
| 1647 | unsigned long task_weight = 0; |
| 1648 | int i; |
| 1649 | |
| 1650 | for_each_cpu_mask(i, sd->span) { |
| 1651 | rq_weight += tg->cfs_rq[i]->load.weight; |
| 1652 | task_weight += tg->cfs_rq[i]->task_weight; |
| 1653 | } |
| 1654 | |
| 1655 | aggregate(tg, sd)->rq_weight = rq_weight; |
| 1656 | aggregate(tg, sd)->task_weight = task_weight; |
| 1657 | } |
| 1658 | |
| 1659 | /* |
| 1660 | * Redistribute tg->shares amongst all tg->cfs_rq[]s. |
| 1661 | */ |
| 1662 | static void __aggregate_redistribute_shares(struct task_group *tg) |
| 1663 | { |
| 1664 | int i, max_cpu = smp_processor_id(); |
| 1665 | unsigned long rq_weight = 0; |
| 1666 | unsigned long shares, max_shares = 0, shares_rem = tg->shares; |
| 1667 | |
| 1668 | for_each_possible_cpu(i) |
| 1669 | rq_weight += tg->cfs_rq[i]->load.weight; |
| 1670 | |
| 1671 | for_each_possible_cpu(i) { |
| 1672 | /* |
| 1673 | * divide shares proportional to the rq_weights. |
| 1674 | */ |
| 1675 | shares = tg->shares * tg->cfs_rq[i]->load.weight; |
| 1676 | shares /= rq_weight + 1; |
| 1677 | |
| 1678 | tg->cfs_rq[i]->shares = shares; |
| 1679 | |
| 1680 | if (shares > max_shares) { |
| 1681 | max_shares = shares; |
| 1682 | max_cpu = i; |
| 1683 | } |
| 1684 | shares_rem -= shares; |
| 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * Ensure it all adds up to tg->shares; we can loose a few |
| 1689 | * due to rounding down when computing the per-cpu shares. |
| 1690 | */ |
| 1691 | if (shares_rem) |
| 1692 | tg->cfs_rq[max_cpu]->shares += shares_rem; |
| 1693 | } |
| 1694 | |
| 1695 | /* |
| 1696 | * Compute the weight of this group on the given cpus. |
| 1697 | */ |
| 1698 | static |
| 1699 | void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd) |
| 1700 | { |
| 1701 | unsigned long shares = 0; |
| 1702 | int i; |
| 1703 | |
| 1704 | again: |
| 1705 | for_each_cpu_mask(i, sd->span) |
| 1706 | shares += tg->cfs_rq[i]->shares; |
| 1707 | |
| 1708 | /* |
| 1709 | * When the span doesn't have any shares assigned, but does have |
| 1710 | * tasks to run do a machine wide rebalance (should be rare). |
| 1711 | */ |
| 1712 | if (unlikely(!shares && aggregate(tg, sd)->rq_weight)) { |
| 1713 | __aggregate_redistribute_shares(tg); |
| 1714 | goto again; |
| 1715 | } |
| 1716 | |
| 1717 | aggregate(tg, sd)->shares = shares; |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * Compute the load fraction assigned to this group, relies on the aggregate |
| 1722 | * weight and this group's parent's load, i.e. top-down. |
| 1723 | */ |
| 1724 | static |
| 1725 | void aggregate_group_load(struct task_group *tg, struct sched_domain *sd) |
| 1726 | { |
| 1727 | unsigned long load; |
| 1728 | |
| 1729 | if (!tg->parent) { |
| 1730 | int i; |
| 1731 | |
| 1732 | load = 0; |
| 1733 | for_each_cpu_mask(i, sd->span) |
| 1734 | load += cpu_rq(i)->load.weight; |
| 1735 | |
| 1736 | } else { |
| 1737 | load = aggregate(tg->parent, sd)->load; |
| 1738 | |
| 1739 | /* |
| 1740 | * shares is our weight in the parent's rq so |
| 1741 | * shares/parent->rq_weight gives our fraction of the load |
| 1742 | */ |
| 1743 | load *= aggregate(tg, sd)->shares; |
| 1744 | load /= aggregate(tg->parent, sd)->rq_weight + 1; |
| 1745 | } |
| 1746 | |
| 1747 | aggregate(tg, sd)->load = load; |
| 1748 | } |
| 1749 | |
| 1750 | static void __set_se_shares(struct sched_entity *se, unsigned long shares); |
| 1751 | |
| 1752 | /* |
| 1753 | * Calculate and set the cpu's group shares. |
| 1754 | */ |
| 1755 | static void |
| 1756 | __update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd, |
| 1757 | int tcpu) |
| 1758 | { |
| 1759 | int boost = 0; |
| 1760 | unsigned long shares; |
| 1761 | unsigned long rq_weight; |
| 1762 | |
| 1763 | if (!tg->se[tcpu]) |
| 1764 | return; |
| 1765 | |
| 1766 | rq_weight = tg->cfs_rq[tcpu]->load.weight; |
| 1767 | |
| 1768 | /* |
| 1769 | * If there are currently no tasks on the cpu pretend there is one of |
| 1770 | * average load so that when a new task gets to run here it will not |
| 1771 | * get delayed by group starvation. |
| 1772 | */ |
| 1773 | if (!rq_weight) { |
| 1774 | boost = 1; |
| 1775 | rq_weight = NICE_0_LOAD; |
| 1776 | } |
| 1777 | |
| 1778 | /* |
| 1779 | * \Sum shares * rq_weight |
| 1780 | * shares = ----------------------- |
| 1781 | * \Sum rq_weight |
| 1782 | * |
| 1783 | */ |
| 1784 | shares = aggregate(tg, sd)->shares * rq_weight; |
| 1785 | shares /= aggregate(tg, sd)->rq_weight + 1; |
| 1786 | |
| 1787 | /* |
| 1788 | * record the actual number of shares, not the boosted amount. |
| 1789 | */ |
| 1790 | tg->cfs_rq[tcpu]->shares = boost ? 0 : shares; |
| 1791 | |
| 1792 | if (shares < MIN_SHARES) |
| 1793 | shares = MIN_SHARES; |
| 1794 | |
| 1795 | __set_se_shares(tg->se[tcpu], shares); |
| 1796 | } |
| 1797 | |
| 1798 | /* |
| 1799 | * Re-adjust the weights on the cpu the task came from and on the cpu the |
| 1800 | * task went to. |
| 1801 | */ |
| 1802 | static void |
| 1803 | __move_group_shares(struct task_group *tg, struct sched_domain *sd, |
| 1804 | int scpu, int dcpu) |
| 1805 | { |
| 1806 | unsigned long shares; |
| 1807 | |
| 1808 | shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares; |
| 1809 | |
| 1810 | __update_group_shares_cpu(tg, sd, scpu); |
| 1811 | __update_group_shares_cpu(tg, sd, dcpu); |
| 1812 | |
| 1813 | /* |
| 1814 | * ensure we never loose shares due to rounding errors in the |
| 1815 | * above redistribution. |
| 1816 | */ |
| 1817 | shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares; |
| 1818 | if (shares) |
| 1819 | tg->cfs_rq[dcpu]->shares += shares; |
| 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * Because changing a group's shares changes the weight of the super-group |
| 1824 | * we need to walk up the tree and change all shares until we hit the root. |
| 1825 | */ |
| 1826 | static void |
| 1827 | move_group_shares(struct task_group *tg, struct sched_domain *sd, |
| 1828 | int scpu, int dcpu) |
| 1829 | { |
| 1830 | while (tg) { |
| 1831 | __move_group_shares(tg, sd, scpu, dcpu); |
| 1832 | tg = tg->parent; |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | static |
| 1837 | void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd) |
| 1838 | { |
| 1839 | unsigned long shares = aggregate(tg, sd)->shares; |
| 1840 | int i; |
| 1841 | |
| 1842 | for_each_cpu_mask(i, sd->span) { |
| 1843 | struct rq *rq = cpu_rq(i); |
| 1844 | unsigned long flags; |
| 1845 | |
| 1846 | spin_lock_irqsave(&rq->lock, flags); |
| 1847 | __update_group_shares_cpu(tg, sd, i); |
| 1848 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1849 | } |
| 1850 | |
| 1851 | aggregate_group_shares(tg, sd); |
| 1852 | |
| 1853 | /* |
| 1854 | * ensure we never loose shares due to rounding errors in the |
| 1855 | * above redistribution. |
| 1856 | */ |
| 1857 | shares -= aggregate(tg, sd)->shares; |
| 1858 | if (shares) { |
| 1859 | tg->cfs_rq[sd->first_cpu]->shares += shares; |
| 1860 | aggregate(tg, sd)->shares += shares; |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | /* |
| 1865 | * Calculate the accumulative weight and recursive load of each task group |
| 1866 | * while walking down the tree. |
| 1867 | */ |
| 1868 | static |
| 1869 | void aggregate_get_down(struct task_group *tg, struct sched_domain *sd) |
| 1870 | { |
| 1871 | aggregate_group_weight(tg, sd); |
| 1872 | aggregate_group_shares(tg, sd); |
| 1873 | aggregate_group_load(tg, sd); |
| 1874 | } |
| 1875 | |
| 1876 | /* |
| 1877 | * Rebalance the cpu shares while walking back up the tree. |
| 1878 | */ |
| 1879 | static |
| 1880 | void aggregate_get_up(struct task_group *tg, struct sched_domain *sd) |
| 1881 | { |
| 1882 | aggregate_group_set_shares(tg, sd); |
| 1883 | } |
| 1884 | |
| 1885 | static DEFINE_PER_CPU(spinlock_t, aggregate_lock); |
| 1886 | |
| 1887 | static void __init init_aggregate(void) |
| 1888 | { |
| 1889 | int i; |
| 1890 | |
| 1891 | for_each_possible_cpu(i) |
| 1892 | spin_lock_init(&per_cpu(aggregate_lock, i)); |
| 1893 | } |
| 1894 | |
| 1895 | static int get_aggregate(struct sched_domain *sd) |
| 1896 | { |
| 1897 | if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu))) |
| 1898 | return 0; |
| 1899 | |
| 1900 | aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd); |
| 1901 | return 1; |
| 1902 | } |
| 1903 | |
| 1904 | static void put_aggregate(struct sched_domain *sd) |
| 1905 | { |
| 1906 | spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu)); |
| 1907 | } |
| 1908 | |
| 1909 | static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares) |
| 1910 | { |
| 1911 | cfs_rq->shares = shares; |
| 1912 | } |
| 1913 | |
| 1914 | #else |
| 1915 | |
| 1916 | static inline void init_aggregate(void) |
| 1917 | { |
| 1918 | } |
| 1919 | |
| 1920 | static inline int get_aggregate(struct sched_domain *sd) |
| 1921 | { |
| 1922 | return 0; |
| 1923 | } |
| 1924 | |
| 1925 | static inline void put_aggregate(struct sched_domain *sd) |
| 1926 | { |
| 1927 | } |
| 1928 | #endif |
| 1929 | |
| 1930 | #else /* CONFIG_SMP */ |
| 1931 | |
| 1932 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 1933 | static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares) |
| 1934 | { |
| 1935 | } |
| 1936 | #endif |
| 1937 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1938 | #endif /* CONFIG_SMP */ |
| 1939 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1940 | #include "sched_stats.h" |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1941 | #include "sched_idletask.c" |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1942 | #include "sched_fair.c" |
| 1943 | #include "sched_rt.c" |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1944 | #ifdef CONFIG_SCHED_DEBUG |
| 1945 | # include "sched_debug.c" |
| 1946 | #endif |
| 1947 | |
| 1948 | #define sched_class_highest (&rt_sched_class) |
| 1949 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1950 | static void inc_nr_running(struct rq *rq) |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1951 | { |
| 1952 | rq->nr_running++; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1953 | } |
| 1954 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1955 | static void dec_nr_running(struct rq *rq) |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1956 | { |
| 1957 | rq->nr_running--; |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1958 | } |
| 1959 | |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1960 | static void set_load_weight(struct task_struct *p) |
| 1961 | { |
| 1962 | if (task_has_rt_policy(p)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1963 | p->se.load.weight = prio_to_weight[0] * 2; |
| 1964 | p->se.load.inv_weight = prio_to_wmult[0] >> 1; |
| 1965 | return; |
| 1966 | } |
| 1967 | |
| 1968 | /* |
| 1969 | * SCHED_IDLE tasks get minimal weight: |
| 1970 | */ |
| 1971 | if (p->policy == SCHED_IDLE) { |
| 1972 | p->se.load.weight = WEIGHT_IDLEPRIO; |
| 1973 | p->se.load.inv_weight = WMULT_IDLEPRIO; |
| 1974 | return; |
| 1975 | } |
| 1976 | |
| 1977 | p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO]; |
| 1978 | 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] | 1979 | } |
| 1980 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1981 | 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] | 1982 | { |
| 1983 | sched_info_queued(p); |
Ingo Molnar | fd390f6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1984 | p->sched_class->enqueue_task(rq, p, wakeup); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1985 | p->se.on_rq = 1; |
| 1986 | } |
| 1987 | |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1988 | 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] | 1989 | { |
Ingo Molnar | f02231e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1990 | p->sched_class->dequeue_task(rq, p, sleep); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1991 | p->se.on_rq = 0; |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1995 | * __normal_prio - return the priority that is based on the static prio |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1996 | */ |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1997 | static inline int __normal_prio(struct task_struct *p) |
| 1998 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1999 | return p->static_prio; |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2003 | * Calculate the expected normal priority: i.e. priority |
| 2004 | * without taking RT-inheritance into account. Might be |
| 2005 | * boosted by interactivity modifiers. Changes upon fork, |
| 2006 | * setprio syscalls, and whenever the interactivity |
| 2007 | * estimator recalculates. |
| 2008 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2009 | static inline int normal_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2010 | { |
| 2011 | int prio; |
| 2012 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2013 | if (task_has_rt_policy(p)) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2014 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
| 2015 | else |
| 2016 | prio = __normal_prio(p); |
| 2017 | return prio; |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * Calculate the current priority, i.e. the priority |
| 2022 | * taken into account by the scheduler. This value might |
| 2023 | * be boosted by RT tasks, or might be boosted by |
| 2024 | * interactivity modifiers. Will be RT if the task got |
| 2025 | * RT-boosted. If not then it returns p->normal_prio. |
| 2026 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2027 | static int effective_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2028 | { |
| 2029 | p->normal_prio = normal_prio(p); |
| 2030 | /* |
| 2031 | * If we are RT tasks or we were boosted to RT priority, |
| 2032 | * keep the priority unchanged. Otherwise, update priority |
| 2033 | * to the normal priority: |
| 2034 | */ |
| 2035 | if (!rt_prio(p->prio)) |
| 2036 | return p->normal_prio; |
| 2037 | return p->prio; |
| 2038 | } |
| 2039 | |
| 2040 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2041 | * activate_task - move a task to the runqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2043 | 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] | 2044 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 2045 | if (task_contributes_to_load(p)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2046 | rq->nr_uninterruptible--; |
| 2047 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2048 | enqueue_task(rq, p, wakeup); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 2049 | inc_nr_running(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | * deactivate_task - remove a task from the runqueue. |
| 2054 | */ |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2055 | 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] | 2056 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 2057 | if (task_contributes_to_load(p)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2058 | rq->nr_uninterruptible++; |
| 2059 | |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2060 | dequeue_task(rq, p, sleep); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 2061 | dec_nr_running(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | /** |
| 2065 | * task_curr - is this task currently executing on a CPU? |
| 2066 | * @p: the task in question. |
| 2067 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2068 | inline int task_curr(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | { |
| 2070 | return cpu_curr(task_cpu(p)) == p; |
| 2071 | } |
| 2072 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2073 | /* Used instead of source_load when we know the type == 0 */ |
| 2074 | unsigned long weighted_cpuload(const int cpu) |
| 2075 | { |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 2076 | return cpu_rq(cpu)->load.weight; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) |
| 2080 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 2081 | set_task_rq(p, cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2082 | #ifdef CONFIG_SMP |
Dmitry Adamushko | ce96b5a | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 2083 | /* |
| 2084 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be |
| 2085 | * successfuly executed on another CPU. We must ensure that updates of |
| 2086 | * per-task data have been completed by this moment. |
| 2087 | */ |
| 2088 | smp_wmb(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2089 | task_thread_info(p)->cpu = cpu; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2090 | #endif |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2093 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
| 2094 | const struct sched_class *prev_class, |
| 2095 | int oldprio, int running) |
| 2096 | { |
| 2097 | if (prev_class != p->sched_class) { |
| 2098 | if (prev_class->switched_from) |
| 2099 | prev_class->switched_from(rq, p, running); |
| 2100 | p->sched_class->switched_to(rq, p, running); |
| 2101 | } else |
| 2102 | p->sched_class->prio_changed(rq, p, oldprio, running); |
| 2103 | } |
| 2104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | #ifdef CONFIG_SMP |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2106 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2107 | /* |
| 2108 | * Is this task likely cache-hot: |
| 2109 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2110 | static int |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2111 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) |
| 2112 | { |
| 2113 | s64 delta; |
| 2114 | |
Ingo Molnar | f540a60 | 2008-03-15 17:10:34 +0100 | [diff] [blame] | 2115 | /* |
| 2116 | * Buddy candidates are cache hot: |
| 2117 | */ |
Ingo Molnar | d25ce4c | 2008-03-17 09:36:53 +0100 | [diff] [blame] | 2118 | 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] | 2119 | return 1; |
| 2120 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2121 | if (p->sched_class != &fair_sched_class) |
| 2122 | return 0; |
| 2123 | |
Ingo Molnar | 6bc1665 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2124 | if (sysctl_sched_migration_cost == -1) |
| 2125 | return 1; |
| 2126 | if (sysctl_sched_migration_cost == 0) |
| 2127 | return 0; |
| 2128 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2129 | delta = now - p->se.exec_start; |
| 2130 | |
| 2131 | return delta < (s64)sysctl_sched_migration_cost; |
| 2132 | } |
| 2133 | |
| 2134 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2135 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2136 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2137 | int old_cpu = task_cpu(p); |
| 2138 | 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] | 2139 | struct cfs_rq *old_cfsrq = task_cfs_rq(p), |
| 2140 | *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu); |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 2141 | u64 clock_offset; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2142 | |
| 2143 | clock_offset = old_rq->clock - new_rq->clock; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2144 | |
| 2145 | #ifdef CONFIG_SCHEDSTATS |
| 2146 | if (p->se.wait_start) |
| 2147 | p->se.wait_start -= clock_offset; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2148 | if (p->se.sleep_start) |
| 2149 | p->se.sleep_start -= clock_offset; |
| 2150 | if (p->se.block_start) |
| 2151 | p->se.block_start -= clock_offset; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2152 | if (old_cpu != new_cpu) { |
| 2153 | schedstat_inc(p, se.nr_migrations); |
| 2154 | if (task_hot(p, old_rq->clock, NULL)) |
| 2155 | schedstat_inc(p, se.nr_forced2_migrations); |
| 2156 | } |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2157 | #endif |
Srivatsa Vaddagiri | 2830cf8 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2158 | p->se.vruntime -= old_cfsrq->min_vruntime - |
| 2159 | new_cfsrq->min_vruntime; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2160 | |
| 2161 | __set_task_cpu(p, new_cpu); |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2162 | } |
| 2163 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2164 | struct migration_req { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2167 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | int dest_cpu; |
| 2169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | struct completion done; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2171 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | |
| 2173 | /* |
| 2174 | * The task's runqueue lock must be held. |
| 2175 | * Returns true if you have to wait for migration thread. |
| 2176 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2177 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2178 | 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] | 2179 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2180 | struct rq *rq = task_rq(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | |
| 2182 | /* |
| 2183 | * If the task is not on a runqueue (and not running), then |
| 2184 | * it is sufficient to simply update the task's cpu field. |
| 2185 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2186 | if (!p->se.on_rq && !task_running(rq, p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | set_task_cpu(p, dest_cpu); |
| 2188 | return 0; |
| 2189 | } |
| 2190 | |
| 2191 | init_completion(&req->done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | req->task = p; |
| 2193 | req->dest_cpu = dest_cpu; |
| 2194 | list_add(&req->list, &rq->migration_queue); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | return 1; |
| 2197 | } |
| 2198 | |
| 2199 | /* |
| 2200 | * wait_task_inactive - wait for a thread to unschedule. |
| 2201 | * |
| 2202 | * The caller must ensure that the task *will* unschedule sometime soon, |
| 2203 | * else this function might spin for a *long* time. This function can't |
| 2204 | * be called with interrupts off, or it may introduce deadlock with |
| 2205 | * smp_call_function() if an IPI is sent by the same process we are |
| 2206 | * waiting to become inactive. |
| 2207 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2208 | void wait_task_inactive(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | { |
| 2210 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2211 | int running, on_rq; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2212 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2214 | for (;;) { |
| 2215 | /* |
| 2216 | * We do the initial early heuristics without holding |
| 2217 | * any task-queue locks at all. We'll only try to get |
| 2218 | * the runqueue lock when things look like they will |
| 2219 | * work out! |
| 2220 | */ |
| 2221 | rq = task_rq(p); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 2222 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2223 | /* |
| 2224 | * If the task is actively running on another CPU |
| 2225 | * still, just relax and busy-wait without holding |
| 2226 | * any locks. |
| 2227 | * |
| 2228 | * NOTE! Since we don't hold any locks, it's not |
| 2229 | * even sure that "rq" stays as the right runqueue! |
| 2230 | * But we don't care, since "task_running()" will |
| 2231 | * return false if the runqueue has changed and p |
| 2232 | * is actually now running somewhere else! |
| 2233 | */ |
| 2234 | while (task_running(rq, p)) |
| 2235 | cpu_relax(); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 2236 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2237 | /* |
| 2238 | * Ok, time to look more closely! We need the rq |
| 2239 | * lock now, to be *sure*. If we're wrong, we'll |
| 2240 | * just go back and repeat. |
| 2241 | */ |
| 2242 | rq = task_rq_lock(p, &flags); |
| 2243 | running = task_running(rq, p); |
| 2244 | on_rq = p->se.on_rq; |
| 2245 | task_rq_unlock(rq, &flags); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 2246 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2247 | /* |
| 2248 | * Was it really running after all now that we |
| 2249 | * checked with the proper locks actually held? |
| 2250 | * |
| 2251 | * Oops. Go back and try again.. |
| 2252 | */ |
| 2253 | if (unlikely(running)) { |
| 2254 | cpu_relax(); |
| 2255 | continue; |
| 2256 | } |
| 2257 | |
| 2258 | /* |
| 2259 | * It's not enough that it's not actively running, |
| 2260 | * it must be off the runqueue _entirely_, and not |
| 2261 | * preempted! |
| 2262 | * |
| 2263 | * So if it wa still runnable (but just not actively |
| 2264 | * running right now), it's preempted, and we should |
| 2265 | * yield - it could be a while. |
| 2266 | */ |
| 2267 | if (unlikely(on_rq)) { |
| 2268 | schedule_timeout_uninterruptible(1); |
| 2269 | continue; |
| 2270 | } |
| 2271 | |
| 2272 | /* |
| 2273 | * Ahh, all good. It wasn't running, and it wasn't |
| 2274 | * runnable, which means that it will never become |
| 2275 | * running in the future either. We're all done! |
| 2276 | */ |
| 2277 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | /*** |
| 2282 | * kick_process - kick a running thread to enter/exit the kernel |
| 2283 | * @p: the to-be-kicked thread |
| 2284 | * |
| 2285 | * Cause a process which is running on another CPU to enter |
| 2286 | * kernel-mode, without any delay. (to get signals handled.) |
| 2287 | * |
| 2288 | * NOTE: this function doesnt have to take the runqueue lock, |
| 2289 | * because all it wants to ensure is that the remote task enters |
| 2290 | * the kernel. If the IPI races and the task has been migrated |
| 2291 | * to another CPU then no harm is done and the purpose has been |
| 2292 | * achieved as well. |
| 2293 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2294 | void kick_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | { |
| 2296 | int cpu; |
| 2297 | |
| 2298 | preempt_disable(); |
| 2299 | cpu = task_cpu(p); |
| 2300 | if ((cpu != smp_processor_id()) && task_curr(p)) |
| 2301 | smp_send_reschedule(cpu); |
| 2302 | preempt_enable(); |
| 2303 | } |
| 2304 | |
| 2305 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2306 | * Return a low guess at the load of a migration-source cpu weighted |
| 2307 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | * |
| 2309 | * We want to under-estimate the load of migration sources, to |
| 2310 | * balance conservatively. |
| 2311 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2312 | static unsigned long source_load(int cpu, int type) |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 2313 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2314 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2315 | unsigned long total = weighted_cpuload(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2316 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2317 | if (type == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2318 | return total; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2319 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2320 | return min(rq->cpu_load[type-1], total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2324 | * Return a high guess at the load of a migration-target cpu weighted |
| 2325 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2327 | static unsigned long target_load(int cpu, int type) |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 2328 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2329 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2330 | unsigned long total = weighted_cpuload(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2331 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2332 | if (type == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2333 | return total; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2334 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2335 | return max(rq->cpu_load[type-1], total); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | /* |
| 2339 | * Return the average load per task on the cpu's run queue |
| 2340 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2341 | static unsigned long cpu_avg_load_per_task(int cpu) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2342 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2343 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2344 | unsigned long total = weighted_cpuload(cpu); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2345 | unsigned long n = rq->nr_running; |
| 2346 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2347 | return n ? total / n : SCHED_LOAD_SCALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2350 | /* |
| 2351 | * find_idlest_group finds and returns the least busy CPU group within the |
| 2352 | * domain. |
| 2353 | */ |
| 2354 | static struct sched_group * |
| 2355 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) |
| 2356 | { |
| 2357 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; |
| 2358 | unsigned long min_load = ULONG_MAX, this_load = 0; |
| 2359 | int load_idx = sd->forkexec_idx; |
| 2360 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
| 2361 | |
| 2362 | do { |
| 2363 | unsigned long load, avg_load; |
| 2364 | int local_group; |
| 2365 | int i; |
| 2366 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 2367 | /* Skip over this group if it has no CPUs allowed */ |
| 2368 | if (!cpus_intersects(group->cpumask, p->cpus_allowed)) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2369 | continue; |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 2370 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2371 | local_group = cpu_isset(this_cpu, group->cpumask); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2372 | |
| 2373 | /* Tally up the load of all CPUs in the group */ |
| 2374 | avg_load = 0; |
| 2375 | |
| 2376 | for_each_cpu_mask(i, group->cpumask) { |
| 2377 | /* Bias balancing toward cpus of our domain */ |
| 2378 | if (local_group) |
| 2379 | load = source_load(i, load_idx); |
| 2380 | else |
| 2381 | load = target_load(i, load_idx); |
| 2382 | |
| 2383 | avg_load += load; |
| 2384 | } |
| 2385 | |
| 2386 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2387 | avg_load = sg_div_cpu_power(group, |
| 2388 | avg_load * SCHED_LOAD_SCALE); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2389 | |
| 2390 | if (local_group) { |
| 2391 | this_load = avg_load; |
| 2392 | this = group; |
| 2393 | } else if (avg_load < min_load) { |
| 2394 | min_load = avg_load; |
| 2395 | idlest = group; |
| 2396 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2397 | } while (group = group->next, group != sd->groups); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2398 | |
| 2399 | if (!idlest || 100*this_load < imbalance*min_load) |
| 2400 | return NULL; |
| 2401 | return idlest; |
| 2402 | } |
| 2403 | |
| 2404 | /* |
Satoru Takeuchi | 0feaece | 2006-10-03 01:14:10 -0700 | [diff] [blame] | 2405 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2406 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 2407 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2408 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu, |
| 2409 | cpumask_t *tmp) |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2410 | { |
| 2411 | unsigned long load, min_load = ULONG_MAX; |
| 2412 | int idlest = -1; |
| 2413 | int i; |
| 2414 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 2415 | /* Traverse only the allowed CPUs */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2416 | cpus_and(*tmp, group->cpumask, p->cpus_allowed); |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 2417 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2418 | for_each_cpu_mask(i, *tmp) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2419 | load = weighted_cpuload(i); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2420 | |
| 2421 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 2422 | min_load = load; |
| 2423 | idlest = i; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | return idlest; |
| 2428 | } |
| 2429 | |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2430 | /* |
| 2431 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 2432 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 2433 | * SD_BALANCE_EXEC. |
| 2434 | * |
| 2435 | * Balance, ie. select the least loaded group. |
| 2436 | * |
| 2437 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 2438 | * |
| 2439 | * preempt must be disabled. |
| 2440 | */ |
| 2441 | static int sched_balance_self(int cpu, int flag) |
| 2442 | { |
| 2443 | struct task_struct *t = current; |
| 2444 | struct sched_domain *tmp, *sd = NULL; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2445 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2446 | for_each_domain(cpu, tmp) { |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2447 | /* |
| 2448 | * If power savings logic is enabled for a domain, stop there. |
| 2449 | */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2450 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) |
| 2451 | break; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2452 | if (tmp->flags & flag) |
| 2453 | sd = tmp; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2454 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2455 | |
| 2456 | while (sd) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2457 | cpumask_t span, tmpmask; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2458 | struct sched_group *group; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2459 | int new_cpu, weight; |
| 2460 | |
| 2461 | if (!(sd->flags & flag)) { |
| 2462 | sd = sd->child; |
| 2463 | continue; |
| 2464 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2465 | |
| 2466 | span = sd->span; |
| 2467 | group = find_idlest_group(sd, t, cpu); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2468 | if (!group) { |
| 2469 | sd = sd->child; |
| 2470 | continue; |
| 2471 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2472 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2473 | new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2474 | if (new_cpu == -1 || new_cpu == cpu) { |
| 2475 | /* Now try balancing at a lower domain level of cpu */ |
| 2476 | sd = sd->child; |
| 2477 | continue; |
| 2478 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2479 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2480 | /* Now try balancing at a lower domain level of new_cpu */ |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2481 | cpu = new_cpu; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2482 | sd = NULL; |
| 2483 | weight = cpus_weight(span); |
| 2484 | for_each_domain(cpu, tmp) { |
| 2485 | if (weight <= cpus_weight(tmp->span)) |
| 2486 | break; |
| 2487 | if (tmp->flags & flag) |
| 2488 | sd = tmp; |
| 2489 | } |
| 2490 | /* while loop will break here if sd == NULL */ |
| 2491 | } |
| 2492 | |
| 2493 | return cpu; |
| 2494 | } |
| 2495 | |
| 2496 | #endif /* CONFIG_SMP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | /*** |
| 2499 | * try_to_wake_up - wake up a thread |
| 2500 | * @p: the to-be-woken-up thread |
| 2501 | * @state: the mask of task states that can be woken |
| 2502 | * @sync: do a synchronous wakeup? |
| 2503 | * |
| 2504 | * Put it on the run-queue if it's not already there. The "current" |
| 2505 | * thread is always on the run-queue (except when the actual |
| 2506 | * re-schedule is in progress), and as such you're allowed to do |
| 2507 | * the simpler "current->state = TASK_RUNNING" to mark yourself |
| 2508 | * runnable without the overhead of this. |
| 2509 | * |
| 2510 | * returns failure only if the task is already active. |
| 2511 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2512 | 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] | 2513 | { |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2514 | int cpu, orig_cpu, this_cpu, success = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | unsigned long flags; |
| 2516 | long old_state; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2517 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | |
Ingo Molnar | b85d066 | 2008-03-16 20:03:22 +0100 | [diff] [blame] | 2519 | if (!sched_feat(SYNC_WAKEUPS)) |
| 2520 | sync = 0; |
| 2521 | |
Linus Torvalds | 04e2f17 | 2008-02-23 18:05:03 -0800 | [diff] [blame] | 2522 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | rq = task_rq_lock(p, &flags); |
| 2524 | old_state = p->state; |
| 2525 | if (!(old_state & state)) |
| 2526 | goto out; |
| 2527 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2528 | if (p->se.on_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | goto out_running; |
| 2530 | |
| 2531 | cpu = task_cpu(p); |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2532 | orig_cpu = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | this_cpu = smp_processor_id(); |
| 2534 | |
| 2535 | #ifdef CONFIG_SMP |
| 2536 | if (unlikely(task_running(rq, p))) |
| 2537 | goto out_activate; |
| 2538 | |
Dmitry Adamushko | 5d2f5a6 | 2008-01-25 21:08:21 +0100 | [diff] [blame] | 2539 | cpu = p->sched_class->select_task_rq(p, sync); |
| 2540 | if (cpu != orig_cpu) { |
| 2541 | set_task_cpu(p, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | task_rq_unlock(rq, &flags); |
| 2543 | /* might preempt at this point */ |
| 2544 | rq = task_rq_lock(p, &flags); |
| 2545 | old_state = p->state; |
| 2546 | if (!(old_state & state)) |
| 2547 | goto out; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2548 | if (p->se.on_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | goto out_running; |
| 2550 | |
| 2551 | this_cpu = smp_processor_id(); |
| 2552 | cpu = task_cpu(p); |
| 2553 | } |
| 2554 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2555 | #ifdef CONFIG_SCHEDSTATS |
| 2556 | schedstat_inc(rq, ttwu_count); |
| 2557 | if (cpu == this_cpu) |
| 2558 | schedstat_inc(rq, ttwu_local); |
| 2559 | else { |
| 2560 | struct sched_domain *sd; |
| 2561 | for_each_domain(this_cpu, sd) { |
| 2562 | if (cpu_isset(cpu, sd->span)) { |
| 2563 | schedstat_inc(sd, ttwu_wake_remote); |
| 2564 | break; |
| 2565 | } |
| 2566 | } |
| 2567 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2568 | #endif |
| 2569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | out_activate: |
| 2571 | #endif /* CONFIG_SMP */ |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2572 | schedstat_inc(p, se.nr_wakeups); |
| 2573 | if (sync) |
| 2574 | schedstat_inc(p, se.nr_wakeups_sync); |
| 2575 | if (orig_cpu != cpu) |
| 2576 | schedstat_inc(p, se.nr_wakeups_migrate); |
| 2577 | if (cpu == this_cpu) |
| 2578 | schedstat_inc(p, se.nr_wakeups_local); |
| 2579 | else |
| 2580 | schedstat_inc(p, se.nr_wakeups_remote); |
Ingo Molnar | 2daa357 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 2581 | update_rq_clock(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2582 | activate_task(rq, p, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | success = 1; |
| 2584 | |
| 2585 | out_running: |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 2586 | check_preempt_curr(rq, p); |
| 2587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | p->state = TASK_RUNNING; |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2589 | #ifdef CONFIG_SMP |
| 2590 | if (p->sched_class->task_wake_up) |
| 2591 | p->sched_class->task_wake_up(rq, p); |
| 2592 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | out: |
| 2594 | task_rq_unlock(rq, &flags); |
| 2595 | |
| 2596 | return success; |
| 2597 | } |
| 2598 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2599 | int wake_up_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 2601 | return try_to_wake_up(p, TASK_ALL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | EXPORT_SYMBOL(wake_up_process); |
| 2604 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2605 | int wake_up_state(struct task_struct *p, unsigned int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | { |
| 2607 | return try_to_wake_up(p, state, 0); |
| 2608 | } |
| 2609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | /* |
| 2611 | * Perform scheduler related setup for a newly forked process p. |
| 2612 | * p is forked by current. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2613 | * |
| 2614 | * __sched_fork() is basic setup used by init_idle() too: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2616 | static void __sched_fork(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2618 | p->se.exec_start = 0; |
| 2619 | p->se.sum_exec_runtime = 0; |
Ingo Molnar | f6cf891 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 2620 | p->se.prev_sum_exec_runtime = 0; |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 2621 | p->se.last_wakeup = 0; |
| 2622 | p->se.avg_overlap = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2623 | |
| 2624 | #ifdef CONFIG_SCHEDSTATS |
| 2625 | p->se.wait_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2626 | p->se.sum_sleep_runtime = 0; |
| 2627 | p->se.sleep_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2628 | p->se.block_start = 0; |
| 2629 | p->se.sleep_max = 0; |
| 2630 | p->se.block_max = 0; |
| 2631 | p->se.exec_max = 0; |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 2632 | p->se.slice_max = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2633 | p->se.wait_max = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2634 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2635 | |
Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 2636 | INIT_LIST_HEAD(&p->rt.run_list); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2637 | p->se.on_rq = 0; |
Peter Zijlstra | 4a55bd5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 2638 | INIT_LIST_HEAD(&p->se.group_node); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2639 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2640 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 2641 | INIT_HLIST_HEAD(&p->preempt_notifiers); |
| 2642 | #endif |
| 2643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | /* |
| 2645 | * We mark the process as running here, but have not actually |
| 2646 | * inserted it onto the runqueue yet. This guarantees that |
| 2647 | * nobody will actually run it, and a signal or other external |
| 2648 | * event cannot wake it up and insert it on the runqueue either. |
| 2649 | */ |
| 2650 | p->state = TASK_RUNNING; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | /* |
| 2654 | * fork()/clone()-time setup: |
| 2655 | */ |
| 2656 | void sched_fork(struct task_struct *p, int clone_flags) |
| 2657 | { |
| 2658 | int cpu = get_cpu(); |
| 2659 | |
| 2660 | __sched_fork(p); |
| 2661 | |
| 2662 | #ifdef CONFIG_SMP |
| 2663 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); |
| 2664 | #endif |
Ingo Molnar | 02e4bac | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 2665 | set_task_cpu(p, cpu); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2666 | |
| 2667 | /* |
| 2668 | * Make sure we do not leak PI boosting priority to the child: |
| 2669 | */ |
| 2670 | p->prio = current->normal_prio; |
Hiroshi Shimamoto | 2ddbf95 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 2671 | if (!rt_prio(p->prio)) |
| 2672 | p->sched_class = &fair_sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2673 | |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 2674 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2675 | if (likely(sched_info_on())) |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 2676 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | #endif |
Chen, Kenneth W | d6077cb | 2006-02-14 13:53:10 -0800 | [diff] [blame] | 2678 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2679 | p->oncpu = 0; |
| 2680 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | #ifdef CONFIG_PREEMPT |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2682 | /* Want to start with kernel preemption disabled. */ |
Al Viro | a1261f5 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 2683 | task_thread_info(p)->preempt_count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2685 | put_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | /* |
| 2689 | * wake_up_new_task - wake up a newly created task for the first time. |
| 2690 | * |
| 2691 | * This function will do some initial scheduler statistics housekeeping |
| 2692 | * that must be done for every newly created context, then puts the task |
| 2693 | * on the runqueue and wakes it. |
| 2694 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2695 | 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] | 2696 | { |
| 2697 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2698 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | |
| 2700 | rq = task_rq_lock(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | BUG_ON(p->state != TASK_RUNNING); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 2702 | update_rq_clock(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | |
| 2704 | p->prio = effective_prio(p); |
| 2705 | |
Srivatsa Vaddagiri | b9dca1e | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2706 | if (!p->sched_class->task_new || !current->se.on_rq) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2707 | activate_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2710 | * Let the scheduling class do new task startup |
| 2711 | * management (if any): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | */ |
Ingo Molnar | ee0827d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2713 | p->sched_class->task_new(rq, p); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 2714 | inc_nr_running(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2716 | check_preempt_curr(rq, p); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2717 | #ifdef CONFIG_SMP |
| 2718 | if (p->sched_class->task_wake_up) |
| 2719 | p->sched_class->task_wake_up(rq, p); |
| 2720 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2721 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | } |
| 2723 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2724 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 2725 | |
| 2726 | /** |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2727 | * preempt_notifier_register - tell me when current is being being preempted & rescheduled |
| 2728 | * @notifier: notifier struct to register |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2729 | */ |
| 2730 | void preempt_notifier_register(struct preempt_notifier *notifier) |
| 2731 | { |
| 2732 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); |
| 2733 | } |
| 2734 | EXPORT_SYMBOL_GPL(preempt_notifier_register); |
| 2735 | |
| 2736 | /** |
| 2737 | * preempt_notifier_unregister - no longer interested in preemption notifications |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2738 | * @notifier: notifier struct to unregister |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2739 | * |
| 2740 | * This is safe to call from within a preemption notifier. |
| 2741 | */ |
| 2742 | void preempt_notifier_unregister(struct preempt_notifier *notifier) |
| 2743 | { |
| 2744 | hlist_del(¬ifier->link); |
| 2745 | } |
| 2746 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); |
| 2747 | |
| 2748 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 2749 | { |
| 2750 | struct preempt_notifier *notifier; |
| 2751 | struct hlist_node *node; |
| 2752 | |
| 2753 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) |
| 2754 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
| 2755 | } |
| 2756 | |
| 2757 | static void |
| 2758 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 2759 | struct task_struct *next) |
| 2760 | { |
| 2761 | struct preempt_notifier *notifier; |
| 2762 | struct hlist_node *node; |
| 2763 | |
| 2764 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) |
| 2765 | notifier->ops->sched_out(notifier, next); |
| 2766 | } |
| 2767 | |
| 2768 | #else |
| 2769 | |
| 2770 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 2771 | { |
| 2772 | } |
| 2773 | |
| 2774 | static void |
| 2775 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 2776 | struct task_struct *next) |
| 2777 | { |
| 2778 | } |
| 2779 | |
| 2780 | #endif |
| 2781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | /** |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2783 | * prepare_task_switch - prepare to switch tasks |
| 2784 | * @rq: the runqueue preparing to switch |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2785 | * @prev: the current task that is being switched out |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2786 | * @next: the task we are going to switch to. |
| 2787 | * |
| 2788 | * This is called with the rq lock held and interrupts off. It must |
| 2789 | * be paired with a subsequent finish_task_switch after the context |
| 2790 | * switch. |
| 2791 | * |
| 2792 | * prepare_task_switch sets up locking and calls architecture specific |
| 2793 | * hooks. |
| 2794 | */ |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2795 | static inline void |
| 2796 | prepare_task_switch(struct rq *rq, struct task_struct *prev, |
| 2797 | struct task_struct *next) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2798 | { |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2799 | fire_sched_out_preempt_notifiers(prev, next); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2800 | prepare_lock_switch(rq, next); |
| 2801 | prepare_arch_switch(next); |
| 2802 | } |
| 2803 | |
| 2804 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | * finish_task_switch - clean up after a task-switch |
Jeff Garzik | 344baba | 2005-09-07 01:15:17 -0400 | [diff] [blame] | 2806 | * @rq: runqueue associated with task-switch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | * @prev: the thread we just switched away from. |
| 2808 | * |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2809 | * finish_task_switch must be called after the context switch, paired |
| 2810 | * with a prepare_task_switch call before the context switch. |
| 2811 | * finish_task_switch will reconcile locking set up by prepare_task_switch, |
| 2812 | * and do any other architecture-specific cleanup actions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | * |
| 2814 | * 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] | 2815 | * so, we finish that here outside of the runqueue lock. (Doing it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | * with the lock held can cause deadlocks; see schedule() for |
| 2817 | * details.) |
| 2818 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2819 | static void finish_task_switch(struct rq *rq, struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | __releases(rq->lock) |
| 2821 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | struct mm_struct *mm = rq->prev_mm; |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 2823 | long prev_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | |
| 2825 | rq->prev_mm = NULL; |
| 2826 | |
| 2827 | /* |
| 2828 | * A task struct has one reference for the use as "current". |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2829 | * 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] | 2830 | * schedule one last time. The schedule call will never return, and |
| 2831 | * the scheduled task must drop that reference. |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2832 | * The test for TASK_DEAD must occur while the runqueue locks are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | * still held, otherwise prev could be scheduled on another cpu, die |
| 2834 | * there before we look at prev->state, and then the reference would |
| 2835 | * be dropped twice. |
| 2836 | * Manfred Spraul <manfred@colorfullife.com> |
| 2837 | */ |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 2838 | prev_state = prev->state; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2839 | finish_arch_switch(prev); |
| 2840 | finish_lock_switch(rq, prev); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2841 | #ifdef CONFIG_SMP |
| 2842 | if (current->sched_class->post_schedule) |
| 2843 | current->sched_class->post_schedule(rq); |
| 2844 | #endif |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2845 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2846 | fire_sched_in_preempt_notifiers(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | if (mm) |
| 2848 | mmdrop(mm); |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2849 | if (unlikely(prev_state == TASK_DEAD)) { |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2850 | /* |
| 2851 | * Remove function-return probe instances associated with this |
| 2852 | * task and put them back on the free list. |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2853 | */ |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2854 | kprobe_flush_task(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | put_task_struct(prev); |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | /** |
| 2860 | * schedule_tail - first thing a freshly forked thread must call. |
| 2861 | * @prev: the thread we just switched away from. |
| 2862 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2863 | asmlinkage void schedule_tail(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | __releases(rq->lock) |
| 2865 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2866 | struct rq *rq = this_rq(); |
| 2867 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2868 | finish_task_switch(rq, prev); |
| 2869 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW |
| 2870 | /* In this case, finish_task_switch does not reenable preemption */ |
| 2871 | preempt_enable(); |
| 2872 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | if (current->set_child_tid) |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2874 | put_user(task_pid_vnr(current), current->set_child_tid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | /* |
| 2878 | * context_switch - switch to the new MM and the new |
| 2879 | * thread's register state. |
| 2880 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2881 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2882 | context_switch(struct rq *rq, struct task_struct *prev, |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2883 | struct task_struct *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2885 | struct mm_struct *mm, *oldmm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2887 | prepare_task_switch(rq, prev, next); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2888 | mm = next->mm; |
| 2889 | oldmm = prev->active_mm; |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 2890 | /* |
| 2891 | * For paravirt, this is coupled with an exit in switch_to to |
| 2892 | * combine the page table reload and the switch backend into |
| 2893 | * one hypercall. |
| 2894 | */ |
| 2895 | arch_enter_lazy_cpu_mode(); |
| 2896 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2897 | if (unlikely(!mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | next->active_mm = oldmm; |
| 2899 | atomic_inc(&oldmm->mm_count); |
| 2900 | enter_lazy_tlb(oldmm, next); |
| 2901 | } else |
| 2902 | switch_mm(oldmm, mm, next); |
| 2903 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2904 | if (unlikely(!prev->mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | prev->active_mm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | rq->prev_mm = oldmm; |
| 2907 | } |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 2908 | /* |
| 2909 | * Since the runqueue lock will be released by the next |
| 2910 | * task (which is an invalid locking op but in the case |
| 2911 | * of the scheduler it's an obvious special-case), so we |
| 2912 | * do an early lockdep release here: |
| 2913 | */ |
| 2914 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 2915 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 2916 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | |
| 2918 | /* Here we just switch the register state and the stack. */ |
| 2919 | switch_to(prev, next, prev); |
| 2920 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2921 | barrier(); |
| 2922 | /* |
| 2923 | * this_rq must be evaluated again because prev may have moved |
| 2924 | * CPUs since it called schedule(), thus the 'rq' on its stack |
| 2925 | * frame will be invalid. |
| 2926 | */ |
| 2927 | finish_task_switch(this_rq(), prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | /* |
| 2931 | * nr_running, nr_uninterruptible and nr_context_switches: |
| 2932 | * |
| 2933 | * externally visible scheduler statistics: current number of runnable |
| 2934 | * threads, current number of uninterruptible-sleeping threads, total |
| 2935 | * number of context switches performed since bootup. |
| 2936 | */ |
| 2937 | unsigned long nr_running(void) |
| 2938 | { |
| 2939 | unsigned long i, sum = 0; |
| 2940 | |
| 2941 | for_each_online_cpu(i) |
| 2942 | sum += cpu_rq(i)->nr_running; |
| 2943 | |
| 2944 | return sum; |
| 2945 | } |
| 2946 | |
| 2947 | unsigned long nr_uninterruptible(void) |
| 2948 | { |
| 2949 | unsigned long i, sum = 0; |
| 2950 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2951 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | sum += cpu_rq(i)->nr_uninterruptible; |
| 2953 | |
| 2954 | /* |
| 2955 | * Since we read the counters lockless, it might be slightly |
| 2956 | * inaccurate. Do not allow it to go below zero though: |
| 2957 | */ |
| 2958 | if (unlikely((long)sum < 0)) |
| 2959 | sum = 0; |
| 2960 | |
| 2961 | return sum; |
| 2962 | } |
| 2963 | |
| 2964 | unsigned long long nr_context_switches(void) |
| 2965 | { |
Steven Rostedt | cc94abf | 2006-06-27 02:54:31 -0700 | [diff] [blame] | 2966 | int i; |
| 2967 | unsigned long long sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2969 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | sum += cpu_rq(i)->nr_switches; |
| 2971 | |
| 2972 | return sum; |
| 2973 | } |
| 2974 | |
| 2975 | unsigned long nr_iowait(void) |
| 2976 | { |
| 2977 | unsigned long i, sum = 0; |
| 2978 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2979 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
| 2981 | |
| 2982 | return sum; |
| 2983 | } |
| 2984 | |
Jack Steiner | db1b1fe | 2006-03-31 02:31:21 -0800 | [diff] [blame] | 2985 | unsigned long nr_active(void) |
| 2986 | { |
| 2987 | unsigned long i, running = 0, uninterruptible = 0; |
| 2988 | |
| 2989 | for_each_online_cpu(i) { |
| 2990 | running += cpu_rq(i)->nr_running; |
| 2991 | uninterruptible += cpu_rq(i)->nr_uninterruptible; |
| 2992 | } |
| 2993 | |
| 2994 | if (unlikely((long)uninterruptible < 0)) |
| 2995 | uninterruptible = 0; |
| 2996 | |
| 2997 | return running + uninterruptible; |
| 2998 | } |
| 2999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3001 | * Update rq->cpu_load[] statistics. This function is usually called every |
| 3002 | * scheduler tick (TICK_NSEC). |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3003 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3004 | static void update_cpu_load(struct rq *this_rq) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3005 | { |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 3006 | unsigned long this_load = this_rq->load.weight; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3007 | int i, scale; |
| 3008 | |
| 3009 | this_rq->nr_load_updates++; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3010 | |
| 3011 | /* Update our load: */ |
| 3012 | for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { |
| 3013 | unsigned long old_load, new_load; |
| 3014 | |
| 3015 | /* scale is effectively 1 << i now, and >> i divides by scale */ |
| 3016 | |
| 3017 | old_load = this_rq->cpu_load[i]; |
| 3018 | new_load = this_load; |
Ingo Molnar | a25707f | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 3019 | /* |
| 3020 | * Round up the averaging division if load is increasing. This |
| 3021 | * prevents us from getting stuck on 9 if the load is 10, for |
| 3022 | * example. |
| 3023 | */ |
| 3024 | if (new_load > old_load) |
| 3025 | new_load += scale-1; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3026 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; |
| 3027 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3028 | } |
| 3029 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3030 | #ifdef CONFIG_SMP |
| 3031 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3032 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | * double_rq_lock - safely lock two runqueues |
| 3034 | * |
| 3035 | * Note this does not disable interrupts like task_rq_lock, |
| 3036 | * you need to do so manually before calling. |
| 3037 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3038 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | __acquires(rq1->lock) |
| 3040 | __acquires(rq2->lock) |
| 3041 | { |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 3042 | BUG_ON(!irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | if (rq1 == rq2) { |
| 3044 | spin_lock(&rq1->lock); |
| 3045 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 3046 | } else { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3047 | if (rq1 < rq2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | spin_lock(&rq1->lock); |
| 3049 | spin_lock(&rq2->lock); |
| 3050 | } else { |
| 3051 | spin_lock(&rq2->lock); |
| 3052 | spin_lock(&rq1->lock); |
| 3053 | } |
| 3054 | } |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3055 | update_rq_clock(rq1); |
| 3056 | update_rq_clock(rq2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | /* |
| 3060 | * double_rq_unlock - safely unlock two runqueues |
| 3061 | * |
| 3062 | * Note this does not restore interrupts like task_rq_unlock, |
| 3063 | * you need to do so manually after calling. |
| 3064 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3065 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | __releases(rq1->lock) |
| 3067 | __releases(rq2->lock) |
| 3068 | { |
| 3069 | spin_unlock(&rq1->lock); |
| 3070 | if (rq1 != rq2) |
| 3071 | spin_unlock(&rq2->lock); |
| 3072 | else |
| 3073 | __release(rq2->lock); |
| 3074 | } |
| 3075 | |
| 3076 | /* |
| 3077 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. |
| 3078 | */ |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 3079 | static int double_lock_balance(struct rq *this_rq, struct rq *busiest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3080 | __releases(this_rq->lock) |
| 3081 | __acquires(busiest->lock) |
| 3082 | __acquires(this_rq->lock) |
| 3083 | { |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 3084 | int ret = 0; |
| 3085 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 3086 | if (unlikely(!irqs_disabled())) { |
| 3087 | /* printk() doesn't work good under rq->lock */ |
| 3088 | spin_unlock(&this_rq->lock); |
| 3089 | BUG_ON(1); |
| 3090 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | if (unlikely(!spin_trylock(&busiest->lock))) { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3092 | if (busiest < this_rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | spin_unlock(&this_rq->lock); |
| 3094 | spin_lock(&busiest->lock); |
| 3095 | spin_lock(&this_rq->lock); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 3096 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | } else |
| 3098 | spin_lock(&busiest->lock); |
| 3099 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 3100 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | * If dest_cpu is allowed for this process, migrate the task to it. |
| 3105 | * This is accomplished by forcing the cpu_allowed mask to only |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3106 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | * the cpu_allowed mask is restored. |
| 3108 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3109 | static void sched_migrate_task(struct task_struct *p, int dest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3111 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3113 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | |
| 3115 | rq = task_rq_lock(p, &flags); |
| 3116 | if (!cpu_isset(dest_cpu, p->cpus_allowed) |
| 3117 | || unlikely(cpu_is_offline(dest_cpu))) |
| 3118 | goto out; |
| 3119 | |
| 3120 | /* force the process onto the specified CPU */ |
| 3121 | if (migrate_task(p, dest_cpu, &req)) { |
| 3122 | /* Need to wait for migration thread (might exit: take ref). */ |
| 3123 | struct task_struct *mt = rq->migration_thread; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | get_task_struct(mt); |
| 3126 | task_rq_unlock(rq, &flags); |
| 3127 | wake_up_process(mt); |
| 3128 | put_task_struct(mt); |
| 3129 | wait_for_completion(&req.done); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | return; |
| 3132 | } |
| 3133 | out: |
| 3134 | task_rq_unlock(rq, &flags); |
| 3135 | } |
| 3136 | |
| 3137 | /* |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 3138 | * sched_exec - execve() is a valuable balancing opportunity, because at |
| 3139 | * this point the task has the smallest effective memory and cache footprint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | */ |
| 3141 | void sched_exec(void) |
| 3142 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | int new_cpu, this_cpu = get_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 3144 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | put_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 3146 | if (new_cpu != this_cpu) |
| 3147 | sched_migrate_task(current, new_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | /* |
| 3151 | * pull_task - move a task from a remote runqueue to the local runqueue. |
| 3152 | * Both runqueues must be locked. |
| 3153 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3154 | static void pull_task(struct rq *src_rq, struct task_struct *p, |
| 3155 | struct rq *this_rq, int this_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 3157 | deactivate_task(src_rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | set_task_cpu(p, this_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3159 | activate_task(this_rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | /* |
| 3161 | * Note that idle threads have a prio of MAX_PRIO, for this test |
| 3162 | * to be always true for them. |
| 3163 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3164 | check_preempt_curr(this_rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | /* |
| 3168 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 3169 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3170 | static |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3171 | 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] | 3172 | struct sched_domain *sd, enum cpu_idle_type idle, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 3173 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | { |
| 3175 | /* |
| 3176 | * We do not migrate tasks that are: |
| 3177 | * 1) running (obviously), or |
| 3178 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 3179 | * 3) are cache-hot on their current CPU. |
| 3180 | */ |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3181 | if (!cpu_isset(this_cpu, p->cpus_allowed)) { |
| 3182 | schedstat_inc(p, se.nr_failed_migrations_affine); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3184 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3185 | *all_pinned = 0; |
| 3186 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3187 | if (task_running(rq, p)) { |
| 3188 | schedstat_inc(p, se.nr_failed_migrations_running); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3189 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3192 | /* |
| 3193 | * Aggressive migration if: |
| 3194 | * 1) task is cache cold, or |
| 3195 | * 2) too many balance attempts have failed. |
| 3196 | */ |
| 3197 | |
Ingo Molnar | 6bc1665 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3198 | if (!task_hot(p, rq->clock, sd) || |
| 3199 | sd->nr_balance_failed > sd->cache_nice_tries) { |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3200 | #ifdef CONFIG_SCHEDSTATS |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3201 | if (task_hot(p, rq->clock, sd)) { |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3202 | schedstat_inc(sd, lb_hot_gained[idle]); |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3203 | schedstat_inc(p, se.nr_forced_migrations); |
| 3204 | } |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3205 | #endif |
| 3206 | return 1; |
| 3207 | } |
| 3208 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3209 | if (task_hot(p, rq->clock, sd)) { |
| 3210 | schedstat_inc(p, se.nr_failed_migrations_hot); |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3211 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3213 | return 1; |
| 3214 | } |
| 3215 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3216 | static unsigned long |
| 3217 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 3218 | unsigned long max_load_move, struct sched_domain *sd, |
| 3219 | enum cpu_idle_type idle, int *all_pinned, |
| 3220 | int *this_best_prio, struct rq_iterator *iterator) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3221 | { |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 3222 | int loops = 0, pulled = 0, pinned = 0, skip_for_load; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3223 | struct task_struct *p; |
| 3224 | long rem_load_move = max_load_move; |
| 3225 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3226 | if (max_load_move == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3227 | goto out; |
| 3228 | |
| 3229 | pinned = 1; |
| 3230 | |
| 3231 | /* |
| 3232 | * Start the load-balancing iterator: |
| 3233 | */ |
| 3234 | p = iterator->start(iterator->arg); |
| 3235 | next: |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 3236 | if (!p || loops++ > sysctl_sched_nr_migrate) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3237 | goto out; |
| 3238 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 3239 | * To help distribute high priority tasks across CPUs we don't |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3240 | * skip a task if it will be the highest priority task (i.e. smallest |
| 3241 | * prio value) on its new queue regardless of its load weight |
| 3242 | */ |
| 3243 | skip_for_load = (p->se.load.weight >> 1) > rem_load_move + |
| 3244 | SCHED_LOAD_SCALE_FUZZ; |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3245 | if ((skip_for_load && p->prio >= *this_best_prio) || |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3246 | !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3247 | p = iterator->next(iterator->arg); |
| 3248 | goto next; |
| 3249 | } |
| 3250 | |
| 3251 | pull_task(busiest, p, this_rq, this_cpu); |
| 3252 | pulled++; |
| 3253 | rem_load_move -= p->se.load.weight; |
| 3254 | |
| 3255 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 3256 | * 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] | 3257 | */ |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3258 | if (rem_load_move > 0) { |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3259 | if (p->prio < *this_best_prio) |
| 3260 | *this_best_prio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3261 | p = iterator->next(iterator->arg); |
| 3262 | goto next; |
| 3263 | } |
| 3264 | out: |
| 3265 | /* |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3266 | * 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] | 3267 | * so we can safely collect pull_task() stats here rather than |
| 3268 | * inside pull_task(). |
| 3269 | */ |
| 3270 | schedstat_add(sd, lb_gained[idle], pulled); |
| 3271 | |
| 3272 | if (all_pinned) |
| 3273 | *all_pinned = pinned; |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3274 | |
| 3275 | return max_load_move - rem_load_move; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3276 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | /* |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3279 | * move_tasks tries to move up to max_load_move weighted load from busiest to |
| 3280 | * this_rq, as part of a balancing operation within domain "sd". |
| 3281 | * Returns 1 if successful and 0 otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | * |
| 3283 | * Called with both runqueues locked. |
| 3284 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3285 | 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] | 3286 | unsigned long max_load_move, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3287 | struct sched_domain *sd, enum cpu_idle_type idle, |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3288 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3290 | const struct sched_class *class = sched_class_highest; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3291 | unsigned long total_load_moved = 0; |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3292 | int this_best_prio = this_rq->curr->prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3293 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3294 | do { |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3295 | total_load_moved += |
| 3296 | class->load_balance(this_rq, this_cpu, busiest, |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3297 | max_load_move - total_load_moved, |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3298 | sd, idle, all_pinned, &this_best_prio); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3299 | class = class->next; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3300 | } while (class && max_load_move > total_load_moved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3302 | return total_load_moved > 0; |
| 3303 | } |
| 3304 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3305 | static int |
| 3306 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 3307 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 3308 | struct rq_iterator *iterator) |
| 3309 | { |
| 3310 | struct task_struct *p = iterator->start(iterator->arg); |
| 3311 | int pinned = 0; |
| 3312 | |
| 3313 | while (p) { |
| 3314 | if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
| 3315 | pull_task(busiest, p, this_rq, this_cpu); |
| 3316 | /* |
| 3317 | * Right now, this is only the second place pull_task() |
| 3318 | * is called, so we can safely collect pull_task() |
| 3319 | * stats here rather than inside pull_task(). |
| 3320 | */ |
| 3321 | schedstat_inc(sd, lb_gained[idle]); |
| 3322 | |
| 3323 | return 1; |
| 3324 | } |
| 3325 | p = iterator->next(iterator->arg); |
| 3326 | } |
| 3327 | |
| 3328 | return 0; |
| 3329 | } |
| 3330 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3331 | /* |
| 3332 | * move_one_task tries to move exactly one task from busiest to this_rq, as |
| 3333 | * part of active balancing operations within "domain". |
| 3334 | * Returns 1 if successful and 0 otherwise. |
| 3335 | * |
| 3336 | * Called with both runqueues locked. |
| 3337 | */ |
| 3338 | static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 3339 | struct sched_domain *sd, enum cpu_idle_type idle) |
| 3340 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3341 | const struct sched_class *class; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3342 | |
| 3343 | for (class = sched_class_highest; class; class = class->next) |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3344 | if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle)) |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3345 | return 1; |
| 3346 | |
| 3347 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | /* |
| 3351 | * find_busiest_group finds and returns the busiest CPU group within the |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3352 | * domain. It calculates and returns the amount of weighted load which |
| 3353 | * should be moved to restore balance via the imbalance parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | */ |
| 3355 | static struct sched_group * |
| 3356 | find_busiest_group(struct sched_domain *sd, int this_cpu, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3357 | unsigned long *imbalance, enum cpu_idle_type idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3358 | int *sd_idle, const cpumask_t *cpus, int *balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | { |
| 3360 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; |
| 3361 | 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] | 3362 | unsigned long max_pull; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3363 | unsigned long busiest_load_per_task, busiest_nr_running; |
| 3364 | unsigned long this_load_per_task, this_nr_running; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3365 | int load_idx, group_imb = 0; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3366 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 3367 | int power_savings_balance = 1; |
| 3368 | unsigned long leader_nr_running = 0, min_load_per_task = 0; |
| 3369 | unsigned long min_nr_running = ULONG_MAX; |
| 3370 | struct sched_group *group_min = NULL, *group_leader = NULL; |
| 3371 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | |
| 3373 | max_load = this_load = total_load = total_pwr = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3374 | busiest_load_per_task = busiest_nr_running = 0; |
| 3375 | this_load_per_task = this_nr_running = 0; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3376 | if (idle == CPU_NOT_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 3377 | load_idx = sd->busy_idx; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3378 | else if (idle == CPU_NEWLY_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 3379 | load_idx = sd->newidle_idx; |
| 3380 | else |
| 3381 | load_idx = sd->idle_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3382 | |
| 3383 | do { |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3384 | unsigned long load, group_capacity, max_cpu_load, min_cpu_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | int local_group; |
| 3386 | int i; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3387 | int __group_imb = 0; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3388 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3389 | unsigned long sum_nr_running, sum_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | |
| 3391 | local_group = cpu_isset(this_cpu, group->cpumask); |
| 3392 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3393 | if (local_group) |
| 3394 | balance_cpu = first_cpu(group->cpumask); |
| 3395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3396 | /* Tally up the load of all CPUs in the group */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3397 | sum_weighted_load = sum_nr_running = avg_load = 0; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3398 | max_cpu_load = 0; |
| 3399 | min_cpu_load = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | |
| 3401 | for_each_cpu_mask(i, group->cpumask) { |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3402 | struct rq *rq; |
| 3403 | |
| 3404 | if (!cpu_isset(i, *cpus)) |
| 3405 | continue; |
| 3406 | |
| 3407 | rq = cpu_rq(i); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3408 | |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3409 | if (*sd_idle && rq->nr_running) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3410 | *sd_idle = 0; |
| 3411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | /* Bias balancing toward cpus of our domain */ |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3413 | if (local_group) { |
| 3414 | if (idle_cpu(i) && !first_idle_cpu) { |
| 3415 | first_idle_cpu = 1; |
| 3416 | balance_cpu = i; |
| 3417 | } |
| 3418 | |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 3419 | load = target_load(i, load_idx); |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3420 | } else { |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 3421 | load = source_load(i, load_idx); |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3422 | if (load > max_cpu_load) |
| 3423 | max_cpu_load = load; |
| 3424 | if (min_cpu_load > load) |
| 3425 | min_cpu_load = load; |
| 3426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3427 | |
| 3428 | avg_load += load; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3429 | sum_nr_running += rq->nr_running; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3430 | sum_weighted_load += weighted_cpuload(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | } |
| 3432 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3433 | /* |
| 3434 | * First idle cpu or the first cpu(busiest) in this sched group |
| 3435 | * is eligible for doing load balancing at this and above |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3436 | * domains. In the newly idle case, we will allow all the cpu's |
| 3437 | * to do the newly idle load balance. |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3438 | */ |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3439 | if (idle != CPU_NEWLY_IDLE && local_group && |
| 3440 | balance_cpu != this_cpu && balance) { |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3441 | *balance = 0; |
| 3442 | goto ret; |
| 3443 | } |
| 3444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | total_load += avg_load; |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3446 | total_pwr += group->__cpu_power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | |
| 3448 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3449 | avg_load = sg_div_cpu_power(group, |
| 3450 | avg_load * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3452 | if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE) |
| 3453 | __group_imb = 1; |
| 3454 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3455 | group_capacity = group->__cpu_power / SCHED_LOAD_SCALE; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | if (local_group) { |
| 3458 | this_load = avg_load; |
| 3459 | this = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3460 | this_nr_running = sum_nr_running; |
| 3461 | this_load_per_task = sum_weighted_load; |
| 3462 | } else if (avg_load > max_load && |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3463 | (sum_nr_running > group_capacity || __group_imb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | max_load = avg_load; |
| 3465 | busiest = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3466 | busiest_nr_running = sum_nr_running; |
| 3467 | busiest_load_per_task = sum_weighted_load; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3468 | group_imb = __group_imb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3470 | |
| 3471 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 3472 | /* |
| 3473 | * Busy processors will not participate in power savings |
| 3474 | * balance. |
| 3475 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3476 | if (idle == CPU_NOT_IDLE || |
| 3477 | !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
| 3478 | goto group_next; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3479 | |
| 3480 | /* |
| 3481 | * If the local group is idle or completely loaded |
| 3482 | * no need to do power savings balance at this domain |
| 3483 | */ |
| 3484 | if (local_group && (this_nr_running >= group_capacity || |
| 3485 | !this_nr_running)) |
| 3486 | power_savings_balance = 0; |
| 3487 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3488 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3489 | * If a group is already running at full capacity or idle, |
| 3490 | * don't include that group in power savings calculations |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3491 | */ |
| 3492 | if (!power_savings_balance || sum_nr_running >= group_capacity |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3493 | || !sum_nr_running) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3494 | goto group_next; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3495 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3496 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3497 | * Calculate the group which has the least non-idle load. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3498 | * This is the group from where we need to pick up the load |
| 3499 | * for saving power |
| 3500 | */ |
| 3501 | if ((sum_nr_running < min_nr_running) || |
| 3502 | (sum_nr_running == min_nr_running && |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3503 | first_cpu(group->cpumask) < |
| 3504 | first_cpu(group_min->cpumask))) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3505 | group_min = group; |
| 3506 | min_nr_running = sum_nr_running; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3507 | min_load_per_task = sum_weighted_load / |
| 3508 | sum_nr_running; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3509 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3510 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3511 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3512 | * Calculate the group which is almost near its |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3513 | * capacity but still has some space to pick up some load |
| 3514 | * from other group and save more power |
| 3515 | */ |
| 3516 | if (sum_nr_running <= group_capacity - 1) { |
| 3517 | if (sum_nr_running > leader_nr_running || |
| 3518 | (sum_nr_running == leader_nr_running && |
| 3519 | first_cpu(group->cpumask) > |
| 3520 | first_cpu(group_leader->cpumask))) { |
| 3521 | group_leader = group; |
| 3522 | leader_nr_running = sum_nr_running; |
| 3523 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3524 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3525 | group_next: |
| 3526 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | group = group->next; |
| 3528 | } while (group != sd->groups); |
| 3529 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3530 | if (!busiest || this_load >= max_load || busiest_nr_running == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | goto out_balanced; |
| 3532 | |
| 3533 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; |
| 3534 | |
| 3535 | if (this_load >= avg_load || |
| 3536 | 100*max_load <= sd->imbalance_pct*this_load) |
| 3537 | goto out_balanced; |
| 3538 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3539 | busiest_load_per_task /= busiest_nr_running; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3540 | if (group_imb) |
| 3541 | busiest_load_per_task = min(busiest_load_per_task, avg_load); |
| 3542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | /* |
| 3544 | * We're trying to get all the cpus to the average_load, so we don't |
| 3545 | * want to push ourselves above the average load, nor do we wish to |
| 3546 | * reduce the max loaded cpu below the average load, as either of these |
| 3547 | * actions would just result in more rebalancing later, and ping-pong |
| 3548 | * tasks around. Thus we look for the minimum possible imbalance. |
| 3549 | * Negative imbalances (*we* are more loaded than anyone else) will |
| 3550 | * 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] | 3551 | * 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] | 3552 | * appear as very large values with unsigned longs. |
| 3553 | */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3554 | if (max_load <= busiest_load_per_task) |
| 3555 | goto out_balanced; |
| 3556 | |
| 3557 | /* |
| 3558 | * In the presence of smp nice balancing, certain scenarios can have |
| 3559 | * max load less than avg load(as we skip the groups at or below |
| 3560 | * its cpu_power, while calculating max_load..) |
| 3561 | */ |
| 3562 | if (max_load < avg_load) { |
| 3563 | *imbalance = 0; |
| 3564 | goto small_imbalance; |
| 3565 | } |
Siddha, Suresh B | 0c117f1 | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3566 | |
| 3567 | /* 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] | 3568 | 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] | 3569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | /* How much load to actually move to equalise the imbalance */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3571 | *imbalance = min(max_pull * busiest->__cpu_power, |
| 3572 | (avg_load - this_load) * this->__cpu_power) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | / SCHED_LOAD_SCALE; |
| 3574 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3575 | /* |
| 3576 | * if *imbalance is less than the average load per runnable task |
| 3577 | * there is no gaurantee that any tasks will be moved so we'll have |
| 3578 | * a think about bumping its value to force at least one task to be |
| 3579 | * moved |
| 3580 | */ |
Suresh Siddha | 7fd0d2d | 2007-09-05 14:32:48 +0200 | [diff] [blame] | 3581 | if (*imbalance < busiest_load_per_task) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3582 | unsigned long tmp, pwr_now, pwr_move; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3583 | unsigned int imbn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3585 | small_imbalance: |
| 3586 | pwr_move = pwr_now = 0; |
| 3587 | imbn = 2; |
| 3588 | if (this_nr_running) { |
| 3589 | this_load_per_task /= this_nr_running; |
| 3590 | if (busiest_load_per_task > this_load_per_task) |
| 3591 | imbn = 1; |
| 3592 | } else |
| 3593 | this_load_per_task = SCHED_LOAD_SCALE; |
| 3594 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3595 | if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >= |
| 3596 | busiest_load_per_task * imbn) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3597 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | return busiest; |
| 3599 | } |
| 3600 | |
| 3601 | /* |
| 3602 | * OK, we don't have enough imbalance to justify moving tasks, |
| 3603 | * however we may be able to increase total CPU power used by |
| 3604 | * moving them. |
| 3605 | */ |
| 3606 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3607 | pwr_now += busiest->__cpu_power * |
| 3608 | min(busiest_load_per_task, max_load); |
| 3609 | pwr_now += this->__cpu_power * |
| 3610 | min(this_load_per_task, this_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | pwr_now /= SCHED_LOAD_SCALE; |
| 3612 | |
| 3613 | /* Amount of load we'd subtract */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3614 | tmp = sg_div_cpu_power(busiest, |
| 3615 | busiest_load_per_task * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | if (max_load > tmp) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3617 | pwr_move += busiest->__cpu_power * |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3618 | min(busiest_load_per_task, max_load - tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | |
| 3620 | /* Amount of load we'd add */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3621 | if (max_load * busiest->__cpu_power < |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 3622 | busiest_load_per_task * SCHED_LOAD_SCALE) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3623 | tmp = sg_div_cpu_power(this, |
| 3624 | max_load * busiest->__cpu_power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | else |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3626 | tmp = sg_div_cpu_power(this, |
| 3627 | busiest_load_per_task * SCHED_LOAD_SCALE); |
| 3628 | pwr_move += this->__cpu_power * |
| 3629 | min(this_load_per_task, this_load + tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | pwr_move /= SCHED_LOAD_SCALE; |
| 3631 | |
| 3632 | /* Move if we gain throughput */ |
Suresh Siddha | 7fd0d2d | 2007-09-05 14:32:48 +0200 | [diff] [blame] | 3633 | if (pwr_move > pwr_now) |
| 3634 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | } |
| 3636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | return busiest; |
| 3638 | |
| 3639 | out_balanced: |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3640 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3641 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3642 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3644 | if (this == group_leader && group_leader != group_min) { |
| 3645 | *imbalance = min_load_per_task; |
| 3646 | return group_min; |
| 3647 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3648 | #endif |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3649 | ret: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | *imbalance = 0; |
| 3651 | return NULL; |
| 3652 | } |
| 3653 | |
| 3654 | /* |
| 3655 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 3656 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3657 | static struct rq * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3658 | find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3659 | unsigned long imbalance, const cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3661 | struct rq *busiest = NULL, *rq; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3662 | unsigned long max_load = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | int i; |
| 3664 | |
| 3665 | for_each_cpu_mask(i, group->cpumask) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3666 | unsigned long wl; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3667 | |
| 3668 | if (!cpu_isset(i, *cpus)) |
| 3669 | continue; |
| 3670 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3671 | rq = cpu_rq(i); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3672 | wl = weighted_cpuload(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3674 | if (rq->nr_running == 1 && wl > imbalance) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3675 | continue; |
| 3676 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3677 | if (wl > max_load) { |
| 3678 | max_load = wl; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3679 | busiest = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | } |
| 3681 | } |
| 3682 | |
| 3683 | return busiest; |
| 3684 | } |
| 3685 | |
| 3686 | /* |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 3687 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 3688 | * so long as it is large enough. |
| 3689 | */ |
| 3690 | #define MAX_PINNED_INTERVAL 512 |
| 3691 | |
| 3692 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3694 | * tasks if there is an imbalance. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3695 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3696 | static int load_balance(int this_cpu, struct rq *this_rq, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3697 | struct sched_domain *sd, enum cpu_idle_type idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3698 | int *balance, cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3699 | { |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3700 | int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | struct sched_group *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | unsigned long imbalance; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3703 | struct rq *busiest; |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3704 | unsigned long flags; |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 3705 | int unlock_aggregate; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3706 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3707 | cpus_setall(*cpus); |
| 3708 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 3709 | unlock_aggregate = get_aggregate(sd); |
| 3710 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3711 | /* |
| 3712 | * When power savings policy is enabled for the parent domain, idle |
| 3713 | * sibling can pick up load irrespective of busy siblings. In this case, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3714 | * 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] | 3715 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3716 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3717 | if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3718 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3719 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3721 | schedstat_inc(sd, lb_count[idle]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3722 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3723 | redo: |
| 3724 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3725 | cpus, balance); |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3726 | |
Chen, Kenneth W | 0606671 | 2006-12-10 02:20:35 -0800 | [diff] [blame] | 3727 | if (*balance == 0) |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3728 | goto out_balanced; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | if (!group) { |
| 3731 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 3732 | goto out_balanced; |
| 3733 | } |
| 3734 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3735 | busiest = find_busiest_queue(group, idle, imbalance, cpus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3736 | if (!busiest) { |
| 3737 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 3738 | goto out_balanced; |
| 3739 | } |
| 3740 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3741 | BUG_ON(busiest == this_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | |
| 3743 | schedstat_add(sd, lb_imbalance[idle], imbalance); |
| 3744 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3745 | ld_moved = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | if (busiest->nr_running > 1) { |
| 3747 | /* |
| 3748 | * Attempt to move tasks. If find_busiest_group has found |
| 3749 | * an imbalance but busiest->nr_running <= 1, the group is |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3750 | * still unbalanced. ld_moved simply stays zero, so it is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | * correctly treated as an imbalance. |
| 3752 | */ |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3753 | local_irq_save(flags); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 3754 | double_rq_lock(this_rq, busiest); |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3755 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3756 | imbalance, sd, idle, &all_pinned); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 3757 | double_rq_unlock(this_rq, busiest); |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3758 | local_irq_restore(flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3759 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3760 | /* |
| 3761 | * some other cpu did the load balance for us. |
| 3762 | */ |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3763 | if (ld_moved && this_cpu != smp_processor_id()) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3764 | resched_cpu(this_cpu); |
| 3765 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3766 | /* All tasks on this runqueue were pinned by CPU affinity */ |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3767 | if (unlikely(all_pinned)) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3768 | cpu_clear(cpu_of(busiest), *cpus); |
| 3769 | if (!cpus_empty(*cpus)) |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3770 | goto redo; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3771 | goto out_balanced; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3773 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3774 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3775 | if (!ld_moved) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | schedstat_inc(sd, lb_failed[idle]); |
| 3777 | sd->nr_balance_failed++; |
| 3778 | |
| 3779 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3781 | spin_lock_irqsave(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3782 | |
| 3783 | /* don't kick the migration_thread, if the curr |
| 3784 | * task on busiest cpu can't be moved to this_cpu |
| 3785 | */ |
| 3786 | if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3787 | spin_unlock_irqrestore(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3788 | all_pinned = 1; |
| 3789 | goto out_one_pinned; |
| 3790 | } |
| 3791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3792 | if (!busiest->active_balance) { |
| 3793 | busiest->active_balance = 1; |
| 3794 | busiest->push_cpu = this_cpu; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3795 | active_balance = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | } |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3797 | spin_unlock_irqrestore(&busiest->lock, flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3798 | if (active_balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | wake_up_process(busiest->migration_thread); |
| 3800 | |
| 3801 | /* |
| 3802 | * We've kicked active balancing, reset the failure |
| 3803 | * counter. |
| 3804 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3805 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3806 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3807 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | sd->nr_balance_failed = 0; |
| 3809 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3810 | if (likely(!active_balance)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | /* We were unbalanced, so reset the balancing interval */ |
| 3812 | sd->balance_interval = sd->min_interval; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3813 | } else { |
| 3814 | /* |
| 3815 | * If we've begun active balancing, start to back off. This |
| 3816 | * case may not be covered by the all_pinned logic if there |
| 3817 | * is only 1 task on the busy runqueue (because we don't call |
| 3818 | * move_tasks). |
| 3819 | */ |
| 3820 | if (sd->balance_interval < sd->max_interval) |
| 3821 | sd->balance_interval *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3822 | } |
| 3823 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3824 | if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3825 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 3826 | ld_moved = -1; |
| 3827 | |
| 3828 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | |
| 3830 | out_balanced: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | schedstat_inc(sd, lb_balanced[idle]); |
| 3832 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3833 | sd->nr_balance_failed = 0; |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3834 | |
| 3835 | out_one_pinned: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3836 | /* tune up the balancing interval */ |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 3837 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
| 3838 | (sd->balance_interval < sd->max_interval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | sd->balance_interval *= 2; |
| 3840 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3841 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3842 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 3843 | ld_moved = -1; |
| 3844 | else |
| 3845 | ld_moved = 0; |
| 3846 | out: |
| 3847 | if (unlock_aggregate) |
| 3848 | put_aggregate(sd); |
| 3849 | return ld_moved; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | /* |
| 3853 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3854 | * tasks if there is an imbalance. |
| 3855 | * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3856 | * 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] | 3857 | * this_rq is locked. |
| 3858 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3859 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3860 | load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd, |
| 3861 | cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | { |
| 3863 | struct sched_group *group; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3864 | struct rq *busiest = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | unsigned long imbalance; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3866 | int ld_moved = 0; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3867 | int sd_idle = 0; |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3868 | int all_pinned = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3869 | |
| 3870 | cpus_setall(*cpus); |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3871 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3872 | /* |
| 3873 | * When power savings policy is enabled for the parent domain, idle |
| 3874 | * sibling can pick up load irrespective of busy siblings. In this case, |
| 3875 | * let the state of idle sibling percolate up as IDLE, instead of |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3876 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3877 | */ |
| 3878 | if (sd->flags & SD_SHARE_CPUPOWER && |
| 3879 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3880 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3882 | schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3883 | redo: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3884 | group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3885 | &sd_idle, cpus, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | if (!group) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3887 | schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3888 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | } |
| 3890 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3891 | busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus); |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3892 | if (!busiest) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3893 | schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3894 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | } |
| 3896 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3897 | BUG_ON(busiest == this_rq); |
| 3898 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3899 | schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3900 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3901 | ld_moved = 0; |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3902 | if (busiest->nr_running > 1) { |
| 3903 | /* Attempt to move tasks */ |
| 3904 | double_lock_balance(this_rq, busiest); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3905 | /* this_rq->clock is already updated */ |
| 3906 | update_rq_clock(busiest); |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3907 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3908 | imbalance, sd, CPU_NEWLY_IDLE, |
| 3909 | &all_pinned); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3910 | spin_unlock(&busiest->lock); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3911 | |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3912 | if (unlikely(all_pinned)) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3913 | cpu_clear(cpu_of(busiest), *cpus); |
| 3914 | if (!cpus_empty(*cpus)) |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3915 | goto redo; |
| 3916 | } |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3917 | } |
| 3918 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3919 | if (!ld_moved) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3920 | schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3921 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 3922 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3923 | return -1; |
| 3924 | } else |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3925 | sd->nr_balance_failed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3927 | return ld_moved; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3928 | |
| 3929 | out_balanced: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3930 | schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3931 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3932 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3933 | return -1; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3934 | sd->nr_balance_failed = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3935 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3936 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | /* |
| 3940 | * idle_balance is called by schedule() if this_cpu is about to become |
| 3941 | * idle. Attempts to pull tasks from other CPUs. |
| 3942 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3943 | static void idle_balance(int this_cpu, struct rq *this_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | { |
| 3945 | struct sched_domain *sd; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3946 | int pulled_task = -1; |
| 3947 | unsigned long next_balance = jiffies + HZ; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3948 | cpumask_t tmpmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | |
| 3950 | for_each_domain(this_cpu, sd) { |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 3951 | unsigned long interval; |
| 3952 | |
| 3953 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 3954 | continue; |
| 3955 | |
| 3956 | if (sd->flags & SD_BALANCE_NEWIDLE) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3957 | /* If we've pulled tasks over stop searching: */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3958 | pulled_task = load_balance_newidle(this_cpu, this_rq, |
| 3959 | sd, &tmpmask); |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 3960 | |
| 3961 | interval = msecs_to_jiffies(sd->balance_interval); |
| 3962 | if (time_after(next_balance, sd->last_balance + interval)) |
| 3963 | next_balance = sd->last_balance + interval; |
| 3964 | if (pulled_task) |
| 3965 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3967 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 3968 | /* |
| 3969 | * We are going idle. next_balance may be set based on |
| 3970 | * a busy processor. So reset next_balance. |
| 3971 | */ |
| 3972 | this_rq->next_balance = next_balance; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3973 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | /* |
| 3977 | * active_load_balance is run by migration threads. It pushes running tasks |
| 3978 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be |
| 3979 | * running on each physical CPU where possible, and avoids physical / |
| 3980 | * logical imbalances. |
| 3981 | * |
| 3982 | * Called with busiest_rq locked. |
| 3983 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3984 | static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3985 | { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3986 | int target_cpu = busiest_rq->push_cpu; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3987 | struct sched_domain *sd; |
| 3988 | struct rq *target_rq; |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3989 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3990 | /* Is there any task to move? */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3991 | if (busiest_rq->nr_running <= 1) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3992 | return; |
| 3993 | |
| 3994 | target_rq = cpu_rq(target_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3995 | |
| 3996 | /* |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3997 | * This condition is "impossible", if it occurs |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3998 | * we need to fix it. Originally reported by |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3999 | * Bjorn Helgaas on a 128-cpu setup. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 4001 | BUG_ON(busiest_rq == target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 4003 | /* move a task from busiest_rq to target_rq */ |
| 4004 | double_lock_balance(busiest_rq, target_rq); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 4005 | update_rq_clock(busiest_rq); |
| 4006 | update_rq_clock(target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 4008 | /* Search for an sd spanning us and the target CPU. */ |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 4009 | for_each_domain(target_cpu, sd) { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 4010 | if ((sd->flags & SD_LOAD_BALANCE) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4011 | cpu_isset(busiest_cpu, sd->span)) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 4012 | break; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 4013 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4015 | if (likely(sd)) { |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4016 | schedstat_inc(sd, alb_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 4018 | if (move_one_task(target_rq, target_cpu, busiest_rq, |
| 4019 | sd, CPU_IDLE)) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4020 | schedstat_inc(sd, alb_pushed); |
| 4021 | else |
| 4022 | schedstat_inc(sd, alb_failed); |
| 4023 | } |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 4024 | spin_unlock(&target_rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4025 | } |
| 4026 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4027 | #ifdef CONFIG_NO_HZ |
| 4028 | static struct { |
| 4029 | atomic_t load_balancer; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4030 | cpumask_t cpu_mask; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4031 | } nohz ____cacheline_aligned = { |
| 4032 | .load_balancer = ATOMIC_INIT(-1), |
| 4033 | .cpu_mask = CPU_MASK_NONE, |
| 4034 | }; |
| 4035 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4036 | /* |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4037 | * This routine will try to nominate the ilb (idle load balancing) |
| 4038 | * owner among the cpus whose ticks are stopped. ilb owner will do the idle |
| 4039 | * load balancing on behalf of all those cpus. If all the cpus in the system |
| 4040 | * go into this tickless mode, then there will be no ilb owner (as there is |
| 4041 | * no need for one) and all the cpus will sleep till the next wakeup event |
| 4042 | * arrives... |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4043 | * |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4044 | * For the ilb owner, tick is not stopped. And this tick will be used |
| 4045 | * for idle load balancing. ilb owner will still be part of |
| 4046 | * nohz.cpu_mask.. |
| 4047 | * |
| 4048 | * While stopping the tick, this cpu will become the ilb owner if there |
| 4049 | * is no other owner. And will be the owner till that cpu becomes busy |
| 4050 | * or if all cpus in the system stop their ticks at which point |
| 4051 | * there is no need for ilb owner. |
| 4052 | * |
| 4053 | * When the ilb owner becomes busy, it nominates another owner, during the |
| 4054 | * next busy scheduler_tick() |
| 4055 | */ |
| 4056 | int select_nohz_load_balancer(int stop_tick) |
| 4057 | { |
| 4058 | int cpu = smp_processor_id(); |
| 4059 | |
| 4060 | if (stop_tick) { |
| 4061 | cpu_set(cpu, nohz.cpu_mask); |
| 4062 | cpu_rq(cpu)->in_nohz_recently = 1; |
| 4063 | |
| 4064 | /* |
| 4065 | * If we are going offline and still the leader, give up! |
| 4066 | */ |
| 4067 | if (cpu_is_offline(cpu) && |
| 4068 | atomic_read(&nohz.load_balancer) == cpu) { |
| 4069 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 4070 | BUG(); |
| 4071 | return 0; |
| 4072 | } |
| 4073 | |
| 4074 | /* time for ilb owner also to sleep */ |
| 4075 | if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 4076 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 4077 | atomic_set(&nohz.load_balancer, -1); |
| 4078 | return 0; |
| 4079 | } |
| 4080 | |
| 4081 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 4082 | /* make me the ilb owner */ |
| 4083 | if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) |
| 4084 | return 1; |
| 4085 | } else if (atomic_read(&nohz.load_balancer) == cpu) |
| 4086 | return 1; |
| 4087 | } else { |
| 4088 | if (!cpu_isset(cpu, nohz.cpu_mask)) |
| 4089 | return 0; |
| 4090 | |
| 4091 | cpu_clear(cpu, nohz.cpu_mask); |
| 4092 | |
| 4093 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 4094 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 4095 | BUG(); |
| 4096 | } |
| 4097 | return 0; |
| 4098 | } |
| 4099 | #endif |
| 4100 | |
| 4101 | static DEFINE_SPINLOCK(balancing); |
| 4102 | |
| 4103 | /* |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4104 | * It checks each scheduling domain to see if it is due to be balanced, |
| 4105 | * and initiates a balancing operation if so. |
| 4106 | * |
| 4107 | * Balancing parameters are set up in arch_init_sched_domains. |
| 4108 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4109 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4110 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4111 | int balance = 1; |
| 4112 | struct rq *rq = cpu_rq(cpu); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4113 | unsigned long interval; |
| 4114 | struct sched_domain *sd; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4115 | /* Earliest time when we have to do rebalance again */ |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 4116 | unsigned long next_balance = jiffies + 60*HZ; |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 4117 | int update_next_balance = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 4118 | cpumask_t tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4120 | for_each_domain(cpu, sd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 4122 | continue; |
| 4123 | |
| 4124 | interval = sd->balance_interval; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 4125 | if (idle != CPU_IDLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | interval *= sd->busy_factor; |
| 4127 | |
| 4128 | /* scale ms to jiffies */ |
| 4129 | interval = msecs_to_jiffies(interval); |
| 4130 | if (unlikely(!interval)) |
| 4131 | interval = 1; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4132 | if (interval > HZ*NR_CPUS/10) |
| 4133 | interval = HZ*NR_CPUS/10; |
| 4134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 4136 | if (sd->flags & SD_SERIALIZE) { |
| 4137 | if (!spin_trylock(&balancing)) |
| 4138 | goto out; |
| 4139 | } |
| 4140 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 4141 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 4142 | if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) { |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 4143 | /* |
| 4144 | * We've pulled tasks over so either we're no |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 4145 | * longer idle, or one of our SMT siblings is |
| 4146 | * not idle. |
| 4147 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 4148 | idle = CPU_NOT_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | } |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 4150 | sd->last_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | } |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 4152 | if (sd->flags & SD_SERIALIZE) |
| 4153 | spin_unlock(&balancing); |
| 4154 | out: |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 4155 | if (time_after(next_balance, sd->last_balance + interval)) { |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 4156 | next_balance = sd->last_balance + interval; |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 4157 | update_next_balance = 1; |
| 4158 | } |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 4159 | |
| 4160 | /* |
| 4161 | * Stop the load balance at this level. There is another |
| 4162 | * CPU in our sched group which is doing load balancing more |
| 4163 | * actively. |
| 4164 | */ |
| 4165 | if (!balance) |
| 4166 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | } |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 4168 | |
| 4169 | /* |
| 4170 | * next_balance will be updated only when there is a need. |
| 4171 | * When the cpu is attached to null domain for ex, it will not be |
| 4172 | * updated. |
| 4173 | */ |
| 4174 | if (likely(update_next_balance)) |
| 4175 | rq->next_balance = next_balance; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | /* |
| 4179 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
| 4180 | * In CONFIG_NO_HZ case, the idle load balance owner will do the |
| 4181 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 4182 | */ |
| 4183 | static void run_rebalance_domains(struct softirq_action *h) |
| 4184 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4185 | int this_cpu = smp_processor_id(); |
| 4186 | struct rq *this_rq = cpu_rq(this_cpu); |
| 4187 | enum cpu_idle_type idle = this_rq->idle_at_tick ? |
| 4188 | CPU_IDLE : CPU_NOT_IDLE; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4189 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4190 | rebalance_domains(this_cpu, idle); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4191 | |
| 4192 | #ifdef CONFIG_NO_HZ |
| 4193 | /* |
| 4194 | * If this cpu is the owner for idle load balancing, then do the |
| 4195 | * balancing on behalf of the other idle cpus whose ticks are |
| 4196 | * stopped. |
| 4197 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4198 | if (this_rq->idle_at_tick && |
| 4199 | atomic_read(&nohz.load_balancer) == this_cpu) { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4200 | cpumask_t cpus = nohz.cpu_mask; |
| 4201 | struct rq *rq; |
| 4202 | int balance_cpu; |
| 4203 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4204 | cpu_clear(this_cpu, cpus); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4205 | for_each_cpu_mask(balance_cpu, cpus) { |
| 4206 | /* |
| 4207 | * If this cpu gets work to do, stop the load balancing |
| 4208 | * work being done for other cpus. Next load |
| 4209 | * balancing owner will pick it up. |
| 4210 | */ |
| 4211 | if (need_resched()) |
| 4212 | break; |
| 4213 | |
Oleg Nesterov | de0cf89 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 4214 | rebalance_domains(balance_cpu, CPU_IDLE); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4215 | |
| 4216 | rq = cpu_rq(balance_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4217 | if (time_after(this_rq->next_balance, rq->next_balance)) |
| 4218 | this_rq->next_balance = rq->next_balance; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4219 | } |
| 4220 | } |
| 4221 | #endif |
| 4222 | } |
| 4223 | |
| 4224 | /* |
| 4225 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. |
| 4226 | * |
| 4227 | * In case of CONFIG_NO_HZ, this is the place where we nominate a new |
| 4228 | * idle load balancing owner or decide to stop the periodic load balancing, |
| 4229 | * if the whole system is idle. |
| 4230 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4231 | static inline void trigger_load_balance(struct rq *rq, int cpu) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4232 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4233 | #ifdef CONFIG_NO_HZ |
| 4234 | /* |
| 4235 | * If we were in the nohz mode recently and busy at the current |
| 4236 | * scheduler tick, then check if we need to nominate new idle |
| 4237 | * load balancer. |
| 4238 | */ |
| 4239 | if (rq->in_nohz_recently && !rq->idle_at_tick) { |
| 4240 | rq->in_nohz_recently = 0; |
| 4241 | |
| 4242 | if (atomic_read(&nohz.load_balancer) == cpu) { |
| 4243 | cpu_clear(cpu, nohz.cpu_mask); |
| 4244 | atomic_set(&nohz.load_balancer, -1); |
| 4245 | } |
| 4246 | |
| 4247 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 4248 | /* |
| 4249 | * simple selection for now: Nominate the |
| 4250 | * first cpu in the nohz list to be the next |
| 4251 | * ilb owner. |
| 4252 | * |
| 4253 | * TBD: Traverse the sched domains and nominate |
| 4254 | * the nearest cpu in the nohz.cpu_mask. |
| 4255 | */ |
| 4256 | int ilb = first_cpu(nohz.cpu_mask); |
| 4257 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 4258 | if (ilb < nr_cpu_ids) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 4259 | resched_cpu(ilb); |
| 4260 | } |
| 4261 | } |
| 4262 | |
| 4263 | /* |
| 4264 | * If this cpu is idle and doing idle load balancing for all the |
| 4265 | * cpus with ticks stopped, is it time for that to stop? |
| 4266 | */ |
| 4267 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && |
| 4268 | cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 4269 | resched_cpu(cpu); |
| 4270 | return; |
| 4271 | } |
| 4272 | |
| 4273 | /* |
| 4274 | * If this cpu is idle and the idle load balancing is done by |
| 4275 | * someone else, then no need raise the SCHED_SOFTIRQ |
| 4276 | */ |
| 4277 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && |
| 4278 | cpu_isset(cpu, nohz.cpu_mask)) |
| 4279 | return; |
| 4280 | #endif |
| 4281 | if (time_after_eq(jiffies, rq->next_balance)) |
| 4282 | raise_softirq(SCHED_SOFTIRQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4284 | |
| 4285 | #else /* CONFIG_SMP */ |
| 4286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | /* |
| 4288 | * on UP we do not need to balance between CPUs: |
| 4289 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4290 | static inline void idle_balance(int cpu, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | { |
| 4292 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | #endif |
| 4295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
| 4297 | |
| 4298 | EXPORT_PER_CPU_SYMBOL(kstat); |
| 4299 | |
| 4300 | /* |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4301 | * Return p->sum_exec_runtime plus any more ns on the sched_clock |
| 4302 | * 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] | 4303 | */ |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4304 | unsigned long long task_sched_runtime(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | unsigned long flags; |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4307 | u64 ns, delta_exec; |
| 4308 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4309 | |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4310 | rq = task_rq_lock(p, &flags); |
| 4311 | ns = p->se.sum_exec_runtime; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 4312 | if (task_current(rq, p)) { |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4313 | update_rq_clock(rq); |
| 4314 | delta_exec = rq->clock - p->se.exec_start; |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4315 | if ((s64)delta_exec > 0) |
| 4316 | ns += delta_exec; |
| 4317 | } |
| 4318 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | return ns; |
| 4321 | } |
| 4322 | |
| 4323 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4324 | * Account user cpu time to a process. |
| 4325 | * @p: the process that the cpu time gets accounted to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | * @cputime: the cpu time spent in user space since the last update |
| 4327 | */ |
| 4328 | void account_user_time(struct task_struct *p, cputime_t cputime) |
| 4329 | { |
| 4330 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 4331 | cputime64_t tmp; |
| 4332 | |
| 4333 | p->utime = cputime_add(p->utime, cputime); |
| 4334 | |
| 4335 | /* Add user time to cpustat. */ |
| 4336 | tmp = cputime_to_cputime64(cputime); |
| 4337 | if (TASK_NICE(p) > 0) |
| 4338 | cpustat->nice = cputime64_add(cpustat->nice, tmp); |
| 4339 | else |
| 4340 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 4341 | } |
| 4342 | |
| 4343 | /* |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 4344 | * Account guest cpu time to a process. |
| 4345 | * @p: the process that the cpu time gets accounted to |
| 4346 | * @cputime: the cpu time spent in virtual machine since the last update |
| 4347 | */ |
Adrian Bunk | f7402e0 | 2007-10-29 21:18:10 +0100 | [diff] [blame] | 4348 | static void account_guest_time(struct task_struct *p, cputime_t cputime) |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 4349 | { |
| 4350 | cputime64_t tmp; |
| 4351 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 4352 | |
| 4353 | tmp = cputime_to_cputime64(cputime); |
| 4354 | |
| 4355 | p->utime = cputime_add(p->utime, cputime); |
| 4356 | p->gtime = cputime_add(p->gtime, cputime); |
| 4357 | |
| 4358 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 4359 | cpustat->guest = cputime64_add(cpustat->guest, tmp); |
| 4360 | } |
| 4361 | |
| 4362 | /* |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 4363 | * Account scaled user cpu time to a process. |
| 4364 | * @p: the process that the cpu time gets accounted to |
| 4365 | * @cputime: the cpu time spent in user space since the last update |
| 4366 | */ |
| 4367 | void account_user_time_scaled(struct task_struct *p, cputime_t cputime) |
| 4368 | { |
| 4369 | p->utimescaled = cputime_add(p->utimescaled, cputime); |
| 4370 | } |
| 4371 | |
| 4372 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | * Account system cpu time to a process. |
| 4374 | * @p: the process that the cpu time gets accounted to |
| 4375 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 4376 | * @cputime: the cpu time spent in kernel space since the last update |
| 4377 | */ |
| 4378 | void account_system_time(struct task_struct *p, int hardirq_offset, |
| 4379 | cputime_t cputime) |
| 4380 | { |
| 4381 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4382 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | cputime64_t tmp; |
| 4384 | |
Christian Borntraeger | 9778385 | 2007-11-15 20:57:39 +0100 | [diff] [blame] | 4385 | if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) |
| 4386 | return account_guest_time(p, cputime); |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 4387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | p->stime = cputime_add(p->stime, cputime); |
| 4389 | |
| 4390 | /* Add system time to cpustat. */ |
| 4391 | tmp = cputime_to_cputime64(cputime); |
| 4392 | if (hardirq_count() - hardirq_offset) |
| 4393 | cpustat->irq = cputime64_add(cpustat->irq, tmp); |
| 4394 | else if (softirq_count()) |
| 4395 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 4396 | else if (p != rq->idle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | cpustat->system = cputime64_add(cpustat->system, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 4398 | else if (atomic_read(&rq->nr_iowait) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 4400 | else |
| 4401 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 4402 | /* Account for system time used */ |
| 4403 | acct_update_integrals(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | /* |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 4407 | * Account scaled system cpu time to a process. |
| 4408 | * @p: the process that the cpu time gets accounted to |
| 4409 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 4410 | * @cputime: the cpu time spent in kernel space since the last update |
| 4411 | */ |
| 4412 | void account_system_time_scaled(struct task_struct *p, cputime_t cputime) |
| 4413 | { |
| 4414 | p->stimescaled = cputime_add(p->stimescaled, cputime); |
| 4415 | } |
| 4416 | |
| 4417 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | * Account for involuntary wait time. |
| 4419 | * @p: the process from which the cpu time has been stolen |
| 4420 | * @steal: the cpu time spent in involuntary wait |
| 4421 | */ |
| 4422 | void account_steal_time(struct task_struct *p, cputime_t steal) |
| 4423 | { |
| 4424 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 4425 | cputime64_t tmp = cputime_to_cputime64(steal); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4426 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | |
| 4428 | if (p == rq->idle) { |
| 4429 | p->stime = cputime_add(p->stime, steal); |
| 4430 | if (atomic_read(&rq->nr_iowait) > 0) |
| 4431 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 4432 | else |
| 4433 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 4434 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | cpustat->steal = cputime64_add(cpustat->steal, tmp); |
| 4436 | } |
| 4437 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4438 | /* |
| 4439 | * This function gets called by the timer code, with HZ frequency. |
| 4440 | * We call it with interrupts disabled. |
| 4441 | * |
| 4442 | * It also gets called by the fork code, when changing the parent's |
| 4443 | * timeslices. |
| 4444 | */ |
| 4445 | void scheduler_tick(void) |
| 4446 | { |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4447 | int cpu = smp_processor_id(); |
| 4448 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4449 | struct task_struct *curr = rq->curr; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 4450 | u64 next_tick = rq->tick_timestamp + TICK_NSEC; |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4451 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4452 | spin_lock(&rq->lock); |
Ingo Molnar | 546fe3c | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 4453 | __update_rq_clock(rq); |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 4454 | /* |
| 4455 | * Let rq->clock advance by at least TICK_NSEC: |
| 4456 | */ |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 4457 | if (unlikely(rq->clock < next_tick)) { |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 4458 | rq->clock = next_tick; |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 4459 | rq->clock_underflows++; |
| 4460 | } |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 4461 | rq->tick_timestamp = rq->clock; |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 4462 | update_last_tick_seen(rq); |
Ingo Molnar | f1a438d | 2007-08-09 11:16:45 +0200 | [diff] [blame] | 4463 | update_cpu_load(rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4464 | curr->sched_class->task_tick(rq, curr, 0); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4465 | spin_unlock(&rq->lock); |
| 4466 | |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 4467 | #ifdef CONFIG_SMP |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4468 | rq->idle_at_tick = idle_cpu(cpu); |
| 4469 | trigger_load_balance(rq, cpu); |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 4470 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | } |
| 4472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) |
| 4474 | |
Srinivasa Ds | 4362758 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 4475 | void __kprobes add_preempt_count(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | { |
| 4477 | /* |
| 4478 | * Underflow? |
| 4479 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 4480 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
| 4481 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | preempt_count() += val; |
| 4483 | /* |
| 4484 | * Spinlock count overflowing soon? |
| 4485 | */ |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 4486 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
| 4487 | PREEMPT_MASK - 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | } |
| 4489 | EXPORT_SYMBOL(add_preempt_count); |
| 4490 | |
Srinivasa Ds | 4362758 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 4491 | void __kprobes sub_preempt_count(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | { |
| 4493 | /* |
| 4494 | * Underflow? |
| 4495 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 4496 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
| 4497 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | /* |
| 4499 | * Is the spinlock portion underflowing? |
| 4500 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 4501 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
| 4502 | !(preempt_count() & PREEMPT_MASK))) |
| 4503 | return; |
| 4504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | preempt_count() -= val; |
| 4506 | } |
| 4507 | EXPORT_SYMBOL(sub_preempt_count); |
| 4508 | |
| 4509 | #endif |
| 4510 | |
| 4511 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4512 | * Print scheduling while atomic bug: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4514 | static noinline void __schedule_bug(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | { |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 4516 | struct pt_regs *regs = get_irq_regs(); |
| 4517 | |
| 4518 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
| 4519 | prev->comm, prev->pid, preempt_count()); |
| 4520 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4521 | debug_show_held_locks(prev); |
| 4522 | if (irqs_disabled()) |
| 4523 | print_irqtrace_events(prev); |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 4524 | |
| 4525 | if (regs) |
| 4526 | show_regs(regs); |
| 4527 | else |
| 4528 | dump_stack(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4531 | /* |
| 4532 | * Various schedule()-time debugging checks and statistics: |
| 4533 | */ |
| 4534 | static inline void schedule_debug(struct task_struct *prev) |
| 4535 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4537 | * Test if we are atomic. Since do_exit() needs to call into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | * schedule() atomically, we ignore that path for now. |
| 4539 | * Otherwise, whine if we are scheduling when we should not be. |
| 4540 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4541 | if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state)) |
| 4542 | __schedule_bug(prev); |
| 4543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
| 4545 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4546 | schedstat_inc(this_rq(), sched_count); |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 4547 | #ifdef CONFIG_SCHEDSTATS |
| 4548 | if (unlikely(prev->lock_depth >= 0)) { |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4549 | schedstat_inc(this_rq(), bkl_count); |
| 4550 | schedstat_inc(prev, sched_info.bkl_count); |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 4551 | } |
| 4552 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4553 | } |
| 4554 | |
| 4555 | /* |
| 4556 | * Pick up the highest-prio task: |
| 4557 | */ |
| 4558 | static inline struct task_struct * |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4559 | pick_next_task(struct rq *rq, struct task_struct *prev) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4560 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4561 | const struct sched_class *class; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4562 | struct task_struct *p; |
| 4563 | |
| 4564 | /* |
| 4565 | * Optimization: we know that if all tasks are in |
| 4566 | * the fair class we can call that function directly: |
| 4567 | */ |
| 4568 | if (likely(rq->nr_running == rq->cfs.nr_running)) { |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 4569 | p = fair_sched_class.pick_next_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4570 | if (likely(p)) |
| 4571 | return p; |
| 4572 | } |
| 4573 | |
| 4574 | class = sched_class_highest; |
| 4575 | for ( ; ; ) { |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 4576 | p = class->pick_next_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4577 | if (p) |
| 4578 | return p; |
| 4579 | /* |
| 4580 | * Will never be NULL as the idle class always |
| 4581 | * returns a non-NULL p: |
| 4582 | */ |
| 4583 | class = class->next; |
| 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | /* |
| 4588 | * schedule() is the main scheduler function. |
| 4589 | */ |
| 4590 | asmlinkage void __sched schedule(void) |
| 4591 | { |
| 4592 | struct task_struct *prev, *next; |
Harvey Harrison | 67ca7bd | 2008-02-15 09:56:36 -0800 | [diff] [blame] | 4593 | unsigned long *switch_count; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4594 | struct rq *rq; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4595 | int cpu; |
| 4596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | need_resched: |
| 4598 | preempt_disable(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4599 | cpu = smp_processor_id(); |
| 4600 | rq = cpu_rq(cpu); |
| 4601 | rcu_qsctr_inc(cpu); |
| 4602 | prev = rq->curr; |
| 4603 | switch_count = &prev->nivcsw; |
| 4604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | release_kernel_lock(prev); |
| 4606 | need_resched_nonpreemptible: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4608 | schedule_debug(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4610 | hrtick_clear(rq); |
| 4611 | |
Ingo Molnar | 1e81995 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4612 | /* |
| 4613 | * Do the rq-clock update outside the rq lock: |
| 4614 | */ |
| 4615 | local_irq_disable(); |
Ingo Molnar | c1b3da3 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4616 | __update_rq_clock(rq); |
Ingo Molnar | 1e81995 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4617 | spin_lock(&rq->lock); |
| 4618 | clear_tsk_need_resched(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4620 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
| 4621 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && |
Roel Kluin | 23e3c3c | 2008-03-13 17:41:59 +0100 | [diff] [blame] | 4622 | signal_pending(prev))) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4623 | prev->state = TASK_RUNNING; |
| 4624 | } else { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4625 | deactivate_task(rq, prev, 1); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4626 | } |
| 4627 | switch_count = &prev->nvcsw; |
| 4628 | } |
| 4629 | |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4630 | #ifdef CONFIG_SMP |
| 4631 | if (prev->sched_class->pre_schedule) |
| 4632 | prev->sched_class->pre_schedule(rq, prev); |
| 4633 | #endif |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 4634 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4635 | if (unlikely(!rq->nr_running)) |
| 4636 | idle_balance(cpu, rq); |
| 4637 | |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4638 | prev->sched_class->put_prev_task(rq, prev); |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4639 | next = pick_next_task(rq, prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | |
| 4641 | sched_info_switch(prev, next); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | if (likely(prev != next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | rq->nr_switches++; |
| 4645 | rq->curr = next; |
| 4646 | ++*switch_count; |
| 4647 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4648 | context_switch(rq, prev, next); /* unlocks the rq */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4649 | /* |
| 4650 | * the context switch might have flipped the stack from under |
| 4651 | * us, hence refresh the local variables. |
| 4652 | */ |
| 4653 | cpu = smp_processor_id(); |
| 4654 | rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | } else |
| 4656 | spin_unlock_irq(&rq->lock); |
| 4657 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4658 | hrtick_set(rq); |
| 4659 | |
| 4660 | if (unlikely(reacquire_kernel_lock(current) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | goto need_resched_nonpreemptible; |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | preempt_enable_no_resched(); |
| 4664 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 4665 | goto need_resched; |
| 4666 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | EXPORT_SYMBOL(schedule); |
| 4668 | |
| 4669 | #ifdef CONFIG_PREEMPT |
| 4670 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4671 | * this is the entry point to schedule() from in-kernel preemption |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4672 | * off of preempt_enable. Kernel preemptions off return from interrupt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | * occur there and call schedule directly. |
| 4674 | */ |
| 4675 | asmlinkage void __sched preempt_schedule(void) |
| 4676 | { |
| 4677 | struct thread_info *ti = current_thread_info(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | struct task_struct *task = current; |
| 4679 | int saved_lock_depth; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 4680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | /* |
| 4682 | * If there is a non-zero preempt_count or interrupts are disabled, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4683 | * we do not want to preempt the current task. Just return.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | */ |
Nick Piggin | beed33a | 2006-10-11 01:21:52 -0700 | [diff] [blame] | 4685 | if (likely(ti->preempt_count || irqs_disabled())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | return; |
| 4687 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4688 | do { |
| 4689 | add_preempt_count(PREEMPT_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4691 | /* |
| 4692 | * We keep the big kernel semaphore locked, but we |
| 4693 | * clear ->lock_depth so that schedule() doesnt |
| 4694 | * auto-release the semaphore: |
| 4695 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4696 | saved_lock_depth = task->lock_depth; |
| 4697 | task->lock_depth = -1; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4698 | schedule(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4699 | task->lock_depth = saved_lock_depth; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4700 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4701 | |
| 4702 | /* |
| 4703 | * Check again in case we missed a preemption opportunity |
| 4704 | * between schedule and now. |
| 4705 | */ |
| 4706 | barrier(); |
| 4707 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | EXPORT_SYMBOL(preempt_schedule); |
| 4710 | |
| 4711 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4712 | * this is the entry point to schedule() from kernel preemption |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | * off of irq context. |
| 4714 | * Note, that this is called and return with irqs disabled. This will |
| 4715 | * protect us against recursive calling from irq. |
| 4716 | */ |
| 4717 | asmlinkage void __sched preempt_schedule_irq(void) |
| 4718 | { |
| 4719 | struct thread_info *ti = current_thread_info(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 | struct task_struct *task = current; |
| 4721 | int saved_lock_depth; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 4722 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4723 | /* Catch callers which need to be fixed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
| 4725 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4726 | do { |
| 4727 | add_preempt_count(PREEMPT_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4729 | /* |
| 4730 | * We keep the big kernel semaphore locked, but we |
| 4731 | * clear ->lock_depth so that schedule() doesnt |
| 4732 | * auto-release the semaphore: |
| 4733 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4734 | saved_lock_depth = task->lock_depth; |
| 4735 | task->lock_depth = -1; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4736 | local_irq_enable(); |
| 4737 | schedule(); |
| 4738 | local_irq_disable(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4739 | task->lock_depth = saved_lock_depth; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4740 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4741 | |
| 4742 | /* |
| 4743 | * Check again in case we missed a preemption opportunity |
| 4744 | * between schedule and now. |
| 4745 | */ |
| 4746 | barrier(); |
| 4747 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | #endif /* CONFIG_PREEMPT */ |
| 4751 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4752 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, |
| 4753 | void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4755 | return try_to_wake_up(curr->private, mode, sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | EXPORT_SYMBOL(default_wake_function); |
| 4758 | |
| 4759 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4760 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just |
| 4761 | * 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] | 4762 | * number) then we wake all the non-exclusive tasks and one exclusive task. |
| 4763 | * |
| 4764 | * 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] | 4765 | * 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] | 4766 | * zero in this (rare) case, and we handle it by continuing to scan the queue. |
| 4767 | */ |
| 4768 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, |
| 4769 | int nr_exclusive, int sync, void *key) |
| 4770 | { |
Matthias Kaehlcke | 2e45874 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 4771 | wait_queue_t *curr, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | |
Matthias Kaehlcke | 2e45874 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 4773 | list_for_each_entry_safe(curr, next, &q->task_list, task_list) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4774 | unsigned flags = curr->flags; |
| 4775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | if (curr->func(curr, mode, sync, key) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4777 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4778 | break; |
| 4779 | } |
| 4780 | } |
| 4781 | |
| 4782 | /** |
| 4783 | * __wake_up - wake up threads blocked on a waitqueue. |
| 4784 | * @q: the waitqueue |
| 4785 | * @mode: which threads |
| 4786 | * @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] | 4787 | * @key: is directly passed to the wakeup function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4788 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4789 | void __wake_up(wait_queue_head_t *q, unsigned int mode, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4790 | int nr_exclusive, void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | { |
| 4792 | unsigned long flags; |
| 4793 | |
| 4794 | spin_lock_irqsave(&q->lock, flags); |
| 4795 | __wake_up_common(q, mode, nr_exclusive, 0, key); |
| 4796 | spin_unlock_irqrestore(&q->lock, flags); |
| 4797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | EXPORT_SYMBOL(__wake_up); |
| 4799 | |
| 4800 | /* |
| 4801 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. |
| 4802 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4803 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | { |
| 4805 | __wake_up_common(q, mode, 1, 0, NULL); |
| 4806 | } |
| 4807 | |
| 4808 | /** |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 4809 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4810 | * @q: the waitqueue |
| 4811 | * @mode: which threads |
| 4812 | * @nr_exclusive: how many wake-one or wake-many threads to wake up |
| 4813 | * |
| 4814 | * The sync wakeup differs that the waker knows that it will schedule |
| 4815 | * away soon, so while the target thread will be woken up, it will not |
| 4816 | * be migrated to another CPU - ie. the two threads are 'synchronized' |
| 4817 | * with each other. This can prevent needless bouncing between CPUs. |
| 4818 | * |
| 4819 | * On UP it can prevent extra preemption. |
| 4820 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4821 | void |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4822 | __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] | 4823 | { |
| 4824 | unsigned long flags; |
| 4825 | int sync = 1; |
| 4826 | |
| 4827 | if (unlikely(!q)) |
| 4828 | return; |
| 4829 | |
| 4830 | if (unlikely(!nr_exclusive)) |
| 4831 | sync = 0; |
| 4832 | |
| 4833 | spin_lock_irqsave(&q->lock, flags); |
| 4834 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); |
| 4835 | spin_unlock_irqrestore(&q->lock, flags); |
| 4836 | } |
| 4837 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ |
| 4838 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4839 | void complete(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | { |
| 4841 | unsigned long flags; |
| 4842 | |
| 4843 | spin_lock_irqsave(&x->wait.lock, flags); |
| 4844 | x->done++; |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 4845 | __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 4847 | } |
| 4848 | EXPORT_SYMBOL(complete); |
| 4849 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4850 | void complete_all(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | { |
| 4852 | unsigned long flags; |
| 4853 | |
| 4854 | spin_lock_irqsave(&x->wait.lock, flags); |
| 4855 | x->done += UINT_MAX/2; |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 4856 | __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 4858 | } |
| 4859 | EXPORT_SYMBOL(complete_all); |
| 4860 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4861 | static inline long __sched |
| 4862 | do_wait_for_common(struct completion *x, long timeout, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | if (!x->done) { |
| 4865 | DECLARE_WAITQUEUE(wait, current); |
| 4866 | |
| 4867 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 4868 | __add_wait_queue_tail(&x->wait, &wait); |
| 4869 | do { |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 4870 | if ((state == TASK_INTERRUPTIBLE && |
| 4871 | signal_pending(current)) || |
| 4872 | (state == TASK_KILLABLE && |
| 4873 | fatal_signal_pending(current))) { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4874 | __remove_wait_queue(&x->wait, &wait); |
| 4875 | return -ERESTARTSYS; |
| 4876 | } |
| 4877 | __set_current_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | spin_unlock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4879 | timeout = schedule_timeout(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4880 | spin_lock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4881 | if (!timeout) { |
| 4882 | __remove_wait_queue(&x->wait, &wait); |
| 4883 | return timeout; |
| 4884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | } while (!x->done); |
| 4886 | __remove_wait_queue(&x->wait, &wait); |
| 4887 | } |
| 4888 | x->done--; |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4889 | return timeout; |
| 4890 | } |
| 4891 | |
| 4892 | static long __sched |
| 4893 | wait_for_common(struct completion *x, long timeout, int state) |
| 4894 | { |
| 4895 | might_sleep(); |
| 4896 | |
| 4897 | spin_lock_irq(&x->wait.lock); |
| 4898 | timeout = do_wait_for_common(x, timeout, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4899 | spin_unlock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4900 | return timeout; |
| 4901 | } |
| 4902 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4903 | void __sched wait_for_completion(struct completion *x) |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4904 | { |
| 4905 | wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | } |
| 4907 | EXPORT_SYMBOL(wait_for_completion); |
| 4908 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4909 | unsigned long __sched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) |
| 4911 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4912 | return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4913 | } |
| 4914 | EXPORT_SYMBOL(wait_for_completion_timeout); |
| 4915 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4916 | int __sched wait_for_completion_interruptible(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4917 | { |
Andi Kleen | 51e9799 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 4918 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE); |
| 4919 | if (t == -ERESTARTSYS) |
| 4920 | return t; |
| 4921 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | } |
| 4923 | EXPORT_SYMBOL(wait_for_completion_interruptible); |
| 4924 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4925 | unsigned long __sched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | wait_for_completion_interruptible_timeout(struct completion *x, |
| 4927 | unsigned long timeout) |
| 4928 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4929 | return wait_for_common(x, timeout, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | } |
| 4931 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); |
| 4932 | |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 4933 | int __sched wait_for_completion_killable(struct completion *x) |
| 4934 | { |
| 4935 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE); |
| 4936 | if (t == -ERESTARTSYS) |
| 4937 | return t; |
| 4938 | return 0; |
| 4939 | } |
| 4940 | EXPORT_SYMBOL(wait_for_completion_killable); |
| 4941 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4942 | static long __sched |
| 4943 | sleep_on_common(wait_queue_head_t *q, int state, long timeout) |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4944 | { |
| 4945 | unsigned long flags; |
| 4946 | wait_queue_t wait; |
| 4947 | |
| 4948 | init_waitqueue_entry(&wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4950 | __set_current_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4952 | spin_lock_irqsave(&q->lock, flags); |
| 4953 | __add_wait_queue(q, &wait); |
| 4954 | spin_unlock(&q->lock); |
| 4955 | timeout = schedule_timeout(timeout); |
| 4956 | spin_lock_irq(&q->lock); |
| 4957 | __remove_wait_queue(q, &wait); |
| 4958 | spin_unlock_irqrestore(&q->lock, flags); |
| 4959 | |
| 4960 | return timeout; |
| 4961 | } |
| 4962 | |
| 4963 | void __sched interruptible_sleep_on(wait_queue_head_t *q) |
| 4964 | { |
| 4965 | sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | EXPORT_SYMBOL(interruptible_sleep_on); |
| 4968 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4969 | long __sched |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4970 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4972 | return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
| 4975 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4976 | void __sched sleep_on(wait_queue_head_t *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4978 | sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | EXPORT_SYMBOL(sleep_on); |
| 4981 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4982 | long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4984 | return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | EXPORT_SYMBOL(sleep_on_timeout); |
| 4987 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4988 | #ifdef CONFIG_RT_MUTEXES |
| 4989 | |
| 4990 | /* |
| 4991 | * rt_mutex_setprio - set the current priority of a task |
| 4992 | * @p: task |
| 4993 | * @prio: prio value (kernel-internal form) |
| 4994 | * |
| 4995 | * This function changes the 'effective' priority of a task. It does |
| 4996 | * not touch ->normal_prio like __setscheduler(). |
| 4997 | * |
| 4998 | * Used by the rt_mutex code to implement priority inheritance logic. |
| 4999 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5000 | void rt_mutex_setprio(struct task_struct *p, int prio) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5001 | { |
| 5002 | unsigned long flags; |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5003 | int oldprio, on_rq, running; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5004 | struct rq *rq; |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5005 | const struct sched_class *prev_class = p->sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5006 | |
| 5007 | BUG_ON(prio < 0 || prio > MAX_PRIO); |
| 5008 | |
| 5009 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 5010 | update_rq_clock(rq); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5011 | |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 5012 | oldprio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5013 | on_rq = p->se.on_rq; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 5014 | running = task_current(rq, p); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 5015 | if (on_rq) |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5016 | dequeue_task(rq, p, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 5017 | if (running) |
| 5018 | p->sched_class->put_prev_task(rq, p); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5019 | |
| 5020 | if (rt_prio(prio)) |
| 5021 | p->sched_class = &rt_sched_class; |
| 5022 | else |
| 5023 | p->sched_class = &fair_sched_class; |
| 5024 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5025 | p->prio = prio; |
| 5026 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 5027 | if (running) |
| 5028 | p->sched_class->set_curr_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5029 | if (on_rq) { |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5030 | enqueue_task(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5031 | |
| 5032 | check_class_changed(rq, p, prev_class, oldprio, running); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5033 | } |
| 5034 | task_rq_unlock(rq, &flags); |
| 5035 | } |
| 5036 | |
| 5037 | #endif |
| 5038 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5039 | void set_user_nice(struct task_struct *p, long nice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5041 | int old_prio, delta, on_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5043 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5044 | |
| 5045 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) |
| 5046 | return; |
| 5047 | /* |
| 5048 | * We have to be careful, if called from sys_setpriority(), |
| 5049 | * the task might be in the middle of scheduling on another CPU. |
| 5050 | */ |
| 5051 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 5052 | update_rq_clock(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | /* |
| 5054 | * The RT priorities are set via sched_setscheduler(), but we still |
| 5055 | * allow the 'normal' nice value to be set - but as expected |
| 5056 | * it wont have any effect on scheduling until the task is |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5057 | * SCHED_FIFO/SCHED_RR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5058 | */ |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5059 | if (task_has_rt_policy(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | p->static_prio = NICE_TO_PRIO(nice); |
| 5061 | goto out_unlock; |
| 5062 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5063 | on_rq = p->se.on_rq; |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 5064 | if (on_rq) |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5065 | dequeue_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | p->static_prio = NICE_TO_PRIO(nice); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 5068 | set_load_weight(p); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5069 | old_prio = p->prio; |
| 5070 | p->prio = effective_prio(p); |
| 5071 | delta = p->prio - old_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5073 | if (on_rq) { |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5074 | enqueue_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | /* |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 5076 | * If the task increased its priority or is running and |
| 5077 | * lowered its priority, then reschedule its CPU: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | */ |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 5079 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | resched_task(rq->curr); |
| 5081 | } |
| 5082 | out_unlock: |
| 5083 | task_rq_unlock(rq, &flags); |
| 5084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | EXPORT_SYMBOL(set_user_nice); |
| 5086 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 5087 | /* |
| 5088 | * can_nice - check if a task can reduce its nice value |
| 5089 | * @p: task |
| 5090 | * @nice: nice value |
| 5091 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5092 | int can_nice(const struct task_struct *p, const int nice) |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 5093 | { |
Matt Mackall | 024f474 | 2005-08-18 11:24:19 -0700 | [diff] [blame] | 5094 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
| 5095 | int nice_rlim = 20 - nice; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5096 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 5097 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
| 5098 | capable(CAP_SYS_NICE)); |
| 5099 | } |
| 5100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | #ifdef __ARCH_WANT_SYS_NICE |
| 5102 | |
| 5103 | /* |
| 5104 | * sys_nice - change the priority of the current process. |
| 5105 | * @increment: priority increment |
| 5106 | * |
| 5107 | * sys_setpriority is a more generic, but much slower function that |
| 5108 | * does similar things. |
| 5109 | */ |
| 5110 | asmlinkage long sys_nice(int increment) |
| 5111 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5112 | long nice, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | |
| 5114 | /* |
| 5115 | * Setpriority might change our priority at the same moment. |
| 5116 | * We don't have to worry. Conceptually one call occurs first |
| 5117 | * and we have a single winner. |
| 5118 | */ |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 5119 | if (increment < -40) |
| 5120 | increment = -40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | if (increment > 40) |
| 5122 | increment = 40; |
| 5123 | |
| 5124 | nice = PRIO_TO_NICE(current->static_prio) + increment; |
| 5125 | if (nice < -20) |
| 5126 | nice = -20; |
| 5127 | if (nice > 19) |
| 5128 | nice = 19; |
| 5129 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 5130 | if (increment < 0 && !can_nice(current, nice)) |
| 5131 | return -EPERM; |
| 5132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | retval = security_task_setnice(current, nice); |
| 5134 | if (retval) |
| 5135 | return retval; |
| 5136 | |
| 5137 | set_user_nice(current, nice); |
| 5138 | return 0; |
| 5139 | } |
| 5140 | |
| 5141 | #endif |
| 5142 | |
| 5143 | /** |
| 5144 | * task_prio - return the priority value of a given task. |
| 5145 | * @p: the task in question. |
| 5146 | * |
| 5147 | * This is the priority value as seen by users in /proc. |
| 5148 | * RT tasks are offset by -200. Normal tasks are centered |
| 5149 | * around 0, value goes from -16 to +15. |
| 5150 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5151 | int task_prio(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | { |
| 5153 | return p->prio - MAX_RT_PRIO; |
| 5154 | } |
| 5155 | |
| 5156 | /** |
| 5157 | * task_nice - return the nice value of a given task. |
| 5158 | * @p: the task in question. |
| 5159 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5160 | int task_nice(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | { |
| 5162 | return TASK_NICE(p); |
| 5163 | } |
Pavel Roskin | 150d8be | 2008-03-05 16:56:37 -0500 | [diff] [blame] | 5164 | EXPORT_SYMBOL(task_nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5165 | |
| 5166 | /** |
| 5167 | * idle_cpu - is a given cpu idle currently? |
| 5168 | * @cpu: the processor in question. |
| 5169 | */ |
| 5170 | int idle_cpu(int cpu) |
| 5171 | { |
| 5172 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; |
| 5173 | } |
| 5174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | /** |
| 5176 | * idle_task - return the idle task for a given cpu. |
| 5177 | * @cpu: the processor in question. |
| 5178 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5179 | struct task_struct *idle_task(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | { |
| 5181 | return cpu_rq(cpu)->idle; |
| 5182 | } |
| 5183 | |
| 5184 | /** |
| 5185 | * find_process_by_pid - find a process with a matching PID value. |
| 5186 | * @pid: the pid in question. |
| 5187 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5188 | static struct task_struct *find_process_by_pid(pid_t pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | { |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 5190 | return pid ? find_task_by_vpid(pid) : current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | } |
| 5192 | |
| 5193 | /* Actually do priority change: must hold rq lock. */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5194 | static void |
| 5195 | __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5197 | BUG_ON(p->se.on_rq); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | p->policy = policy; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5200 | switch (p->policy) { |
| 5201 | case SCHED_NORMAL: |
| 5202 | case SCHED_BATCH: |
| 5203 | case SCHED_IDLE: |
| 5204 | p->sched_class = &fair_sched_class; |
| 5205 | break; |
| 5206 | case SCHED_FIFO: |
| 5207 | case SCHED_RR: |
| 5208 | p->sched_class = &rt_sched_class; |
| 5209 | break; |
| 5210 | } |
| 5211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | p->rt_priority = prio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5213 | p->normal_prio = normal_prio(p); |
| 5214 | /* we are holding p->pi_lock already */ |
| 5215 | p->prio = rt_mutex_getprio(p); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 5216 | set_load_weight(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 5220 | * 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] | 5221 | * @p: the task in question. |
| 5222 | * @policy: new policy. |
| 5223 | * @param: structure containing the new RT priority. |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 5224 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 5225 | * NOTE that the task may be already dead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5227 | int sched_setscheduler(struct task_struct *p, int policy, |
| 5228 | struct sched_param *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5230 | int retval, oldprio, oldpolicy = -1, on_rq, running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | unsigned long flags; |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5232 | const struct sched_class *prev_class = p->sched_class; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5233 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | |
Steven Rostedt | 66e5393 | 2006-06-27 02:54:44 -0700 | [diff] [blame] | 5235 | /* may grab non-irq protected spin_locks */ |
| 5236 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | recheck: |
| 5238 | /* double check policy once rq lock held */ |
| 5239 | if (policy < 0) |
| 5240 | policy = oldpolicy = p->policy; |
| 5241 | else if (policy != SCHED_FIFO && policy != SCHED_RR && |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5242 | policy != SCHED_NORMAL && policy != SCHED_BATCH && |
| 5243 | policy != SCHED_IDLE) |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5244 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | /* |
| 5246 | * Valid priorities for SCHED_FIFO and SCHED_RR are |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5247 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
| 5248 | * SCHED_BATCH and SCHED_IDLE is 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | */ |
| 5250 | if (param->sched_priority < 0 || |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5251 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
Steven Rostedt | d46523e | 2005-07-25 16:28:39 -0400 | [diff] [blame] | 5252 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | return -EINVAL; |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5254 | if (rt_policy(policy) != (param->sched_priority != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | return -EINVAL; |
| 5256 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 5257 | /* |
| 5258 | * Allow unprivileged RT tasks to decrease priority: |
| 5259 | */ |
| 5260 | if (!capable(CAP_SYS_NICE)) { |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5261 | if (rt_policy(policy)) { |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 5262 | unsigned long rlim_rtprio; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 5263 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 5264 | if (!lock_task_sighand(p, &flags)) |
| 5265 | return -ESRCH; |
| 5266 | rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur; |
| 5267 | unlock_task_sighand(p, &flags); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 5268 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 5269 | /* can't set/change the rt policy */ |
| 5270 | if (policy != p->policy && !rlim_rtprio) |
| 5271 | return -EPERM; |
| 5272 | |
| 5273 | /* can't increase priority */ |
| 5274 | if (param->sched_priority > p->rt_priority && |
| 5275 | param->sched_priority > rlim_rtprio) |
| 5276 | return -EPERM; |
| 5277 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5278 | /* |
| 5279 | * Like positive nice levels, dont allow tasks to |
| 5280 | * move out of SCHED_IDLE either: |
| 5281 | */ |
| 5282 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) |
| 5283 | return -EPERM; |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 5284 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 5285 | /* can't change other user's priorities */ |
| 5286 | if ((current->euid != p->euid) && |
| 5287 | (current->euid != p->uid)) |
| 5288 | return -EPERM; |
| 5289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 5291 | #ifdef CONFIG_RT_GROUP_SCHED |
| 5292 | /* |
| 5293 | * Do not allow realtime tasks into groups that have no runtime |
| 5294 | * assigned. |
| 5295 | */ |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 5296 | if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0) |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 5297 | return -EPERM; |
| 5298 | #endif |
| 5299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | retval = security_task_setscheduler(p, policy, param); |
| 5301 | if (retval) |
| 5302 | return retval; |
| 5303 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5304 | * make sure no PI-waiters arrive (or leave) while we are |
| 5305 | * changing the priority of the task: |
| 5306 | */ |
| 5307 | spin_lock_irqsave(&p->pi_lock, flags); |
| 5308 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | * To be able to change p->policy safely, the apropriate |
| 5310 | * runqueue lock must be held. |
| 5311 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5312 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | /* recheck policy now with rq lock held */ |
| 5314 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
| 5315 | policy = oldpolicy = -1; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5316 | __task_rq_unlock(rq); |
| 5317 | spin_unlock_irqrestore(&p->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | goto recheck; |
| 5319 | } |
Ingo Molnar | 2daa357 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5320 | update_rq_clock(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5321 | on_rq = p->se.on_rq; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 5322 | running = task_current(rq, p); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 5323 | if (on_rq) |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5324 | deactivate_task(rq, p, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 5325 | if (running) |
| 5326 | p->sched_class->put_prev_task(rq, p); |
Dmitry Adamushko | f6b5320 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | oldprio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5329 | __setscheduler(rq, p, policy, param->sched_priority); |
Dmitry Adamushko | f6b5320 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5330 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 5331 | if (running) |
| 5332 | p->sched_class->set_curr_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5333 | if (on_rq) { |
| 5334 | activate_task(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5335 | |
| 5336 | check_class_changed(rq, p, prev_class, oldprio, running); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5338 | __task_rq_unlock(rq); |
| 5339 | spin_unlock_irqrestore(&p->pi_lock, flags); |
| 5340 | |
Thomas Gleixner | 95e02ca | 2006-06-27 02:55:02 -0700 | [diff] [blame] | 5341 | rt_mutex_adjust_pi(p); |
| 5342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | return 0; |
| 5344 | } |
| 5345 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
| 5346 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5347 | static int |
| 5348 | 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] | 5349 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | struct sched_param lparam; |
| 5351 | struct task_struct *p; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5352 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | |
| 5354 | if (!param || pid < 0) |
| 5355 | return -EINVAL; |
| 5356 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) |
| 5357 | return -EFAULT; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 5358 | |
| 5359 | rcu_read_lock(); |
| 5360 | retval = -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | p = find_process_by_pid(pid); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 5362 | if (p != NULL) |
| 5363 | retval = sched_setscheduler(p, policy, &lparam); |
| 5364 | rcu_read_unlock(); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | return retval; |
| 5367 | } |
| 5368 | |
| 5369 | /** |
| 5370 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority |
| 5371 | * @pid: the pid in question. |
| 5372 | * @policy: new policy. |
| 5373 | * @param: structure containing the new RT priority. |
| 5374 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5375 | asmlinkage long |
| 5376 | 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] | 5377 | { |
Jason Baron | c21761f | 2006-01-18 17:43:03 -0800 | [diff] [blame] | 5378 | /* negative values for policy are not valid */ |
| 5379 | if (policy < 0) |
| 5380 | return -EINVAL; |
| 5381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | return do_sched_setscheduler(pid, policy, param); |
| 5383 | } |
| 5384 | |
| 5385 | /** |
| 5386 | * sys_sched_setparam - set/change the RT priority of a thread |
| 5387 | * @pid: the pid in question. |
| 5388 | * @param: structure containing the new RT priority. |
| 5389 | */ |
| 5390 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) |
| 5391 | { |
| 5392 | return do_sched_setscheduler(pid, -1, param); |
| 5393 | } |
| 5394 | |
| 5395 | /** |
| 5396 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread |
| 5397 | * @pid: the pid in question. |
| 5398 | */ |
| 5399 | asmlinkage long sys_sched_getscheduler(pid_t pid) |
| 5400 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5401 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5402 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5403 | |
| 5404 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5405 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | |
| 5407 | retval = -ESRCH; |
| 5408 | read_lock(&tasklist_lock); |
| 5409 | p = find_process_by_pid(pid); |
| 5410 | if (p) { |
| 5411 | retval = security_task_getscheduler(p); |
| 5412 | if (!retval) |
| 5413 | retval = p->policy; |
| 5414 | } |
| 5415 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | return retval; |
| 5417 | } |
| 5418 | |
| 5419 | /** |
| 5420 | * sys_sched_getscheduler - get the RT priority of a thread |
| 5421 | * @pid: the pid in question. |
| 5422 | * @param: structure containing the RT priority. |
| 5423 | */ |
| 5424 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) |
| 5425 | { |
| 5426 | struct sched_param lp; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5427 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5428 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | |
| 5430 | if (!param || pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5431 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | |
| 5433 | read_lock(&tasklist_lock); |
| 5434 | p = find_process_by_pid(pid); |
| 5435 | retval = -ESRCH; |
| 5436 | if (!p) |
| 5437 | goto out_unlock; |
| 5438 | |
| 5439 | retval = security_task_getscheduler(p); |
| 5440 | if (retval) |
| 5441 | goto out_unlock; |
| 5442 | |
| 5443 | lp.sched_priority = p->rt_priority; |
| 5444 | read_unlock(&tasklist_lock); |
| 5445 | |
| 5446 | /* |
| 5447 | * This one might sleep, we cannot do it with a spinlock held ... |
| 5448 | */ |
| 5449 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; |
| 5450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | return retval; |
| 5452 | |
| 5453 | out_unlock: |
| 5454 | read_unlock(&tasklist_lock); |
| 5455 | return retval; |
| 5456 | } |
| 5457 | |
Mike Travis | b53e921 | 2008-04-04 18:11:08 -0700 | [diff] [blame] | 5458 | long sched_setaffinity(pid_t pid, const cpumask_t *in_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | cpumask_t cpus_allowed; |
Mike Travis | b53e921 | 2008-04-04 18:11:08 -0700 | [diff] [blame] | 5461 | cpumask_t new_mask = *in_mask; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5462 | struct task_struct *p; |
| 5463 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5465 | get_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | read_lock(&tasklist_lock); |
| 5467 | |
| 5468 | p = find_process_by_pid(pid); |
| 5469 | if (!p) { |
| 5470 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5471 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | return -ESRCH; |
| 5473 | } |
| 5474 | |
| 5475 | /* |
| 5476 | * It is not safe to call set_cpus_allowed with the |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5477 | * tasklist_lock held. We will bump the task_struct's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | * usage count and then drop tasklist_lock. |
| 5479 | */ |
| 5480 | get_task_struct(p); |
| 5481 | read_unlock(&tasklist_lock); |
| 5482 | |
| 5483 | retval = -EPERM; |
| 5484 | if ((current->euid != p->euid) && (current->euid != p->uid) && |
| 5485 | !capable(CAP_SYS_NICE)) |
| 5486 | goto out_unlock; |
| 5487 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 5488 | retval = security_task_setscheduler(p, 0, NULL); |
| 5489 | if (retval) |
| 5490 | goto out_unlock; |
| 5491 | |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 5492 | cpuset_cpus_allowed(p, &cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | cpus_and(new_mask, new_mask, cpus_allowed); |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 5494 | again: |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 5495 | retval = set_cpus_allowed_ptr(p, &new_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 5497 | if (!retval) { |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 5498 | cpuset_cpus_allowed(p, &cpus_allowed); |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 5499 | if (!cpus_subset(new_mask, cpus_allowed)) { |
| 5500 | /* |
| 5501 | * We must have raced with a concurrent cpuset |
| 5502 | * update. Just reset the cpus_allowed to the |
| 5503 | * cpuset's cpus_allowed |
| 5504 | */ |
| 5505 | new_mask = cpus_allowed; |
| 5506 | goto again; |
| 5507 | } |
| 5508 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | out_unlock: |
| 5510 | put_task_struct(p); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5511 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5512 | return retval; |
| 5513 | } |
| 5514 | |
| 5515 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, |
| 5516 | cpumask_t *new_mask) |
| 5517 | { |
| 5518 | if (len < sizeof(cpumask_t)) { |
| 5519 | memset(new_mask, 0, sizeof(cpumask_t)); |
| 5520 | } else if (len > sizeof(cpumask_t)) { |
| 5521 | len = sizeof(cpumask_t); |
| 5522 | } |
| 5523 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
| 5524 | } |
| 5525 | |
| 5526 | /** |
| 5527 | * sys_sched_setaffinity - set the cpu affinity of a process |
| 5528 | * @pid: pid of the process |
| 5529 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 5530 | * @user_mask_ptr: user-space pointer to the new cpu mask |
| 5531 | */ |
| 5532 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, |
| 5533 | unsigned long __user *user_mask_ptr) |
| 5534 | { |
| 5535 | cpumask_t new_mask; |
| 5536 | int retval; |
| 5537 | |
| 5538 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); |
| 5539 | if (retval) |
| 5540 | return retval; |
| 5541 | |
Mike Travis | b53e921 | 2008-04-04 18:11:08 -0700 | [diff] [blame] | 5542 | return sched_setaffinity(pid, &new_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | } |
| 5544 | |
| 5545 | /* |
| 5546 | * Represents all cpu's present in the system |
| 5547 | * In systems capable of hotplug, this map could dynamically grow |
| 5548 | * as new cpu's are detected in the system via any platform specific |
| 5549 | * method, such as ACPI for e.g. |
| 5550 | */ |
| 5551 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 5552 | cpumask_t cpu_present_map __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | EXPORT_SYMBOL(cpu_present_map); |
| 5554 | |
| 5555 | #ifndef CONFIG_SMP |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 5556 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 5557 | EXPORT_SYMBOL(cpu_online_map); |
| 5558 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 5559 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 5560 | EXPORT_SYMBOL(cpu_possible_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | #endif |
| 5562 | |
| 5563 | long sched_getaffinity(pid_t pid, cpumask_t *mask) |
| 5564 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5565 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5568 | get_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | read_lock(&tasklist_lock); |
| 5570 | |
| 5571 | retval = -ESRCH; |
| 5572 | p = find_process_by_pid(pid); |
| 5573 | if (!p) |
| 5574 | goto out_unlock; |
| 5575 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 5576 | retval = security_task_getscheduler(p); |
| 5577 | if (retval) |
| 5578 | goto out_unlock; |
| 5579 | |
Jack Steiner | 2f7016d | 2006-02-01 03:05:18 -0800 | [diff] [blame] | 5580 | cpus_and(*mask, p->cpus_allowed, cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5581 | |
| 5582 | out_unlock: |
| 5583 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5584 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | |
Ulrich Drepper | 9531b62 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5586 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | } |
| 5588 | |
| 5589 | /** |
| 5590 | * sys_sched_getaffinity - get the cpu affinity of a process |
| 5591 | * @pid: pid of the process |
| 5592 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 5593 | * @user_mask_ptr: user-space pointer to hold the current cpu mask |
| 5594 | */ |
| 5595 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, |
| 5596 | unsigned long __user *user_mask_ptr) |
| 5597 | { |
| 5598 | int ret; |
| 5599 | cpumask_t mask; |
| 5600 | |
| 5601 | if (len < sizeof(cpumask_t)) |
| 5602 | return -EINVAL; |
| 5603 | |
| 5604 | ret = sched_getaffinity(pid, &mask); |
| 5605 | if (ret < 0) |
| 5606 | return ret; |
| 5607 | |
| 5608 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) |
| 5609 | return -EFAULT; |
| 5610 | |
| 5611 | return sizeof(cpumask_t); |
| 5612 | } |
| 5613 | |
| 5614 | /** |
| 5615 | * sys_sched_yield - yield the current processor to other threads. |
| 5616 | * |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5617 | * This function yields the current CPU to other tasks. If there are no |
| 5618 | * other threads running on this CPU then this function will return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | */ |
| 5620 | asmlinkage long sys_sched_yield(void) |
| 5621 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5622 | struct rq *rq = this_rq_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 5624 | schedstat_inc(rq, yld_count); |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5625 | current->sched_class->yield_task(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | |
| 5627 | /* |
| 5628 | * Since we are going to call schedule() anyway, there's |
| 5629 | * no need to preempt or enable interrupts: |
| 5630 | */ |
| 5631 | __release(rq->lock); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 5632 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | _raw_spin_unlock(&rq->lock); |
| 5634 | preempt_enable_no_resched(); |
| 5635 | |
| 5636 | schedule(); |
| 5637 | |
| 5638 | return 0; |
| 5639 | } |
| 5640 | |
Andrew Morton | e7b3840 | 2006-06-30 01:56:00 -0700 | [diff] [blame] | 5641 | static void __cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | { |
Ingo Molnar | 8e0a43d | 2006-06-23 02:05:23 -0700 | [diff] [blame] | 5643 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 5644 | __might_sleep(__FILE__, __LINE__); |
| 5645 | #endif |
Ingo Molnar | 5bbcfd9 | 2005-07-07 17:57:04 -0700 | [diff] [blame] | 5646 | /* |
| 5647 | * The BKS might be reacquired before we have dropped |
| 5648 | * PREEMPT_ACTIVE, which could trigger a second |
| 5649 | * cond_resched() call. |
| 5650 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | do { |
| 5652 | add_preempt_count(PREEMPT_ACTIVE); |
| 5653 | schedule(); |
| 5654 | sub_preempt_count(PREEMPT_ACTIVE); |
| 5655 | } while (need_resched()); |
| 5656 | } |
| 5657 | |
Herbert Xu | 02b67cc3 | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 5658 | #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY) |
| 5659 | int __sched _cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | { |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 5661 | if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) && |
| 5662 | system_state == SYSTEM_RUNNING) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | __cond_resched(); |
| 5664 | return 1; |
| 5665 | } |
| 5666 | return 0; |
| 5667 | } |
Herbert Xu | 02b67cc3 | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 5668 | EXPORT_SYMBOL(_cond_resched); |
| 5669 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | |
| 5671 | /* |
| 5672 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, |
| 5673 | * call schedule, and on return reacquire the lock. |
| 5674 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5675 | * 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] | 5676 | * operations here to prevent schedule() from being called twice (once via |
| 5677 | * spin_unlock(), once by hand). |
| 5678 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5679 | int cond_resched_lock(spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | { |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5681 | int resched = need_resched() && system_state == SYSTEM_RUNNING; |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5682 | int ret = 0; |
| 5683 | |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5684 | if (spin_needbreak(lock) || resched) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | spin_unlock(lock); |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5686 | if (resched && need_resched()) |
| 5687 | __cond_resched(); |
| 5688 | else |
| 5689 | cpu_relax(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5690 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | spin_lock(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | } |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5693 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | EXPORT_SYMBOL(cond_resched_lock); |
| 5696 | |
| 5697 | int __sched cond_resched_softirq(void) |
| 5698 | { |
| 5699 | BUG_ON(!in_softirq()); |
| 5700 | |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 5701 | if (need_resched() && system_state == SYSTEM_RUNNING) { |
Thomas Gleixner | 98d82567 | 2007-05-23 13:58:18 -0700 | [diff] [blame] | 5702 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5703 | __cond_resched(); |
| 5704 | local_bh_disable(); |
| 5705 | return 1; |
| 5706 | } |
| 5707 | return 0; |
| 5708 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | EXPORT_SYMBOL(cond_resched_softirq); |
| 5710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | /** |
| 5712 | * yield - yield the current processor to other threads. |
| 5713 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 5714 | * This is a shortcut for kernel-space yielding - it marks the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | * thread runnable and calls sys_sched_yield(). |
| 5716 | */ |
| 5717 | void __sched yield(void) |
| 5718 | { |
| 5719 | set_current_state(TASK_RUNNING); |
| 5720 | sys_sched_yield(); |
| 5721 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | EXPORT_SYMBOL(yield); |
| 5723 | |
| 5724 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5725 | * 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] | 5726 | * that process accounting knows that this is a task in IO wait state. |
| 5727 | * |
| 5728 | * But don't do that if it is a deliberate, throttling IO wait (this task |
| 5729 | * has set its backing_dev_info: the queue against which it should throttle) |
| 5730 | */ |
| 5731 | void __sched io_schedule(void) |
| 5732 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5733 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5735 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | atomic_inc(&rq->nr_iowait); |
| 5737 | schedule(); |
| 5738 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5739 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5741 | EXPORT_SYMBOL(io_schedule); |
| 5742 | |
| 5743 | long __sched io_schedule_timeout(long timeout) |
| 5744 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5745 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | long ret; |
| 5747 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5748 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | atomic_inc(&rq->nr_iowait); |
| 5750 | ret = schedule_timeout(timeout); |
| 5751 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5752 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | return ret; |
| 5754 | } |
| 5755 | |
| 5756 | /** |
| 5757 | * sys_sched_get_priority_max - return maximum RT priority. |
| 5758 | * @policy: scheduling class. |
| 5759 | * |
| 5760 | * this syscall returns the maximum rt_priority that can be used |
| 5761 | * by a given scheduling class. |
| 5762 | */ |
| 5763 | asmlinkage long sys_sched_get_priority_max(int policy) |
| 5764 | { |
| 5765 | int ret = -EINVAL; |
| 5766 | |
| 5767 | switch (policy) { |
| 5768 | case SCHED_FIFO: |
| 5769 | case SCHED_RR: |
| 5770 | ret = MAX_USER_RT_PRIO-1; |
| 5771 | break; |
| 5772 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5773 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5774 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | ret = 0; |
| 5776 | break; |
| 5777 | } |
| 5778 | return ret; |
| 5779 | } |
| 5780 | |
| 5781 | /** |
| 5782 | * sys_sched_get_priority_min - return minimum RT priority. |
| 5783 | * @policy: scheduling class. |
| 5784 | * |
| 5785 | * this syscall returns the minimum rt_priority that can be used |
| 5786 | * by a given scheduling class. |
| 5787 | */ |
| 5788 | asmlinkage long sys_sched_get_priority_min(int policy) |
| 5789 | { |
| 5790 | int ret = -EINVAL; |
| 5791 | |
| 5792 | switch (policy) { |
| 5793 | case SCHED_FIFO: |
| 5794 | case SCHED_RR: |
| 5795 | ret = 1; |
| 5796 | break; |
| 5797 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5798 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5799 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | ret = 0; |
| 5801 | } |
| 5802 | return ret; |
| 5803 | } |
| 5804 | |
| 5805 | /** |
| 5806 | * sys_sched_rr_get_interval - return the default timeslice of a process. |
| 5807 | * @pid: pid of the process. |
| 5808 | * @interval: userspace pointer to the timeslice value. |
| 5809 | * |
| 5810 | * this syscall writes the default timeslice value of a given process |
| 5811 | * into the user-space timespec buffer. A value of '0' means infinity. |
| 5812 | */ |
| 5813 | asmlinkage |
| 5814 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) |
| 5815 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5816 | struct task_struct *p; |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5817 | unsigned int time_slice; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5818 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | struct timespec t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | |
| 5821 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5822 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | |
| 5824 | retval = -ESRCH; |
| 5825 | read_lock(&tasklist_lock); |
| 5826 | p = find_process_by_pid(pid); |
| 5827 | if (!p) |
| 5828 | goto out_unlock; |
| 5829 | |
| 5830 | retval = security_task_getscheduler(p); |
| 5831 | if (retval) |
| 5832 | goto out_unlock; |
| 5833 | |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5834 | /* |
| 5835 | * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER |
| 5836 | * tasks that are on an otherwise idle runqueue: |
| 5837 | */ |
| 5838 | time_slice = 0; |
| 5839 | if (p->policy == SCHED_RR) { |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5840 | time_slice = DEF_TIMESLICE; |
Miao Xie | 1868f95 | 2008-03-07 09:35:06 +0800 | [diff] [blame] | 5841 | } else if (p->policy != SCHED_FIFO) { |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5842 | struct sched_entity *se = &p->se; |
| 5843 | unsigned long flags; |
| 5844 | struct rq *rq; |
| 5845 | |
| 5846 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5847 | if (rq->cfs.load.weight) |
| 5848 | time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5849 | task_rq_unlock(rq, &flags); |
| 5850 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | read_unlock(&tasklist_lock); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5852 | jiffies_to_timespec(time_slice, &t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5853 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | return retval; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | out_unlock: |
| 5857 | read_unlock(&tasklist_lock); |
| 5858 | return retval; |
| 5859 | } |
| 5860 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5861 | static const char stat_nam[] = "RSDTtZX"; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5862 | |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5863 | void sched_show_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | unsigned long free = 0; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5866 | unsigned state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | state = p->state ? __ffs(p->state) + 1 : 0; |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5869 | printk(KERN_INFO "%-13.13s %c", p->comm, |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5870 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5871 | #if BITS_PER_LONG == 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | if (state == TASK_RUNNING) |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5873 | printk(KERN_CONT " running "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | else |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5875 | printk(KERN_CONT " %08lx ", thread_saved_pc(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | #else |
| 5877 | if (state == TASK_RUNNING) |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5878 | printk(KERN_CONT " running task "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | else |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5880 | printk(KERN_CONT " %016lx ", thread_saved_pc(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | #endif |
| 5882 | #ifdef CONFIG_DEBUG_STACK_USAGE |
| 5883 | { |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 5884 | unsigned long *n = end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | while (!*n) |
| 5886 | n++; |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 5887 | free = (unsigned long)n - (unsigned long)end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | } |
| 5889 | #endif |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5890 | printk(KERN_CONT "%5lu %5d %6d\n", free, |
Roland McGrath | fcfd50a | 2008-01-09 00:03:23 -0800 | [diff] [blame] | 5891 | task_pid_nr(p), task_pid_nr(p->real_parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | |
Nick Piggin | 5fb5e6d | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 5893 | show_stack(p, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5894 | } |
| 5895 | |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 5896 | void show_state_filter(unsigned long state_filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5898 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5900 | #if BITS_PER_LONG == 32 |
| 5901 | printk(KERN_INFO |
| 5902 | " task PC stack pid father\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | #else |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5904 | printk(KERN_INFO |
| 5905 | " task PC stack pid father\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | #endif |
| 5907 | read_lock(&tasklist_lock); |
| 5908 | do_each_thread(g, p) { |
| 5909 | /* |
| 5910 | * reset the NMI-timeout, listing all files on a slow |
| 5911 | * console might take alot of time: |
| 5912 | */ |
| 5913 | touch_nmi_watchdog(); |
Ingo Molnar | 39bc89f | 2007-04-25 20:50:03 -0700 | [diff] [blame] | 5914 | if (!state_filter || (p->state & state_filter)) |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5915 | sched_show_task(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | } while_each_thread(g, p); |
| 5917 | |
Jeremy Fitzhardinge | 04c9167 | 2007-05-08 00:28:05 -0700 | [diff] [blame] | 5918 | touch_all_softlockup_watchdogs(); |
| 5919 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5920 | #ifdef CONFIG_SCHED_DEBUG |
| 5921 | sysrq_sched_debug_show(); |
| 5922 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | read_unlock(&tasklist_lock); |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 5924 | /* |
| 5925 | * Only show locks if all tasks are dumped: |
| 5926 | */ |
| 5927 | if (state_filter == -1) |
| 5928 | debug_show_all_locks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | } |
| 5930 | |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5931 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) |
| 5932 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5933 | idle->sched_class = &idle_sched_class; |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5934 | } |
| 5935 | |
Ingo Molnar | f340c0d | 2005-06-28 16:40:42 +0200 | [diff] [blame] | 5936 | /** |
| 5937 | * init_idle - set up an idle thread for a given CPU |
| 5938 | * @idle: task in question |
| 5939 | * @cpu: cpu the idle task belongs to |
| 5940 | * |
| 5941 | * NOTE: this function does not set the idle thread's NEED_RESCHED |
| 5942 | * flag, to make booting more robust. |
| 5943 | */ |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 5944 | void __cpuinit init_idle(struct task_struct *idle, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5946 | struct rq *rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | unsigned long flags; |
| 5948 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5949 | __sched_fork(idle); |
| 5950 | idle->se.exec_start = sched_clock(); |
| 5951 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5952 | idle->prio = idle->normal_prio = MAX_PRIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | idle->cpus_allowed = cpumask_of_cpu(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5954 | __set_task_cpu(idle, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | |
| 5956 | spin_lock_irqsave(&rq->lock, flags); |
| 5957 | rq->curr = rq->idle = idle; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 5958 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
| 5959 | idle->oncpu = 1; |
| 5960 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5962 | |
| 5963 | /* Set the preempt count _outside_ the spinlocks! */ |
Al Viro | a1261f5 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 5964 | task_thread_info(idle)->preempt_count = 0; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 5965 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5966 | /* |
| 5967 | * The idle tasks have their own, simple scheduling class: |
| 5968 | */ |
| 5969 | idle->sched_class = &idle_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | } |
| 5971 | |
| 5972 | /* |
| 5973 | * In a system that switches off the HZ timer nohz_cpu_mask |
| 5974 | * indicates which cpus entered this state. This is used |
| 5975 | * in the rcu update to wait only for active cpus. For system |
| 5976 | * which do not switch off the HZ timer nohz_cpu_mask should |
| 5977 | * always be CPU_MASK_NONE. |
| 5978 | */ |
| 5979 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; |
| 5980 | |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 5981 | /* |
| 5982 | * Increase the granularity value when there are more CPUs, |
| 5983 | * because with more CPUs the 'effective latency' as visible |
| 5984 | * to users decreases. But the relationship is not linear, |
| 5985 | * so pick a second-best guess by going with the log2 of the |
| 5986 | * number of CPUs. |
| 5987 | * |
| 5988 | * This idea comes from the SD scheduler of Con Kolivas: |
| 5989 | */ |
| 5990 | static inline void sched_init_granularity(void) |
| 5991 | { |
| 5992 | unsigned int factor = 1 + ilog2(num_online_cpus()); |
| 5993 | const unsigned long limit = 200000000; |
| 5994 | |
| 5995 | sysctl_sched_min_granularity *= factor; |
| 5996 | if (sysctl_sched_min_granularity > limit) |
| 5997 | sysctl_sched_min_granularity = limit; |
| 5998 | |
| 5999 | sysctl_sched_latency *= factor; |
| 6000 | if (sysctl_sched_latency > limit) |
| 6001 | sysctl_sched_latency = limit; |
| 6002 | |
| 6003 | sysctl_sched_wakeup_granularity *= factor; |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 6004 | } |
| 6005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | #ifdef CONFIG_SMP |
| 6007 | /* |
| 6008 | * This is how migration works: |
| 6009 | * |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6010 | * 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] | 6011 | * runqueue and wake up that CPU's migration thread. |
| 6012 | * 2) we down() the locked semaphore => thread blocks. |
| 6013 | * 3) migration thread wakes up (implicitly it forces the migrated |
| 6014 | * thread off the CPU) |
| 6015 | * 4) it gets the migration request and checks whether the migrated |
| 6016 | * task is still in the wrong runqueue. |
| 6017 | * 5) if it's in the wrong runqueue then the migration thread removes |
| 6018 | * it and puts it into the right queue. |
| 6019 | * 6) migration thread up()s the semaphore. |
| 6020 | * 7) we wake up and the migration is done. |
| 6021 | */ |
| 6022 | |
| 6023 | /* |
| 6024 | * Change a given task's CPU affinity. Migrate the thread to a |
| 6025 | * proper CPU and schedule it away if the CPU it's executing on |
| 6026 | * is removed from the allowed bitmask. |
| 6027 | * |
| 6028 | * NOTE: the caller must have a valid reference to the task, the |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6029 | * task must not exit() & deallocate itself prematurely. The |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | * call is not atomic; no spinlocks may be held. |
| 6031 | */ |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 6032 | 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] | 6033 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6034 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6035 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6036 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6037 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | |
| 6039 | rq = task_rq_lock(p, &flags); |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 6040 | if (!cpus_intersects(*new_mask, cpu_online_map)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6041 | ret = -EINVAL; |
| 6042 | goto out; |
| 6043 | } |
| 6044 | |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 6045 | if (p->sched_class->set_cpus_allowed) |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 6046 | p->sched_class->set_cpus_allowed(p, new_mask); |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 6047 | else { |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 6048 | p->cpus_allowed = *new_mask; |
| 6049 | p->rt.nr_cpus_allowed = cpus_weight(*new_mask); |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 6050 | } |
| 6051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | /* 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] | 6053 | if (cpu_isset(task_cpu(p), *new_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6054 | goto out; |
| 6055 | |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 6056 | if (migrate_task(p, any_online_cpu(*new_mask), &req)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | /* Need help from migration thread: drop lock and wait. */ |
| 6058 | task_rq_unlock(rq, &flags); |
| 6059 | wake_up_process(rq->migration_thread); |
| 6060 | wait_for_completion(&req.done); |
| 6061 | tlb_migrate_finish(p->mm); |
| 6062 | return 0; |
| 6063 | } |
| 6064 | out: |
| 6065 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | return ret; |
| 6068 | } |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 6069 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | |
| 6071 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6072 | * 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] | 6073 | * this because either it can't run here any more (set_cpus_allowed() |
| 6074 | * away from this CPU, or CPU going down), or because we're |
| 6075 | * attempting to rebalance this task on exec (sched_exec). |
| 6076 | * |
| 6077 | * So we race with normal scheduler movements, but that's OK, as long |
| 6078 | * as the task is no longer on this CPU. |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 6079 | * |
| 6080 | * Returns non-zero if task was successfully migrated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | */ |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 6082 | 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] | 6083 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6084 | struct rq *rq_dest, *rq_src; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6085 | int ret = 0, on_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | |
| 6087 | if (unlikely(cpu_is_offline(dest_cpu))) |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 6088 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6089 | |
| 6090 | rq_src = cpu_rq(src_cpu); |
| 6091 | rq_dest = cpu_rq(dest_cpu); |
| 6092 | |
| 6093 | double_rq_lock(rq_src, rq_dest); |
| 6094 | /* Already moved. */ |
| 6095 | if (task_cpu(p) != src_cpu) |
| 6096 | goto out; |
| 6097 | /* Affinity changed (again). */ |
| 6098 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) |
| 6099 | goto out; |
| 6100 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6101 | on_rq = p->se.on_rq; |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 6102 | if (on_rq) |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 6103 | deactivate_task(rq_src, p, 0); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 6104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | set_task_cpu(p, dest_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6106 | if (on_rq) { |
| 6107 | activate_task(rq_dest, p, 0); |
| 6108 | check_preempt_curr(rq_dest, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | } |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 6110 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | out: |
| 6112 | double_rq_unlock(rq_src, rq_dest); |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 6113 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | } |
| 6115 | |
| 6116 | /* |
| 6117 | * migration_thread - this is a highprio system thread that performs |
| 6118 | * thread migration by bumping thread off CPU then 'pushing' onto |
| 6119 | * another runqueue. |
| 6120 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 6121 | static int migration_thread(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6122 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | int cpu = (long)data; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6124 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | |
| 6126 | rq = cpu_rq(cpu); |
| 6127 | BUG_ON(rq->migration_thread != current); |
| 6128 | |
| 6129 | set_current_state(TASK_INTERRUPTIBLE); |
| 6130 | while (!kthread_should_stop()) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6131 | struct migration_req *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6134 | spin_lock_irq(&rq->lock); |
| 6135 | |
| 6136 | if (cpu_is_offline(cpu)) { |
| 6137 | spin_unlock_irq(&rq->lock); |
| 6138 | goto wait_to_die; |
| 6139 | } |
| 6140 | |
| 6141 | if (rq->active_balance) { |
| 6142 | active_load_balance(rq, cpu); |
| 6143 | rq->active_balance = 0; |
| 6144 | } |
| 6145 | |
| 6146 | head = &rq->migration_queue; |
| 6147 | |
| 6148 | if (list_empty(head)) { |
| 6149 | spin_unlock_irq(&rq->lock); |
| 6150 | schedule(); |
| 6151 | set_current_state(TASK_INTERRUPTIBLE); |
| 6152 | continue; |
| 6153 | } |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6154 | req = list_entry(head->next, struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6155 | list_del_init(head->next); |
| 6156 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 6157 | spin_unlock(&rq->lock); |
| 6158 | __migrate_task(req->task, cpu, req->dest_cpu); |
| 6159 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | |
| 6161 | complete(&req->done); |
| 6162 | } |
| 6163 | __set_current_state(TASK_RUNNING); |
| 6164 | return 0; |
| 6165 | |
| 6166 | wait_to_die: |
| 6167 | /* Wait for kthread_stop */ |
| 6168 | set_current_state(TASK_INTERRUPTIBLE); |
| 6169 | while (!kthread_should_stop()) { |
| 6170 | schedule(); |
| 6171 | set_current_state(TASK_INTERRUPTIBLE); |
| 6172 | } |
| 6173 | __set_current_state(TASK_RUNNING); |
| 6174 | return 0; |
| 6175 | } |
| 6176 | |
| 6177 | #ifdef CONFIG_HOTPLUG_CPU |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6178 | |
| 6179 | static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu) |
| 6180 | { |
| 6181 | int ret; |
| 6182 | |
| 6183 | local_irq_disable(); |
| 6184 | ret = __migrate_task(p, src_cpu, dest_cpu); |
| 6185 | local_irq_enable(); |
| 6186 | return ret; |
| 6187 | } |
| 6188 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 6189 | /* |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 6190 | * 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] | 6191 | * NOTE: interrupts should be disabled by the caller |
| 6192 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6193 | 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] | 6194 | { |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 6195 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | cpumask_t mask; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6197 | struct rq *rq; |
| 6198 | int dest_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6199 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6200 | do { |
| 6201 | /* On same node? */ |
| 6202 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); |
| 6203 | cpus_and(mask, mask, p->cpus_allowed); |
| 6204 | dest_cpu = any_online_cpu(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6206 | /* On any allowed CPU? */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6207 | if (dest_cpu >= nr_cpu_ids) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6208 | dest_cpu = any_online_cpu(p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6209 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6210 | /* No more Mr. Nice Guy. */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6211 | if (dest_cpu >= nr_cpu_ids) { |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 6212 | cpumask_t cpus_allowed; |
| 6213 | |
| 6214 | cpuset_cpus_allowed_locked(p, &cpus_allowed); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6215 | /* |
| 6216 | * Try to stay on the same cpuset, where the |
| 6217 | * current cpuset may be a subset of all cpus. |
| 6218 | * The cpuset_cpus_allowed_locked() variant of |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6219 | * cpuset_cpus_allowed() will not block. It must be |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6220 | * called within calls to cpuset_lock/cpuset_unlock. |
| 6221 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6222 | rq = task_rq_lock(p, &flags); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6223 | p->cpus_allowed = cpus_allowed; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6224 | dest_cpu = any_online_cpu(p->cpus_allowed); |
| 6225 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6227 | /* |
| 6228 | * Don't tell them about moving exiting tasks or |
| 6229 | * kernel threads (both mm NULL), since they never |
| 6230 | * leave kernel. |
| 6231 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6232 | if (p->mm && printk_ratelimit()) { |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6233 | printk(KERN_INFO "process %d (%s) no " |
| 6234 | "longer affine to cpu%d\n", |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6235 | task_pid_nr(p), p->comm, dead_cpu); |
| 6236 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6237 | } |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6238 | } while (!__migrate_task_irq(p, dead_cpu, dest_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6239 | } |
| 6240 | |
| 6241 | /* |
| 6242 | * While a dead CPU has no uninterruptible tasks queued at this point, |
| 6243 | * it might still have a nonzero ->nr_uninterruptible counter, because |
| 6244 | * for performance reasons the counter is not stricly tracking tasks to |
| 6245 | * their home CPUs. So we just add the counter to another CPU's counter, |
| 6246 | * to keep the global sum constant after CPU-down: |
| 6247 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6248 | static void migrate_nr_uninterruptible(struct rq *rq_src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6250 | 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] | 6251 | unsigned long flags; |
| 6252 | |
| 6253 | local_irq_save(flags); |
| 6254 | double_rq_lock(rq_src, rq_dest); |
| 6255 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; |
| 6256 | rq_src->nr_uninterruptible = 0; |
| 6257 | double_rq_unlock(rq_src, rq_dest); |
| 6258 | local_irq_restore(flags); |
| 6259 | } |
| 6260 | |
| 6261 | /* Run through task list and migrate tasks from the dead cpu. */ |
| 6262 | static void migrate_live_tasks(int src_cpu) |
| 6263 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6264 | struct task_struct *p, *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6265 | |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6266 | read_lock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6268 | do_each_thread(t, p) { |
| 6269 | if (p == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6270 | continue; |
| 6271 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6272 | if (task_cpu(p) == src_cpu) |
| 6273 | move_task_off_dead_cpu(src_cpu, p); |
| 6274 | } while_each_thread(t, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6275 | |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6276 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6277 | } |
| 6278 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6279 | /* |
| 6280 | * Schedules idle task to be the next runnable task on current CPU. |
Dmitry Adamushko | 94bc9a7 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 6281 | * It does so by boosting its priority to highest possible. |
| 6282 | * Used by CPU offline code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | */ |
| 6284 | void sched_idle_next(void) |
| 6285 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6286 | int this_cpu = smp_processor_id(); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6287 | struct rq *rq = cpu_rq(this_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6288 | struct task_struct *p = rq->idle; |
| 6289 | unsigned long flags; |
| 6290 | |
| 6291 | /* cpu has to be offline */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6292 | BUG_ON(cpu_online(this_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6294 | /* |
| 6295 | * Strictly not necessary since rest of the CPUs are stopped by now |
| 6296 | * and interrupts disabled on the current cpu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | */ |
| 6298 | spin_lock_irqsave(&rq->lock, flags); |
| 6299 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6300 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6301 | |
Dmitry Adamushko | 94bc9a7 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 6302 | update_rq_clock(rq); |
| 6303 | activate_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | |
| 6305 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6306 | } |
| 6307 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6308 | /* |
| 6309 | * 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] | 6310 | * offline. |
| 6311 | */ |
| 6312 | void idle_task_exit(void) |
| 6313 | { |
| 6314 | struct mm_struct *mm = current->active_mm; |
| 6315 | |
| 6316 | BUG_ON(cpu_online(smp_processor_id())); |
| 6317 | |
| 6318 | if (mm != &init_mm) |
| 6319 | switch_mm(mm, &init_mm, current); |
| 6320 | mmdrop(mm); |
| 6321 | } |
| 6322 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 6323 | /* called under rq->lock with disabled interrupts */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 6324 | static void migrate_dead(unsigned int dead_cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6326 | struct rq *rq = cpu_rq(dead_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | |
| 6328 | /* Must be exiting, otherwise would be on tasklist. */ |
Eugene Teo | 270f722 | 2007-10-18 23:40:38 -0700 | [diff] [blame] | 6329 | BUG_ON(!p->exit_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | |
| 6331 | /* Cannot have done final schedule yet: would have vanished. */ |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 6332 | BUG_ON(p->state == TASK_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6334 | get_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6335 | |
| 6336 | /* |
| 6337 | * Drop lock around migration; if someone else moves it, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6338 | * 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] | 6339 | * fine. |
| 6340 | */ |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6341 | spin_unlock_irq(&rq->lock); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6342 | move_task_off_dead_cpu(dead_cpu, p); |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6343 | spin_lock_irq(&rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6344 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6345 | put_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6346 | } |
| 6347 | |
| 6348 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ |
| 6349 | static void migrate_dead_tasks(unsigned int dead_cpu) |
| 6350 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6351 | struct rq *rq = cpu_rq(dead_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6352 | struct task_struct *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6353 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6354 | for ( ; ; ) { |
| 6355 | if (!rq->nr_running) |
| 6356 | break; |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 6357 | update_rq_clock(rq); |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 6358 | next = pick_next_task(rq, rq->curr); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6359 | if (!next) |
| 6360 | break; |
| 6361 | migrate_dead(dead_cpu, next); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | } |
| 6364 | } |
| 6365 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 6366 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6367 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
| 6368 | |
| 6369 | static struct ctl_table sd_ctl_dir[] = { |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6370 | { |
| 6371 | .procname = "sched_domain", |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6372 | .mode = 0555, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6373 | }, |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 6374 | {0, }, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6375 | }; |
| 6376 | |
| 6377 | static struct ctl_table sd_ctl_root[] = { |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6378 | { |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6379 | .ctl_name = CTL_KERN, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6380 | .procname = "kernel", |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6381 | .mode = 0555, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6382 | .child = sd_ctl_dir, |
| 6383 | }, |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 6384 | {0, }, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6385 | }; |
| 6386 | |
| 6387 | static struct ctl_table *sd_alloc_ctl_entry(int n) |
| 6388 | { |
| 6389 | struct ctl_table *entry = |
Milton Miller | 5cf9f06 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6390 | kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6391 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6392 | return entry; |
| 6393 | } |
| 6394 | |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6395 | static void sd_free_ctl_entry(struct ctl_table **tablep) |
| 6396 | { |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 6397 | struct ctl_table *entry; |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6398 | |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 6399 | /* |
| 6400 | * In the intermediate directories, both the child directory and |
| 6401 | * procname are dynamically allocated and could fail but the mode |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6402 | * will always be set. In the lowest directory the names are |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 6403 | * static strings and all have proc handlers. |
| 6404 | */ |
| 6405 | for (entry = *tablep; entry->mode; entry++) { |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6406 | if (entry->child) |
| 6407 | sd_free_ctl_entry(&entry->child); |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 6408 | if (entry->proc_handler == NULL) |
| 6409 | kfree(entry->procname); |
| 6410 | } |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6411 | |
| 6412 | kfree(*tablep); |
| 6413 | *tablep = NULL; |
| 6414 | } |
| 6415 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6416 | static void |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6417 | set_table_entry(struct ctl_table *entry, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6418 | const char *procname, void *data, int maxlen, |
| 6419 | mode_t mode, proc_handler *proc_handler) |
| 6420 | { |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6421 | entry->procname = procname; |
| 6422 | entry->data = data; |
| 6423 | entry->maxlen = maxlen; |
| 6424 | entry->mode = mode; |
| 6425 | entry->proc_handler = proc_handler; |
| 6426 | } |
| 6427 | |
| 6428 | static struct ctl_table * |
| 6429 | sd_alloc_ctl_domain_table(struct sched_domain *sd) |
| 6430 | { |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6431 | struct ctl_table *table = sd_alloc_ctl_entry(12); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6432 | |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6433 | if (table == NULL) |
| 6434 | return NULL; |
| 6435 | |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6436 | set_table_entry(&table[0], "min_interval", &sd->min_interval, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6437 | sizeof(long), 0644, proc_doulongvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6438 | set_table_entry(&table[1], "max_interval", &sd->max_interval, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6439 | sizeof(long), 0644, proc_doulongvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6440 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6441 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6442 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6443 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6444 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6445 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6446 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6447 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6448 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6449 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6450 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6451 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 6452 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6453 | sizeof(int), 0644, proc_dointvec_minmax); |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6454 | set_table_entry(&table[9], "cache_nice_tries", |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6455 | &sd->cache_nice_tries, |
| 6456 | sizeof(int), 0644, proc_dointvec_minmax); |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6457 | set_table_entry(&table[10], "flags", &sd->flags, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6458 | sizeof(int), 0644, proc_dointvec_minmax); |
Milton Miller | 6323469f | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6459 | /* &table[11] is terminator */ |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6460 | |
| 6461 | return table; |
| 6462 | } |
| 6463 | |
Ingo Molnar | 9a4e715 | 2007-11-28 15:52:56 +0100 | [diff] [blame] | 6464 | static ctl_table *sd_alloc_ctl_cpu_table(int cpu) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6465 | { |
| 6466 | struct ctl_table *entry, *table; |
| 6467 | struct sched_domain *sd; |
| 6468 | int domain_num = 0, i; |
| 6469 | char buf[32]; |
| 6470 | |
| 6471 | for_each_domain(cpu, sd) |
| 6472 | domain_num++; |
| 6473 | entry = table = sd_alloc_ctl_entry(domain_num + 1); |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6474 | if (table == NULL) |
| 6475 | return NULL; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6476 | |
| 6477 | i = 0; |
| 6478 | for_each_domain(cpu, sd) { |
| 6479 | snprintf(buf, 32, "domain%d", i); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6480 | entry->procname = kstrdup(buf, GFP_KERNEL); |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6481 | entry->mode = 0555; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6482 | entry->child = sd_alloc_ctl_domain_table(sd); |
| 6483 | entry++; |
| 6484 | i++; |
| 6485 | } |
| 6486 | return table; |
| 6487 | } |
| 6488 | |
| 6489 | static struct ctl_table_header *sd_sysctl_header; |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6490 | static void register_sched_domain_sysctl(void) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6491 | { |
| 6492 | int i, cpu_num = num_online_cpus(); |
| 6493 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); |
| 6494 | char buf[32]; |
| 6495 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6496 | WARN_ON(sd_ctl_dir[0].child); |
| 6497 | sd_ctl_dir[0].child = entry; |
| 6498 | |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6499 | if (entry == NULL) |
| 6500 | return; |
| 6501 | |
Milton Miller | 97b6ea7 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6502 | for_each_online_cpu(i) { |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6503 | snprintf(buf, 32, "cpu%d", i); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6504 | entry->procname = kstrdup(buf, GFP_KERNEL); |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6505 | entry->mode = 0555; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6506 | entry->child = sd_alloc_ctl_cpu_table(i); |
Milton Miller | 97b6ea7 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6507 | entry++; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6508 | } |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6509 | |
| 6510 | WARN_ON(sd_sysctl_header); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6511 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); |
| 6512 | } |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6513 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6514 | /* may be called multiple times per register */ |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6515 | static void unregister_sched_domain_sysctl(void) |
| 6516 | { |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6517 | if (sd_sysctl_header) |
| 6518 | unregister_sysctl_table(sd_sysctl_header); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6519 | sd_sysctl_header = NULL; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6520 | if (sd_ctl_dir[0].child) |
| 6521 | sd_free_ctl_entry(&sd_ctl_dir[0].child); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6522 | } |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6523 | #else |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6524 | static void register_sched_domain_sysctl(void) |
| 6525 | { |
| 6526 | } |
| 6527 | static void unregister_sched_domain_sysctl(void) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6528 | { |
| 6529 | } |
| 6530 | #endif |
| 6531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6532 | /* |
| 6533 | * migration_call - callback that gets triggered when a CPU is added. |
| 6534 | * Here we can start up the necessary migration thread for the new CPU. |
| 6535 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6536 | static int __cpuinit |
| 6537 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6538 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | struct task_struct *p; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6540 | int cpu = (long)hcpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6541 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6542 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | |
| 6544 | switch (action) { |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 6545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6546 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6547 | case CPU_UP_PREPARE_FROZEN: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6548 | p = kthread_create(migration_thread, hcpu, "migration/%d", cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6549 | if (IS_ERR(p)) |
| 6550 | return NOTIFY_BAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6551 | kthread_bind(p, cpu); |
| 6552 | /* Must be high prio: stop_machine expects to yield to it. */ |
| 6553 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6554 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6555 | task_rq_unlock(rq, &flags); |
| 6556 | cpu_rq(cpu)->migration_thread = p; |
| 6557 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6560 | case CPU_ONLINE_FROZEN: |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 6561 | /* Strictly unnecessary, as first user will wake it. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6562 | wake_up_process(cpu_rq(cpu)->migration_thread); |
Gregory Haskins | 1f94ef5 | 2008-03-10 16:52:41 -0400 | [diff] [blame] | 6563 | |
| 6564 | /* Update our root-domain */ |
| 6565 | rq = cpu_rq(cpu); |
| 6566 | spin_lock_irqsave(&rq->lock, flags); |
| 6567 | if (rq->rd) { |
| 6568 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); |
| 6569 | cpu_set(cpu, rq->rd->online); |
| 6570 | } |
| 6571 | spin_unlock_irqrestore(&rq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6572 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | #ifdef CONFIG_HOTPLUG_CPU |
| 6575 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6576 | case CPU_UP_CANCELED_FROZEN: |
Heiko Carstens | fc75cdf | 2006-06-25 05:49:10 -0700 | [diff] [blame] | 6577 | if (!cpu_rq(cpu)->migration_thread) |
| 6578 | break; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6579 | /* Unbind it from offline cpu so it can run. Fall thru. */ |
Heiko Carstens | a4c4af7 | 2005-11-07 00:58:38 -0800 | [diff] [blame] | 6580 | kthread_bind(cpu_rq(cpu)->migration_thread, |
| 6581 | any_online_cpu(cpu_online_map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6582 | kthread_stop(cpu_rq(cpu)->migration_thread); |
| 6583 | cpu_rq(cpu)->migration_thread = NULL; |
| 6584 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6586 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6587 | case CPU_DEAD_FROZEN: |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6588 | cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6589 | migrate_live_tasks(cpu); |
| 6590 | rq = cpu_rq(cpu); |
| 6591 | kthread_stop(rq->migration_thread); |
| 6592 | rq->migration_thread = NULL; |
| 6593 | /* Idle task back to normal (off runqueue, low prio) */ |
Oleg Nesterov | d2da272 | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6594 | spin_lock_irq(&rq->lock); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 6595 | update_rq_clock(rq); |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 6596 | deactivate_task(rq, rq->idle, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | rq->idle->static_prio = MAX_PRIO; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6598 | __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); |
| 6599 | rq->idle->sched_class = &idle_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6600 | migrate_dead_tasks(cpu); |
Oleg Nesterov | d2da272 | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6601 | spin_unlock_irq(&rq->lock); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6602 | cpuset_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6603 | migrate_nr_uninterruptible(rq); |
| 6604 | BUG_ON(rq->nr_running != 0); |
| 6605 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6606 | /* |
| 6607 | * No need to migrate the tasks: it was best-effort if |
| 6608 | * they didn't take sched_hotcpu_mutex. Just wake up |
| 6609 | * the requestors. |
| 6610 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | spin_lock_irq(&rq->lock); |
| 6612 | while (!list_empty(&rq->migration_queue)) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6613 | struct migration_req *req; |
| 6614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | req = list_entry(rq->migration_queue.next, |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6616 | struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6617 | list_del_init(&req->list); |
| 6618 | complete(&req->done); |
| 6619 | } |
| 6620 | spin_unlock_irq(&rq->lock); |
| 6621 | break; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6622 | |
Gregory Haskins | 08f503b | 2008-03-10 17:59:11 -0400 | [diff] [blame] | 6623 | case CPU_DYING: |
| 6624 | case CPU_DYING_FROZEN: |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6625 | /* Update our root-domain */ |
| 6626 | rq = cpu_rq(cpu); |
| 6627 | spin_lock_irqsave(&rq->lock, flags); |
| 6628 | if (rq->rd) { |
| 6629 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); |
| 6630 | cpu_clear(cpu, rq->rd->online); |
| 6631 | } |
| 6632 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6633 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6634 | #endif |
| 6635 | } |
| 6636 | return NOTIFY_OK; |
| 6637 | } |
| 6638 | |
| 6639 | /* Register at highest priority so that task migration (migrate_all_tasks) |
| 6640 | * happens before everything else. |
| 6641 | */ |
Chandra Seetharaman | 26c2143 | 2006-06-27 02:54:10 -0700 | [diff] [blame] | 6642 | static struct notifier_block __cpuinitdata migration_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6643 | .notifier_call = migration_call, |
| 6644 | .priority = 10 |
| 6645 | }; |
| 6646 | |
Adrian Bunk | e6fe664 | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 6647 | void __init migration_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | { |
| 6649 | void *cpu = (void *)(long)smp_processor_id(); |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 6650 | int err; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6651 | |
| 6652 | /* Start one for the boot CPU: */ |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 6653 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
| 6654 | BUG_ON(err == NOTIFY_BAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6655 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
| 6656 | register_cpu_notifier(&migration_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6657 | } |
| 6658 | #endif |
| 6659 | |
| 6660 | #ifdef CONFIG_SMP |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 6661 | |
Ingo Molnar | 3e9830d | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 6662 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6663 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6664 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, |
| 6665 | cpumask_t *groupmask) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6666 | { |
| 6667 | struct sched_group *group = sd->groups; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6668 | char str[256]; |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6669 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6670 | cpulist_scnprintf(str, sizeof(str), sd->span); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6671 | cpus_clear(*groupmask); |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6672 | |
| 6673 | printk(KERN_DEBUG "%*s domain %d: ", level, "", level); |
| 6674 | |
| 6675 | if (!(sd->flags & SD_LOAD_BALANCE)) { |
| 6676 | printk("does not load-balance\n"); |
| 6677 | if (sd->parent) |
| 6678 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" |
| 6679 | " has parent"); |
| 6680 | return -1; |
| 6681 | } |
| 6682 | |
| 6683 | printk(KERN_CONT "span %s\n", str); |
| 6684 | |
| 6685 | if (!cpu_isset(cpu, sd->span)) { |
| 6686 | printk(KERN_ERR "ERROR: domain->span does not contain " |
| 6687 | "CPU%d\n", cpu); |
| 6688 | } |
| 6689 | if (!cpu_isset(cpu, group->cpumask)) { |
| 6690 | printk(KERN_ERR "ERROR: domain->groups does not contain" |
| 6691 | " CPU%d\n", cpu); |
| 6692 | } |
| 6693 | |
| 6694 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); |
| 6695 | do { |
| 6696 | if (!group) { |
| 6697 | printk("\n"); |
| 6698 | printk(KERN_ERR "ERROR: group is NULL\n"); |
| 6699 | break; |
| 6700 | } |
| 6701 | |
| 6702 | if (!group->__cpu_power) { |
| 6703 | printk(KERN_CONT "\n"); |
| 6704 | printk(KERN_ERR "ERROR: domain->cpu_power not " |
| 6705 | "set\n"); |
| 6706 | break; |
| 6707 | } |
| 6708 | |
| 6709 | if (!cpus_weight(group->cpumask)) { |
| 6710 | printk(KERN_CONT "\n"); |
| 6711 | printk(KERN_ERR "ERROR: empty group\n"); |
| 6712 | break; |
| 6713 | } |
| 6714 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6715 | if (cpus_intersects(*groupmask, group->cpumask)) { |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6716 | printk(KERN_CONT "\n"); |
| 6717 | printk(KERN_ERR "ERROR: repeated CPUs\n"); |
| 6718 | break; |
| 6719 | } |
| 6720 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6721 | cpus_or(*groupmask, *groupmask, group->cpumask); |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6722 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6723 | cpulist_scnprintf(str, sizeof(str), group->cpumask); |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6724 | printk(KERN_CONT " %s", str); |
| 6725 | |
| 6726 | group = group->next; |
| 6727 | } while (group != sd->groups); |
| 6728 | printk(KERN_CONT "\n"); |
| 6729 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6730 | if (!cpus_equal(sd->span, *groupmask)) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6731 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
| 6732 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6733 | if (sd->parent && !cpus_subset(*groupmask, sd->parent->span)) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6734 | printk(KERN_ERR "ERROR: parent span is not a superset " |
| 6735 | "of domain->span\n"); |
| 6736 | return 0; |
| 6737 | } |
| 6738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6739 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
| 6740 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6741 | cpumask_t *groupmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6742 | int level = 0; |
| 6743 | |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 6744 | if (!sd) { |
| 6745 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); |
| 6746 | return; |
| 6747 | } |
| 6748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6749 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
| 6750 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6751 | groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL); |
| 6752 | if (!groupmask) { |
| 6753 | printk(KERN_DEBUG "Cannot load-balance (out of memory)\n"); |
| 6754 | return; |
| 6755 | } |
| 6756 | |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6757 | for (;;) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6758 | if (sched_domain_debug_one(sd, cpu, level, groupmask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6759 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | level++; |
| 6761 | sd = sd->parent; |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 6762 | if (!sd) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6763 | break; |
| 6764 | } |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6765 | kfree(groupmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6766 | } |
| 6767 | #else |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6768 | # define sched_domain_debug(sd, cpu) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | #endif |
| 6770 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6771 | static int sd_degenerate(struct sched_domain *sd) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6772 | { |
| 6773 | if (cpus_weight(sd->span) == 1) |
| 6774 | return 1; |
| 6775 | |
| 6776 | /* Following flags need at least 2 groups */ |
| 6777 | if (sd->flags & (SD_LOAD_BALANCE | |
| 6778 | SD_BALANCE_NEWIDLE | |
| 6779 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6780 | SD_BALANCE_EXEC | |
| 6781 | SD_SHARE_CPUPOWER | |
| 6782 | SD_SHARE_PKG_RESOURCES)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6783 | if (sd->groups != sd->groups->next) |
| 6784 | return 0; |
| 6785 | } |
| 6786 | |
| 6787 | /* Following flags don't use groups */ |
| 6788 | if (sd->flags & (SD_WAKE_IDLE | |
| 6789 | SD_WAKE_AFFINE | |
| 6790 | SD_WAKE_BALANCE)) |
| 6791 | return 0; |
| 6792 | |
| 6793 | return 1; |
| 6794 | } |
| 6795 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6796 | static int |
| 6797 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6798 | { |
| 6799 | unsigned long cflags = sd->flags, pflags = parent->flags; |
| 6800 | |
| 6801 | if (sd_degenerate(parent)) |
| 6802 | return 1; |
| 6803 | |
| 6804 | if (!cpus_equal(sd->span, parent->span)) |
| 6805 | return 0; |
| 6806 | |
| 6807 | /* Does parent contain flags not in child? */ |
| 6808 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ |
| 6809 | if (cflags & SD_WAKE_AFFINE) |
| 6810 | pflags &= ~SD_WAKE_BALANCE; |
| 6811 | /* Flags needing groups don't count if only 1 group in parent */ |
| 6812 | if (parent->groups == parent->groups->next) { |
| 6813 | pflags &= ~(SD_LOAD_BALANCE | |
| 6814 | SD_BALANCE_NEWIDLE | |
| 6815 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6816 | SD_BALANCE_EXEC | |
| 6817 | SD_SHARE_CPUPOWER | |
| 6818 | SD_SHARE_PKG_RESOURCES); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6819 | } |
| 6820 | if (~cflags & pflags) |
| 6821 | return 0; |
| 6822 | |
| 6823 | return 1; |
| 6824 | } |
| 6825 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6826 | static void rq_attach_root(struct rq *rq, struct root_domain *rd) |
| 6827 | { |
| 6828 | unsigned long flags; |
| 6829 | const struct sched_class *class; |
| 6830 | |
| 6831 | spin_lock_irqsave(&rq->lock, flags); |
| 6832 | |
| 6833 | if (rq->rd) { |
| 6834 | struct root_domain *old_rd = rq->rd; |
| 6835 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6836 | for (class = sched_class_highest; class; class = class->next) { |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6837 | if (class->leave_domain) |
| 6838 | class->leave_domain(rq); |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6839 | } |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6840 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6841 | cpu_clear(rq->cpu, old_rd->span); |
| 6842 | cpu_clear(rq->cpu, old_rd->online); |
| 6843 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6844 | if (atomic_dec_and_test(&old_rd->refcount)) |
| 6845 | kfree(old_rd); |
| 6846 | } |
| 6847 | |
| 6848 | atomic_inc(&rd->refcount); |
| 6849 | rq->rd = rd; |
| 6850 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6851 | cpu_set(rq->cpu, rd->span); |
Gregory Haskins | 1f94ef5 | 2008-03-10 16:52:41 -0400 | [diff] [blame] | 6852 | if (cpu_isset(rq->cpu, cpu_online_map)) |
| 6853 | cpu_set(rq->cpu, rd->online); |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6854 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6855 | for (class = sched_class_highest; class; class = class->next) { |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6856 | if (class->join_domain) |
| 6857 | class->join_domain(rq); |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6858 | } |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6859 | |
| 6860 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6861 | } |
| 6862 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6863 | static void init_rootdomain(struct root_domain *rd) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6864 | { |
| 6865 | memset(rd, 0, sizeof(*rd)); |
| 6866 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6867 | cpus_clear(rd->span); |
| 6868 | cpus_clear(rd->online); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6869 | } |
| 6870 | |
| 6871 | static void init_defrootdomain(void) |
| 6872 | { |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6873 | init_rootdomain(&def_root_domain); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6874 | atomic_set(&def_root_domain.refcount, 1); |
| 6875 | } |
| 6876 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6877 | static struct root_domain *alloc_rootdomain(void) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6878 | { |
| 6879 | struct root_domain *rd; |
| 6880 | |
| 6881 | rd = kmalloc(sizeof(*rd), GFP_KERNEL); |
| 6882 | if (!rd) |
| 6883 | return NULL; |
| 6884 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6885 | init_rootdomain(rd); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6886 | |
| 6887 | return rd; |
| 6888 | } |
| 6889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | /* |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6891 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | * hold the hotplug lock. |
| 6893 | */ |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6894 | static void |
| 6895 | 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] | 6896 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6897 | struct rq *rq = cpu_rq(cpu); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6898 | struct sched_domain *tmp; |
| 6899 | |
| 6900 | /* Remove the sched domains which do not contribute to scheduling. */ |
| 6901 | for (tmp = sd; tmp; tmp = tmp->parent) { |
| 6902 | struct sched_domain *parent = tmp->parent; |
| 6903 | if (!parent) |
| 6904 | break; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6905 | if (sd_parent_degenerate(tmp, parent)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6906 | tmp->parent = parent->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6907 | if (parent->parent) |
| 6908 | parent->parent->child = tmp; |
| 6909 | } |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6910 | } |
| 6911 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6912 | if (sd && sd_degenerate(sd)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6913 | sd = sd->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6914 | if (sd) |
| 6915 | sd->child = NULL; |
| 6916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6917 | |
| 6918 | sched_domain_debug(sd, cpu); |
| 6919 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6920 | rq_attach_root(rq, rd); |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 6921 | rcu_assign_pointer(rq->sd, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6922 | } |
| 6923 | |
| 6924 | /* cpus with isolated domains */ |
Tim Chen | 67af63a | 2006-12-22 01:07:50 -0800 | [diff] [blame] | 6925 | static cpumask_t cpu_isolated_map = CPU_MASK_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | |
| 6927 | /* Setup the mask of cpus configured for isolated domains */ |
| 6928 | static int __init isolated_cpu_setup(char *str) |
| 6929 | { |
| 6930 | int ints[NR_CPUS], i; |
| 6931 | |
| 6932 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 6933 | cpus_clear(cpu_isolated_map); |
| 6934 | for (i = 1; i <= ints[0]; i++) |
| 6935 | if (ints[i] < NR_CPUS) |
| 6936 | cpu_set(ints[i], cpu_isolated_map); |
| 6937 | return 1; |
| 6938 | } |
| 6939 | |
Ingo Molnar | 8927f49 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 6940 | __setup("isolcpus=", isolated_cpu_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | |
| 6942 | /* |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6943 | * init_sched_build_groups takes the cpumask we wish to span, and a pointer |
| 6944 | * to a function which identifies what group(along with sched group) a CPU |
| 6945 | * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS |
| 6946 | * (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] | 6947 | * |
| 6948 | * init_sched_build_groups will build a circular linked list of the groups |
| 6949 | * covered by the given span, and will set each group's ->cpumask correctly, |
| 6950 | * and ->cpu_power to 0. |
| 6951 | */ |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6952 | static void |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6953 | 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] | 6954 | int (*group_fn)(int cpu, const cpumask_t *cpu_map, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6955 | struct sched_group **sg, |
| 6956 | cpumask_t *tmpmask), |
| 6957 | cpumask_t *covered, cpumask_t *tmpmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6958 | { |
| 6959 | struct sched_group *first = NULL, *last = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | int i; |
| 6961 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6962 | cpus_clear(*covered); |
| 6963 | |
| 6964 | for_each_cpu_mask(i, *span) { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6965 | struct sched_group *sg; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6966 | int group = group_fn(i, cpu_map, &sg, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6967 | int j; |
| 6968 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6969 | if (cpu_isset(i, *covered)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | continue; |
| 6971 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6972 | cpus_clear(sg->cpumask); |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6973 | sg->__cpu_power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6974 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6975 | for_each_cpu_mask(j, *span) { |
| 6976 | if (group_fn(j, cpu_map, NULL, tmpmask) != group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6977 | continue; |
| 6978 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6979 | cpu_set(j, *covered); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | cpu_set(j, sg->cpumask); |
| 6981 | } |
| 6982 | if (!first) |
| 6983 | first = sg; |
| 6984 | if (last) |
| 6985 | last->next = sg; |
| 6986 | last = sg; |
| 6987 | } |
| 6988 | last->next = first; |
| 6989 | } |
| 6990 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6991 | #define SD_NODES_PER_DOMAIN 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6992 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6993 | #ifdef CONFIG_NUMA |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 6994 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6995 | /** |
| 6996 | * find_next_best_node - find the next node to include in a sched_domain |
| 6997 | * @node: node whose sched_domain we're building |
| 6998 | * @used_nodes: nodes already in the sched_domain |
| 6999 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7000 | * Find the next node to include in a given scheduling domain. Simply |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7001 | * finds the closest node not already in the @used_nodes map. |
| 7002 | * |
| 7003 | * Should use nodemask_t. |
| 7004 | */ |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7005 | static int find_next_best_node(int node, nodemask_t *used_nodes) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7006 | { |
| 7007 | int i, n, val, min_val, best_node = 0; |
| 7008 | |
| 7009 | min_val = INT_MAX; |
| 7010 | |
| 7011 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 7012 | /* Start at @node */ |
| 7013 | n = (node + i) % MAX_NUMNODES; |
| 7014 | |
| 7015 | if (!nr_cpus_node(n)) |
| 7016 | continue; |
| 7017 | |
| 7018 | /* Skip already used nodes */ |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7019 | if (node_isset(n, *used_nodes)) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7020 | continue; |
| 7021 | |
| 7022 | /* Simple min distance search */ |
| 7023 | val = node_distance(node, n); |
| 7024 | |
| 7025 | if (val < min_val) { |
| 7026 | min_val = val; |
| 7027 | best_node = n; |
| 7028 | } |
| 7029 | } |
| 7030 | |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7031 | node_set(best_node, *used_nodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7032 | return best_node; |
| 7033 | } |
| 7034 | |
| 7035 | /** |
| 7036 | * sched_domain_node_span - get a cpumask for a node's sched_domain |
| 7037 | * @node: node whose cpumask we're constructing |
Randy Dunlap | 7348672 | 2008-04-22 10:07:22 -0700 | [diff] [blame^] | 7038 | * @span: resulting cpumask |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7039 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7040 | * 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] | 7041 | * should be one that prevents unnecessary balancing, but also spreads tasks |
| 7042 | * out optimally. |
| 7043 | */ |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 7044 | static void sched_domain_node_span(int node, cpumask_t *span) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7045 | { |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7046 | nodemask_t used_nodes; |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7047 | node_to_cpumask_ptr(nodemask, node); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7048 | int i; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7049 | |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 7050 | cpus_clear(*span); |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7051 | nodes_clear(used_nodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7052 | |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 7053 | cpus_or(*span, *span, *nodemask); |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7054 | node_set(node, used_nodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7055 | |
| 7056 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7057 | int next_node = find_next_best_node(node, &used_nodes); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7058 | |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7059 | node_to_cpumask_ptr_next(nodemask, next_node); |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 7060 | cpus_or(*span, *span, *nodemask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7061 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7062 | } |
| 7063 | #endif |
| 7064 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7065 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7066 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7067 | /* |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7068 | * SMT sched-domains: |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7069 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7070 | #ifdef CONFIG_SCHED_SMT |
| 7071 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7072 | static DEFINE_PER_CPU(struct sched_group, sched_group_cpus); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7073 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7074 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7075 | cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 7076 | cpumask_t *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7078 | if (sg) |
| 7079 | *sg = &per_cpu(sched_group_cpus, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7080 | return cpu; |
| 7081 | } |
| 7082 | #endif |
| 7083 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7084 | /* |
| 7085 | * multi-core sched-domains: |
| 7086 | */ |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7087 | #ifdef CONFIG_SCHED_MC |
| 7088 | static DEFINE_PER_CPU(struct sched_domain, core_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7089 | static DEFINE_PER_CPU(struct sched_group, sched_group_core); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7090 | #endif |
| 7091 | |
| 7092 | #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7093 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7094 | cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 7095 | cpumask_t *mask) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7096 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7097 | int group; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7098 | |
| 7099 | *mask = per_cpu(cpu_sibling_map, cpu); |
| 7100 | cpus_and(*mask, *mask, *cpu_map); |
| 7101 | group = first_cpu(*mask); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7102 | if (sg) |
| 7103 | *sg = &per_cpu(sched_group_core, group); |
| 7104 | return group; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7105 | } |
| 7106 | #elif defined(CONFIG_SCHED_MC) |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7107 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7108 | cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 7109 | cpumask_t *unused) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7110 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7111 | if (sg) |
| 7112 | *sg = &per_cpu(sched_group_core, cpu); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7113 | return cpu; |
| 7114 | } |
| 7115 | #endif |
| 7116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7117 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7118 | static DEFINE_PER_CPU(struct sched_group, sched_group_phys); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7119 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7120 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7121 | cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 7122 | cpumask_t *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7123 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7124 | int group; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7125 | #ifdef CONFIG_SCHED_MC |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7126 | *mask = cpu_coregroup_map(cpu); |
| 7127 | cpus_and(*mask, *mask, *cpu_map); |
| 7128 | group = first_cpu(*mask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7129 | #elif defined(CONFIG_SCHED_SMT) |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7130 | *mask = per_cpu(cpu_sibling_map, cpu); |
| 7131 | cpus_and(*mask, *mask, *cpu_map); |
| 7132 | group = first_cpu(*mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | #else |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7134 | group = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | #endif |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7136 | if (sg) |
| 7137 | *sg = &per_cpu(sched_group_phys, group); |
| 7138 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7139 | } |
| 7140 | |
| 7141 | #ifdef CONFIG_NUMA |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7142 | /* |
| 7143 | * The init_sched_build_groups can't handle what we want to do with node |
| 7144 | * groups, so roll our own. Now each node has its own list of groups which |
| 7145 | * gets dynamically allocated. |
| 7146 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7147 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7148 | static struct sched_group ***sched_group_nodes_bycpu; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7149 | |
| 7150 | static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7151 | static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7152 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7153 | 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] | 7154 | struct sched_group **sg, cpumask_t *nodemask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7155 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7156 | int group; |
| 7157 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7158 | *nodemask = node_to_cpumask(cpu_to_node(cpu)); |
| 7159 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 7160 | group = first_cpu(*nodemask); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7161 | |
| 7162 | if (sg) |
| 7163 | *sg = &per_cpu(sched_group_allnodes, group); |
| 7164 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7165 | } |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7166 | |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7167 | static void init_numa_sched_groups_power(struct sched_group *group_head) |
| 7168 | { |
| 7169 | struct sched_group *sg = group_head; |
| 7170 | int j; |
| 7171 | |
| 7172 | if (!sg) |
| 7173 | return; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7174 | do { |
| 7175 | for_each_cpu_mask(j, sg->cpumask) { |
| 7176 | struct sched_domain *sd; |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7177 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7178 | sd = &per_cpu(phys_domains, j); |
| 7179 | if (j != first_cpu(sd->groups->cpumask)) { |
| 7180 | /* |
| 7181 | * Only add "power" once for each |
| 7182 | * physical package. |
| 7183 | */ |
| 7184 | continue; |
| 7185 | } |
| 7186 | |
| 7187 | sg_inc_cpu_power(sg, sd->groups->__cpu_power); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7188 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7189 | sg = sg->next; |
| 7190 | } while (sg != group_head); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | #endif |
| 7193 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7194 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7195 | /* Free memory allocated for various sched_group structures */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7196 | 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] | 7197 | { |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7198 | int cpu, i; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7199 | |
| 7200 | for_each_cpu_mask(cpu, *cpu_map) { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7201 | struct sched_group **sched_group_nodes |
| 7202 | = sched_group_nodes_bycpu[cpu]; |
| 7203 | |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7204 | if (!sched_group_nodes) |
| 7205 | continue; |
| 7206 | |
| 7207 | for (i = 0; i < MAX_NUMNODES; i++) { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7208 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; |
| 7209 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7210 | *nodemask = node_to_cpumask(i); |
| 7211 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 7212 | if (cpus_empty(*nodemask)) |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7213 | continue; |
| 7214 | |
| 7215 | if (sg == NULL) |
| 7216 | continue; |
| 7217 | sg = sg->next; |
| 7218 | next_sg: |
| 7219 | oldsg = sg; |
| 7220 | sg = sg->next; |
| 7221 | kfree(oldsg); |
| 7222 | if (oldsg != sched_group_nodes[i]) |
| 7223 | goto next_sg; |
| 7224 | } |
| 7225 | kfree(sched_group_nodes); |
| 7226 | sched_group_nodes_bycpu[cpu] = NULL; |
| 7227 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7228 | } |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7229 | #else |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7230 | 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] | 7231 | { |
| 7232 | } |
| 7233 | #endif |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7235 | /* |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7236 | * Initialize sched groups cpu_power. |
| 7237 | * |
| 7238 | * cpu_power indicates the capacity of sched group, which is used while |
| 7239 | * distributing the load between different sched groups in a sched domain. |
| 7240 | * Typically cpu_power for all the groups in a sched domain will be same unless |
| 7241 | * there are asymmetries in the topology. If there are asymmetries, group |
| 7242 | * having more cpu_power will pickup more load compared to the group having |
| 7243 | * less cpu_power. |
| 7244 | * |
| 7245 | * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents |
| 7246 | * the maximum number of tasks a group can handle in the presence of other idle |
| 7247 | * or lightly loaded groups in the same sched domain. |
| 7248 | */ |
| 7249 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) |
| 7250 | { |
| 7251 | struct sched_domain *child; |
| 7252 | struct sched_group *group; |
| 7253 | |
| 7254 | WARN_ON(!sd || !sd->groups); |
| 7255 | |
| 7256 | if (cpu != first_cpu(sd->groups->cpumask)) |
| 7257 | return; |
| 7258 | |
| 7259 | child = sd->child; |
| 7260 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7261 | sd->groups->__cpu_power = 0; |
| 7262 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7263 | /* |
| 7264 | * For perf policy, if the groups in child domain share resources |
| 7265 | * (for example cores sharing some portions of the cache hierarchy |
| 7266 | * or SMT), then set this domain groups cpu_power such that each group |
| 7267 | * can handle only one task, when there are other idle groups in the |
| 7268 | * same sched domain. |
| 7269 | */ |
| 7270 | if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) && |
| 7271 | (child->flags & |
| 7272 | (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7273 | sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7274 | return; |
| 7275 | } |
| 7276 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7277 | /* |
| 7278 | * add cpu_power of each child group to this groups cpu_power |
| 7279 | */ |
| 7280 | group = child->groups; |
| 7281 | do { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7282 | sg_inc_cpu_power(sd->groups, group->__cpu_power); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7283 | group = group->next; |
| 7284 | } while (group != child->groups); |
| 7285 | } |
| 7286 | |
| 7287 | /* |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7288 | * Initializers for schedule domains |
| 7289 | * Non-inlined to reduce accumulated stack pressure in build_sched_domains() |
| 7290 | */ |
| 7291 | |
| 7292 | #define SD_INIT(sd, type) sd_init_##type(sd) |
| 7293 | #define SD_INIT_FUNC(type) \ |
| 7294 | static noinline void sd_init_##type(struct sched_domain *sd) \ |
| 7295 | { \ |
| 7296 | memset(sd, 0, sizeof(*sd)); \ |
| 7297 | *sd = SD_##type##_INIT; \ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7298 | sd->level = SD_LV_##type; \ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7299 | } |
| 7300 | |
| 7301 | SD_INIT_FUNC(CPU) |
| 7302 | #ifdef CONFIG_NUMA |
| 7303 | SD_INIT_FUNC(ALLNODES) |
| 7304 | SD_INIT_FUNC(NODE) |
| 7305 | #endif |
| 7306 | #ifdef CONFIG_SCHED_SMT |
| 7307 | SD_INIT_FUNC(SIBLING) |
| 7308 | #endif |
| 7309 | #ifdef CONFIG_SCHED_MC |
| 7310 | SD_INIT_FUNC(MC) |
| 7311 | #endif |
| 7312 | |
| 7313 | /* |
| 7314 | * To minimize stack usage kmalloc room for cpumasks and share the |
| 7315 | * space as the usage in build_sched_domains() dictates. Used only |
| 7316 | * if the amount of space is significant. |
| 7317 | */ |
| 7318 | struct allmasks { |
| 7319 | cpumask_t tmpmask; /* make this one first */ |
| 7320 | union { |
| 7321 | cpumask_t nodemask; |
| 7322 | cpumask_t this_sibling_map; |
| 7323 | cpumask_t this_core_map; |
| 7324 | }; |
| 7325 | cpumask_t send_covered; |
| 7326 | |
| 7327 | #ifdef CONFIG_NUMA |
| 7328 | cpumask_t domainspan; |
| 7329 | cpumask_t covered; |
| 7330 | cpumask_t notcovered; |
| 7331 | #endif |
| 7332 | }; |
| 7333 | |
| 7334 | #if NR_CPUS > 128 |
| 7335 | #define SCHED_CPUMASK_ALLOC 1 |
| 7336 | #define SCHED_CPUMASK_FREE(v) kfree(v) |
| 7337 | #define SCHED_CPUMASK_DECLARE(v) struct allmasks *v |
| 7338 | #else |
| 7339 | #define SCHED_CPUMASK_ALLOC 0 |
| 7340 | #define SCHED_CPUMASK_FREE(v) |
| 7341 | #define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v |
| 7342 | #endif |
| 7343 | |
| 7344 | #define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \ |
| 7345 | ((unsigned long)(a) + offsetof(struct allmasks, v)) |
| 7346 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7347 | static int default_relax_domain_level = -1; |
| 7348 | |
| 7349 | static int __init setup_relax_domain_level(char *str) |
| 7350 | { |
| 7351 | default_relax_domain_level = simple_strtoul(str, NULL, 0); |
| 7352 | return 1; |
| 7353 | } |
| 7354 | __setup("relax_domain_level=", setup_relax_domain_level); |
| 7355 | |
| 7356 | static void set_domain_attribute(struct sched_domain *sd, |
| 7357 | struct sched_domain_attr *attr) |
| 7358 | { |
| 7359 | int request; |
| 7360 | |
| 7361 | if (!attr || attr->relax_domain_level < 0) { |
| 7362 | if (default_relax_domain_level < 0) |
| 7363 | return; |
| 7364 | else |
| 7365 | request = default_relax_domain_level; |
| 7366 | } else |
| 7367 | request = attr->relax_domain_level; |
| 7368 | if (request < sd->level) { |
| 7369 | /* turn off idle balance on this domain */ |
| 7370 | sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE); |
| 7371 | } else { |
| 7372 | /* turn on idle balance on this domain */ |
| 7373 | sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE); |
| 7374 | } |
| 7375 | } |
| 7376 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7377 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7378 | * Build sched domains for a given set of cpus and attach the sched domains |
| 7379 | * to the individual cpus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7380 | */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7381 | static int __build_sched_domains(const cpumask_t *cpu_map, |
| 7382 | struct sched_domain_attr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7383 | { |
| 7384 | int i; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7385 | struct root_domain *rd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7386 | SCHED_CPUMASK_DECLARE(allmasks); |
| 7387 | cpumask_t *tmpmask; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7388 | #ifdef CONFIG_NUMA |
| 7389 | struct sched_group **sched_group_nodes = NULL; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7390 | int sd_allnodes = 0; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7391 | |
| 7392 | /* |
| 7393 | * Allocate the per-node list of sched groups |
| 7394 | */ |
Milton Miller | 5cf9f06 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 7395 | sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *), |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7396 | GFP_KERNEL); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7397 | if (!sched_group_nodes) { |
| 7398 | printk(KERN_WARNING "Can not alloc sched group node list\n"); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7399 | return -ENOMEM; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7400 | } |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7401 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7402 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 7403 | rd = alloc_rootdomain(); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7404 | if (!rd) { |
| 7405 | printk(KERN_WARNING "Cannot alloc root domain\n"); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7406 | #ifdef CONFIG_NUMA |
| 7407 | kfree(sched_group_nodes); |
| 7408 | #endif |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7409 | return -ENOMEM; |
| 7410 | } |
| 7411 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7412 | #if SCHED_CPUMASK_ALLOC |
| 7413 | /* get space for all scratch cpumask variables */ |
| 7414 | allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL); |
| 7415 | if (!allmasks) { |
| 7416 | printk(KERN_WARNING "Cannot alloc cpumask array\n"); |
| 7417 | kfree(rd); |
| 7418 | #ifdef CONFIG_NUMA |
| 7419 | kfree(sched_group_nodes); |
| 7420 | #endif |
| 7421 | return -ENOMEM; |
| 7422 | } |
| 7423 | #endif |
| 7424 | tmpmask = (cpumask_t *)allmasks; |
| 7425 | |
| 7426 | |
| 7427 | #ifdef CONFIG_NUMA |
| 7428 | sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; |
| 7429 | #endif |
| 7430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7431 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7432 | * Set up domains for cpus specified by the cpu_map. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7433 | */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7434 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7435 | struct sched_domain *sd = NULL, *p; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7436 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7437 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7438 | *nodemask = node_to_cpumask(cpu_to_node(i)); |
| 7439 | cpus_and(*nodemask, *nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7440 | |
| 7441 | #ifdef CONFIG_NUMA |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7442 | if (cpus_weight(*cpu_map) > |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7443 | SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7444 | sd = &per_cpu(allnodes_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7445 | SD_INIT(sd, ALLNODES); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7446 | set_domain_attribute(sd, attr); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7447 | sd->span = *cpu_map; |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7448 | sd->first_cpu = first_cpu(sd->span); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7449 | cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7450 | p = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7451 | sd_allnodes = 1; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7452 | } else |
| 7453 | p = NULL; |
| 7454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7455 | sd = &per_cpu(node_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7456 | SD_INIT(sd, NODE); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7457 | set_domain_attribute(sd, attr); |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 7458 | sched_domain_node_span(cpu_to_node(i), &sd->span); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7459 | sd->first_cpu = first_cpu(sd->span); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7460 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 7461 | if (p) |
| 7462 | p->child = sd; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7463 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7464 | #endif |
| 7465 | |
| 7466 | p = sd; |
| 7467 | sd = &per_cpu(phys_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7468 | SD_INIT(sd, CPU); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7469 | set_domain_attribute(sd, attr); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7470 | sd->span = *nodemask; |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7471 | sd->first_cpu = first_cpu(sd->span); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 7473 | if (p) |
| 7474 | p->child = sd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7475 | cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7476 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7477 | #ifdef CONFIG_SCHED_MC |
| 7478 | p = sd; |
| 7479 | sd = &per_cpu(core_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7480 | SD_INIT(sd, MC); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7481 | set_domain_attribute(sd, attr); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7482 | sd->span = cpu_coregroup_map(i); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7483 | sd->first_cpu = first_cpu(sd->span); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7484 | cpus_and(sd->span, sd->span, *cpu_map); |
| 7485 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 7486 | p->child = sd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7487 | cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7488 | #endif |
| 7489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7490 | #ifdef CONFIG_SCHED_SMT |
| 7491 | p = sd; |
| 7492 | sd = &per_cpu(cpu_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7493 | SD_INIT(sd, SIBLING); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7494 | set_domain_attribute(sd, attr); |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 7495 | sd->span = per_cpu(cpu_sibling_map, i); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7496 | sd->first_cpu = first_cpu(sd->span); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7497 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7498 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 7499 | p->child = sd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7500 | cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7501 | #endif |
| 7502 | } |
| 7503 | |
| 7504 | #ifdef CONFIG_SCHED_SMT |
| 7505 | /* Set up CPU (sibling) groups */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7506 | for_each_cpu_mask(i, *cpu_map) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7507 | SCHED_CPUMASK_VAR(this_sibling_map, allmasks); |
| 7508 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
| 7509 | |
| 7510 | *this_sibling_map = per_cpu(cpu_sibling_map, i); |
| 7511 | cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map); |
| 7512 | if (i != first_cpu(*this_sibling_map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | continue; |
| 7514 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7515 | init_sched_build_groups(this_sibling_map, cpu_map, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7516 | &cpu_to_cpu_group, |
| 7517 | send_covered, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7518 | } |
| 7519 | #endif |
| 7520 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7521 | #ifdef CONFIG_SCHED_MC |
| 7522 | /* Set up multi-core groups */ |
| 7523 | for_each_cpu_mask(i, *cpu_map) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7524 | SCHED_CPUMASK_VAR(this_core_map, allmasks); |
| 7525 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
| 7526 | |
| 7527 | *this_core_map = cpu_coregroup_map(i); |
| 7528 | cpus_and(*this_core_map, *this_core_map, *cpu_map); |
| 7529 | if (i != first_cpu(*this_core_map)) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7530 | continue; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7531 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7532 | init_sched_build_groups(this_core_map, cpu_map, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7533 | &cpu_to_core_group, |
| 7534 | send_covered, tmpmask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7535 | } |
| 7536 | #endif |
| 7537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7538 | /* Set up physical groups */ |
| 7539 | for (i = 0; i < MAX_NUMNODES; i++) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7540 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
| 7541 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7542 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7543 | *nodemask = node_to_cpumask(i); |
| 7544 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 7545 | if (cpus_empty(*nodemask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7546 | continue; |
| 7547 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7548 | init_sched_build_groups(nodemask, cpu_map, |
| 7549 | &cpu_to_phys_group, |
| 7550 | send_covered, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7551 | } |
| 7552 | |
| 7553 | #ifdef CONFIG_NUMA |
| 7554 | /* Set up node groups */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7555 | if (sd_allnodes) { |
| 7556 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
| 7557 | |
| 7558 | init_sched_build_groups(cpu_map, cpu_map, |
| 7559 | &cpu_to_allnodes_group, |
| 7560 | send_covered, tmpmask); |
| 7561 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7562 | |
| 7563 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 7564 | /* Set up node groups */ |
| 7565 | struct sched_group *sg, *prev; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7566 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
| 7567 | SCHED_CPUMASK_VAR(domainspan, allmasks); |
| 7568 | SCHED_CPUMASK_VAR(covered, allmasks); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7569 | int j; |
| 7570 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7571 | *nodemask = node_to_cpumask(i); |
| 7572 | cpus_clear(*covered); |
| 7573 | |
| 7574 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 7575 | if (cpus_empty(*nodemask)) { |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7576 | sched_group_nodes[i] = NULL; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7577 | continue; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7578 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7579 | |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 7580 | sched_domain_node_span(i, domainspan); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7581 | cpus_and(*domainspan, *domainspan, *cpu_map); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7582 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 7583 | sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7584 | if (!sg) { |
| 7585 | printk(KERN_WARNING "Can not alloc domain group for " |
| 7586 | "node %d\n", i); |
| 7587 | goto error; |
| 7588 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7589 | sched_group_nodes[i] = sg; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7590 | for_each_cpu_mask(j, *nodemask) { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7591 | struct sched_domain *sd; |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 7592 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7593 | sd = &per_cpu(node_domains, j); |
| 7594 | sd->groups = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7595 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7596 | sg->__cpu_power = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7597 | sg->cpumask = *nodemask; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7598 | sg->next = sg; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7599 | cpus_or(*covered, *covered, *nodemask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7600 | prev = sg; |
| 7601 | |
| 7602 | for (j = 0; j < MAX_NUMNODES; j++) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7603 | SCHED_CPUMASK_VAR(notcovered, allmasks); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7604 | int n = (i + j) % MAX_NUMNODES; |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7605 | node_to_cpumask_ptr(pnodemask, n); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7606 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7607 | cpus_complement(*notcovered, *covered); |
| 7608 | cpus_and(*tmpmask, *notcovered, *cpu_map); |
| 7609 | cpus_and(*tmpmask, *tmpmask, *domainspan); |
| 7610 | if (cpus_empty(*tmpmask)) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7611 | break; |
| 7612 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7613 | cpus_and(*tmpmask, *tmpmask, *pnodemask); |
| 7614 | if (cpus_empty(*tmpmask)) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7615 | continue; |
| 7616 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 7617 | sg = kmalloc_node(sizeof(struct sched_group), |
| 7618 | GFP_KERNEL, i); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7619 | if (!sg) { |
| 7620 | printk(KERN_WARNING |
| 7621 | "Can not alloc domain group for node %d\n", j); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7622 | goto error; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7623 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7624 | sg->__cpu_power = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7625 | sg->cpumask = *tmpmask; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7626 | sg->next = prev->next; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7627 | cpus_or(*covered, *covered, *tmpmask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7628 | prev->next = sg; |
| 7629 | prev = sg; |
| 7630 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7632 | #endif |
| 7633 | |
| 7634 | /* Calculate CPU power for physical packages and nodes */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7635 | #ifdef CONFIG_SCHED_SMT |
| 7636 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7637 | struct sched_domain *sd = &per_cpu(cpu_domains, i); |
| 7638 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7639 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7640 | } |
| 7641 | #endif |
| 7642 | #ifdef CONFIG_SCHED_MC |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7643 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7644 | struct sched_domain *sd = &per_cpu(core_domains, i); |
| 7645 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7646 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7647 | } |
| 7648 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7649 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7650 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7651 | struct sched_domain *sd = &per_cpu(phys_domains, i); |
| 7652 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7653 | init_sched_groups_power(i, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7654 | } |
| 7655 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7656 | #ifdef CONFIG_NUMA |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7657 | for (i = 0; i < MAX_NUMNODES; i++) |
| 7658 | init_numa_sched_groups_power(sched_group_nodes[i]); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7659 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7660 | if (sd_allnodes) { |
| 7661 | struct sched_group *sg; |
Siddha, Suresh B | f712c0c | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 7662 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7663 | cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg, |
| 7664 | tmpmask); |
Siddha, Suresh B | f712c0c | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 7665 | init_numa_sched_groups_power(sg); |
| 7666 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7667 | #endif |
| 7668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7669 | /* Attach the domains */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7670 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7671 | struct sched_domain *sd; |
| 7672 | #ifdef CONFIG_SCHED_SMT |
| 7673 | sd = &per_cpu(cpu_domains, i); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7674 | #elif defined(CONFIG_SCHED_MC) |
| 7675 | sd = &per_cpu(core_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7676 | #else |
| 7677 | sd = &per_cpu(phys_domains, i); |
| 7678 | #endif |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7679 | cpu_attach_domain(sd, rd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7680 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7681 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7682 | SCHED_CPUMASK_FREE((void *)allmasks); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7683 | return 0; |
| 7684 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7685 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7686 | error: |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7687 | free_sched_groups(cpu_map, tmpmask); |
| 7688 | SCHED_CPUMASK_FREE((void *)allmasks); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7689 | return -ENOMEM; |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7690 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7691 | } |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7692 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7693 | static int build_sched_domains(const cpumask_t *cpu_map) |
| 7694 | { |
| 7695 | return __build_sched_domains(cpu_map, NULL); |
| 7696 | } |
| 7697 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7698 | static cpumask_t *doms_cur; /* current sched domains */ |
| 7699 | static int ndoms_cur; /* number of sched domains in 'doms_cur' */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7700 | static struct sched_domain_attr *dattr_cur; /* attribues of custom domains |
| 7701 | in 'doms_cur' */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7702 | |
| 7703 | /* |
| 7704 | * Special case: If a kmalloc of a doms_cur partition (array of |
| 7705 | * cpumask_t) fails, then fallback to a single sched domain, |
| 7706 | * as determined by the single cpumask_t fallback_doms. |
| 7707 | */ |
| 7708 | static cpumask_t fallback_doms; |
| 7709 | |
Heiko Carstens | 22e52b0 | 2008-03-12 18:31:59 +0100 | [diff] [blame] | 7710 | void __attribute__((weak)) arch_update_cpu_topology(void) |
| 7711 | { |
| 7712 | } |
| 7713 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7714 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7715 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7716 | * For now this just excludes isolated cpus, but could be used to |
| 7717 | * exclude other special cases in the future. |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7718 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7719 | static int arch_init_sched_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7720 | { |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7721 | int err; |
| 7722 | |
Heiko Carstens | 22e52b0 | 2008-03-12 18:31:59 +0100 | [diff] [blame] | 7723 | arch_update_cpu_topology(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7724 | ndoms_cur = 1; |
| 7725 | doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL); |
| 7726 | if (!doms_cur) |
| 7727 | doms_cur = &fallback_doms; |
| 7728 | cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7729 | dattr_cur = NULL; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7730 | err = build_sched_domains(doms_cur); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 7731 | register_sched_domain_sysctl(); |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7732 | |
| 7733 | return err; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7734 | } |
| 7735 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7736 | static void arch_destroy_sched_domains(const cpumask_t *cpu_map, |
| 7737 | cpumask_t *tmpmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7738 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7739 | free_sched_groups(cpu_map, tmpmask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7741 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7742 | /* |
| 7743 | * Detach sched domains from a group of cpus specified in cpu_map |
| 7744 | * These cpus will now be attached to the NULL domain |
| 7745 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 7746 | static void detach_destroy_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7747 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7748 | cpumask_t tmpmask; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7749 | int i; |
| 7750 | |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 7751 | unregister_sched_domain_sysctl(); |
| 7752 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7753 | for_each_cpu_mask(i, *cpu_map) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7754 | cpu_attach_domain(NULL, &def_root_domain, i); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7755 | synchronize_sched(); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7756 | arch_destroy_sched_domains(cpu_map, &tmpmask); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7757 | } |
| 7758 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7759 | /* handle null as "default" */ |
| 7760 | static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, |
| 7761 | struct sched_domain_attr *new, int idx_new) |
| 7762 | { |
| 7763 | struct sched_domain_attr tmp; |
| 7764 | |
| 7765 | /* fast path */ |
| 7766 | if (!new && !cur) |
| 7767 | return 1; |
| 7768 | |
| 7769 | tmp = SD_ATTR_INIT; |
| 7770 | return !memcmp(cur ? (cur + idx_cur) : &tmp, |
| 7771 | new ? (new + idx_new) : &tmp, |
| 7772 | sizeof(struct sched_domain_attr)); |
| 7773 | } |
| 7774 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7775 | /* |
| 7776 | * Partition sched domains as specified by the 'ndoms_new' |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7777 | * cpumasks in the array doms_new[] of cpumasks. This compares |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7778 | * doms_new[] to the current sched domain partitioning, doms_cur[]. |
| 7779 | * It destroys each deleted domain and builds each new domain. |
| 7780 | * |
| 7781 | * '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] | 7782 | * The masks don't intersect (don't overlap.) We should setup one |
| 7783 | * sched domain for each mask. CPUs not in any of the cpumasks will |
| 7784 | * not be load balanced. If the same cpumask appears both in the |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7785 | * current 'doms_cur' domains and in the new 'doms_new', we can leave |
| 7786 | * it as it is. |
| 7787 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7788 | * The passed in 'doms_new' should be kmalloc'd. This routine takes |
| 7789 | * 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] | 7790 | * failed the kmalloc call, then it can pass in doms_new == NULL, |
| 7791 | * and partition_sched_domains() will fallback to the single partition |
| 7792 | * 'fallback_doms'. |
| 7793 | * |
| 7794 | * Call with hotplug lock held |
| 7795 | */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7796 | void partition_sched_domains(int ndoms_new, cpumask_t *doms_new, |
| 7797 | struct sched_domain_attr *dattr_new) |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7798 | { |
| 7799 | int i, j; |
| 7800 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7801 | lock_doms_cur(); |
| 7802 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7803 | /* always unregister in case we don't destroy any domains */ |
| 7804 | unregister_sched_domain_sysctl(); |
| 7805 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7806 | if (doms_new == NULL) { |
| 7807 | ndoms_new = 1; |
| 7808 | doms_new = &fallback_doms; |
| 7809 | cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7810 | dattr_new = NULL; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7811 | } |
| 7812 | |
| 7813 | /* Destroy deleted domains */ |
| 7814 | for (i = 0; i < ndoms_cur; i++) { |
| 7815 | for (j = 0; j < ndoms_new; j++) { |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7816 | if (cpus_equal(doms_cur[i], doms_new[j]) |
| 7817 | && dattrs_equal(dattr_cur, i, dattr_new, j)) |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7818 | goto match1; |
| 7819 | } |
| 7820 | /* no match - a current sched domain not in new doms_new[] */ |
| 7821 | detach_destroy_domains(doms_cur + i); |
| 7822 | match1: |
| 7823 | ; |
| 7824 | } |
| 7825 | |
| 7826 | /* Build new domains */ |
| 7827 | for (i = 0; i < ndoms_new; i++) { |
| 7828 | for (j = 0; j < ndoms_cur; j++) { |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7829 | if (cpus_equal(doms_new[i], doms_cur[j]) |
| 7830 | && dattrs_equal(dattr_new, i, dattr_cur, j)) |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7831 | goto match2; |
| 7832 | } |
| 7833 | /* no match - add a new doms_new */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7834 | __build_sched_domains(doms_new + i, |
| 7835 | dattr_new ? dattr_new + i : NULL); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7836 | match2: |
| 7837 | ; |
| 7838 | } |
| 7839 | |
| 7840 | /* Remember the new sched domains */ |
| 7841 | if (doms_cur != &fallback_doms) |
| 7842 | kfree(doms_cur); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7843 | kfree(dattr_cur); /* kfree(NULL) is safe */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7844 | doms_cur = doms_new; |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7845 | dattr_cur = dattr_new; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7846 | ndoms_cur = ndoms_new; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7847 | |
| 7848 | register_sched_domain_sysctl(); |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7849 | |
| 7850 | unlock_doms_cur(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7851 | } |
| 7852 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7853 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Heiko Carstens | 9aefd0a | 2008-03-12 18:31:58 +0100 | [diff] [blame] | 7854 | int arch_reinit_sched_domains(void) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7855 | { |
| 7856 | int err; |
| 7857 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7858 | get_online_cpus(); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7859 | detach_destroy_domains(&cpu_online_map); |
| 7860 | err = arch_init_sched_domains(&cpu_online_map); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7861 | put_online_cpus(); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7862 | |
| 7863 | return err; |
| 7864 | } |
| 7865 | |
| 7866 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) |
| 7867 | { |
| 7868 | int ret; |
| 7869 | |
| 7870 | if (buf[0] != '0' && buf[0] != '1') |
| 7871 | return -EINVAL; |
| 7872 | |
| 7873 | if (smt) |
| 7874 | sched_smt_power_savings = (buf[0] == '1'); |
| 7875 | else |
| 7876 | sched_mc_power_savings = (buf[0] == '1'); |
| 7877 | |
| 7878 | ret = arch_reinit_sched_domains(); |
| 7879 | |
| 7880 | return ret ? ret : count; |
| 7881 | } |
| 7882 | |
Adrian Bunk | 6707de00 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 7883 | #ifdef CONFIG_SCHED_MC |
| 7884 | static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page) |
| 7885 | { |
| 7886 | return sprintf(page, "%u\n", sched_mc_power_savings); |
| 7887 | } |
| 7888 | static ssize_t sched_mc_power_savings_store(struct sys_device *dev, |
| 7889 | const char *buf, size_t count) |
| 7890 | { |
| 7891 | return sched_power_savings_store(buf, count, 0); |
| 7892 | } |
| 7893 | static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show, |
| 7894 | sched_mc_power_savings_store); |
| 7895 | #endif |
| 7896 | |
| 7897 | #ifdef CONFIG_SCHED_SMT |
| 7898 | static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page) |
| 7899 | { |
| 7900 | return sprintf(page, "%u\n", sched_smt_power_savings); |
| 7901 | } |
| 7902 | static ssize_t sched_smt_power_savings_store(struct sys_device *dev, |
| 7903 | const char *buf, size_t count) |
| 7904 | { |
| 7905 | return sched_power_savings_store(buf, count, 1); |
| 7906 | } |
| 7907 | static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show, |
| 7908 | sched_smt_power_savings_store); |
| 7909 | #endif |
| 7910 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7911 | int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) |
| 7912 | { |
| 7913 | int err = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7914 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7915 | #ifdef CONFIG_SCHED_SMT |
| 7916 | if (smt_capable()) |
| 7917 | err = sysfs_create_file(&cls->kset.kobj, |
| 7918 | &attr_sched_smt_power_savings.attr); |
| 7919 | #endif |
| 7920 | #ifdef CONFIG_SCHED_MC |
| 7921 | if (!err && mc_capable()) |
| 7922 | err = sysfs_create_file(&cls->kset.kobj, |
| 7923 | &attr_sched_mc_power_savings.attr); |
| 7924 | #endif |
| 7925 | return err; |
| 7926 | } |
| 7927 | #endif |
| 7928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7929 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7930 | * Force a reinitialization of the sched domains hierarchy. The domains |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7931 | * and groups cannot be updated in place without racing with the balancing |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 7932 | * code, so we temporarily attach all running cpus to the NULL domain |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7933 | * which will prevent rebalancing while the sched domains are recalculated. |
| 7934 | */ |
| 7935 | static int update_sched_domains(struct notifier_block *nfb, |
| 7936 | unsigned long action, void *hcpu) |
| 7937 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7938 | switch (action) { |
| 7939 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7940 | case CPU_UP_PREPARE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7941 | case CPU_DOWN_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7942 | case CPU_DOWN_PREPARE_FROZEN: |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7943 | detach_destroy_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7944 | return NOTIFY_OK; |
| 7945 | |
| 7946 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7947 | case CPU_UP_CANCELED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7948 | case CPU_DOWN_FAILED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7949 | case CPU_DOWN_FAILED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7950 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7951 | case CPU_ONLINE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7952 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7953 | case CPU_DEAD_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7954 | /* |
| 7955 | * Fall through and re-initialise the domains. |
| 7956 | */ |
| 7957 | break; |
| 7958 | default: |
| 7959 | return NOTIFY_DONE; |
| 7960 | } |
| 7961 | |
| 7962 | /* The hotplug lock is already held by cpu_up/cpu_down */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7963 | arch_init_sched_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7964 | |
| 7965 | return NOTIFY_OK; |
| 7966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7967 | |
| 7968 | void __init sched_init_smp(void) |
| 7969 | { |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7970 | cpumask_t non_isolated_cpus; |
| 7971 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7972 | #if defined(CONFIG_NUMA) |
| 7973 | sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **), |
| 7974 | GFP_KERNEL); |
| 7975 | BUG_ON(sched_group_nodes_bycpu == NULL); |
| 7976 | #endif |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7977 | get_online_cpus(); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7978 | arch_init_sched_domains(&cpu_online_map); |
Nathan Lynch | e5e5673 | 2007-01-10 23:15:28 -0800 | [diff] [blame] | 7979 | cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7980 | if (cpus_empty(non_isolated_cpus)) |
| 7981 | cpu_set(smp_processor_id(), non_isolated_cpus); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7982 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7983 | /* XXX: Theoretical race here - CPU may be hotplugged now */ |
| 7984 | hotcpu_notifier(update_sched_domains, 0); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7985 | |
| 7986 | /* Move init over to a non-isolated CPU */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7987 | if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0) |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7988 | BUG(); |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 7989 | sched_init_granularity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7990 | } |
| 7991 | #else |
| 7992 | void __init sched_init_smp(void) |
| 7993 | { |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7994 | #if defined(CONFIG_NUMA) |
| 7995 | sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **), |
| 7996 | GFP_KERNEL); |
| 7997 | BUG_ON(sched_group_nodes_bycpu == NULL); |
| 7998 | #endif |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 7999 | sched_init_granularity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8000 | } |
| 8001 | #endif /* CONFIG_SMP */ |
| 8002 | |
| 8003 | int in_sched_functions(unsigned long addr) |
| 8004 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8005 | return in_lock_functions(addr) || |
| 8006 | (addr >= (unsigned long)__sched_text_start |
| 8007 | && addr < (unsigned long)__sched_text_end); |
| 8008 | } |
| 8009 | |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 8010 | 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] | 8011 | { |
| 8012 | cfs_rq->tasks_timeline = RB_ROOT; |
Peter Zijlstra | 4a55bd5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8013 | INIT_LIST_HEAD(&cfs_rq->tasks); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8014 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8015 | cfs_rq->rq = rq; |
| 8016 | #endif |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 8017 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8018 | } |
| 8019 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 8020 | static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) |
| 8021 | { |
| 8022 | struct rt_prio_array *array; |
| 8023 | int i; |
| 8024 | |
| 8025 | array = &rt_rq->active; |
| 8026 | for (i = 0; i < MAX_RT_PRIO; i++) { |
| 8027 | INIT_LIST_HEAD(array->queue + i); |
| 8028 | __clear_bit(i, array->bitmap); |
| 8029 | } |
| 8030 | /* delimiter for bitsearch: */ |
| 8031 | __set_bit(MAX_RT_PRIO, array->bitmap); |
| 8032 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8033 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 8034 | rt_rq->highest_prio = MAX_RT_PRIO; |
| 8035 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 8036 | #ifdef CONFIG_SMP |
| 8037 | rt_rq->rt_nr_migratory = 0; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 8038 | rt_rq->overloaded = 0; |
| 8039 | #endif |
| 8040 | |
| 8041 | rt_rq->rt_time = 0; |
| 8042 | rt_rq->rt_throttled = 0; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8043 | rt_rq->rt_runtime = 0; |
| 8044 | spin_lock_init(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8045 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8046 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8047 | rt_rq->rt_nr_boosted = 0; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8048 | rt_rq->rq = rq; |
| 8049 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 8050 | } |
| 8051 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8052 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8053 | static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
| 8054 | struct sched_entity *se, int cpu, int add, |
| 8055 | struct sched_entity *parent) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8056 | { |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8057 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8058 | tg->cfs_rq[cpu] = cfs_rq; |
| 8059 | init_cfs_rq(cfs_rq, rq); |
| 8060 | cfs_rq->tg = tg; |
| 8061 | if (add) |
| 8062 | list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list); |
| 8063 | |
| 8064 | tg->se[cpu] = se; |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8065 | /* se could be NULL for init_task_group */ |
| 8066 | if (!se) |
| 8067 | return; |
| 8068 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8069 | if (!parent) |
| 8070 | se->cfs_rq = &rq->cfs; |
| 8071 | else |
| 8072 | se->cfs_rq = parent->my_q; |
| 8073 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8074 | se->my_q = cfs_rq; |
| 8075 | se->load.weight = tg->shares; |
| 8076 | se->load.inv_weight = div64_64(1ULL<<32, se->load.weight); |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8077 | se->parent = parent; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8078 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8079 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8080 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8081 | #ifdef CONFIG_RT_GROUP_SCHED |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8082 | static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, |
| 8083 | struct sched_rt_entity *rt_se, int cpu, int add, |
| 8084 | struct sched_rt_entity *parent) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8085 | { |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8086 | struct rq *rq = cpu_rq(cpu); |
| 8087 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8088 | tg->rt_rq[cpu] = rt_rq; |
| 8089 | init_rt_rq(rt_rq, rq); |
| 8090 | rt_rq->tg = tg; |
| 8091 | rt_rq->rt_se = rt_se; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8092 | rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8093 | if (add) |
| 8094 | list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list); |
| 8095 | |
| 8096 | tg->rt_se[cpu] = rt_se; |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8097 | if (!rt_se) |
| 8098 | return; |
| 8099 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8100 | if (!parent) |
| 8101 | rt_se->rt_rq = &rq->rt; |
| 8102 | else |
| 8103 | rt_se->rt_rq = parent->my_q; |
| 8104 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8105 | rt_se->rt_rq = &rq->rt; |
| 8106 | rt_se->my_q = rt_rq; |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8107 | rt_se->parent = parent; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8108 | INIT_LIST_HEAD(&rt_se->run_list); |
| 8109 | } |
| 8110 | #endif |
| 8111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8112 | void __init sched_init(void) |
| 8113 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8114 | int i, j; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8115 | unsigned long alloc_size = 0, ptr; |
| 8116 | |
| 8117 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8118 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); |
| 8119 | #endif |
| 8120 | #ifdef CONFIG_RT_GROUP_SCHED |
| 8121 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); |
| 8122 | #endif |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8123 | #ifdef CONFIG_USER_SCHED |
| 8124 | alloc_size *= 2; |
| 8125 | #endif |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8126 | /* |
| 8127 | * As sched_init() is called before page_alloc is setup, |
| 8128 | * we use alloc_bootmem(). |
| 8129 | */ |
| 8130 | if (alloc_size) { |
| 8131 | ptr = (unsigned long)alloc_bootmem_low(alloc_size); |
| 8132 | |
| 8133 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8134 | init_task_group.se = (struct sched_entity **)ptr; |
| 8135 | ptr += nr_cpu_ids * sizeof(void **); |
| 8136 | |
| 8137 | init_task_group.cfs_rq = (struct cfs_rq **)ptr; |
| 8138 | ptr += nr_cpu_ids * sizeof(void **); |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8139 | |
| 8140 | #ifdef CONFIG_USER_SCHED |
| 8141 | root_task_group.se = (struct sched_entity **)ptr; |
| 8142 | ptr += nr_cpu_ids * sizeof(void **); |
| 8143 | |
| 8144 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; |
| 8145 | ptr += nr_cpu_ids * sizeof(void **); |
| 8146 | #endif |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8147 | #endif |
| 8148 | #ifdef CONFIG_RT_GROUP_SCHED |
| 8149 | init_task_group.rt_se = (struct sched_rt_entity **)ptr; |
| 8150 | ptr += nr_cpu_ids * sizeof(void **); |
| 8151 | |
| 8152 | init_task_group.rt_rq = (struct rt_rq **)ptr; |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8153 | ptr += nr_cpu_ids * sizeof(void **); |
| 8154 | |
| 8155 | #ifdef CONFIG_USER_SCHED |
| 8156 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; |
| 8157 | ptr += nr_cpu_ids * sizeof(void **); |
| 8158 | |
| 8159 | root_task_group.rt_rq = (struct rt_rq **)ptr; |
| 8160 | ptr += nr_cpu_ids * sizeof(void **); |
| 8161 | #endif |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8162 | #endif |
| 8163 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8164 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 8165 | #ifdef CONFIG_SMP |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8166 | init_aggregate(); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 8167 | init_defrootdomain(); |
| 8168 | #endif |
| 8169 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8170 | init_rt_bandwidth(&def_rt_bandwidth, |
| 8171 | global_rt_period(), global_rt_runtime()); |
| 8172 | |
| 8173 | #ifdef CONFIG_RT_GROUP_SCHED |
| 8174 | init_rt_bandwidth(&init_task_group.rt_bandwidth, |
| 8175 | global_rt_period(), global_rt_runtime()); |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8176 | #ifdef CONFIG_USER_SCHED |
| 8177 | init_rt_bandwidth(&root_task_group.rt_bandwidth, |
| 8178 | global_rt_period(), RUNTIME_INF); |
| 8179 | #endif |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8180 | #endif |
| 8181 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8182 | #ifdef CONFIG_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8183 | list_add(&init_task_group.list, &task_groups); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8184 | INIT_LIST_HEAD(&init_task_group.children); |
| 8185 | |
| 8186 | #ifdef CONFIG_USER_SCHED |
| 8187 | INIT_LIST_HEAD(&root_task_group.children); |
| 8188 | init_task_group.parent = &root_task_group; |
| 8189 | list_add(&init_task_group.siblings, &root_task_group.children); |
| 8190 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8191 | #endif |
| 8192 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 8193 | for_each_possible_cpu(i) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 8194 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8195 | |
| 8196 | rq = cpu_rq(i); |
| 8197 | spin_lock_init(&rq->lock); |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 8198 | lockdep_set_class(&rq->lock, &rq->rq_lock_key); |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 8199 | rq->nr_running = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8200 | rq->clock = 1; |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8201 | update_last_tick_seen(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8202 | init_cfs_rq(&rq->cfs, rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 8203 | init_rt_rq(&rq->rt, rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8204 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8205 | init_task_group.shares = init_task_group_load; |
| 8206 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8207 | #ifdef CONFIG_CGROUP_SCHED |
| 8208 | /* |
| 8209 | * How much cpu bandwidth does init_task_group get? |
| 8210 | * |
| 8211 | * In case of task-groups formed thr' the cgroup filesystem, it |
| 8212 | * gets 100% of the cpu resources in the system. This overall |
| 8213 | * system cpu resource is divided among the tasks of |
| 8214 | * init_task_group and its child task-groups in a fair manner, |
| 8215 | * based on each entity's (task or task-group's) weight |
| 8216 | * (se->load.weight). |
| 8217 | * |
| 8218 | * In other words, if init_task_group has 10 tasks of weight |
| 8219 | * 1024) and two child groups A0 and A1 (of weight 1024 each), |
| 8220 | * then A0's share of the cpu resource is: |
| 8221 | * |
| 8222 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% |
| 8223 | * |
| 8224 | * We achieve this by letting init_task_group's tasks sit |
| 8225 | * directly in rq->cfs (i.e init_task_group->se[] = NULL). |
| 8226 | */ |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8227 | 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] | 8228 | #elif defined CONFIG_USER_SCHED |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8229 | root_task_group.shares = NICE_0_LOAD; |
| 8230 | 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] | 8231 | /* |
| 8232 | * In case of task-groups formed thr' the user id of tasks, |
| 8233 | * init_task_group represents tasks belonging to root user. |
| 8234 | * Hence it forms a sibling of all subsequent groups formed. |
| 8235 | * In this case, init_task_group gets only a fraction of overall |
| 8236 | * system cpu resource, based on the weight assigned to root |
| 8237 | * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished |
| 8238 | * by letting tasks of init_task_group sit in a separate cfs_rq |
| 8239 | * (init_cfs_rq) and having one entity represent this group of |
| 8240 | * tasks in rq->cfs (i.e init_task_group->se[] != NULL). |
| 8241 | */ |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8242 | init_tg_cfs_entry(&init_task_group, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8243 | &per_cpu(init_cfs_rq, i), |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8244 | &per_cpu(init_sched_entity, i), i, 1, |
| 8245 | root_task_group.se[i]); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8246 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8247 | #endif |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8248 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 8249 | |
| 8250 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8251 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8252 | INIT_LIST_HEAD(&rq->leaf_rt_rq_list); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8253 | #ifdef CONFIG_CGROUP_SCHED |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8254 | 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] | 8255 | #elif defined CONFIG_USER_SCHED |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8256 | 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] | 8257 | init_tg_rt_entry(&init_task_group, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8258 | &per_cpu(init_rt_rq, i), |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8259 | &per_cpu(init_sched_rt_entity, i), i, 1, |
| 8260 | root_task_group.rt_se[i]); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8261 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8262 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8263 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8264 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) |
| 8265 | rq->cpu_load[j] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8266 | #ifdef CONFIG_SMP |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 8267 | rq->sd = NULL; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 8268 | rq->rd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8269 | rq->active_balance = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8270 | rq->next_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8271 | rq->push_cpu = 0; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 8272 | rq->cpu = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8273 | rq->migration_thread = NULL; |
| 8274 | INIT_LIST_HEAD(&rq->migration_queue); |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 8275 | rq_attach_root(rq, &def_root_domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8276 | #endif |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 8277 | init_rq_hrtick(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8278 | atomic_set(&rq->nr_iowait, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8279 | } |
| 8280 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 8281 | set_load_weight(&init_task); |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 8282 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 8283 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 8284 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); |
| 8285 | #endif |
| 8286 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 8287 | #ifdef CONFIG_SMP |
| 8288 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL); |
| 8289 | #endif |
| 8290 | |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 8291 | #ifdef CONFIG_RT_MUTEXES |
| 8292 | plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); |
| 8293 | #endif |
| 8294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8295 | /* |
| 8296 | * The boot idle thread does lazy MMU switching as well: |
| 8297 | */ |
| 8298 | atomic_inc(&init_mm.mm_count); |
| 8299 | enter_lazy_tlb(&init_mm, current); |
| 8300 | |
| 8301 | /* |
| 8302 | * Make us the idle thread. Technically, schedule() should not be |
| 8303 | * called from this thread, however somewhere below it might be, |
| 8304 | * but because we are the idle thread, we just pick up running again |
| 8305 | * when this runqueue becomes "idle". |
| 8306 | */ |
| 8307 | init_idle(current, smp_processor_id()); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8308 | /* |
| 8309 | * During early bootup we pretend to be a normal task: |
| 8310 | */ |
| 8311 | current->sched_class = &fair_sched_class; |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 8312 | |
| 8313 | scheduler_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8314 | } |
| 8315 | |
| 8316 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 8317 | void __might_sleep(char *file, int line) |
| 8318 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 8319 | #ifdef in_atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8320 | static unsigned long prev_jiffy; /* ratelimiting */ |
| 8321 | |
| 8322 | if ((in_atomic() || irqs_disabled()) && |
| 8323 | system_state == SYSTEM_RUNNING && !oops_in_progress) { |
| 8324 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 8325 | return; |
| 8326 | prev_jiffy = jiffies; |
Ingo Molnar | 91368d7 | 2006-03-23 03:00:54 -0800 | [diff] [blame] | 8327 | printk(KERN_ERR "BUG: sleeping function called from invalid" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8328 | " context at %s:%d\n", file, line); |
| 8329 | printk("in_atomic():%d, irqs_disabled():%d\n", |
| 8330 | in_atomic(), irqs_disabled()); |
Peter Zijlstra | a4c410f | 2006-12-06 20:37:21 -0800 | [diff] [blame] | 8331 | debug_show_held_locks(current); |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 8332 | if (irqs_disabled()) |
| 8333 | print_irqtrace_events(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8334 | dump_stack(); |
| 8335 | } |
| 8336 | #endif |
| 8337 | } |
| 8338 | EXPORT_SYMBOL(__might_sleep); |
| 8339 | #endif |
| 8340 | |
| 8341 | #ifdef CONFIG_MAGIC_SYSRQ |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 8342 | static void normalize_task(struct rq *rq, struct task_struct *p) |
| 8343 | { |
| 8344 | int on_rq; |
| 8345 | update_rq_clock(rq); |
| 8346 | on_rq = p->se.on_rq; |
| 8347 | if (on_rq) |
| 8348 | deactivate_task(rq, p, 0); |
| 8349 | __setscheduler(rq, p, SCHED_NORMAL, 0); |
| 8350 | if (on_rq) { |
| 8351 | activate_task(rq, p, 0); |
| 8352 | resched_task(rq->curr); |
| 8353 | } |
| 8354 | } |
| 8355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8356 | void normalize_rt_tasks(void) |
| 8357 | { |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 8358 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8359 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 8360 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8361 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8362 | read_lock_irqsave(&tasklist_lock, flags); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 8363 | do_each_thread(g, p) { |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 8364 | /* |
| 8365 | * Only normalize user tasks: |
| 8366 | */ |
| 8367 | if (!p->mm) |
| 8368 | continue; |
| 8369 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8370 | p->se.exec_start = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 8371 | #ifdef CONFIG_SCHEDSTATS |
| 8372 | p->se.wait_start = 0; |
| 8373 | p->se.sleep_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8374 | p->se.block_start = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 8375 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8376 | task_rq(p)->clock = 0; |
| 8377 | |
| 8378 | if (!rt_task(p)) { |
| 8379 | /* |
| 8380 | * Renice negative nice level userspace |
| 8381 | * tasks back to 0: |
| 8382 | */ |
| 8383 | if (TASK_NICE(p) < 0 && p->mm) |
| 8384 | set_user_nice(p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8385 | continue; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8387 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8388 | spin_lock(&p->pi_lock); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 8389 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8390 | |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 8391 | normalize_task(rq, p); |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 8392 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 8393 | __task_rq_unlock(rq); |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8394 | spin_unlock(&p->pi_lock); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 8395 | } while_each_thread(g, p); |
| 8396 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8397 | read_unlock_irqrestore(&tasklist_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8398 | } |
| 8399 | |
| 8400 | #endif /* CONFIG_MAGIC_SYSRQ */ |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 8401 | |
| 8402 | #ifdef CONFIG_IA64 |
| 8403 | /* |
| 8404 | * These functions are only useful for the IA64 MCA handling. |
| 8405 | * |
| 8406 | * They can only be called when the whole system has been |
| 8407 | * stopped - every CPU needs to be quiescent, and no scheduling |
| 8408 | * activity can take place. Using them for anything else would |
| 8409 | * be a serious bug, and as a result, they aren't even visible |
| 8410 | * under any other configuration. |
| 8411 | */ |
| 8412 | |
| 8413 | /** |
| 8414 | * curr_task - return the current task for a given cpu. |
| 8415 | * @cpu: the processor in question. |
| 8416 | * |
| 8417 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 8418 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 8419 | struct task_struct *curr_task(int cpu) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 8420 | { |
| 8421 | return cpu_curr(cpu); |
| 8422 | } |
| 8423 | |
| 8424 | /** |
| 8425 | * set_curr_task - set the current task for a given cpu. |
| 8426 | * @cpu: the processor in question. |
| 8427 | * @p: the task pointer to set. |
| 8428 | * |
| 8429 | * Description: This function must only be used when non-maskable interrupts |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8430 | * are serviced on a separate stack. It allows the architecture to switch the |
| 8431 | * 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] | 8432 | * must be called with all CPU's synchronized, and interrupts disabled, the |
| 8433 | * and caller must save the original value of the current task (see |
| 8434 | * curr_task() above) and restore that value before reenabling interrupts and |
| 8435 | * re-starting the system. |
| 8436 | * |
| 8437 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 8438 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 8439 | void set_curr_task(int cpu, struct task_struct *p) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 8440 | { |
| 8441 | cpu_curr(cpu) = p; |
| 8442 | } |
| 8443 | |
| 8444 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8445 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8446 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8447 | static void free_fair_sched_group(struct task_group *tg) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8448 | { |
| 8449 | int i; |
| 8450 | |
| 8451 | for_each_possible_cpu(i) { |
| 8452 | if (tg->cfs_rq) |
| 8453 | kfree(tg->cfs_rq[i]); |
| 8454 | if (tg->se) |
| 8455 | kfree(tg->se[i]); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8456 | } |
| 8457 | |
| 8458 | kfree(tg->cfs_rq); |
| 8459 | kfree(tg->se); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8460 | } |
| 8461 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8462 | static |
| 8463 | 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] | 8464 | { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8465 | struct cfs_rq *cfs_rq; |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8466 | struct sched_entity *se, *parent_se; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8467 | struct rq *rq; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8468 | int i; |
| 8469 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8470 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8471 | if (!tg->cfs_rq) |
| 8472 | goto err; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8473 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8474 | if (!tg->se) |
| 8475 | goto err; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8476 | |
| 8477 | tg->shares = NICE_0_LOAD; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8478 | |
| 8479 | for_each_possible_cpu(i) { |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8480 | rq = cpu_rq(i); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8481 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8482 | cfs_rq = kmalloc_node(sizeof(struct cfs_rq), |
| 8483 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8484 | if (!cfs_rq) |
| 8485 | goto err; |
| 8486 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8487 | se = kmalloc_node(sizeof(struct sched_entity), |
| 8488 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8489 | if (!se) |
| 8490 | goto err; |
| 8491 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8492 | parent_se = parent ? parent->se[i] : NULL; |
| 8493 | init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8494 | } |
| 8495 | |
| 8496 | return 1; |
| 8497 | |
| 8498 | err: |
| 8499 | return 0; |
| 8500 | } |
| 8501 | |
| 8502 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) |
| 8503 | { |
| 8504 | list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list, |
| 8505 | &cpu_rq(cpu)->leaf_cfs_rq_list); |
| 8506 | } |
| 8507 | |
| 8508 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 8509 | { |
| 8510 | list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list); |
| 8511 | } |
| 8512 | #else |
| 8513 | static inline void free_fair_sched_group(struct task_group *tg) |
| 8514 | { |
| 8515 | } |
| 8516 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8517 | static inline |
| 8518 | 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] | 8519 | { |
| 8520 | return 1; |
| 8521 | } |
| 8522 | |
| 8523 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) |
| 8524 | { |
| 8525 | } |
| 8526 | |
| 8527 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 8528 | { |
| 8529 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8530 | #endif |
| 8531 | |
| 8532 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8533 | static void free_rt_sched_group(struct task_group *tg) |
| 8534 | { |
| 8535 | int i; |
| 8536 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8537 | destroy_rt_bandwidth(&tg->rt_bandwidth); |
| 8538 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8539 | for_each_possible_cpu(i) { |
| 8540 | if (tg->rt_rq) |
| 8541 | kfree(tg->rt_rq[i]); |
| 8542 | if (tg->rt_se) |
| 8543 | kfree(tg->rt_se[i]); |
| 8544 | } |
| 8545 | |
| 8546 | kfree(tg->rt_rq); |
| 8547 | kfree(tg->rt_se); |
| 8548 | } |
| 8549 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8550 | static |
| 8551 | 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] | 8552 | { |
| 8553 | struct rt_rq *rt_rq; |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8554 | struct sched_rt_entity *rt_se, *parent_se; |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8555 | struct rq *rq; |
| 8556 | int i; |
| 8557 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8558 | tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8559 | if (!tg->rt_rq) |
| 8560 | goto err; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8561 | tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8562 | if (!tg->rt_se) |
| 8563 | goto err; |
| 8564 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8565 | init_rt_bandwidth(&tg->rt_bandwidth, |
| 8566 | ktime_to_ns(def_rt_bandwidth.rt_period), 0); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8567 | |
| 8568 | for_each_possible_cpu(i) { |
| 8569 | rq = cpu_rq(i); |
| 8570 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8571 | rt_rq = kmalloc_node(sizeof(struct rt_rq), |
| 8572 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
| 8573 | if (!rt_rq) |
| 8574 | goto err; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8575 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8576 | rt_se = kmalloc_node(sizeof(struct sched_rt_entity), |
| 8577 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
| 8578 | if (!rt_se) |
| 8579 | goto err; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8580 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8581 | parent_se = parent ? parent->rt_se[i] : NULL; |
| 8582 | 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] | 8583 | } |
| 8584 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8585 | return 1; |
| 8586 | |
| 8587 | err: |
| 8588 | return 0; |
| 8589 | } |
| 8590 | |
| 8591 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) |
| 8592 | { |
| 8593 | list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list, |
| 8594 | &cpu_rq(cpu)->leaf_rt_rq_list); |
| 8595 | } |
| 8596 | |
| 8597 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) |
| 8598 | { |
| 8599 | list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list); |
| 8600 | } |
| 8601 | #else |
| 8602 | static inline void free_rt_sched_group(struct task_group *tg) |
| 8603 | { |
| 8604 | } |
| 8605 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8606 | static inline |
| 8607 | 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] | 8608 | { |
| 8609 | return 1; |
| 8610 | } |
| 8611 | |
| 8612 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) |
| 8613 | { |
| 8614 | } |
| 8615 | |
| 8616 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) |
| 8617 | { |
| 8618 | } |
| 8619 | #endif |
| 8620 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8621 | #ifdef CONFIG_GROUP_SCHED |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8622 | static void free_sched_group(struct task_group *tg) |
| 8623 | { |
| 8624 | free_fair_sched_group(tg); |
| 8625 | free_rt_sched_group(tg); |
| 8626 | kfree(tg); |
| 8627 | } |
| 8628 | |
| 8629 | /* allocate runqueue etc for a new task group */ |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8630 | struct task_group *sched_create_group(struct task_group *parent) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8631 | { |
| 8632 | struct task_group *tg; |
| 8633 | unsigned long flags; |
| 8634 | int i; |
| 8635 | |
| 8636 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); |
| 8637 | if (!tg) |
| 8638 | return ERR_PTR(-ENOMEM); |
| 8639 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8640 | if (!alloc_fair_sched_group(tg, parent)) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8641 | goto err; |
| 8642 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8643 | if (!alloc_rt_sched_group(tg, parent)) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8644 | goto err; |
| 8645 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8646 | spin_lock_irqsave(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8647 | for_each_possible_cpu(i) { |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8648 | register_fair_sched_group(tg, i); |
| 8649 | register_rt_sched_group(tg, i); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8650 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8651 | list_add_rcu(&tg->list, &task_groups); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8652 | |
| 8653 | WARN_ON(!parent); /* root should already exist */ |
| 8654 | |
| 8655 | tg->parent = parent; |
| 8656 | list_add_rcu(&tg->siblings, &parent->children); |
| 8657 | INIT_LIST_HEAD(&tg->children); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8658 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8659 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8660 | return tg; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8661 | |
| 8662 | err: |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8663 | free_sched_group(tg); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8664 | return ERR_PTR(-ENOMEM); |
| 8665 | } |
| 8666 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8667 | /* rcu callback to free various structures associated with a task group */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8668 | static void free_sched_group_rcu(struct rcu_head *rhp) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8669 | { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8670 | /* now it should be safe to free those cfs_rqs */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8671 | free_sched_group(container_of(rhp, struct task_group, rcu)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8672 | } |
| 8673 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8674 | /* Destroy runqueue etc associated with a task group */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8675 | void sched_destroy_group(struct task_group *tg) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8676 | { |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8677 | unsigned long flags; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8678 | int i; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8679 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8680 | spin_lock_irqsave(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8681 | for_each_possible_cpu(i) { |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8682 | unregister_fair_sched_group(tg, i); |
| 8683 | unregister_rt_sched_group(tg, i); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8684 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8685 | list_del_rcu(&tg->list); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8686 | list_del_rcu(&tg->siblings); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8687 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8688 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8689 | /* wait for possible concurrent references to cfs_rqs complete */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8690 | call_rcu(&tg->rcu, free_sched_group_rcu); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8691 | } |
| 8692 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8693 | /* change task's runqueue when it moves between groups. |
Ingo Molnar | 3a25201 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 8694 | * The caller of this function should have put the task in its new group |
| 8695 | * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to |
| 8696 | * reflect its new group. |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8697 | */ |
| 8698 | void sched_move_task(struct task_struct *tsk) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8699 | { |
| 8700 | int on_rq, running; |
| 8701 | unsigned long flags; |
| 8702 | struct rq *rq; |
| 8703 | |
| 8704 | rq = task_rq_lock(tsk, &flags); |
| 8705 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8706 | update_rq_clock(rq); |
| 8707 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 8708 | running = task_current(rq, tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8709 | on_rq = tsk->se.on_rq; |
| 8710 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 8711 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8712 | dequeue_task(rq, tsk, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 8713 | if (unlikely(running)) |
| 8714 | tsk->sched_class->put_prev_task(rq, tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8715 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8716 | set_task_rq(tsk, task_cpu(tsk)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8717 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 8718 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8719 | if (tsk->sched_class->moved_group) |
| 8720 | tsk->sched_class->moved_group(tsk); |
| 8721 | #endif |
| 8722 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 8723 | if (unlikely(running)) |
| 8724 | tsk->sched_class->set_curr_task(rq); |
| 8725 | if (on_rq) |
Dmitry Adamushko | 7074bad | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8726 | enqueue_task(rq, tsk, 0); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8727 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8728 | task_rq_unlock(rq, &flags); |
| 8729 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8730 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8731 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8732 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8733 | static void __set_se_shares(struct sched_entity *se, unsigned long shares) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8734 | { |
| 8735 | struct cfs_rq *cfs_rq = se->cfs_rq; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8736 | int on_rq; |
| 8737 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8738 | on_rq = se->on_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8739 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8740 | dequeue_entity(cfs_rq, se, 0); |
| 8741 | |
| 8742 | se->load.weight = shares; |
| 8743 | se->load.inv_weight = div64_64((1ULL<<32), shares); |
| 8744 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8745 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8746 | enqueue_entity(cfs_rq, se, 0); |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8747 | } |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8748 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8749 | static void set_se_shares(struct sched_entity *se, unsigned long shares) |
| 8750 | { |
| 8751 | struct cfs_rq *cfs_rq = se->cfs_rq; |
| 8752 | struct rq *rq = cfs_rq->rq; |
| 8753 | unsigned long flags; |
| 8754 | |
| 8755 | spin_lock_irqsave(&rq->lock, flags); |
| 8756 | __set_se_shares(se, shares); |
| 8757 | spin_unlock_irqrestore(&rq->lock, flags); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8758 | } |
| 8759 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8760 | static DEFINE_MUTEX(shares_mutex); |
| 8761 | |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8762 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8763 | { |
| 8764 | int i; |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8765 | unsigned long flags; |
Ingo Molnar | c61935f | 2008-01-22 11:24:58 +0100 | [diff] [blame] | 8766 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8767 | /* |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8768 | * We can't change the weight of the root cgroup. |
| 8769 | */ |
| 8770 | if (!tg->se[0]) |
| 8771 | return -EINVAL; |
| 8772 | |
| 8773 | /* |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8774 | * A weight of 0 or 1 can cause arithmetics problems. |
| 8775 | * (The default weight is 1024 - so there's no practical |
| 8776 | * limitation from this.) |
| 8777 | */ |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8778 | if (shares < MIN_SHARES) |
| 8779 | shares = MIN_SHARES; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8780 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8781 | mutex_lock(&shares_mutex); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8782 | if (tg->shares == shares) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8783 | goto done; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8784 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8785 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8786 | for_each_possible_cpu(i) |
| 8787 | unregister_fair_sched_group(tg, i); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8788 | list_del_rcu(&tg->siblings); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8789 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 8790 | |
| 8791 | /* wait for any ongoing reference to this group to finish */ |
| 8792 | synchronize_sched(); |
| 8793 | |
| 8794 | /* |
| 8795 | * Now we are free to modify the group's share on each cpu |
| 8796 | * w/o tripping rebalance_share or load_balance_fair. |
| 8797 | */ |
| 8798 | tg->shares = shares; |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8799 | for_each_possible_cpu(i) { |
| 8800 | /* |
| 8801 | * force a rebalance |
| 8802 | */ |
| 8803 | cfs_rq_set_shares(tg->cfs_rq[i], 0); |
| 8804 | set_se_shares(tg->se[i], shares/nr_cpu_ids); |
| 8805 | } |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 8806 | |
| 8807 | /* |
| 8808 | * Enable load balance activity on this group, by inserting it back on |
| 8809 | * each cpu's rq->leaf_cfs_rq_list. |
| 8810 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8811 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8812 | for_each_possible_cpu(i) |
| 8813 | register_fair_sched_group(tg, i); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8814 | list_add_rcu(&tg->siblings, &tg->parent->children); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8815 | spin_unlock_irqrestore(&task_group_lock, flags); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8816 | done: |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8817 | mutex_unlock(&shares_mutex); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8818 | return 0; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8819 | } |
| 8820 | |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8821 | unsigned long sched_group_shares(struct task_group *tg) |
| 8822 | { |
| 8823 | return tg->shares; |
| 8824 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8825 | #endif |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8826 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8827 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8828 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8829 | * Ensure that the real time constraints are schedulable. |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8830 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8831 | static DEFINE_MUTEX(rt_constraints_mutex); |
| 8832 | |
| 8833 | static unsigned long to_ratio(u64 period, u64 runtime) |
| 8834 | { |
| 8835 | if (runtime == RUNTIME_INF) |
| 8836 | return 1ULL << 16; |
| 8837 | |
Peter Zijlstra | 2692a24 | 2008-02-27 12:00:46 +0100 | [diff] [blame] | 8838 | return div64_64(runtime << 16, period); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8839 | } |
| 8840 | |
Peter Zijlstra | b40b2e8 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8841 | #ifdef CONFIG_CGROUP_SCHED |
| 8842 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
| 8843 | { |
| 8844 | struct task_group *tgi, *parent = tg->parent; |
| 8845 | unsigned long total = 0; |
| 8846 | |
| 8847 | if (!parent) { |
| 8848 | if (global_rt_period() < period) |
| 8849 | return 0; |
| 8850 | |
| 8851 | return to_ratio(period, runtime) < |
| 8852 | to_ratio(global_rt_period(), global_rt_runtime()); |
| 8853 | } |
| 8854 | |
| 8855 | if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period) |
| 8856 | return 0; |
| 8857 | |
| 8858 | rcu_read_lock(); |
| 8859 | list_for_each_entry_rcu(tgi, &parent->children, siblings) { |
| 8860 | if (tgi == tg) |
| 8861 | continue; |
| 8862 | |
| 8863 | total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period), |
| 8864 | tgi->rt_bandwidth.rt_runtime); |
| 8865 | } |
| 8866 | rcu_read_unlock(); |
| 8867 | |
| 8868 | return total + to_ratio(period, runtime) < |
| 8869 | to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period), |
| 8870 | parent->rt_bandwidth.rt_runtime); |
| 8871 | } |
| 8872 | #elif defined CONFIG_USER_SCHED |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8873 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8874 | { |
| 8875 | struct task_group *tgi; |
| 8876 | unsigned long total = 0; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8877 | unsigned long global_ratio = |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8878 | to_ratio(global_rt_period(), global_rt_runtime()); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8879 | |
| 8880 | rcu_read_lock(); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8881 | list_for_each_entry_rcu(tgi, &task_groups, list) { |
| 8882 | if (tgi == tg) |
| 8883 | continue; |
| 8884 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8885 | total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period), |
| 8886 | tgi->rt_bandwidth.rt_runtime); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8887 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8888 | rcu_read_unlock(); |
| 8889 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8890 | return total + to_ratio(period, runtime) < global_ratio; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8891 | } |
Peter Zijlstra | b40b2e8 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8892 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8893 | |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8894 | /* Must be called with tasklist_lock held */ |
| 8895 | static inline int tg_has_rt_tasks(struct task_group *tg) |
| 8896 | { |
| 8897 | struct task_struct *g, *p; |
| 8898 | do_each_thread(g, p) { |
| 8899 | if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg) |
| 8900 | return 1; |
| 8901 | } while_each_thread(g, p); |
| 8902 | return 0; |
| 8903 | } |
| 8904 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8905 | static int tg_set_bandwidth(struct task_group *tg, |
| 8906 | u64 rt_period, u64 rt_runtime) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8907 | { |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8908 | int i, err = 0; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8909 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8910 | mutex_lock(&rt_constraints_mutex); |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8911 | read_lock(&tasklist_lock); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8912 | if (rt_runtime == 0 && tg_has_rt_tasks(tg)) { |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8913 | err = -EBUSY; |
| 8914 | goto unlock; |
| 8915 | } |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8916 | if (!__rt_schedulable(tg, rt_period, rt_runtime)) { |
| 8917 | err = -EINVAL; |
| 8918 | goto unlock; |
| 8919 | } |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8920 | |
| 8921 | spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock); |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8922 | tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period); |
| 8923 | tg->rt_bandwidth.rt_runtime = rt_runtime; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8924 | |
| 8925 | for_each_possible_cpu(i) { |
| 8926 | struct rt_rq *rt_rq = tg->rt_rq[i]; |
| 8927 | |
| 8928 | spin_lock(&rt_rq->rt_runtime_lock); |
| 8929 | rt_rq->rt_runtime = rt_runtime; |
| 8930 | spin_unlock(&rt_rq->rt_runtime_lock); |
| 8931 | } |
| 8932 | spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8933 | unlock: |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8934 | read_unlock(&tasklist_lock); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8935 | mutex_unlock(&rt_constraints_mutex); |
| 8936 | |
| 8937 | return err; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8938 | } |
| 8939 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8940 | int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) |
| 8941 | { |
| 8942 | u64 rt_runtime, rt_period; |
| 8943 | |
| 8944 | rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period); |
| 8945 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; |
| 8946 | if (rt_runtime_us < 0) |
| 8947 | rt_runtime = RUNTIME_INF; |
| 8948 | |
| 8949 | return tg_set_bandwidth(tg, rt_period, rt_runtime); |
| 8950 | } |
| 8951 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8952 | long sched_group_rt_runtime(struct task_group *tg) |
| 8953 | { |
| 8954 | u64 rt_runtime_us; |
| 8955 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8956 | if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF) |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8957 | return -1; |
| 8958 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8959 | rt_runtime_us = tg->rt_bandwidth.rt_runtime; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8960 | do_div(rt_runtime_us, NSEC_PER_USEC); |
| 8961 | return rt_runtime_us; |
| 8962 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8963 | |
| 8964 | int sched_group_set_rt_period(struct task_group *tg, long rt_period_us) |
| 8965 | { |
| 8966 | u64 rt_runtime, rt_period; |
| 8967 | |
| 8968 | rt_period = (u64)rt_period_us * NSEC_PER_USEC; |
| 8969 | rt_runtime = tg->rt_bandwidth.rt_runtime; |
| 8970 | |
| 8971 | return tg_set_bandwidth(tg, rt_period, rt_runtime); |
| 8972 | } |
| 8973 | |
| 8974 | long sched_group_rt_period(struct task_group *tg) |
| 8975 | { |
| 8976 | u64 rt_period_us; |
| 8977 | |
| 8978 | rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period); |
| 8979 | do_div(rt_period_us, NSEC_PER_USEC); |
| 8980 | return rt_period_us; |
| 8981 | } |
| 8982 | |
| 8983 | static int sched_rt_global_constraints(void) |
| 8984 | { |
| 8985 | int ret = 0; |
| 8986 | |
| 8987 | mutex_lock(&rt_constraints_mutex); |
| 8988 | if (!__rt_schedulable(NULL, 1, 0)) |
| 8989 | ret = -EINVAL; |
| 8990 | mutex_unlock(&rt_constraints_mutex); |
| 8991 | |
| 8992 | return ret; |
| 8993 | } |
| 8994 | #else |
| 8995 | static int sched_rt_global_constraints(void) |
| 8996 | { |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8997 | unsigned long flags; |
| 8998 | int i; |
| 8999 | |
| 9000 | spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); |
| 9001 | for_each_possible_cpu(i) { |
| 9002 | struct rt_rq *rt_rq = &cpu_rq(i)->rt; |
| 9003 | |
| 9004 | spin_lock(&rt_rq->rt_runtime_lock); |
| 9005 | rt_rq->rt_runtime = global_rt_runtime(); |
| 9006 | spin_unlock(&rt_rq->rt_runtime_lock); |
| 9007 | } |
| 9008 | spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags); |
| 9009 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 9010 | return 0; |
| 9011 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9012 | #endif |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 9013 | |
| 9014 | int sched_rt_handler(struct ctl_table *table, int write, |
| 9015 | struct file *filp, void __user *buffer, size_t *lenp, |
| 9016 | loff_t *ppos) |
| 9017 | { |
| 9018 | int ret; |
| 9019 | int old_period, old_runtime; |
| 9020 | static DEFINE_MUTEX(mutex); |
| 9021 | |
| 9022 | mutex_lock(&mutex); |
| 9023 | old_period = sysctl_sched_rt_period; |
| 9024 | old_runtime = sysctl_sched_rt_runtime; |
| 9025 | |
| 9026 | ret = proc_dointvec(table, write, filp, buffer, lenp, ppos); |
| 9027 | |
| 9028 | if (!ret && write) { |
| 9029 | ret = sched_rt_global_constraints(); |
| 9030 | if (ret) { |
| 9031 | sysctl_sched_rt_period = old_period; |
| 9032 | sysctl_sched_rt_runtime = old_runtime; |
| 9033 | } else { |
| 9034 | def_rt_bandwidth.rt_runtime = global_rt_runtime(); |
| 9035 | def_rt_bandwidth.rt_period = |
| 9036 | ns_to_ktime(global_rt_period()); |
| 9037 | } |
| 9038 | } |
| 9039 | mutex_unlock(&mutex); |
| 9040 | |
| 9041 | return ret; |
| 9042 | } |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9043 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9044 | #ifdef CONFIG_CGROUP_SCHED |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9045 | |
| 9046 | /* return corresponding task_group object of a cgroup */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9047 | static inline struct task_group *cgroup_tg(struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9048 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9049 | return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id), |
| 9050 | struct task_group, css); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9051 | } |
| 9052 | |
| 9053 | static struct cgroup_subsys_state * |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9054 | cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9055 | { |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9056 | struct task_group *tg, *parent; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9057 | |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9058 | if (!cgrp->parent) { |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9059 | /* This is early initialization for the top cgroup */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9060 | init_task_group.css.cgroup = cgrp; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9061 | return &init_task_group.css; |
| 9062 | } |
| 9063 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9064 | parent = cgroup_tg(cgrp->parent); |
| 9065 | tg = sched_create_group(parent); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9066 | if (IS_ERR(tg)) |
| 9067 | return ERR_PTR(-ENOMEM); |
| 9068 | |
| 9069 | /* Bind the cgroup to task_group object we just created */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9070 | tg->css.cgroup = cgrp; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9071 | |
| 9072 | return &tg->css; |
| 9073 | } |
| 9074 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 9075 | static void |
| 9076 | cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9077 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9078 | struct task_group *tg = cgroup_tg(cgrp); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9079 | |
| 9080 | sched_destroy_group(tg); |
| 9081 | } |
| 9082 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 9083 | static int |
| 9084 | cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
| 9085 | struct task_struct *tsk) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9086 | { |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9087 | #ifdef CONFIG_RT_GROUP_SCHED |
| 9088 | /* 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] | 9089 | if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0) |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9090 | return -EINVAL; |
| 9091 | #else |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9092 | /* We don't support RT-tasks being in separate groups */ |
| 9093 | if (tsk->sched_class != &fair_sched_class) |
| 9094 | return -EINVAL; |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9095 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9096 | |
| 9097 | return 0; |
| 9098 | } |
| 9099 | |
| 9100 | static void |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9101 | cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9102 | struct cgroup *old_cont, struct task_struct *tsk) |
| 9103 | { |
| 9104 | sched_move_task(tsk); |
| 9105 | } |
| 9106 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9107 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9108 | static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype, |
| 9109 | u64 shareval) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9110 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9111 | return sched_group_set_shares(cgroup_tg(cgrp), shareval); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9112 | } |
| 9113 | |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9114 | static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9115 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 9116 | struct task_group *tg = cgroup_tg(cgrp); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9117 | |
| 9118 | return (u64) tg->shares; |
| 9119 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9120 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9121 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9122 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 9123 | 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] | 9124 | struct file *file, |
| 9125 | const char __user *userbuf, |
| 9126 | size_t nbytes, loff_t *unused_ppos) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9127 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9128 | char buffer[64]; |
| 9129 | int retval = 0; |
| 9130 | s64 val; |
| 9131 | char *end; |
| 9132 | |
| 9133 | if (!nbytes) |
| 9134 | return -EINVAL; |
| 9135 | if (nbytes >= sizeof(buffer)) |
| 9136 | return -E2BIG; |
| 9137 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 9138 | return -EFAULT; |
| 9139 | |
| 9140 | buffer[nbytes] = 0; /* nul-terminate */ |
| 9141 | |
| 9142 | /* strip newline if necessary */ |
| 9143 | if (nbytes && (buffer[nbytes-1] == '\n')) |
| 9144 | buffer[nbytes-1] = 0; |
| 9145 | val = simple_strtoll(buffer, &end, 0); |
| 9146 | if (*end) |
| 9147 | return -EINVAL; |
| 9148 | |
| 9149 | /* Pass to subsystem */ |
| 9150 | retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val); |
| 9151 | if (!retval) |
| 9152 | retval = nbytes; |
| 9153 | return retval; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9154 | } |
| 9155 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9156 | static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft, |
| 9157 | struct file *file, |
| 9158 | char __user *buf, size_t nbytes, |
| 9159 | loff_t *ppos) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9160 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9161 | char tmp[64]; |
| 9162 | long val = sched_group_rt_runtime(cgroup_tg(cgrp)); |
| 9163 | int len = sprintf(tmp, "%ld\n", val); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9164 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9165 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9166 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 9167 | |
| 9168 | static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype, |
| 9169 | u64 rt_period_us) |
| 9170 | { |
| 9171 | return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us); |
| 9172 | } |
| 9173 | |
| 9174 | static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft) |
| 9175 | { |
| 9176 | return sched_group_rt_period(cgroup_tg(cgrp)); |
| 9177 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9178 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9179 | |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 9180 | static struct cftype cpu_files[] = { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9181 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 9182 | { |
| 9183 | .name = "shares", |
| 9184 | .read_uint = cpu_shares_read_uint, |
| 9185 | .write_uint = cpu_shares_write_uint, |
| 9186 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9187 | #endif |
| 9188 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9189 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9190 | .name = "rt_runtime_us", |
| 9191 | .read = cpu_rt_runtime_read, |
| 9192 | .write = cpu_rt_runtime_write, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9193 | }, |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 9194 | { |
| 9195 | .name = "rt_period_us", |
| 9196 | .read_uint = cpu_rt_period_read_uint, |
| 9197 | .write_uint = cpu_rt_period_write_uint, |
| 9198 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9199 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9200 | }; |
| 9201 | |
| 9202 | static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont) |
| 9203 | { |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 9204 | return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files)); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9205 | } |
| 9206 | |
| 9207 | struct cgroup_subsys cpu_cgroup_subsys = { |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 9208 | .name = "cpu", |
| 9209 | .create = cpu_cgroup_create, |
| 9210 | .destroy = cpu_cgroup_destroy, |
| 9211 | .can_attach = cpu_cgroup_can_attach, |
| 9212 | .attach = cpu_cgroup_attach, |
| 9213 | .populate = cpu_cgroup_populate, |
| 9214 | .subsys_id = cpu_cgroup_subsys_id, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9215 | .early_init = 1, |
| 9216 | }; |
| 9217 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9218 | #endif /* CONFIG_CGROUP_SCHED */ |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9219 | |
| 9220 | #ifdef CONFIG_CGROUP_CPUACCT |
| 9221 | |
| 9222 | /* |
| 9223 | * CPU accounting code for task groups. |
| 9224 | * |
| 9225 | * Based on the work by Paul Menage (menage@google.com) and Balbir Singh |
| 9226 | * (balbir@in.ibm.com). |
| 9227 | */ |
| 9228 | |
| 9229 | /* track cpu usage of a group of tasks */ |
| 9230 | struct cpuacct { |
| 9231 | struct cgroup_subsys_state css; |
| 9232 | /* cpuusage holds pointer to a u64-type object on every cpu */ |
| 9233 | u64 *cpuusage; |
| 9234 | }; |
| 9235 | |
| 9236 | struct cgroup_subsys cpuacct_subsys; |
| 9237 | |
| 9238 | /* return cpu accounting group corresponding to this container */ |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9239 | static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9240 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9241 | return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id), |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9242 | struct cpuacct, css); |
| 9243 | } |
| 9244 | |
| 9245 | /* return cpu accounting group to which this task belongs */ |
| 9246 | static inline struct cpuacct *task_ca(struct task_struct *tsk) |
| 9247 | { |
| 9248 | return container_of(task_subsys_state(tsk, cpuacct_subsys_id), |
| 9249 | struct cpuacct, css); |
| 9250 | } |
| 9251 | |
| 9252 | /* create a new cpu accounting group */ |
| 9253 | static struct cgroup_subsys_state *cpuacct_create( |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9254 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9255 | { |
| 9256 | struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL); |
| 9257 | |
| 9258 | if (!ca) |
| 9259 | return ERR_PTR(-ENOMEM); |
| 9260 | |
| 9261 | ca->cpuusage = alloc_percpu(u64); |
| 9262 | if (!ca->cpuusage) { |
| 9263 | kfree(ca); |
| 9264 | return ERR_PTR(-ENOMEM); |
| 9265 | } |
| 9266 | |
| 9267 | return &ca->css; |
| 9268 | } |
| 9269 | |
| 9270 | /* destroy an existing cpu accounting group */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 9271 | static void |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9272 | cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9273 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9274 | struct cpuacct *ca = cgroup_ca(cgrp); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9275 | |
| 9276 | free_percpu(ca->cpuusage); |
| 9277 | kfree(ca); |
| 9278 | } |
| 9279 | |
| 9280 | /* return total cpu usage (in nanoseconds) of a group */ |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9281 | static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9282 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9283 | struct cpuacct *ca = cgroup_ca(cgrp); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9284 | u64 totalcpuusage = 0; |
| 9285 | int i; |
| 9286 | |
| 9287 | for_each_possible_cpu(i) { |
| 9288 | u64 *cpuusage = percpu_ptr(ca->cpuusage, i); |
| 9289 | |
| 9290 | /* |
| 9291 | * Take rq->lock to make 64-bit addition safe on 32-bit |
| 9292 | * platforms. |
| 9293 | */ |
| 9294 | spin_lock_irq(&cpu_rq(i)->lock); |
| 9295 | totalcpuusage += *cpuusage; |
| 9296 | spin_unlock_irq(&cpu_rq(i)->lock); |
| 9297 | } |
| 9298 | |
| 9299 | return totalcpuusage; |
| 9300 | } |
| 9301 | |
Dhaval Giani | 0297b80 | 2008-02-29 10:02:44 +0530 | [diff] [blame] | 9302 | static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype, |
| 9303 | u64 reset) |
| 9304 | { |
| 9305 | struct cpuacct *ca = cgroup_ca(cgrp); |
| 9306 | int err = 0; |
| 9307 | int i; |
| 9308 | |
| 9309 | if (reset) { |
| 9310 | err = -EINVAL; |
| 9311 | goto out; |
| 9312 | } |
| 9313 | |
| 9314 | for_each_possible_cpu(i) { |
| 9315 | u64 *cpuusage = percpu_ptr(ca->cpuusage, i); |
| 9316 | |
| 9317 | spin_lock_irq(&cpu_rq(i)->lock); |
| 9318 | *cpuusage = 0; |
| 9319 | spin_unlock_irq(&cpu_rq(i)->lock); |
| 9320 | } |
| 9321 | out: |
| 9322 | return err; |
| 9323 | } |
| 9324 | |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9325 | static struct cftype files[] = { |
| 9326 | { |
| 9327 | .name = "usage", |
| 9328 | .read_uint = cpuusage_read, |
Dhaval Giani | 0297b80 | 2008-02-29 10:02:44 +0530 | [diff] [blame] | 9329 | .write_uint = cpuusage_write, |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9330 | }, |
| 9331 | }; |
| 9332 | |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9333 | static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9334 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 9335 | return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files)); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 9336 | } |
| 9337 | |
| 9338 | /* |
| 9339 | * charge this task's execution time to its accounting group. |
| 9340 | * |
| 9341 | * called with rq->lock held. |
| 9342 | */ |
| 9343 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime) |
| 9344 | { |
| 9345 | struct cpuacct *ca; |
| 9346 | |
| 9347 | if (!cpuacct_subsys.active) |
| 9348 | return; |
| 9349 | |
| 9350 | ca = task_ca(tsk); |
| 9351 | if (ca) { |
| 9352 | u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk)); |
| 9353 | |
| 9354 | *cpuusage += cputime; |
| 9355 | } |
| 9356 | } |
| 9357 | |
| 9358 | struct cgroup_subsys cpuacct_subsys = { |
| 9359 | .name = "cpuacct", |
| 9360 | .create = cpuacct_create, |
| 9361 | .destroy = cpuacct_destroy, |
| 9362 | .populate = cpuacct_populate, |
| 9363 | .subsys_id = cpuacct_subsys_id, |
| 9364 | }; |
| 9365 | #endif /* CONFIG_CGROUP_CPUACCT */ |