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 | /* |
| 78 | * Convert user-nice values [ -20 ... 0 ... 19 ] |
| 79 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], |
| 80 | * and back. |
| 81 | */ |
| 82 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) |
| 83 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) |
| 84 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) |
| 85 | |
| 86 | /* |
| 87 | * 'User priority' is the nice value converted to something we |
| 88 | * can work with better when scaling various scheduler parameters, |
| 89 | * it's a [ 0 ... 39 ] range. |
| 90 | */ |
| 91 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) |
| 92 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) |
| 93 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) |
| 94 | |
| 95 | /* |
Ingo Molnar | d7876a0 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 96 | * Helpers for converting nanosecond timing to jiffy resolution |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | */ |
Eric Dumazet | d6322fa | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 98 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 100 | #define NICE_0_LOAD SCHED_LOAD_SCALE |
| 101 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* |
| 104 | * These are the 'tuning knobs' of the scheduler: |
| 105 | * |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 106 | * default timeslice is 100 msecs (used only for SCHED_RR tasks). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | * Timeslices get refilled after they expire. |
| 108 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | #define DEF_TIMESLICE (100 * HZ / 1000) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 110 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 111 | /* |
| 112 | * single value that denotes runtime == period, ie unlimited time. |
| 113 | */ |
| 114 | #define RUNTIME_INF ((u64)~0ULL) |
| 115 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 116 | #ifdef CONFIG_SMP |
| 117 | /* |
| 118 | * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) |
| 119 | * Since cpu_power is a 'constant', we can use a reciprocal divide. |
| 120 | */ |
| 121 | static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load) |
| 122 | { |
| 123 | return reciprocal_divide(load, sg->reciprocal_cpu_power); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Each time a sched group cpu_power is changed, |
| 128 | * we must compute its reciprocal value |
| 129 | */ |
| 130 | static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val) |
| 131 | { |
| 132 | sg->__cpu_power += val; |
| 133 | sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power); |
| 134 | } |
| 135 | #endif |
| 136 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 137 | static inline int rt_policy(int policy) |
| 138 | { |
Roel Kluin | 3f33a7c | 2008-05-13 23:44:11 +0200 | [diff] [blame] | 139 | if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR)) |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 140 | return 1; |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static inline int task_has_rt_policy(struct task_struct *p) |
| 145 | { |
| 146 | return rt_policy(p->policy); |
| 147 | } |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | /* |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 150 | * This is the priority-queue data structure of the RT scheduling class: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | */ |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 152 | struct rt_prio_array { |
| 153 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ |
| 154 | struct list_head queue[MAX_RT_PRIO]; |
| 155 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 157 | struct rt_bandwidth { |
Ingo Molnar | ea736ed | 2008-03-25 13:51:45 +0100 | [diff] [blame] | 158 | /* nests inside the rq lock: */ |
| 159 | spinlock_t rt_runtime_lock; |
| 160 | ktime_t rt_period; |
| 161 | u64 rt_runtime; |
| 162 | struct hrtimer rt_period_timer; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static struct rt_bandwidth def_rt_bandwidth; |
| 166 | |
| 167 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); |
| 168 | |
| 169 | static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) |
| 170 | { |
| 171 | struct rt_bandwidth *rt_b = |
| 172 | container_of(timer, struct rt_bandwidth, rt_period_timer); |
| 173 | ktime_t now; |
| 174 | int overrun; |
| 175 | int idle = 0; |
| 176 | |
| 177 | for (;;) { |
| 178 | now = hrtimer_cb_get_time(timer); |
| 179 | overrun = hrtimer_forward(timer, now, rt_b->rt_period); |
| 180 | |
| 181 | if (!overrun) |
| 182 | break; |
| 183 | |
| 184 | idle = do_sched_rt_period_timer(rt_b, overrun); |
| 185 | } |
| 186 | |
| 187 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; |
| 188 | } |
| 189 | |
| 190 | static |
| 191 | void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) |
| 192 | { |
| 193 | rt_b->rt_period = ns_to_ktime(period); |
| 194 | rt_b->rt_runtime = runtime; |
| 195 | |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 196 | spin_lock_init(&rt_b->rt_runtime_lock); |
| 197 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 198 | hrtimer_init(&rt_b->rt_period_timer, |
| 199 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 200 | rt_b->rt_period_timer.function = sched_rt_period_timer; |
| 201 | rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; |
| 202 | } |
| 203 | |
| 204 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) |
| 205 | { |
| 206 | ktime_t now; |
| 207 | |
| 208 | if (rt_b->rt_runtime == RUNTIME_INF) |
| 209 | return; |
| 210 | |
| 211 | if (hrtimer_active(&rt_b->rt_period_timer)) |
| 212 | return; |
| 213 | |
| 214 | spin_lock(&rt_b->rt_runtime_lock); |
| 215 | for (;;) { |
| 216 | if (hrtimer_active(&rt_b->rt_period_timer)) |
| 217 | break; |
| 218 | |
| 219 | now = hrtimer_cb_get_time(&rt_b->rt_period_timer); |
| 220 | hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period); |
| 221 | hrtimer_start(&rt_b->rt_period_timer, |
| 222 | rt_b->rt_period_timer.expires, |
| 223 | HRTIMER_MODE_ABS); |
| 224 | } |
| 225 | spin_unlock(&rt_b->rt_runtime_lock); |
| 226 | } |
| 227 | |
| 228 | #ifdef CONFIG_RT_GROUP_SCHED |
| 229 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) |
| 230 | { |
| 231 | hrtimer_cancel(&rt_b->rt_period_timer); |
| 232 | } |
| 233 | #endif |
| 234 | |
Heiko Carstens | 712555e | 2008-04-28 11:33:07 +0200 | [diff] [blame] | 235 | /* |
| 236 | * sched_domains_mutex serializes calls to arch_init_sched_domains, |
| 237 | * detach_destroy_domains and partition_sched_domains. |
| 238 | */ |
| 239 | static DEFINE_MUTEX(sched_domains_mutex); |
| 240 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 241 | #ifdef CONFIG_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 242 | |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 243 | #include <linux/cgroup.h> |
| 244 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 245 | struct cfs_rq; |
| 246 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 247 | static LIST_HEAD(task_groups); |
| 248 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 249 | /* task group related information */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 250 | struct task_group { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 251 | #ifdef CONFIG_CGROUP_SCHED |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 252 | struct cgroup_subsys_state css; |
| 253 | #endif |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 254 | |
| 255 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 256 | /* schedulable entities of this group on each cpu */ |
| 257 | struct sched_entity **se; |
| 258 | /* runqueue "owned" by this group on each cpu */ |
| 259 | struct cfs_rq **cfs_rq; |
| 260 | unsigned long shares; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 261 | #endif |
| 262 | |
| 263 | #ifdef CONFIG_RT_GROUP_SCHED |
| 264 | struct sched_rt_entity **rt_se; |
| 265 | struct rt_rq **rt_rq; |
| 266 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 267 | struct rt_bandwidth rt_bandwidth; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 268 | #endif |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 269 | |
Srivatsa Vaddagiri | ae8393e | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 270 | struct rcu_head rcu; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 271 | struct list_head list; |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 272 | |
| 273 | struct task_group *parent; |
| 274 | struct list_head siblings; |
| 275 | struct list_head children; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 276 | }; |
| 277 | |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 278 | #ifdef CONFIG_USER_SCHED |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | * Root task group. |
| 282 | * Every UID task group (including init_task_group aka UID-0) will |
| 283 | * be a child to this group. |
| 284 | */ |
| 285 | struct task_group root_task_group; |
| 286 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 287 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 288 | /* Default task group's sched entity on each cpu */ |
| 289 | static DEFINE_PER_CPU(struct sched_entity, init_sched_entity); |
| 290 | /* Default task group's cfs_rq on each cpu */ |
| 291 | 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] | 292 | #endif |
| 293 | |
| 294 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 295 | static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity); |
| 296 | 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] | 297 | #endif |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 298 | #else |
| 299 | #define root_task_group init_task_group |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 300 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 301 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 302 | /* 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] | 303 | * a task group's cpu shares. |
| 304 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 305 | static DEFINE_SPINLOCK(task_group_lock); |
Srivatsa Vaddagiri | ec2c507 | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 306 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 307 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 308 | #ifdef CONFIG_USER_SCHED |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 309 | # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD) |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 310 | #else |
Srivatsa Vaddagiri | 93f992c | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 311 | # define INIT_TASK_GROUP_LOAD NICE_0_LOAD |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 312 | #endif |
| 313 | |
Miao Xie | cb4ad1f | 2008-04-28 12:54:56 +0800 | [diff] [blame] | 314 | /* |
| 315 | * A weight of 0, 1 or ULONG_MAX can cause arithmetics problems. |
| 316 | * (The default weight is 1024 - so there's no practical |
| 317 | * limitation from this.) |
| 318 | */ |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 319 | #define MIN_SHARES 2 |
Miao Xie | cb4ad1f | 2008-04-28 12:54:56 +0800 | [diff] [blame] | 320 | #define MAX_SHARES (ULONG_MAX - 1) |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 321 | |
Srivatsa Vaddagiri | 93f992c | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 322 | static int init_task_group_load = INIT_TASK_GROUP_LOAD; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 323 | #endif |
| 324 | |
| 325 | /* Default task group. |
| 326 | * Every task in system belong to this group at bootup. |
| 327 | */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 328 | struct task_group init_task_group; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 329 | |
| 330 | /* return group to which a task belongs */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 331 | static inline struct task_group *task_group(struct task_struct *p) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 332 | { |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 333 | struct task_group *tg; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 334 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 335 | #ifdef CONFIG_USER_SCHED |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 336 | tg = p->user->tg; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 337 | #elif defined(CONFIG_CGROUP_SCHED) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 338 | tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id), |
| 339 | struct task_group, css); |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 340 | #else |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 341 | tg = &init_task_group; |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 342 | #endif |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 343 | return tg; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* 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] | 347 | 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] | 348 | { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 349 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Dmitry Adamushko | ce96b5a | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 350 | p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; |
| 351 | p->se.parent = task_group(p)->se[cpu]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 352 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 353 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 354 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 355 | p->rt.rt_rq = task_group(p)->rt_rq[cpu]; |
| 356 | p->rt.parent = task_group(p)->rt_se[cpu]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 357 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | #else |
| 361 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 362 | 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] | 363 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 364 | #endif /* CONFIG_GROUP_SCHED */ |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 365 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 366 | /* CFS-related fields in a runqueue */ |
| 367 | struct cfs_rq { |
| 368 | struct load_weight load; |
| 369 | unsigned long nr_running; |
| 370 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 371 | u64 exec_clock; |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 372 | u64 min_vruntime; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 373 | |
| 374 | struct rb_root tasks_timeline; |
| 375 | struct rb_node *rb_leftmost; |
Peter Zijlstra | 4a55bd5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 376 | |
| 377 | struct list_head tasks; |
| 378 | struct list_head *balance_iterator; |
| 379 | |
| 380 | /* |
| 381 | * 'curr' points to currently running entity on this cfs_rq. |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 382 | * It is set to NULL otherwise (i.e when none are currently running). |
| 383 | */ |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 384 | struct sched_entity *curr, *next; |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 385 | |
| 386 | unsigned long nr_spread_over; |
| 387 | |
Ingo Molnar | 62160e3 | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 388 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 389 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ |
| 390 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 391 | /* |
| 392 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 393 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities |
| 394 | * (like users, containers etc.) |
| 395 | * |
| 396 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This |
| 397 | * list is used during load balance. |
| 398 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 399 | struct list_head leaf_cfs_rq_list; |
| 400 | struct task_group *tg; /* group that "owns" this runqueue */ |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 401 | #endif |
| 402 | }; |
| 403 | |
| 404 | /* Real-Time classes' related field in a runqueue: */ |
| 405 | struct rt_rq { |
| 406 | struct rt_prio_array active; |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 407 | unsigned long rt_nr_running; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 408 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 409 | int highest_prio; /* highest queued rt task prio */ |
| 410 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 411 | #ifdef CONFIG_SMP |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 412 | unsigned long rt_nr_migratory; |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 413 | int overloaded; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 414 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 415 | int rt_throttled; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 416 | u64 rt_time; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 417 | u64 rt_runtime; |
Ingo Molnar | ea736ed | 2008-03-25 13:51:45 +0100 | [diff] [blame] | 418 | /* Nests inside the rq lock: */ |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 419 | spinlock_t rt_runtime_lock; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 420 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 421 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 422 | unsigned long rt_nr_boosted; |
| 423 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 424 | struct rq *rq; |
| 425 | struct list_head leaf_rt_rq_list; |
| 426 | struct task_group *tg; |
| 427 | struct sched_rt_entity *rt_se; |
| 428 | #endif |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 429 | }; |
| 430 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 431 | #ifdef CONFIG_SMP |
| 432 | |
| 433 | /* |
| 434 | * 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] | 435 | * variables. Each exclusive cpuset essentially defines an island domain by |
| 436 | * fully partitioning the member cpus from any other cpuset. Whenever a new |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 437 | * exclusive cpuset is created, we also create and attach a new root-domain |
| 438 | * object. |
| 439 | * |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 440 | */ |
| 441 | struct root_domain { |
| 442 | atomic_t refcount; |
| 443 | cpumask_t span; |
| 444 | cpumask_t online; |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 445 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 446 | /* |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 447 | * The "RT overload" flag: it gets set if a CPU has more than |
| 448 | * one runnable RT task. |
| 449 | */ |
| 450 | cpumask_t rto_mask; |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 451 | atomic_t rto_count; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 452 | }; |
| 453 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 454 | /* |
| 455 | * By default the system creates a single root-domain with all cpus as |
| 456 | * members (mimicking the global state we have today). |
| 457 | */ |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 458 | static struct root_domain def_root_domain; |
| 459 | |
| 460 | #endif |
| 461 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 462 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * This is the main, per-CPU runqueue data structure. |
| 464 | * |
| 465 | * Locking rule: those places that want to lock multiple runqueues |
| 466 | * (such as the load balancing or the thread migration code), lock |
| 467 | * acquire operations must be ordered by ascending &runqueue. |
| 468 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 469 | struct rq { |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 470 | /* runqueue lock: */ |
| 471 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | /* |
| 474 | * nr_running and cpu_load should be in the same cacheline because |
| 475 | * remote CPUs use both these fields when doing load calculation. |
| 476 | */ |
| 477 | unsigned long nr_running; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 478 | #define CPU_LOAD_IDX_MAX 5 |
| 479 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; |
Siddha, Suresh B | bdecea3 | 2007-05-08 00:32:48 -0700 | [diff] [blame] | 480 | unsigned char idle_at_tick; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 481 | #ifdef CONFIG_NO_HZ |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 482 | unsigned long last_tick_seen; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 483 | unsigned char in_nohz_recently; |
| 484 | #endif |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 485 | /* capture load from *all* tasks on this cpu: */ |
| 486 | struct load_weight load; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 487 | unsigned long nr_load_updates; |
| 488 | u64 nr_switches; |
| 489 | |
| 490 | struct cfs_rq cfs; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 491 | struct rt_rq rt; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 492 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 493 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 494 | /* list of leaf cfs_rq on this cpu: */ |
| 495 | struct list_head leaf_cfs_rq_list; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 496 | #endif |
| 497 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 498 | struct list_head leaf_rt_rq_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | /* |
| 502 | * This is part of a global counter where only the total sum |
| 503 | * over all CPUs matters. A task can increase this counter on |
| 504 | * one CPU and if it got migrated afterwards it may decrease |
| 505 | * it on another CPU. Always updated under the runqueue lock: |
| 506 | */ |
| 507 | unsigned long nr_uninterruptible; |
| 508 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 509 | struct task_struct *curr, *idle; |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 510 | unsigned long next_balance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | struct mm_struct *prev_mm; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 512 | |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 513 | u64 clock; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | atomic_t nr_iowait; |
| 516 | |
| 517 | #ifdef CONFIG_SMP |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 518 | struct root_domain *rd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | struct sched_domain *sd; |
| 520 | |
| 521 | /* For active balancing */ |
| 522 | int active_balance; |
| 523 | int push_cpu; |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 524 | /* cpu of this runqueue: */ |
| 525 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 527 | struct task_struct *migration_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | struct list_head migration_queue; |
| 529 | #endif |
| 530 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 531 | #ifdef CONFIG_SCHED_HRTICK |
| 532 | unsigned long hrtick_flags; |
| 533 | ktime_t hrtick_expire; |
| 534 | struct hrtimer hrtick_timer; |
| 535 | #endif |
| 536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | #ifdef CONFIG_SCHEDSTATS |
| 538 | /* latency stats */ |
| 539 | struct sched_info rq_sched_info; |
| 540 | |
| 541 | /* sys_sched_yield() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 542 | unsigned int yld_exp_empty; |
| 543 | unsigned int yld_act_empty; |
| 544 | unsigned int yld_both_empty; |
| 545 | unsigned int yld_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | /* schedule() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 548 | unsigned int sched_switch; |
| 549 | unsigned int sched_count; |
| 550 | unsigned int sched_goidle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | /* try_to_wake_up() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 553 | unsigned int ttwu_count; |
| 554 | unsigned int ttwu_local; |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 555 | |
| 556 | /* BKL stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 557 | unsigned int bkl_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | #endif |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 559 | struct lock_class_key rq_lock_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | }; |
| 561 | |
Fenghua Yu | f34e3b6 | 2007-07-19 01:48:13 -0700 | [diff] [blame] | 562 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 564 | static inline void check_preempt_curr(struct rq *rq, struct task_struct *p) |
| 565 | { |
| 566 | rq->curr->sched_class->check_preempt_curr(rq, p); |
| 567 | } |
| 568 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 569 | static inline int cpu_of(struct rq *rq) |
| 570 | { |
| 571 | #ifdef CONFIG_SMP |
| 572 | return rq->cpu; |
| 573 | #else |
| 574 | return 0; |
| 575 | #endif |
| 576 | } |
| 577 | |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 578 | /* |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 579 | * 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] | 580 | * See detach_destroy_domains: synchronize_sched for details. |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 581 | * |
| 582 | * The domain tree of any CPU may only be accessed from within |
| 583 | * preempt-disabled sections. |
| 584 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 585 | #define for_each_domain(cpu, __sd) \ |
| 586 | for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
| 589 | #define this_rq() (&__get_cpu_var(runqueues)) |
| 590 | #define task_rq(p) cpu_rq(task_cpu(p)) |
| 591 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) |
| 592 | |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 593 | static inline void update_rq_clock(struct rq *rq) |
| 594 | { |
| 595 | rq->clock = sched_clock_cpu(cpu_of(rq)); |
| 596 | } |
| 597 | |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 598 | /* |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 599 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: |
| 600 | */ |
| 601 | #ifdef CONFIG_SCHED_DEBUG |
| 602 | # define const_debug __read_mostly |
| 603 | #else |
| 604 | # define const_debug static const |
| 605 | #endif |
| 606 | |
| 607 | /* |
| 608 | * Debugging: various feature bits |
| 609 | */ |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 610 | |
| 611 | #define SCHED_FEAT(name, enabled) \ |
| 612 | __SCHED_FEAT_##name , |
| 613 | |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 614 | enum { |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 615 | #include "sched_features.h" |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 616 | }; |
| 617 | |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 618 | #undef SCHED_FEAT |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 619 | |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 620 | #define SCHED_FEAT(name, enabled) \ |
| 621 | (1UL << __SCHED_FEAT_##name) * enabled | |
| 622 | |
| 623 | const_debug unsigned int sysctl_sched_features = |
| 624 | #include "sched_features.h" |
| 625 | 0; |
| 626 | |
| 627 | #undef SCHED_FEAT |
| 628 | |
| 629 | #ifdef CONFIG_SCHED_DEBUG |
| 630 | #define SCHED_FEAT(name, enabled) \ |
| 631 | #name , |
| 632 | |
Harvey Harrison | 983ed7a | 2008-04-24 18:17:55 -0700 | [diff] [blame] | 633 | static __read_mostly char *sched_feat_names[] = { |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 634 | #include "sched_features.h" |
| 635 | NULL |
| 636 | }; |
| 637 | |
| 638 | #undef SCHED_FEAT |
| 639 | |
Harvey Harrison | 983ed7a | 2008-04-24 18:17:55 -0700 | [diff] [blame] | 640 | static int sched_feat_open(struct inode *inode, struct file *filp) |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 641 | { |
| 642 | filp->private_data = inode->i_private; |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static ssize_t |
| 647 | sched_feat_read(struct file *filp, char __user *ubuf, |
| 648 | size_t cnt, loff_t *ppos) |
| 649 | { |
| 650 | char *buf; |
| 651 | int r = 0; |
| 652 | int len = 0; |
| 653 | int i; |
| 654 | |
| 655 | for (i = 0; sched_feat_names[i]; i++) { |
| 656 | len += strlen(sched_feat_names[i]); |
| 657 | len += 4; |
| 658 | } |
| 659 | |
| 660 | buf = kmalloc(len + 2, GFP_KERNEL); |
| 661 | if (!buf) |
| 662 | return -ENOMEM; |
| 663 | |
| 664 | for (i = 0; sched_feat_names[i]; i++) { |
| 665 | if (sysctl_sched_features & (1UL << i)) |
| 666 | r += sprintf(buf + r, "%s ", sched_feat_names[i]); |
| 667 | else |
Ingo Molnar | c24b7c5 | 2008-04-18 10:55:34 +0200 | [diff] [blame] | 668 | r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]); |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | r += sprintf(buf + r, "\n"); |
| 672 | WARN_ON(r >= len + 2); |
| 673 | |
| 674 | r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 675 | |
| 676 | kfree(buf); |
| 677 | |
| 678 | return r; |
| 679 | } |
| 680 | |
| 681 | static ssize_t |
| 682 | sched_feat_write(struct file *filp, const char __user *ubuf, |
| 683 | size_t cnt, loff_t *ppos) |
| 684 | { |
| 685 | char buf[64]; |
| 686 | char *cmp = buf; |
| 687 | int neg = 0; |
| 688 | int i; |
| 689 | |
| 690 | if (cnt > 63) |
| 691 | cnt = 63; |
| 692 | |
| 693 | if (copy_from_user(&buf, ubuf, cnt)) |
| 694 | return -EFAULT; |
| 695 | |
| 696 | buf[cnt] = 0; |
| 697 | |
Ingo Molnar | c24b7c5 | 2008-04-18 10:55:34 +0200 | [diff] [blame] | 698 | if (strncmp(buf, "NO_", 3) == 0) { |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 699 | neg = 1; |
| 700 | cmp += 3; |
| 701 | } |
| 702 | |
| 703 | for (i = 0; sched_feat_names[i]; i++) { |
| 704 | int len = strlen(sched_feat_names[i]); |
| 705 | |
| 706 | if (strncmp(cmp, sched_feat_names[i], len) == 0) { |
| 707 | if (neg) |
| 708 | sysctl_sched_features &= ~(1UL << i); |
| 709 | else |
| 710 | sysctl_sched_features |= (1UL << i); |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | if (!sched_feat_names[i]) |
| 716 | return -EINVAL; |
| 717 | |
| 718 | filp->f_pos += cnt; |
| 719 | |
| 720 | return cnt; |
| 721 | } |
| 722 | |
| 723 | static struct file_operations sched_feat_fops = { |
| 724 | .open = sched_feat_open, |
| 725 | .read = sched_feat_read, |
| 726 | .write = sched_feat_write, |
| 727 | }; |
| 728 | |
| 729 | static __init int sched_init_debug(void) |
| 730 | { |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 731 | debugfs_create_file("sched_features", 0644, NULL, NULL, |
| 732 | &sched_feat_fops); |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | late_initcall(sched_init_debug); |
| 737 | |
| 738 | #endif |
| 739 | |
| 740 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 741 | |
| 742 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 743 | * Number of tasks to iterate in a single balance run. |
| 744 | * Limited because this is done with IRQs disabled. |
| 745 | */ |
| 746 | const_debug unsigned int sysctl_sched_nr_migrate = 32; |
| 747 | |
| 748 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 749 | * period over which we measure -rt task cpu usage in us. |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 750 | * default: 1s |
| 751 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 752 | unsigned int sysctl_sched_rt_period = 1000000; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 753 | |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 754 | static __read_mostly int scheduler_running; |
| 755 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 756 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 757 | * part of the period that we allow rt tasks to run in us. |
| 758 | * default: 0.95s |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 759 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 760 | int sysctl_sched_rt_runtime = 950000; |
| 761 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 762 | static inline u64 global_rt_period(void) |
| 763 | { |
| 764 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; |
| 765 | } |
| 766 | |
| 767 | static inline u64 global_rt_runtime(void) |
| 768 | { |
| 769 | if (sysctl_sched_rt_period < 0) |
| 770 | return RUNTIME_INF; |
| 771 | |
| 772 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; |
| 773 | } |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 774 | |
Ingo Molnar | 690229a | 2008-04-23 09:31:35 +0200 | [diff] [blame] | 775 | unsigned long long time_sync_thresh = 100000; |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 776 | |
| 777 | static DEFINE_PER_CPU(unsigned long long, time_offset); |
| 778 | static DEFINE_PER_CPU(unsigned long long, prev_cpu_time); |
| 779 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 780 | /* |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 781 | * Global lock which we take every now and then to synchronize |
| 782 | * the CPUs time. This method is not warp-safe, but it's good |
| 783 | * enough to synchronize slowly diverging time sources and thus |
| 784 | * it's good enough for tracing: |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 785 | */ |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 786 | static DEFINE_SPINLOCK(time_sync_lock); |
| 787 | static unsigned long long prev_global_time; |
| 788 | |
Ingo Molnar | dfbf4a1 | 2008-04-23 09:24:06 +0200 | [diff] [blame] | 789 | static unsigned long long __sync_cpu_clock(unsigned long long time, int cpu) |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 790 | { |
Ingo Molnar | dfbf4a1 | 2008-04-23 09:24:06 +0200 | [diff] [blame] | 791 | /* |
| 792 | * We want this inlined, to not get tracer function calls |
| 793 | * in this critical section: |
| 794 | */ |
| 795 | spin_acquire(&time_sync_lock.dep_map, 0, 0, _THIS_IP_); |
| 796 | __raw_spin_lock(&time_sync_lock.raw_lock); |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 797 | |
| 798 | if (time < prev_global_time) { |
| 799 | per_cpu(time_offset, cpu) += prev_global_time - time; |
| 800 | time = prev_global_time; |
| 801 | } else { |
| 802 | prev_global_time = time; |
| 803 | } |
| 804 | |
Ingo Molnar | dfbf4a1 | 2008-04-23 09:24:06 +0200 | [diff] [blame] | 805 | __raw_spin_unlock(&time_sync_lock.raw_lock); |
| 806 | spin_release(&time_sync_lock.dep_map, 1, _THIS_IP_); |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 807 | |
| 808 | return time; |
| 809 | } |
| 810 | |
| 811 | static unsigned long long __cpu_clock(int cpu) |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 812 | { |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 813 | unsigned long long now; |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 814 | |
Ingo Molnar | 8ced5f6 | 2007-12-07 19:02:47 +0100 | [diff] [blame] | 815 | /* |
| 816 | * Only call sched_clock() if the scheduler has already been |
| 817 | * initialized (some code might call cpu_clock() very early): |
| 818 | */ |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 819 | if (unlikely(!scheduler_running)) |
| 820 | return 0; |
| 821 | |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 822 | now = sched_clock_cpu(cpu); |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 823 | |
| 824 | return now; |
| 825 | } |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 826 | |
| 827 | /* |
| 828 | * For kernel-internal use: high-speed (but slightly incorrect) per-cpu |
| 829 | * clock constructed from sched_clock(): |
| 830 | */ |
| 831 | unsigned long long cpu_clock(int cpu) |
| 832 | { |
| 833 | unsigned long long prev_cpu_time, time, delta_time; |
Ingo Molnar | dfbf4a1 | 2008-04-23 09:24:06 +0200 | [diff] [blame] | 834 | unsigned long flags; |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 835 | |
Ingo Molnar | dfbf4a1 | 2008-04-23 09:24:06 +0200 | [diff] [blame] | 836 | local_irq_save(flags); |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 837 | prev_cpu_time = per_cpu(prev_cpu_time, cpu); |
| 838 | time = __cpu_clock(cpu) + per_cpu(time_offset, cpu); |
| 839 | delta_time = time-prev_cpu_time; |
| 840 | |
Ingo Molnar | dfbf4a1 | 2008-04-23 09:24:06 +0200 | [diff] [blame] | 841 | if (unlikely(delta_time > time_sync_thresh)) { |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 842 | time = __sync_cpu_clock(time, cpu); |
Ingo Molnar | dfbf4a1 | 2008-04-23 09:24:06 +0200 | [diff] [blame] | 843 | per_cpu(prev_cpu_time, cpu) = time; |
| 844 | } |
| 845 | local_irq_restore(flags); |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 846 | |
| 847 | return time; |
| 848 | } |
Paul E. McKenney | a58f6f2 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 849 | EXPORT_SYMBOL_GPL(cpu_clock); |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | #ifndef prepare_arch_switch |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 852 | # define prepare_arch_switch(next) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | #endif |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 854 | #ifndef finish_arch_switch |
| 855 | # define finish_arch_switch(prev) do { } while (0) |
| 856 | #endif |
| 857 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 858 | static inline int task_current(struct rq *rq, struct task_struct *p) |
| 859 | { |
| 860 | return rq->curr == p; |
| 861 | } |
| 862 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 863 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 864 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 865 | { |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 866 | return task_current(rq, p); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 869 | 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] | 870 | { |
| 871 | } |
| 872 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 873 | 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] | 874 | { |
Ingo Molnar | da04c03 | 2005-09-13 11:17:59 +0200 | [diff] [blame] | 875 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 876 | /* this is a valid case when another task releases the spinlock */ |
| 877 | rq->lock.owner = current; |
| 878 | #endif |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 879 | /* |
| 880 | * If we are tracking spinlock dependencies then we have to |
| 881 | * fix up the runqueue lock - which gets 'carried over' from |
| 882 | * prev into current: |
| 883 | */ |
| 884 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); |
| 885 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 886 | spin_unlock_irq(&rq->lock); |
| 887 | } |
| 888 | |
| 889 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 890 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 891 | { |
| 892 | #ifdef CONFIG_SMP |
| 893 | return p->oncpu; |
| 894 | #else |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 895 | return task_current(rq, p); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 896 | #endif |
| 897 | } |
| 898 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 899 | 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] | 900 | { |
| 901 | #ifdef CONFIG_SMP |
| 902 | /* |
| 903 | * We can optimise this out completely for !SMP, because the |
| 904 | * SMP rebalancing from interrupt is the only thing that cares |
| 905 | * here. |
| 906 | */ |
| 907 | next->oncpu = 1; |
| 908 | #endif |
| 909 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 910 | spin_unlock_irq(&rq->lock); |
| 911 | #else |
| 912 | spin_unlock(&rq->lock); |
| 913 | #endif |
| 914 | } |
| 915 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 916 | 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] | 917 | { |
| 918 | #ifdef CONFIG_SMP |
| 919 | /* |
| 920 | * After ->oncpu is cleared, the task can be moved to a different CPU. |
| 921 | * We must ensure this doesn't happen until the switch is completely |
| 922 | * finished. |
| 923 | */ |
| 924 | smp_wmb(); |
| 925 | prev->oncpu = 0; |
| 926 | #endif |
| 927 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 928 | local_irq_enable(); |
| 929 | #endif |
| 930 | } |
| 931 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
| 933 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 934 | * __task_rq_lock - lock the runqueue a given task resides on. |
| 935 | * Must be called interrupts disabled. |
| 936 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 937 | static inline struct rq *__task_rq_lock(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 938 | __acquires(rq->lock) |
| 939 | { |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 940 | for (;;) { |
| 941 | struct rq *rq = task_rq(p); |
| 942 | spin_lock(&rq->lock); |
| 943 | if (likely(rq == task_rq(p))) |
| 944 | return rq; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 945 | spin_unlock(&rq->lock); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 946 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | * 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] | 951 | * interrupts. Note the ordering: we can safely lookup the task_rq without |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | * explicitly disabling preemption. |
| 953 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 954 | 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] | 955 | __acquires(rq->lock) |
| 956 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 957 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 959 | for (;;) { |
| 960 | local_irq_save(*flags); |
| 961 | rq = task_rq(p); |
| 962 | spin_lock(&rq->lock); |
| 963 | if (likely(rq == task_rq(p))) |
| 964 | return rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | spin_unlock_irqrestore(&rq->lock, *flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 969 | static void __task_rq_unlock(struct rq *rq) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 970 | __releases(rq->lock) |
| 971 | { |
| 972 | spin_unlock(&rq->lock); |
| 973 | } |
| 974 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 975 | static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | __releases(rq->lock) |
| 977 | { |
| 978 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 979 | } |
| 980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | /* |
Robert P. J. Day | cc2a73b | 2006-12-10 02:20:00 -0800 | [diff] [blame] | 982 | * this_rq_lock - lock this runqueue and disable interrupts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 984 | static struct rq *this_rq_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | __acquires(rq->lock) |
| 986 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 987 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
| 989 | local_irq_disable(); |
| 990 | rq = this_rq(); |
| 991 | spin_lock(&rq->lock); |
| 992 | |
| 993 | return rq; |
| 994 | } |
| 995 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 996 | static void __resched_task(struct task_struct *p, int tif_bit); |
| 997 | |
| 998 | static inline void resched_task(struct task_struct *p) |
| 999 | { |
| 1000 | __resched_task(p, TIF_NEED_RESCHED); |
| 1001 | } |
| 1002 | |
| 1003 | #ifdef CONFIG_SCHED_HRTICK |
| 1004 | /* |
| 1005 | * Use HR-timers to deliver accurate preemption points. |
| 1006 | * |
| 1007 | * Its all a bit involved since we cannot program an hrt while holding the |
| 1008 | * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a |
| 1009 | * reschedule event. |
| 1010 | * |
| 1011 | * When we get rescheduled we reprogram the hrtick_timer outside of the |
| 1012 | * rq->lock. |
| 1013 | */ |
| 1014 | static inline void resched_hrt(struct task_struct *p) |
| 1015 | { |
| 1016 | __resched_task(p, TIF_HRTICK_RESCHED); |
| 1017 | } |
| 1018 | |
| 1019 | static inline void resched_rq(struct rq *rq) |
| 1020 | { |
| 1021 | unsigned long flags; |
| 1022 | |
| 1023 | spin_lock_irqsave(&rq->lock, flags); |
| 1024 | resched_task(rq->curr); |
| 1025 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1026 | } |
| 1027 | |
| 1028 | enum { |
| 1029 | HRTICK_SET, /* re-programm hrtick_timer */ |
| 1030 | HRTICK_RESET, /* not a new slice */ |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 1031 | HRTICK_BLOCK, /* stop hrtick operations */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1032 | }; |
| 1033 | |
| 1034 | /* |
| 1035 | * Use hrtick when: |
| 1036 | * - enabled by features |
| 1037 | * - hrtimer is actually high res |
| 1038 | */ |
| 1039 | static inline int hrtick_enabled(struct rq *rq) |
| 1040 | { |
| 1041 | if (!sched_feat(HRTICK)) |
| 1042 | return 0; |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 1043 | if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags))) |
| 1044 | return 0; |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1045 | return hrtimer_is_hres_active(&rq->hrtick_timer); |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * Called to set the hrtick timer state. |
| 1050 | * |
| 1051 | * called with rq->lock held and irqs disabled |
| 1052 | */ |
| 1053 | static void hrtick_start(struct rq *rq, u64 delay, int reset) |
| 1054 | { |
| 1055 | assert_spin_locked(&rq->lock); |
| 1056 | |
| 1057 | /* |
| 1058 | * preempt at: now + delay |
| 1059 | */ |
| 1060 | rq->hrtick_expire = |
| 1061 | ktime_add_ns(rq->hrtick_timer.base->get_time(), delay); |
| 1062 | /* |
| 1063 | * indicate we need to program the timer |
| 1064 | */ |
| 1065 | __set_bit(HRTICK_SET, &rq->hrtick_flags); |
| 1066 | if (reset) |
| 1067 | __set_bit(HRTICK_RESET, &rq->hrtick_flags); |
| 1068 | |
| 1069 | /* |
| 1070 | * New slices are called from the schedule path and don't need a |
| 1071 | * forced reschedule. |
| 1072 | */ |
| 1073 | if (reset) |
| 1074 | resched_hrt(rq->curr); |
| 1075 | } |
| 1076 | |
| 1077 | static void hrtick_clear(struct rq *rq) |
| 1078 | { |
| 1079 | if (hrtimer_active(&rq->hrtick_timer)) |
| 1080 | hrtimer_cancel(&rq->hrtick_timer); |
| 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | * Update the timer from the possible pending state. |
| 1085 | */ |
| 1086 | static void hrtick_set(struct rq *rq) |
| 1087 | { |
| 1088 | ktime_t time; |
| 1089 | int set, reset; |
| 1090 | unsigned long flags; |
| 1091 | |
| 1092 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 1093 | |
| 1094 | spin_lock_irqsave(&rq->lock, flags); |
| 1095 | set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags); |
| 1096 | reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags); |
| 1097 | time = rq->hrtick_expire; |
| 1098 | clear_thread_flag(TIF_HRTICK_RESCHED); |
| 1099 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1100 | |
| 1101 | if (set) { |
| 1102 | hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS); |
| 1103 | if (reset && !hrtimer_active(&rq->hrtick_timer)) |
| 1104 | resched_rq(rq); |
| 1105 | } else |
| 1106 | hrtick_clear(rq); |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * High-resolution timer tick. |
| 1111 | * Runs from hardirq context with interrupts disabled. |
| 1112 | */ |
| 1113 | static enum hrtimer_restart hrtick(struct hrtimer *timer) |
| 1114 | { |
| 1115 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); |
| 1116 | |
| 1117 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 1118 | |
| 1119 | spin_lock(&rq->lock); |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 1120 | update_rq_clock(rq); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1121 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
| 1122 | spin_unlock(&rq->lock); |
| 1123 | |
| 1124 | return HRTIMER_NORESTART; |
| 1125 | } |
| 1126 | |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 1127 | static void hotplug_hrtick_disable(int cpu) |
| 1128 | { |
| 1129 | struct rq *rq = cpu_rq(cpu); |
| 1130 | unsigned long flags; |
| 1131 | |
| 1132 | spin_lock_irqsave(&rq->lock, flags); |
| 1133 | rq->hrtick_flags = 0; |
| 1134 | __set_bit(HRTICK_BLOCK, &rq->hrtick_flags); |
| 1135 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1136 | |
| 1137 | hrtick_clear(rq); |
| 1138 | } |
| 1139 | |
| 1140 | static void hotplug_hrtick_enable(int cpu) |
| 1141 | { |
| 1142 | struct rq *rq = cpu_rq(cpu); |
| 1143 | unsigned long flags; |
| 1144 | |
| 1145 | spin_lock_irqsave(&rq->lock, flags); |
| 1146 | __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags); |
| 1147 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1148 | } |
| 1149 | |
| 1150 | static int |
| 1151 | hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu) |
| 1152 | { |
| 1153 | int cpu = (int)(long)hcpu; |
| 1154 | |
| 1155 | switch (action) { |
| 1156 | case CPU_UP_CANCELED: |
| 1157 | case CPU_UP_CANCELED_FROZEN: |
| 1158 | case CPU_DOWN_PREPARE: |
| 1159 | case CPU_DOWN_PREPARE_FROZEN: |
| 1160 | case CPU_DEAD: |
| 1161 | case CPU_DEAD_FROZEN: |
| 1162 | hotplug_hrtick_disable(cpu); |
| 1163 | return NOTIFY_OK; |
| 1164 | |
| 1165 | case CPU_UP_PREPARE: |
| 1166 | case CPU_UP_PREPARE_FROZEN: |
| 1167 | case CPU_DOWN_FAILED: |
| 1168 | case CPU_DOWN_FAILED_FROZEN: |
| 1169 | case CPU_ONLINE: |
| 1170 | case CPU_ONLINE_FROZEN: |
| 1171 | hotplug_hrtick_enable(cpu); |
| 1172 | return NOTIFY_OK; |
| 1173 | } |
| 1174 | |
| 1175 | return NOTIFY_DONE; |
| 1176 | } |
| 1177 | |
| 1178 | static void init_hrtick(void) |
| 1179 | { |
| 1180 | hotcpu_notifier(hotplug_hrtick, 0); |
| 1181 | } |
| 1182 | |
| 1183 | static void init_rq_hrtick(struct rq *rq) |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1184 | { |
| 1185 | rq->hrtick_flags = 0; |
| 1186 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1187 | rq->hrtick_timer.function = hrtick; |
| 1188 | rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; |
| 1189 | } |
| 1190 | |
| 1191 | void hrtick_resched(void) |
| 1192 | { |
| 1193 | struct rq *rq; |
| 1194 | unsigned long flags; |
| 1195 | |
| 1196 | if (!test_thread_flag(TIF_HRTICK_RESCHED)) |
| 1197 | return; |
| 1198 | |
| 1199 | local_irq_save(flags); |
| 1200 | rq = cpu_rq(smp_processor_id()); |
| 1201 | hrtick_set(rq); |
| 1202 | local_irq_restore(flags); |
| 1203 | } |
| 1204 | #else |
| 1205 | static inline void hrtick_clear(struct rq *rq) |
| 1206 | { |
| 1207 | } |
| 1208 | |
| 1209 | static inline void hrtick_set(struct rq *rq) |
| 1210 | { |
| 1211 | } |
| 1212 | |
| 1213 | static inline void init_rq_hrtick(struct rq *rq) |
| 1214 | { |
| 1215 | } |
| 1216 | |
| 1217 | void hrtick_resched(void) |
| 1218 | { |
| 1219 | } |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 1220 | |
| 1221 | static inline void init_hrtick(void) |
| 1222 | { |
| 1223 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1224 | #endif |
| 1225 | |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1226 | /* |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1227 | * resched_task - mark a task 'to be rescheduled now'. |
| 1228 | * |
| 1229 | * On UP this means the setting of the need_resched flag, on SMP it |
| 1230 | * might also involve a cross-CPU call to trigger the scheduler on |
| 1231 | * the target CPU. |
| 1232 | */ |
| 1233 | #ifdef CONFIG_SMP |
| 1234 | |
| 1235 | #ifndef tsk_is_polling |
| 1236 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) |
| 1237 | #endif |
| 1238 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1239 | static void __resched_task(struct task_struct *p, int tif_bit) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1240 | { |
| 1241 | int cpu; |
| 1242 | |
| 1243 | assert_spin_locked(&task_rq(p)->lock); |
| 1244 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1245 | if (unlikely(test_tsk_thread_flag(p, tif_bit))) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1246 | return; |
| 1247 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1248 | set_tsk_thread_flag(p, tif_bit); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1249 | |
| 1250 | cpu = task_cpu(p); |
| 1251 | if (cpu == smp_processor_id()) |
| 1252 | return; |
| 1253 | |
| 1254 | /* NEED_RESCHED must be visible before we test polling */ |
| 1255 | smp_mb(); |
| 1256 | if (!tsk_is_polling(p)) |
| 1257 | smp_send_reschedule(cpu); |
| 1258 | } |
| 1259 | |
| 1260 | static void resched_cpu(int cpu) |
| 1261 | { |
| 1262 | struct rq *rq = cpu_rq(cpu); |
| 1263 | unsigned long flags; |
| 1264 | |
| 1265 | if (!spin_trylock_irqsave(&rq->lock, flags)) |
| 1266 | return; |
| 1267 | resched_task(cpu_curr(cpu)); |
| 1268 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1269 | } |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 1270 | |
| 1271 | #ifdef CONFIG_NO_HZ |
| 1272 | /* |
| 1273 | * When add_timer_on() enqueues a timer into the timer wheel of an |
| 1274 | * idle CPU then this timer might expire before the next timer event |
| 1275 | * which is scheduled to wake up that CPU. In case of a completely |
| 1276 | * idle system the next event might even be infinite time into the |
| 1277 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and |
| 1278 | * leaves the inner idle loop so the newly added timer is taken into |
| 1279 | * account when the CPU goes back to idle and evaluates the timer |
| 1280 | * wheel for the next timer event. |
| 1281 | */ |
| 1282 | void wake_up_idle_cpu(int cpu) |
| 1283 | { |
| 1284 | struct rq *rq = cpu_rq(cpu); |
| 1285 | |
| 1286 | if (cpu == smp_processor_id()) |
| 1287 | return; |
| 1288 | |
| 1289 | /* |
| 1290 | * This is safe, as this function is called with the timer |
| 1291 | * wheel base lock of (cpu) held. When the CPU is on the way |
| 1292 | * to idle and has not yet set rq->curr to idle then it will |
| 1293 | * be serialized on the timer wheel base lock and take the new |
| 1294 | * timer into account automatically. |
| 1295 | */ |
| 1296 | if (rq->curr != rq->idle) |
| 1297 | return; |
| 1298 | |
| 1299 | /* |
| 1300 | * We can set TIF_RESCHED on the idle task of the other CPU |
| 1301 | * lockless. The worst case is that the other CPU runs the |
| 1302 | * idle task through an additional NOOP schedule() |
| 1303 | */ |
| 1304 | set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED); |
| 1305 | |
| 1306 | /* NEED_RESCHED must be visible before we test polling */ |
| 1307 | smp_mb(); |
| 1308 | if (!tsk_is_polling(rq->idle)) |
| 1309 | smp_send_reschedule(cpu); |
| 1310 | } |
| 1311 | #endif |
| 1312 | |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1313 | #else |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1314 | static void __resched_task(struct task_struct *p, int tif_bit) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1315 | { |
| 1316 | assert_spin_locked(&task_rq(p)->lock); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1317 | set_tsk_thread_flag(p, tif_bit); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1318 | } |
| 1319 | #endif |
| 1320 | |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1321 | #if BITS_PER_LONG == 32 |
| 1322 | # define WMULT_CONST (~0UL) |
| 1323 | #else |
| 1324 | # define WMULT_CONST (1UL << 32) |
| 1325 | #endif |
| 1326 | |
| 1327 | #define WMULT_SHIFT 32 |
| 1328 | |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1329 | /* |
| 1330 | * Shift right and round: |
| 1331 | */ |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1332 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1333 | |
Ingo Molnar | cb1c4fc | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1334 | static unsigned long |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1335 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, |
| 1336 | struct load_weight *lw) |
| 1337 | { |
| 1338 | u64 tmp; |
| 1339 | |
Peter Zijlstra | e05510d | 2008-05-05 23:56:17 +0200 | [diff] [blame] | 1340 | if (!lw->inv_weight) |
| 1341 | lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1342 | |
| 1343 | tmp = (u64)delta_exec * weight; |
| 1344 | /* |
| 1345 | * Check whether we'd overflow the 64-bit multiplication: |
| 1346 | */ |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1347 | if (unlikely(tmp > WMULT_CONST)) |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1348 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1349 | WMULT_SHIFT/2); |
| 1350 | else |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1351 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1352 | |
Ingo Molnar | ecf691d | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1353 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1354 | } |
| 1355 | |
Ingo Molnar | f9305d4 | 2008-05-29 11:23:17 +0200 | [diff] [blame] | 1356 | static inline unsigned long |
| 1357 | calc_delta_fair(unsigned long delta_exec, struct load_weight *lw) |
| 1358 | { |
| 1359 | return calc_delta_mine(delta_exec, NICE_0_LOAD, lw); |
| 1360 | } |
| 1361 | |
Ingo Molnar | 1091985 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1362 | 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] | 1363 | { |
| 1364 | lw->weight += inc; |
Ingo Molnar | e89996a | 2008-03-14 23:48:28 +0100 | [diff] [blame] | 1365 | lw->inv_weight = 0; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1366 | } |
| 1367 | |
Ingo Molnar | 1091985 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1368 | 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] | 1369 | { |
| 1370 | lw->weight -= dec; |
Ingo Molnar | e89996a | 2008-03-14 23:48:28 +0100 | [diff] [blame] | 1371 | lw->inv_weight = 0; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1375 | * To aid in avoiding the subversion of "niceness" due to uneven distribution |
| 1376 | * of tasks with abnormal "nice" values across CPUs the contribution that |
| 1377 | * 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] | 1378 | * 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] | 1379 | * scaled version of the new time slice allocation that they receive on time |
| 1380 | * slice expiry etc. |
| 1381 | */ |
| 1382 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1383 | #define WEIGHT_IDLEPRIO 2 |
| 1384 | #define WMULT_IDLEPRIO (1 << 31) |
| 1385 | |
| 1386 | /* |
| 1387 | * Nice levels are multiplicative, with a gentle 10% change for every |
| 1388 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to |
| 1389 | * nice 1, it will get ~10% less CPU time than another CPU-bound task |
| 1390 | * that remained on nice 0. |
| 1391 | * |
| 1392 | * The "10% effect" is relative and cumulative: from _any_ nice level, |
| 1393 | * 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] | 1394 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. |
| 1395 | * If a task goes up by ~10% and another task goes down by ~10% then |
| 1396 | * the relative distance between them is ~25%.) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1397 | */ |
| 1398 | static const int prio_to_weight[40] = { |
Ingo Molnar | 254753d | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1399 | /* -20 */ 88761, 71755, 56483, 46273, 36291, |
| 1400 | /* -15 */ 29154, 23254, 18705, 14949, 11916, |
| 1401 | /* -10 */ 9548, 7620, 6100, 4904, 3906, |
| 1402 | /* -5 */ 3121, 2501, 1991, 1586, 1277, |
| 1403 | /* 0 */ 1024, 820, 655, 526, 423, |
| 1404 | /* 5 */ 335, 272, 215, 172, 137, |
| 1405 | /* 10 */ 110, 87, 70, 56, 45, |
| 1406 | /* 15 */ 36, 29, 23, 18, 15, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1407 | }; |
| 1408 | |
Ingo Molnar | 5714d2d | 2007-07-16 09:46:31 +0200 | [diff] [blame] | 1409 | /* |
| 1410 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. |
| 1411 | * |
| 1412 | * In cases where the weight does not change often, we can use the |
| 1413 | * precalculated inverse to speed up arithmetics by turning divisions |
| 1414 | * into multiplications: |
| 1415 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1416 | static const u32 prio_to_wmult[40] = { |
Ingo Molnar | 254753d | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1417 | /* -20 */ 48388, 59856, 76040, 92818, 118348, |
| 1418 | /* -15 */ 147320, 184698, 229616, 287308, 360437, |
| 1419 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, |
| 1420 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, |
| 1421 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, |
| 1422 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, |
| 1423 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, |
| 1424 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1425 | }; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1426 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1427 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup); |
| 1428 | |
| 1429 | /* |
| 1430 | * runqueue iterator, to support SMP load-balancing between different |
| 1431 | * scheduling classes, without having to expose their internal data |
| 1432 | * structures to the load-balancing proper: |
| 1433 | */ |
| 1434 | struct rq_iterator { |
| 1435 | void *arg; |
| 1436 | struct task_struct *(*start)(void *); |
| 1437 | struct task_struct *(*next)(void *); |
| 1438 | }; |
| 1439 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1440 | #ifdef CONFIG_SMP |
| 1441 | static unsigned long |
| 1442 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1443 | unsigned long max_load_move, struct sched_domain *sd, |
| 1444 | enum cpu_idle_type idle, int *all_pinned, |
| 1445 | int *this_best_prio, struct rq_iterator *iterator); |
| 1446 | |
| 1447 | static int |
| 1448 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1449 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 1450 | struct rq_iterator *iterator); |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1451 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1452 | |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 1453 | #ifdef CONFIG_CGROUP_CPUACCT |
| 1454 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime); |
| 1455 | #else |
| 1456 | static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} |
| 1457 | #endif |
| 1458 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1459 | static inline void inc_cpu_load(struct rq *rq, unsigned long load) |
| 1460 | { |
| 1461 | update_load_add(&rq->load, load); |
| 1462 | } |
| 1463 | |
| 1464 | static inline void dec_cpu_load(struct rq *rq, unsigned long load) |
| 1465 | { |
| 1466 | update_load_sub(&rq->load, load); |
| 1467 | } |
| 1468 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1469 | #ifdef CONFIG_SMP |
| 1470 | static unsigned long source_load(int cpu, int type); |
| 1471 | static unsigned long target_load(int cpu, int type); |
| 1472 | static unsigned long cpu_avg_load_per_task(int cpu); |
| 1473 | 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] | 1474 | #else /* CONFIG_SMP */ |
| 1475 | |
| 1476 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 1477 | static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares) |
| 1478 | { |
| 1479 | } |
| 1480 | #endif |
| 1481 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1482 | #endif /* CONFIG_SMP */ |
| 1483 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1484 | #include "sched_stats.h" |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1485 | #include "sched_idletask.c" |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1486 | #include "sched_fair.c" |
| 1487 | #include "sched_rt.c" |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1488 | #ifdef CONFIG_SCHED_DEBUG |
| 1489 | # include "sched_debug.c" |
| 1490 | #endif |
| 1491 | |
| 1492 | #define sched_class_highest (&rt_sched_class) |
| 1493 | |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 1494 | static inline void inc_load(struct rq *rq, const struct task_struct *p) |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1495 | { |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 1496 | update_load_add(&rq->load, p->se.load.weight); |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1497 | } |
| 1498 | |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 1499 | static inline void dec_load(struct rq *rq, const struct task_struct *p) |
| 1500 | { |
| 1501 | update_load_sub(&rq->load, p->se.load.weight); |
| 1502 | } |
| 1503 | |
| 1504 | static void inc_nr_running(struct task_struct *p, struct rq *rq) |
| 1505 | { |
| 1506 | rq->nr_running++; |
| 1507 | inc_load(rq, p); |
| 1508 | } |
| 1509 | |
| 1510 | static void dec_nr_running(struct task_struct *p, struct rq *rq) |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1511 | { |
| 1512 | rq->nr_running--; |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 1513 | dec_load(rq, p); |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1514 | } |
| 1515 | |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1516 | static void set_load_weight(struct task_struct *p) |
| 1517 | { |
| 1518 | if (task_has_rt_policy(p)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1519 | p->se.load.weight = prio_to_weight[0] * 2; |
| 1520 | p->se.load.inv_weight = prio_to_wmult[0] >> 1; |
| 1521 | return; |
| 1522 | } |
| 1523 | |
| 1524 | /* |
| 1525 | * SCHED_IDLE tasks get minimal weight: |
| 1526 | */ |
| 1527 | if (p->policy == SCHED_IDLE) { |
| 1528 | p->se.load.weight = WEIGHT_IDLEPRIO; |
| 1529 | p->se.load.inv_weight = WMULT_IDLEPRIO; |
| 1530 | return; |
| 1531 | } |
| 1532 | |
| 1533 | p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO]; |
| 1534 | 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] | 1535 | } |
| 1536 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1537 | 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] | 1538 | { |
| 1539 | sched_info_queued(p); |
Ingo Molnar | fd390f6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1540 | p->sched_class->enqueue_task(rq, p, wakeup); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1541 | p->se.on_rq = 1; |
| 1542 | } |
| 1543 | |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1544 | 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] | 1545 | { |
Ingo Molnar | f02231e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1546 | p->sched_class->dequeue_task(rq, p, sleep); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1547 | p->se.on_rq = 0; |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1551 | * __normal_prio - return the priority that is based on the static prio |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1552 | */ |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1553 | static inline int __normal_prio(struct task_struct *p) |
| 1554 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1555 | return p->static_prio; |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1559 | * Calculate the expected normal priority: i.e. priority |
| 1560 | * without taking RT-inheritance into account. Might be |
| 1561 | * boosted by interactivity modifiers. Changes upon fork, |
| 1562 | * setprio syscalls, and whenever the interactivity |
| 1563 | * estimator recalculates. |
| 1564 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1565 | static inline int normal_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1566 | { |
| 1567 | int prio; |
| 1568 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1569 | if (task_has_rt_policy(p)) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1570 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
| 1571 | else |
| 1572 | prio = __normal_prio(p); |
| 1573 | return prio; |
| 1574 | } |
| 1575 | |
| 1576 | /* |
| 1577 | * Calculate the current priority, i.e. the priority |
| 1578 | * taken into account by the scheduler. This value might |
| 1579 | * be boosted by RT tasks, or might be boosted by |
| 1580 | * interactivity modifiers. Will be RT if the task got |
| 1581 | * RT-boosted. If not then it returns p->normal_prio. |
| 1582 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1583 | static int effective_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1584 | { |
| 1585 | p->normal_prio = normal_prio(p); |
| 1586 | /* |
| 1587 | * If we are RT tasks or we were boosted to RT priority, |
| 1588 | * keep the priority unchanged. Otherwise, update priority |
| 1589 | * to the normal priority: |
| 1590 | */ |
| 1591 | if (!rt_prio(p->prio)) |
| 1592 | return p->normal_prio; |
| 1593 | return p->prio; |
| 1594 | } |
| 1595 | |
| 1596 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1597 | * activate_task - move a task to the runqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1599 | 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] | 1600 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 1601 | if (task_contributes_to_load(p)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1602 | rq->nr_uninterruptible--; |
| 1603 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1604 | enqueue_task(rq, p, wakeup); |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 1605 | inc_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | * deactivate_task - remove a task from the runqueue. |
| 1610 | */ |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1611 | 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] | 1612 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 1613 | if (task_contributes_to_load(p)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1614 | rq->nr_uninterruptible++; |
| 1615 | |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1616 | dequeue_task(rq, p, sleep); |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 1617 | dec_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | /** |
| 1621 | * task_curr - is this task currently executing on a CPU? |
| 1622 | * @p: the task in question. |
| 1623 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1624 | inline int task_curr(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | { |
| 1626 | return cpu_curr(task_cpu(p)) == p; |
| 1627 | } |
| 1628 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1629 | /* Used instead of source_load when we know the type == 0 */ |
| 1630 | unsigned long weighted_cpuload(const int cpu) |
| 1631 | { |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1632 | return cpu_rq(cpu)->load.weight; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) |
| 1636 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1637 | set_task_rq(p, cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1638 | #ifdef CONFIG_SMP |
Dmitry Adamushko | ce96b5a | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 1639 | /* |
| 1640 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be |
| 1641 | * successfuly executed on another CPU. We must ensure that updates of |
| 1642 | * per-task data have been completed by this moment. |
| 1643 | */ |
| 1644 | smp_wmb(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1645 | task_thread_info(p)->cpu = cpu; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1646 | #endif |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1649 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
| 1650 | const struct sched_class *prev_class, |
| 1651 | int oldprio, int running) |
| 1652 | { |
| 1653 | if (prev_class != p->sched_class) { |
| 1654 | if (prev_class->switched_from) |
| 1655 | prev_class->switched_from(rq, p, running); |
| 1656 | p->sched_class->switched_to(rq, p, running); |
| 1657 | } else |
| 1658 | p->sched_class->prio_changed(rq, p, oldprio, running); |
| 1659 | } |
| 1660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | #ifdef CONFIG_SMP |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1662 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1663 | /* |
| 1664 | * Is this task likely cache-hot: |
| 1665 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1666 | static int |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1667 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) |
| 1668 | { |
| 1669 | s64 delta; |
| 1670 | |
Ingo Molnar | f540a60 | 2008-03-15 17:10:34 +0100 | [diff] [blame] | 1671 | /* |
| 1672 | * Buddy candidates are cache hot: |
| 1673 | */ |
Ingo Molnar | d25ce4c | 2008-03-17 09:36:53 +0100 | [diff] [blame] | 1674 | 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] | 1675 | return 1; |
| 1676 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1677 | if (p->sched_class != &fair_sched_class) |
| 1678 | return 0; |
| 1679 | |
Ingo Molnar | 6bc1665 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1680 | if (sysctl_sched_migration_cost == -1) |
| 1681 | return 1; |
| 1682 | if (sysctl_sched_migration_cost == 0) |
| 1683 | return 0; |
| 1684 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1685 | delta = now - p->se.exec_start; |
| 1686 | |
| 1687 | return delta < (s64)sysctl_sched_migration_cost; |
| 1688 | } |
| 1689 | |
| 1690 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1691 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1692 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1693 | int old_cpu = task_cpu(p); |
| 1694 | 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] | 1695 | struct cfs_rq *old_cfsrq = task_cfs_rq(p), |
| 1696 | *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu); |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1697 | u64 clock_offset; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1698 | |
| 1699 | clock_offset = old_rq->clock - new_rq->clock; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1700 | |
| 1701 | #ifdef CONFIG_SCHEDSTATS |
| 1702 | if (p->se.wait_start) |
| 1703 | p->se.wait_start -= clock_offset; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1704 | if (p->se.sleep_start) |
| 1705 | p->se.sleep_start -= clock_offset; |
| 1706 | if (p->se.block_start) |
| 1707 | p->se.block_start -= clock_offset; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1708 | if (old_cpu != new_cpu) { |
| 1709 | schedstat_inc(p, se.nr_migrations); |
| 1710 | if (task_hot(p, old_rq->clock, NULL)) |
| 1711 | schedstat_inc(p, se.nr_forced2_migrations); |
| 1712 | } |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1713 | #endif |
Srivatsa Vaddagiri | 2830cf8 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1714 | p->se.vruntime -= old_cfsrq->min_vruntime - |
| 1715 | new_cfsrq->min_vruntime; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1716 | |
| 1717 | __set_task_cpu(p, new_cpu); |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1720 | struct migration_req { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1723 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | int dest_cpu; |
| 1725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | struct completion done; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1727 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | |
| 1729 | /* |
| 1730 | * The task's runqueue lock must be held. |
| 1731 | * Returns true if you have to wait for migration thread. |
| 1732 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1733 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1734 | 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] | 1735 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1736 | struct rq *rq = task_rq(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | |
| 1738 | /* |
| 1739 | * If the task is not on a runqueue (and not running), then |
| 1740 | * it is sufficient to simply update the task's cpu field. |
| 1741 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1742 | if (!p->se.on_rq && !task_running(rq, p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | set_task_cpu(p, dest_cpu); |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | init_completion(&req->done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | req->task = p; |
| 1749 | req->dest_cpu = dest_cpu; |
| 1750 | list_add(&req->list, &rq->migration_queue); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 1751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | return 1; |
| 1753 | } |
| 1754 | |
| 1755 | /* |
| 1756 | * wait_task_inactive - wait for a thread to unschedule. |
| 1757 | * |
| 1758 | * The caller must ensure that the task *will* unschedule sometime soon, |
| 1759 | * else this function might spin for a *long* time. This function can't |
| 1760 | * be called with interrupts off, or it may introduce deadlock with |
| 1761 | * smp_call_function() if an IPI is sent by the same process we are |
| 1762 | * waiting to become inactive. |
| 1763 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1764 | void wait_task_inactive(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | { |
| 1766 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1767 | int running, on_rq; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1768 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1770 | for (;;) { |
| 1771 | /* |
| 1772 | * We do the initial early heuristics without holding |
| 1773 | * any task-queue locks at all. We'll only try to get |
| 1774 | * the runqueue lock when things look like they will |
| 1775 | * work out! |
| 1776 | */ |
| 1777 | rq = task_rq(p); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 1778 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1779 | /* |
| 1780 | * If the task is actively running on another CPU |
| 1781 | * still, just relax and busy-wait without holding |
| 1782 | * any locks. |
| 1783 | * |
| 1784 | * NOTE! Since we don't hold any locks, it's not |
| 1785 | * even sure that "rq" stays as the right runqueue! |
| 1786 | * But we don't care, since "task_running()" will |
| 1787 | * return false if the runqueue has changed and p |
| 1788 | * is actually now running somewhere else! |
| 1789 | */ |
| 1790 | while (task_running(rq, p)) |
| 1791 | cpu_relax(); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 1792 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1793 | /* |
| 1794 | * Ok, time to look more closely! We need the rq |
| 1795 | * lock now, to be *sure*. If we're wrong, we'll |
| 1796 | * just go back and repeat. |
| 1797 | */ |
| 1798 | rq = task_rq_lock(p, &flags); |
| 1799 | running = task_running(rq, p); |
| 1800 | on_rq = p->se.on_rq; |
| 1801 | task_rq_unlock(rq, &flags); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 1802 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1803 | /* |
| 1804 | * Was it really running after all now that we |
| 1805 | * checked with the proper locks actually held? |
| 1806 | * |
| 1807 | * Oops. Go back and try again.. |
| 1808 | */ |
| 1809 | if (unlikely(running)) { |
| 1810 | cpu_relax(); |
| 1811 | continue; |
| 1812 | } |
| 1813 | |
| 1814 | /* |
| 1815 | * It's not enough that it's not actively running, |
| 1816 | * it must be off the runqueue _entirely_, and not |
| 1817 | * preempted! |
| 1818 | * |
| 1819 | * So if it wa still runnable (but just not actively |
| 1820 | * running right now), it's preempted, and we should |
| 1821 | * yield - it could be a while. |
| 1822 | */ |
| 1823 | if (unlikely(on_rq)) { |
| 1824 | schedule_timeout_uninterruptible(1); |
| 1825 | continue; |
| 1826 | } |
| 1827 | |
| 1828 | /* |
| 1829 | * Ahh, all good. It wasn't running, and it wasn't |
| 1830 | * runnable, which means that it will never become |
| 1831 | * running in the future either. We're all done! |
| 1832 | */ |
| 1833 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | /*** |
| 1838 | * kick_process - kick a running thread to enter/exit the kernel |
| 1839 | * @p: the to-be-kicked thread |
| 1840 | * |
| 1841 | * Cause a process which is running on another CPU to enter |
| 1842 | * kernel-mode, without any delay. (to get signals handled.) |
| 1843 | * |
| 1844 | * NOTE: this function doesnt have to take the runqueue lock, |
| 1845 | * because all it wants to ensure is that the remote task enters |
| 1846 | * the kernel. If the IPI races and the task has been migrated |
| 1847 | * to another CPU then no harm is done and the purpose has been |
| 1848 | * achieved as well. |
| 1849 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1850 | void kick_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | { |
| 1852 | int cpu; |
| 1853 | |
| 1854 | preempt_disable(); |
| 1855 | cpu = task_cpu(p); |
| 1856 | if ((cpu != smp_processor_id()) && task_curr(p)) |
| 1857 | smp_send_reschedule(cpu); |
| 1858 | preempt_enable(); |
| 1859 | } |
| 1860 | |
| 1861 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1862 | * Return a low guess at the load of a migration-source cpu weighted |
| 1863 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | * |
| 1865 | * We want to under-estimate the load of migration sources, to |
| 1866 | * balance conservatively. |
| 1867 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1868 | static unsigned long source_load(int cpu, int type) |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 1869 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1870 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1871 | unsigned long total = weighted_cpuload(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 1872 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1873 | if (type == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1874 | return total; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1875 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1876 | return min(rq->cpu_load[type-1], total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1880 | * Return a high guess at the load of a migration-target cpu weighted |
| 1881 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1883 | static unsigned long target_load(int cpu, int type) |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 1884 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1885 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1886 | unsigned long total = weighted_cpuload(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 1887 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1888 | if (type == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1889 | return total; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1890 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1891 | return max(rq->cpu_load[type-1], total); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | /* |
| 1895 | * Return the average load per task on the cpu's run queue |
| 1896 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1897 | static unsigned long cpu_avg_load_per_task(int cpu) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1898 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1899 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1900 | unsigned long total = weighted_cpuload(cpu); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1901 | unsigned long n = rq->nr_running; |
| 1902 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1903 | return n ? total / n : SCHED_LOAD_SCALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1906 | /* |
| 1907 | * find_idlest_group finds and returns the least busy CPU group within the |
| 1908 | * domain. |
| 1909 | */ |
| 1910 | static struct sched_group * |
| 1911 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) |
| 1912 | { |
| 1913 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; |
| 1914 | unsigned long min_load = ULONG_MAX, this_load = 0; |
| 1915 | int load_idx = sd->forkexec_idx; |
| 1916 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
| 1917 | |
| 1918 | do { |
| 1919 | unsigned long load, avg_load; |
| 1920 | int local_group; |
| 1921 | int i; |
| 1922 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1923 | /* Skip over this group if it has no CPUs allowed */ |
| 1924 | if (!cpus_intersects(group->cpumask, p->cpus_allowed)) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1925 | continue; |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1926 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1927 | local_group = cpu_isset(this_cpu, group->cpumask); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1928 | |
| 1929 | /* Tally up the load of all CPUs in the group */ |
| 1930 | avg_load = 0; |
| 1931 | |
| 1932 | for_each_cpu_mask(i, group->cpumask) { |
| 1933 | /* Bias balancing toward cpus of our domain */ |
| 1934 | if (local_group) |
| 1935 | load = source_load(i, load_idx); |
| 1936 | else |
| 1937 | load = target_load(i, load_idx); |
| 1938 | |
| 1939 | avg_load += load; |
| 1940 | } |
| 1941 | |
| 1942 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 1943 | avg_load = sg_div_cpu_power(group, |
| 1944 | avg_load * SCHED_LOAD_SCALE); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1945 | |
| 1946 | if (local_group) { |
| 1947 | this_load = avg_load; |
| 1948 | this = group; |
| 1949 | } else if (avg_load < min_load) { |
| 1950 | min_load = avg_load; |
| 1951 | idlest = group; |
| 1952 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1953 | } while (group = group->next, group != sd->groups); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1954 | |
| 1955 | if (!idlest || 100*this_load < imbalance*min_load) |
| 1956 | return NULL; |
| 1957 | return idlest; |
| 1958 | } |
| 1959 | |
| 1960 | /* |
Satoru Takeuchi | 0feaece | 2006-10-03 01:14:10 -0700 | [diff] [blame] | 1961 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1962 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 1963 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 1964 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu, |
| 1965 | cpumask_t *tmp) |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1966 | { |
| 1967 | unsigned long load, min_load = ULONG_MAX; |
| 1968 | int idlest = -1; |
| 1969 | int i; |
| 1970 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1971 | /* Traverse only the allowed CPUs */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 1972 | cpus_and(*tmp, group->cpumask, p->cpus_allowed); |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1973 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 1974 | for_each_cpu_mask(i, *tmp) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1975 | load = weighted_cpuload(i); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1976 | |
| 1977 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 1978 | min_load = load; |
| 1979 | idlest = i; |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | return idlest; |
| 1984 | } |
| 1985 | |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1986 | /* |
| 1987 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 1988 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 1989 | * SD_BALANCE_EXEC. |
| 1990 | * |
| 1991 | * Balance, ie. select the least loaded group. |
| 1992 | * |
| 1993 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 1994 | * |
| 1995 | * preempt must be disabled. |
| 1996 | */ |
| 1997 | static int sched_balance_self(int cpu, int flag) |
| 1998 | { |
| 1999 | struct task_struct *t = current; |
| 2000 | struct sched_domain *tmp, *sd = NULL; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 2001 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2002 | for_each_domain(cpu, tmp) { |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2003 | /* |
| 2004 | * If power savings logic is enabled for a domain, stop there. |
| 2005 | */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2006 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) |
| 2007 | break; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2008 | if (tmp->flags & flag) |
| 2009 | sd = tmp; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2010 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2011 | |
| 2012 | while (sd) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2013 | cpumask_t span, tmpmask; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2014 | struct sched_group *group; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2015 | int new_cpu, weight; |
| 2016 | |
| 2017 | if (!(sd->flags & flag)) { |
| 2018 | sd = sd->child; |
| 2019 | continue; |
| 2020 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2021 | |
| 2022 | span = sd->span; |
| 2023 | group = find_idlest_group(sd, t, cpu); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2024 | if (!group) { |
| 2025 | sd = sd->child; |
| 2026 | continue; |
| 2027 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2028 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2029 | new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2030 | if (new_cpu == -1 || new_cpu == cpu) { |
| 2031 | /* Now try balancing at a lower domain level of cpu */ |
| 2032 | sd = sd->child; |
| 2033 | continue; |
| 2034 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2035 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 2036 | /* Now try balancing at a lower domain level of new_cpu */ |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2037 | cpu = new_cpu; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2038 | sd = NULL; |
| 2039 | weight = cpus_weight(span); |
| 2040 | for_each_domain(cpu, tmp) { |
| 2041 | if (weight <= cpus_weight(tmp->span)) |
| 2042 | break; |
| 2043 | if (tmp->flags & flag) |
| 2044 | sd = tmp; |
| 2045 | } |
| 2046 | /* while loop will break here if sd == NULL */ |
| 2047 | } |
| 2048 | |
| 2049 | return cpu; |
| 2050 | } |
| 2051 | |
| 2052 | #endif /* CONFIG_SMP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | /*** |
| 2055 | * try_to_wake_up - wake up a thread |
| 2056 | * @p: the to-be-woken-up thread |
| 2057 | * @state: the mask of task states that can be woken |
| 2058 | * @sync: do a synchronous wakeup? |
| 2059 | * |
| 2060 | * Put it on the run-queue if it's not already there. The "current" |
| 2061 | * thread is always on the run-queue (except when the actual |
| 2062 | * re-schedule is in progress), and as such you're allowed to do |
| 2063 | * the simpler "current->state = TASK_RUNNING" to mark yourself |
| 2064 | * runnable without the overhead of this. |
| 2065 | * |
| 2066 | * returns failure only if the task is already active. |
| 2067 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2068 | 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] | 2069 | { |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2070 | int cpu, orig_cpu, this_cpu, success = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | unsigned long flags; |
| 2072 | long old_state; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2073 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
Ingo Molnar | b85d066 | 2008-03-16 20:03:22 +0100 | [diff] [blame] | 2075 | if (!sched_feat(SYNC_WAKEUPS)) |
| 2076 | sync = 0; |
| 2077 | |
Linus Torvalds | 04e2f17 | 2008-02-23 18:05:03 -0800 | [diff] [blame] | 2078 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | rq = task_rq_lock(p, &flags); |
| 2080 | old_state = p->state; |
| 2081 | if (!(old_state & state)) |
| 2082 | goto out; |
| 2083 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2084 | if (p->se.on_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | goto out_running; |
| 2086 | |
| 2087 | cpu = task_cpu(p); |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2088 | orig_cpu = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | this_cpu = smp_processor_id(); |
| 2090 | |
| 2091 | #ifdef CONFIG_SMP |
| 2092 | if (unlikely(task_running(rq, p))) |
| 2093 | goto out_activate; |
| 2094 | |
Dmitry Adamushko | 5d2f5a6 | 2008-01-25 21:08:21 +0100 | [diff] [blame] | 2095 | cpu = p->sched_class->select_task_rq(p, sync); |
| 2096 | if (cpu != orig_cpu) { |
| 2097 | set_task_cpu(p, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | task_rq_unlock(rq, &flags); |
| 2099 | /* might preempt at this point */ |
| 2100 | rq = task_rq_lock(p, &flags); |
| 2101 | old_state = p->state; |
| 2102 | if (!(old_state & state)) |
| 2103 | goto out; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2104 | if (p->se.on_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | goto out_running; |
| 2106 | |
| 2107 | this_cpu = smp_processor_id(); |
| 2108 | cpu = task_cpu(p); |
| 2109 | } |
| 2110 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2111 | #ifdef CONFIG_SCHEDSTATS |
| 2112 | schedstat_inc(rq, ttwu_count); |
| 2113 | if (cpu == this_cpu) |
| 2114 | schedstat_inc(rq, ttwu_local); |
| 2115 | else { |
| 2116 | struct sched_domain *sd; |
| 2117 | for_each_domain(this_cpu, sd) { |
| 2118 | if (cpu_isset(cpu, sd->span)) { |
| 2119 | schedstat_inc(sd, ttwu_wake_remote); |
| 2120 | break; |
| 2121 | } |
| 2122 | } |
| 2123 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2124 | #endif |
| 2125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | out_activate: |
| 2127 | #endif /* CONFIG_SMP */ |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2128 | schedstat_inc(p, se.nr_wakeups); |
| 2129 | if (sync) |
| 2130 | schedstat_inc(p, se.nr_wakeups_sync); |
| 2131 | if (orig_cpu != cpu) |
| 2132 | schedstat_inc(p, se.nr_wakeups_migrate); |
| 2133 | if (cpu == this_cpu) |
| 2134 | schedstat_inc(p, se.nr_wakeups_local); |
| 2135 | else |
| 2136 | schedstat_inc(p, se.nr_wakeups_remote); |
Ingo Molnar | 2daa357 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 2137 | update_rq_clock(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2138 | activate_task(rq, p, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | success = 1; |
| 2140 | |
| 2141 | out_running: |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 2142 | check_preempt_curr(rq, p); |
| 2143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | p->state = TASK_RUNNING; |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2145 | #ifdef CONFIG_SMP |
| 2146 | if (p->sched_class->task_wake_up) |
| 2147 | p->sched_class->task_wake_up(rq, p); |
| 2148 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | out: |
| 2150 | task_rq_unlock(rq, &flags); |
| 2151 | |
| 2152 | return success; |
| 2153 | } |
| 2154 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2155 | int wake_up_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 2157 | return try_to_wake_up(p, TASK_ALL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | EXPORT_SYMBOL(wake_up_process); |
| 2160 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2161 | int wake_up_state(struct task_struct *p, unsigned int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | { |
| 2163 | return try_to_wake_up(p, state, 0); |
| 2164 | } |
| 2165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | /* |
| 2167 | * Perform scheduler related setup for a newly forked process p. |
| 2168 | * p is forked by current. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2169 | * |
| 2170 | * __sched_fork() is basic setup used by init_idle() too: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2172 | static void __sched_fork(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2174 | p->se.exec_start = 0; |
| 2175 | p->se.sum_exec_runtime = 0; |
Ingo Molnar | f6cf891 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 2176 | p->se.prev_sum_exec_runtime = 0; |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 2177 | p->se.last_wakeup = 0; |
| 2178 | p->se.avg_overlap = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2179 | |
| 2180 | #ifdef CONFIG_SCHEDSTATS |
| 2181 | p->se.wait_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2182 | p->se.sum_sleep_runtime = 0; |
| 2183 | p->se.sleep_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2184 | p->se.block_start = 0; |
| 2185 | p->se.sleep_max = 0; |
| 2186 | p->se.block_max = 0; |
| 2187 | p->se.exec_max = 0; |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 2188 | p->se.slice_max = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2189 | p->se.wait_max = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2190 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2191 | |
Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 2192 | INIT_LIST_HEAD(&p->rt.run_list); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2193 | p->se.on_rq = 0; |
Peter Zijlstra | 4a55bd5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 2194 | INIT_LIST_HEAD(&p->se.group_node); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2195 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2196 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 2197 | INIT_HLIST_HEAD(&p->preempt_notifiers); |
| 2198 | #endif |
| 2199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | /* |
| 2201 | * We mark the process as running here, but have not actually |
| 2202 | * inserted it onto the runqueue yet. This guarantees that |
| 2203 | * nobody will actually run it, and a signal or other external |
| 2204 | * event cannot wake it up and insert it on the runqueue either. |
| 2205 | */ |
| 2206 | p->state = TASK_RUNNING; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * fork()/clone()-time setup: |
| 2211 | */ |
| 2212 | void sched_fork(struct task_struct *p, int clone_flags) |
| 2213 | { |
| 2214 | int cpu = get_cpu(); |
| 2215 | |
| 2216 | __sched_fork(p); |
| 2217 | |
| 2218 | #ifdef CONFIG_SMP |
| 2219 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); |
| 2220 | #endif |
Ingo Molnar | 02e4bac2 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 2221 | set_task_cpu(p, cpu); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2222 | |
| 2223 | /* |
| 2224 | * Make sure we do not leak PI boosting priority to the child: |
| 2225 | */ |
| 2226 | p->prio = current->normal_prio; |
Hiroshi Shimamoto | 2ddbf95 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 2227 | if (!rt_prio(p->prio)) |
| 2228 | p->sched_class = &fair_sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2229 | |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 2230 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2231 | if (likely(sched_info_on())) |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 2232 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | #endif |
Chen, Kenneth W | d6077cb | 2006-02-14 13:53:10 -0800 | [diff] [blame] | 2234 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2235 | p->oncpu = 0; |
| 2236 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | #ifdef CONFIG_PREEMPT |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2238 | /* Want to start with kernel preemption disabled. */ |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 2239 | task_thread_info(p)->preempt_count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2241 | put_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | /* |
| 2245 | * wake_up_new_task - wake up a newly created task for the first time. |
| 2246 | * |
| 2247 | * This function will do some initial scheduler statistics housekeeping |
| 2248 | * that must be done for every newly created context, then puts the task |
| 2249 | * on the runqueue and wakes it. |
| 2250 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2251 | 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] | 2252 | { |
| 2253 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2254 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | |
| 2256 | rq = task_rq_lock(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | BUG_ON(p->state != TASK_RUNNING); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 2258 | update_rq_clock(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | |
| 2260 | p->prio = effective_prio(p); |
| 2261 | |
Srivatsa Vaddagiri | b9dca1e | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2262 | if (!p->sched_class->task_new || !current->se.on_rq) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2263 | activate_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2266 | * Let the scheduling class do new task startup |
| 2267 | * management (if any): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | */ |
Ingo Molnar | ee0827d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2269 | p->sched_class->task_new(rq, p); |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 2270 | inc_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2272 | check_preempt_curr(rq, p); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2273 | #ifdef CONFIG_SMP |
| 2274 | if (p->sched_class->task_wake_up) |
| 2275 | p->sched_class->task_wake_up(rq, p); |
| 2276 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2277 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | } |
| 2279 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2280 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 2281 | |
| 2282 | /** |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2283 | * preempt_notifier_register - tell me when current is being being preempted & rescheduled |
| 2284 | * @notifier: notifier struct to register |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2285 | */ |
| 2286 | void preempt_notifier_register(struct preempt_notifier *notifier) |
| 2287 | { |
| 2288 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); |
| 2289 | } |
| 2290 | EXPORT_SYMBOL_GPL(preempt_notifier_register); |
| 2291 | |
| 2292 | /** |
| 2293 | * preempt_notifier_unregister - no longer interested in preemption notifications |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2294 | * @notifier: notifier struct to unregister |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2295 | * |
| 2296 | * This is safe to call from within a preemption notifier. |
| 2297 | */ |
| 2298 | void preempt_notifier_unregister(struct preempt_notifier *notifier) |
| 2299 | { |
| 2300 | hlist_del(¬ifier->link); |
| 2301 | } |
| 2302 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); |
| 2303 | |
| 2304 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 2305 | { |
| 2306 | struct preempt_notifier *notifier; |
| 2307 | struct hlist_node *node; |
| 2308 | |
| 2309 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) |
| 2310 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
| 2311 | } |
| 2312 | |
| 2313 | static void |
| 2314 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 2315 | struct task_struct *next) |
| 2316 | { |
| 2317 | struct preempt_notifier *notifier; |
| 2318 | struct hlist_node *node; |
| 2319 | |
| 2320 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) |
| 2321 | notifier->ops->sched_out(notifier, next); |
| 2322 | } |
| 2323 | |
| 2324 | #else |
| 2325 | |
| 2326 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 2327 | { |
| 2328 | } |
| 2329 | |
| 2330 | static void |
| 2331 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 2332 | struct task_struct *next) |
| 2333 | { |
| 2334 | } |
| 2335 | |
| 2336 | #endif |
| 2337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | /** |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2339 | * prepare_task_switch - prepare to switch tasks |
| 2340 | * @rq: the runqueue preparing to switch |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2341 | * @prev: the current task that is being switched out |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2342 | * @next: the task we are going to switch to. |
| 2343 | * |
| 2344 | * This is called with the rq lock held and interrupts off. It must |
| 2345 | * be paired with a subsequent finish_task_switch after the context |
| 2346 | * switch. |
| 2347 | * |
| 2348 | * prepare_task_switch sets up locking and calls architecture specific |
| 2349 | * hooks. |
| 2350 | */ |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2351 | static inline void |
| 2352 | prepare_task_switch(struct rq *rq, struct task_struct *prev, |
| 2353 | struct task_struct *next) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2354 | { |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2355 | fire_sched_out_preempt_notifiers(prev, next); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2356 | prepare_lock_switch(rq, next); |
| 2357 | prepare_arch_switch(next); |
| 2358 | } |
| 2359 | |
| 2360 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | * finish_task_switch - clean up after a task-switch |
Jeff Garzik | 344baba | 2005-09-07 01:15:17 -0400 | [diff] [blame] | 2362 | * @rq: runqueue associated with task-switch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | * @prev: the thread we just switched away from. |
| 2364 | * |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2365 | * finish_task_switch must be called after the context switch, paired |
| 2366 | * with a prepare_task_switch call before the context switch. |
| 2367 | * finish_task_switch will reconcile locking set up by prepare_task_switch, |
| 2368 | * and do any other architecture-specific cleanup actions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | * |
| 2370 | * 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] | 2371 | * so, we finish that here outside of the runqueue lock. (Doing it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | * with the lock held can cause deadlocks; see schedule() for |
| 2373 | * details.) |
| 2374 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2375 | static void finish_task_switch(struct rq *rq, struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | __releases(rq->lock) |
| 2377 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | struct mm_struct *mm = rq->prev_mm; |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 2379 | long prev_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | |
| 2381 | rq->prev_mm = NULL; |
| 2382 | |
| 2383 | /* |
| 2384 | * A task struct has one reference for the use as "current". |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2385 | * 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] | 2386 | * schedule one last time. The schedule call will never return, and |
| 2387 | * the scheduled task must drop that reference. |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2388 | * The test for TASK_DEAD must occur while the runqueue locks are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | * still held, otherwise prev could be scheduled on another cpu, die |
| 2390 | * there before we look at prev->state, and then the reference would |
| 2391 | * be dropped twice. |
| 2392 | * Manfred Spraul <manfred@colorfullife.com> |
| 2393 | */ |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 2394 | prev_state = prev->state; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2395 | finish_arch_switch(prev); |
| 2396 | finish_lock_switch(rq, prev); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2397 | #ifdef CONFIG_SMP |
| 2398 | if (current->sched_class->post_schedule) |
| 2399 | current->sched_class->post_schedule(rq); |
| 2400 | #endif |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2401 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2402 | fire_sched_in_preempt_notifiers(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | if (mm) |
| 2404 | mmdrop(mm); |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2405 | if (unlikely(prev_state == TASK_DEAD)) { |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2406 | /* |
| 2407 | * Remove function-return probe instances associated with this |
| 2408 | * task and put them back on the free list. |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2409 | */ |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2410 | kprobe_flush_task(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | put_task_struct(prev); |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | /** |
| 2416 | * schedule_tail - first thing a freshly forked thread must call. |
| 2417 | * @prev: the thread we just switched away from. |
| 2418 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2419 | asmlinkage void schedule_tail(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | __releases(rq->lock) |
| 2421 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2422 | struct rq *rq = this_rq(); |
| 2423 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2424 | finish_task_switch(rq, prev); |
| 2425 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW |
| 2426 | /* In this case, finish_task_switch does not reenable preemption */ |
| 2427 | preempt_enable(); |
| 2428 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | if (current->set_child_tid) |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2430 | put_user(task_pid_vnr(current), current->set_child_tid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | /* |
| 2434 | * context_switch - switch to the new MM and the new |
| 2435 | * thread's register state. |
| 2436 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2437 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2438 | context_switch(struct rq *rq, struct task_struct *prev, |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2439 | struct task_struct *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2441 | struct mm_struct *mm, *oldmm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2443 | prepare_task_switch(rq, prev, next); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2444 | mm = next->mm; |
| 2445 | oldmm = prev->active_mm; |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 2446 | /* |
| 2447 | * For paravirt, this is coupled with an exit in switch_to to |
| 2448 | * combine the page table reload and the switch backend into |
| 2449 | * one hypercall. |
| 2450 | */ |
| 2451 | arch_enter_lazy_cpu_mode(); |
| 2452 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2453 | if (unlikely(!mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | next->active_mm = oldmm; |
| 2455 | atomic_inc(&oldmm->mm_count); |
| 2456 | enter_lazy_tlb(oldmm, next); |
| 2457 | } else |
| 2458 | switch_mm(oldmm, mm, next); |
| 2459 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2460 | if (unlikely(!prev->mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | prev->active_mm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | rq->prev_mm = oldmm; |
| 2463 | } |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 2464 | /* |
| 2465 | * Since the runqueue lock will be released by the next |
| 2466 | * task (which is an invalid locking op but in the case |
| 2467 | * of the scheduler it's an obvious special-case), so we |
| 2468 | * do an early lockdep release here: |
| 2469 | */ |
| 2470 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 2471 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 2472 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | |
| 2474 | /* Here we just switch the register state and the stack. */ |
| 2475 | switch_to(prev, next, prev); |
| 2476 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2477 | barrier(); |
| 2478 | /* |
| 2479 | * this_rq must be evaluated again because prev may have moved |
| 2480 | * CPUs since it called schedule(), thus the 'rq' on its stack |
| 2481 | * frame will be invalid. |
| 2482 | */ |
| 2483 | finish_task_switch(this_rq(), prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | /* |
| 2487 | * nr_running, nr_uninterruptible and nr_context_switches: |
| 2488 | * |
| 2489 | * externally visible scheduler statistics: current number of runnable |
| 2490 | * threads, current number of uninterruptible-sleeping threads, total |
| 2491 | * number of context switches performed since bootup. |
| 2492 | */ |
| 2493 | unsigned long nr_running(void) |
| 2494 | { |
| 2495 | unsigned long i, sum = 0; |
| 2496 | |
| 2497 | for_each_online_cpu(i) |
| 2498 | sum += cpu_rq(i)->nr_running; |
| 2499 | |
| 2500 | return sum; |
| 2501 | } |
| 2502 | |
| 2503 | unsigned long nr_uninterruptible(void) |
| 2504 | { |
| 2505 | unsigned long i, sum = 0; |
| 2506 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2507 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | sum += cpu_rq(i)->nr_uninterruptible; |
| 2509 | |
| 2510 | /* |
| 2511 | * Since we read the counters lockless, it might be slightly |
| 2512 | * inaccurate. Do not allow it to go below zero though: |
| 2513 | */ |
| 2514 | if (unlikely((long)sum < 0)) |
| 2515 | sum = 0; |
| 2516 | |
| 2517 | return sum; |
| 2518 | } |
| 2519 | |
| 2520 | unsigned long long nr_context_switches(void) |
| 2521 | { |
Steven Rostedt | cc94abf | 2006-06-27 02:54:31 -0700 | [diff] [blame] | 2522 | int i; |
| 2523 | unsigned long long sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2525 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | sum += cpu_rq(i)->nr_switches; |
| 2527 | |
| 2528 | return sum; |
| 2529 | } |
| 2530 | |
| 2531 | unsigned long nr_iowait(void) |
| 2532 | { |
| 2533 | unsigned long i, sum = 0; |
| 2534 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2535 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
| 2537 | |
| 2538 | return sum; |
| 2539 | } |
| 2540 | |
Jack Steiner | db1b1fe | 2006-03-31 02:31:21 -0800 | [diff] [blame] | 2541 | unsigned long nr_active(void) |
| 2542 | { |
| 2543 | unsigned long i, running = 0, uninterruptible = 0; |
| 2544 | |
| 2545 | for_each_online_cpu(i) { |
| 2546 | running += cpu_rq(i)->nr_running; |
| 2547 | uninterruptible += cpu_rq(i)->nr_uninterruptible; |
| 2548 | } |
| 2549 | |
| 2550 | if (unlikely((long)uninterruptible < 0)) |
| 2551 | uninterruptible = 0; |
| 2552 | |
| 2553 | return running + uninterruptible; |
| 2554 | } |
| 2555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2557 | * Update rq->cpu_load[] statistics. This function is usually called every |
| 2558 | * scheduler tick (TICK_NSEC). |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2559 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2560 | static void update_cpu_load(struct rq *this_rq) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2561 | { |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 2562 | unsigned long this_load = this_rq->load.weight; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2563 | int i, scale; |
| 2564 | |
| 2565 | this_rq->nr_load_updates++; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2566 | |
| 2567 | /* Update our load: */ |
| 2568 | for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { |
| 2569 | unsigned long old_load, new_load; |
| 2570 | |
| 2571 | /* scale is effectively 1 << i now, and >> i divides by scale */ |
| 2572 | |
| 2573 | old_load = this_rq->cpu_load[i]; |
| 2574 | new_load = this_load; |
Ingo Molnar | a25707f | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 2575 | /* |
| 2576 | * Round up the averaging division if load is increasing. This |
| 2577 | * prevents us from getting stuck on 9 if the load is 10, for |
| 2578 | * example. |
| 2579 | */ |
| 2580 | if (new_load > old_load) |
| 2581 | new_load += scale-1; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2582 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; |
| 2583 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2584 | } |
| 2585 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2586 | #ifdef CONFIG_SMP |
| 2587 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2588 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | * double_rq_lock - safely lock two runqueues |
| 2590 | * |
| 2591 | * Note this does not disable interrupts like task_rq_lock, |
| 2592 | * you need to do so manually before calling. |
| 2593 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2594 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | __acquires(rq1->lock) |
| 2596 | __acquires(rq2->lock) |
| 2597 | { |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 2598 | BUG_ON(!irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | if (rq1 == rq2) { |
| 2600 | spin_lock(&rq1->lock); |
| 2601 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 2602 | } else { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2603 | if (rq1 < rq2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | spin_lock(&rq1->lock); |
| 2605 | spin_lock(&rq2->lock); |
| 2606 | } else { |
| 2607 | spin_lock(&rq2->lock); |
| 2608 | spin_lock(&rq1->lock); |
| 2609 | } |
| 2610 | } |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 2611 | update_rq_clock(rq1); |
| 2612 | update_rq_clock(rq2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | /* |
| 2616 | * double_rq_unlock - safely unlock two runqueues |
| 2617 | * |
| 2618 | * Note this does not restore interrupts like task_rq_unlock, |
| 2619 | * you need to do so manually after calling. |
| 2620 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2621 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | __releases(rq1->lock) |
| 2623 | __releases(rq2->lock) |
| 2624 | { |
| 2625 | spin_unlock(&rq1->lock); |
| 2626 | if (rq1 != rq2) |
| 2627 | spin_unlock(&rq2->lock); |
| 2628 | else |
| 2629 | __release(rq2->lock); |
| 2630 | } |
| 2631 | |
| 2632 | /* |
| 2633 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. |
| 2634 | */ |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2635 | static int double_lock_balance(struct rq *this_rq, struct rq *busiest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | __releases(this_rq->lock) |
| 2637 | __acquires(busiest->lock) |
| 2638 | __acquires(this_rq->lock) |
| 2639 | { |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2640 | int ret = 0; |
| 2641 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 2642 | if (unlikely(!irqs_disabled())) { |
| 2643 | /* printk() doesn't work good under rq->lock */ |
| 2644 | spin_unlock(&this_rq->lock); |
| 2645 | BUG_ON(1); |
| 2646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | if (unlikely(!spin_trylock(&busiest->lock))) { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2648 | if (busiest < this_rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | spin_unlock(&this_rq->lock); |
| 2650 | spin_lock(&busiest->lock); |
| 2651 | spin_lock(&this_rq->lock); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2652 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | } else |
| 2654 | spin_lock(&busiest->lock); |
| 2655 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2656 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | * If dest_cpu is allowed for this process, migrate the task to it. |
| 2661 | * This is accomplished by forcing the cpu_allowed mask to only |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 2662 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | * the cpu_allowed mask is restored. |
| 2664 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2665 | static void sched_migrate_task(struct task_struct *p, int dest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2667 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2669 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | |
| 2671 | rq = task_rq_lock(p, &flags); |
| 2672 | if (!cpu_isset(dest_cpu, p->cpus_allowed) |
| 2673 | || unlikely(cpu_is_offline(dest_cpu))) |
| 2674 | goto out; |
| 2675 | |
| 2676 | /* force the process onto the specified CPU */ |
| 2677 | if (migrate_task(p, dest_cpu, &req)) { |
| 2678 | /* Need to wait for migration thread (might exit: take ref). */ |
| 2679 | struct task_struct *mt = rq->migration_thread; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | get_task_struct(mt); |
| 2682 | task_rq_unlock(rq, &flags); |
| 2683 | wake_up_process(mt); |
| 2684 | put_task_struct(mt); |
| 2685 | wait_for_completion(&req.done); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | return; |
| 2688 | } |
| 2689 | out: |
| 2690 | task_rq_unlock(rq, &flags); |
| 2691 | } |
| 2692 | |
| 2693 | /* |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2694 | * sched_exec - execve() is a valuable balancing opportunity, because at |
| 2695 | * this point the task has the smallest effective memory and cache footprint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | */ |
| 2697 | void sched_exec(void) |
| 2698 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | int new_cpu, this_cpu = get_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2700 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | put_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2702 | if (new_cpu != this_cpu) |
| 2703 | sched_migrate_task(current, new_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | /* |
| 2707 | * pull_task - move a task from a remote runqueue to the local runqueue. |
| 2708 | * Both runqueues must be locked. |
| 2709 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2710 | static void pull_task(struct rq *src_rq, struct task_struct *p, |
| 2711 | struct rq *this_rq, int this_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2713 | deactivate_task(src_rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | set_task_cpu(p, this_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2715 | activate_task(this_rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | /* |
| 2717 | * Note that idle threads have a prio of MAX_PRIO, for this test |
| 2718 | * to be always true for them. |
| 2719 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2720 | check_preempt_curr(this_rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | /* |
| 2724 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 2725 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 2726 | static |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2727 | 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] | 2728 | struct sched_domain *sd, enum cpu_idle_type idle, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 2729 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | { |
| 2731 | /* |
| 2732 | * We do not migrate tasks that are: |
| 2733 | * 1) running (obviously), or |
| 2734 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 2735 | * 3) are cache-hot on their current CPU. |
| 2736 | */ |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2737 | if (!cpu_isset(this_cpu, p->cpus_allowed)) { |
| 2738 | schedstat_inc(p, se.nr_failed_migrations_affine); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2740 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2741 | *all_pinned = 0; |
| 2742 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2743 | if (task_running(rq, p)) { |
| 2744 | schedstat_inc(p, se.nr_failed_migrations_running); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2745 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2746 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2748 | /* |
| 2749 | * Aggressive migration if: |
| 2750 | * 1) task is cache cold, or |
| 2751 | * 2) too many balance attempts have failed. |
| 2752 | */ |
| 2753 | |
Ingo Molnar | 6bc1665 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2754 | if (!task_hot(p, rq->clock, sd) || |
| 2755 | sd->nr_balance_failed > sd->cache_nice_tries) { |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2756 | #ifdef CONFIG_SCHEDSTATS |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2757 | if (task_hot(p, rq->clock, sd)) { |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2758 | schedstat_inc(sd, lb_hot_gained[idle]); |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2759 | schedstat_inc(p, se.nr_forced_migrations); |
| 2760 | } |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2761 | #endif |
| 2762 | return 1; |
| 2763 | } |
| 2764 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2765 | if (task_hot(p, rq->clock, sd)) { |
| 2766 | schedstat_inc(p, se.nr_failed_migrations_hot); |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2767 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | return 1; |
| 2770 | } |
| 2771 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2772 | static unsigned long |
| 2773 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2774 | unsigned long max_load_move, struct sched_domain *sd, |
| 2775 | enum cpu_idle_type idle, int *all_pinned, |
| 2776 | int *this_best_prio, struct rq_iterator *iterator) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2777 | { |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2778 | int loops = 0, pulled = 0, pinned = 0, skip_for_load; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2779 | struct task_struct *p; |
| 2780 | long rem_load_move = max_load_move; |
| 2781 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2782 | if (max_load_move == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2783 | goto out; |
| 2784 | |
| 2785 | pinned = 1; |
| 2786 | |
| 2787 | /* |
| 2788 | * Start the load-balancing iterator: |
| 2789 | */ |
| 2790 | p = iterator->start(iterator->arg); |
| 2791 | next: |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2792 | if (!p || loops++ > sysctl_sched_nr_migrate) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2793 | goto out; |
| 2794 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2795 | * To help distribute high priority tasks across CPUs we don't |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2796 | * skip a task if it will be the highest priority task (i.e. smallest |
| 2797 | * prio value) on its new queue regardless of its load weight |
| 2798 | */ |
| 2799 | skip_for_load = (p->se.load.weight >> 1) > rem_load_move + |
| 2800 | SCHED_LOAD_SCALE_FUZZ; |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2801 | if ((skip_for_load && p->prio >= *this_best_prio) || |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2802 | !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2803 | p = iterator->next(iterator->arg); |
| 2804 | goto next; |
| 2805 | } |
| 2806 | |
| 2807 | pull_task(busiest, p, this_rq, this_cpu); |
| 2808 | pulled++; |
| 2809 | rem_load_move -= p->se.load.weight; |
| 2810 | |
| 2811 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2812 | * 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] | 2813 | */ |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2814 | if (rem_load_move > 0) { |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2815 | if (p->prio < *this_best_prio) |
| 2816 | *this_best_prio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2817 | p = iterator->next(iterator->arg); |
| 2818 | goto next; |
| 2819 | } |
| 2820 | out: |
| 2821 | /* |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2822 | * 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] | 2823 | * so we can safely collect pull_task() stats here rather than |
| 2824 | * inside pull_task(). |
| 2825 | */ |
| 2826 | schedstat_add(sd, lb_gained[idle], pulled); |
| 2827 | |
| 2828 | if (all_pinned) |
| 2829 | *all_pinned = pinned; |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2830 | |
| 2831 | return max_load_move - rem_load_move; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2832 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | /* |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2835 | * move_tasks tries to move up to max_load_move weighted load from busiest to |
| 2836 | * this_rq, as part of a balancing operation within domain "sd". |
| 2837 | * Returns 1 if successful and 0 otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | * |
| 2839 | * Called with both runqueues locked. |
| 2840 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2841 | 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] | 2842 | unsigned long max_load_move, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2843 | struct sched_domain *sd, enum cpu_idle_type idle, |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2844 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2846 | const struct sched_class *class = sched_class_highest; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2847 | unsigned long total_load_moved = 0; |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2848 | int this_best_prio = this_rq->curr->prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2850 | do { |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2851 | total_load_moved += |
| 2852 | class->load_balance(this_rq, this_cpu, busiest, |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2853 | max_load_move - total_load_moved, |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2854 | sd, idle, all_pinned, &this_best_prio); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2855 | class = class->next; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2856 | } while (class && max_load_move > total_load_moved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2858 | return total_load_moved > 0; |
| 2859 | } |
| 2860 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2861 | static int |
| 2862 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2863 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 2864 | struct rq_iterator *iterator) |
| 2865 | { |
| 2866 | struct task_struct *p = iterator->start(iterator->arg); |
| 2867 | int pinned = 0; |
| 2868 | |
| 2869 | while (p) { |
| 2870 | if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
| 2871 | pull_task(busiest, p, this_rq, this_cpu); |
| 2872 | /* |
| 2873 | * Right now, this is only the second place pull_task() |
| 2874 | * is called, so we can safely collect pull_task() |
| 2875 | * stats here rather than inside pull_task(). |
| 2876 | */ |
| 2877 | schedstat_inc(sd, lb_gained[idle]); |
| 2878 | |
| 2879 | return 1; |
| 2880 | } |
| 2881 | p = iterator->next(iterator->arg); |
| 2882 | } |
| 2883 | |
| 2884 | return 0; |
| 2885 | } |
| 2886 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2887 | /* |
| 2888 | * move_one_task tries to move exactly one task from busiest to this_rq, as |
| 2889 | * part of active balancing operations within "domain". |
| 2890 | * Returns 1 if successful and 0 otherwise. |
| 2891 | * |
| 2892 | * Called with both runqueues locked. |
| 2893 | */ |
| 2894 | static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2895 | struct sched_domain *sd, enum cpu_idle_type idle) |
| 2896 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2897 | const struct sched_class *class; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2898 | |
| 2899 | for (class = sched_class_highest; class; class = class->next) |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2900 | if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle)) |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2901 | return 1; |
| 2902 | |
| 2903 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | /* |
| 2907 | * find_busiest_group finds and returns the busiest CPU group within the |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2908 | * domain. It calculates and returns the amount of weighted load which |
| 2909 | * should be moved to restore balance via the imbalance parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | */ |
| 2911 | static struct sched_group * |
| 2912 | find_busiest_group(struct sched_domain *sd, int this_cpu, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2913 | unsigned long *imbalance, enum cpu_idle_type idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 2914 | int *sd_idle, const cpumask_t *cpus, int *balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | { |
| 2916 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; |
| 2917 | 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] | 2918 | unsigned long max_pull; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2919 | unsigned long busiest_load_per_task, busiest_nr_running; |
| 2920 | unsigned long this_load_per_task, this_nr_running; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2921 | int load_idx, group_imb = 0; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2922 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2923 | int power_savings_balance = 1; |
| 2924 | unsigned long leader_nr_running = 0, min_load_per_task = 0; |
| 2925 | unsigned long min_nr_running = ULONG_MAX; |
| 2926 | struct sched_group *group_min = NULL, *group_leader = NULL; |
| 2927 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | |
| 2929 | max_load = this_load = total_load = total_pwr = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2930 | busiest_load_per_task = busiest_nr_running = 0; |
| 2931 | this_load_per_task = this_nr_running = 0; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2932 | if (idle == CPU_NOT_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2933 | load_idx = sd->busy_idx; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2934 | else if (idle == CPU_NEWLY_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2935 | load_idx = sd->newidle_idx; |
| 2936 | else |
| 2937 | load_idx = sd->idle_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | |
| 2939 | do { |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2940 | unsigned long load, group_capacity, max_cpu_load, min_cpu_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | int local_group; |
| 2942 | int i; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2943 | int __group_imb = 0; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2944 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2945 | unsigned long sum_nr_running, sum_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | |
| 2947 | local_group = cpu_isset(this_cpu, group->cpumask); |
| 2948 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2949 | if (local_group) |
| 2950 | balance_cpu = first_cpu(group->cpumask); |
| 2951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | /* Tally up the load of all CPUs in the group */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2953 | sum_weighted_load = sum_nr_running = avg_load = 0; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2954 | max_cpu_load = 0; |
| 2955 | min_cpu_load = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | |
| 2957 | for_each_cpu_mask(i, group->cpumask) { |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2958 | struct rq *rq; |
| 2959 | |
| 2960 | if (!cpu_isset(i, *cpus)) |
| 2961 | continue; |
| 2962 | |
| 2963 | rq = cpu_rq(i); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2964 | |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 2965 | if (*sd_idle && rq->nr_running) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2966 | *sd_idle = 0; |
| 2967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | /* Bias balancing toward cpus of our domain */ |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2969 | if (local_group) { |
| 2970 | if (idle_cpu(i) && !first_idle_cpu) { |
| 2971 | first_idle_cpu = 1; |
| 2972 | balance_cpu = i; |
| 2973 | } |
| 2974 | |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2975 | load = target_load(i, load_idx); |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2976 | } else { |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2977 | load = source_load(i, load_idx); |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2978 | if (load > max_cpu_load) |
| 2979 | max_cpu_load = load; |
| 2980 | if (min_cpu_load > load) |
| 2981 | min_cpu_load = load; |
| 2982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | |
| 2984 | avg_load += load; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2985 | sum_nr_running += rq->nr_running; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2986 | sum_weighted_load += weighted_cpuload(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | } |
| 2988 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2989 | /* |
| 2990 | * First idle cpu or the first cpu(busiest) in this sched group |
| 2991 | * is eligible for doing load balancing at this and above |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 2992 | * domains. In the newly idle case, we will allow all the cpu's |
| 2993 | * to do the newly idle load balance. |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2994 | */ |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 2995 | if (idle != CPU_NEWLY_IDLE && local_group && |
| 2996 | balance_cpu != this_cpu && balance) { |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2997 | *balance = 0; |
| 2998 | goto ret; |
| 2999 | } |
| 3000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | total_load += avg_load; |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3002 | total_pwr += group->__cpu_power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | |
| 3004 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3005 | avg_load = sg_div_cpu_power(group, |
| 3006 | avg_load * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3007 | |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3008 | if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE) |
| 3009 | __group_imb = 1; |
| 3010 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3011 | group_capacity = group->__cpu_power / SCHED_LOAD_SCALE; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | if (local_group) { |
| 3014 | this_load = avg_load; |
| 3015 | this = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3016 | this_nr_running = sum_nr_running; |
| 3017 | this_load_per_task = sum_weighted_load; |
| 3018 | } else if (avg_load > max_load && |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3019 | (sum_nr_running > group_capacity || __group_imb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | max_load = avg_load; |
| 3021 | busiest = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3022 | busiest_nr_running = sum_nr_running; |
| 3023 | busiest_load_per_task = sum_weighted_load; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3024 | group_imb = __group_imb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3026 | |
| 3027 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 3028 | /* |
| 3029 | * Busy processors will not participate in power savings |
| 3030 | * balance. |
| 3031 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3032 | if (idle == CPU_NOT_IDLE || |
| 3033 | !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
| 3034 | goto group_next; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3035 | |
| 3036 | /* |
| 3037 | * If the local group is idle or completely loaded |
| 3038 | * no need to do power savings balance at this domain |
| 3039 | */ |
| 3040 | if (local_group && (this_nr_running >= group_capacity || |
| 3041 | !this_nr_running)) |
| 3042 | power_savings_balance = 0; |
| 3043 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3044 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3045 | * If a group is already running at full capacity or idle, |
| 3046 | * don't include that group in power savings calculations |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3047 | */ |
| 3048 | if (!power_savings_balance || sum_nr_running >= group_capacity |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3049 | || !sum_nr_running) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3050 | goto group_next; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3051 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3052 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3053 | * Calculate the group which has the least non-idle load. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3054 | * This is the group from where we need to pick up the load |
| 3055 | * for saving power |
| 3056 | */ |
| 3057 | if ((sum_nr_running < min_nr_running) || |
| 3058 | (sum_nr_running == min_nr_running && |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3059 | first_cpu(group->cpumask) < |
| 3060 | first_cpu(group_min->cpumask))) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3061 | group_min = group; |
| 3062 | min_nr_running = sum_nr_running; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3063 | min_load_per_task = sum_weighted_load / |
| 3064 | sum_nr_running; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3065 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3066 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3067 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3068 | * Calculate the group which is almost near its |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3069 | * capacity but still has some space to pick up some load |
| 3070 | * from other group and save more power |
| 3071 | */ |
| 3072 | if (sum_nr_running <= group_capacity - 1) { |
| 3073 | if (sum_nr_running > leader_nr_running || |
| 3074 | (sum_nr_running == leader_nr_running && |
| 3075 | first_cpu(group->cpumask) > |
| 3076 | first_cpu(group_leader->cpumask))) { |
| 3077 | group_leader = group; |
| 3078 | leader_nr_running = sum_nr_running; |
| 3079 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3080 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3081 | group_next: |
| 3082 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | group = group->next; |
| 3084 | } while (group != sd->groups); |
| 3085 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3086 | if (!busiest || this_load >= max_load || busiest_nr_running == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3087 | goto out_balanced; |
| 3088 | |
| 3089 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; |
| 3090 | |
| 3091 | if (this_load >= avg_load || |
| 3092 | 100*max_load <= sd->imbalance_pct*this_load) |
| 3093 | goto out_balanced; |
| 3094 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3095 | busiest_load_per_task /= busiest_nr_running; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 3096 | if (group_imb) |
| 3097 | busiest_load_per_task = min(busiest_load_per_task, avg_load); |
| 3098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | /* |
| 3100 | * We're trying to get all the cpus to the average_load, so we don't |
| 3101 | * want to push ourselves above the average load, nor do we wish to |
| 3102 | * reduce the max loaded cpu below the average load, as either of these |
| 3103 | * actions would just result in more rebalancing later, and ping-pong |
| 3104 | * tasks around. Thus we look for the minimum possible imbalance. |
| 3105 | * Negative imbalances (*we* are more loaded than anyone else) will |
| 3106 | * 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] | 3107 | * 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] | 3108 | * appear as very large values with unsigned longs. |
| 3109 | */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3110 | if (max_load <= busiest_load_per_task) |
| 3111 | goto out_balanced; |
| 3112 | |
| 3113 | /* |
| 3114 | * In the presence of smp nice balancing, certain scenarios can have |
| 3115 | * max load less than avg load(as we skip the groups at or below |
| 3116 | * its cpu_power, while calculating max_load..) |
| 3117 | */ |
| 3118 | if (max_load < avg_load) { |
| 3119 | *imbalance = 0; |
| 3120 | goto small_imbalance; |
| 3121 | } |
Siddha, Suresh B | 0c117f1 | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3122 | |
| 3123 | /* 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] | 3124 | 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] | 3125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | /* How much load to actually move to equalise the imbalance */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3127 | *imbalance = min(max_pull * busiest->__cpu_power, |
| 3128 | (avg_load - this_load) * this->__cpu_power) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | / SCHED_LOAD_SCALE; |
| 3130 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3131 | /* |
| 3132 | * if *imbalance is less than the average load per runnable task |
| 3133 | * there is no gaurantee that any tasks will be moved so we'll have |
| 3134 | * a think about bumping its value to force at least one task to be |
| 3135 | * moved |
| 3136 | */ |
Suresh Siddha | 7fd0d2d | 2007-09-05 14:32:48 +0200 | [diff] [blame] | 3137 | if (*imbalance < busiest_load_per_task) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3138 | unsigned long tmp, pwr_now, pwr_move; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3139 | unsigned int imbn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3141 | small_imbalance: |
| 3142 | pwr_move = pwr_now = 0; |
| 3143 | imbn = 2; |
| 3144 | if (this_nr_running) { |
| 3145 | this_load_per_task /= this_nr_running; |
| 3146 | if (busiest_load_per_task > this_load_per_task) |
| 3147 | imbn = 1; |
| 3148 | } else |
| 3149 | this_load_per_task = SCHED_LOAD_SCALE; |
| 3150 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3151 | if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >= |
| 3152 | busiest_load_per_task * imbn) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3153 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | return busiest; |
| 3155 | } |
| 3156 | |
| 3157 | /* |
| 3158 | * OK, we don't have enough imbalance to justify moving tasks, |
| 3159 | * however we may be able to increase total CPU power used by |
| 3160 | * moving them. |
| 3161 | */ |
| 3162 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3163 | pwr_now += busiest->__cpu_power * |
| 3164 | min(busiest_load_per_task, max_load); |
| 3165 | pwr_now += this->__cpu_power * |
| 3166 | min(this_load_per_task, this_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | pwr_now /= SCHED_LOAD_SCALE; |
| 3168 | |
| 3169 | /* Amount of load we'd subtract */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3170 | tmp = sg_div_cpu_power(busiest, |
| 3171 | busiest_load_per_task * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | if (max_load > tmp) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3173 | pwr_move += busiest->__cpu_power * |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3174 | min(busiest_load_per_task, max_load - tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | |
| 3176 | /* Amount of load we'd add */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3177 | if (max_load * busiest->__cpu_power < |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 3178 | busiest_load_per_task * SCHED_LOAD_SCALE) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3179 | tmp = sg_div_cpu_power(this, |
| 3180 | max_load * busiest->__cpu_power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | else |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3182 | tmp = sg_div_cpu_power(this, |
| 3183 | busiest_load_per_task * SCHED_LOAD_SCALE); |
| 3184 | pwr_move += this->__cpu_power * |
| 3185 | min(this_load_per_task, this_load + tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | pwr_move /= SCHED_LOAD_SCALE; |
| 3187 | |
| 3188 | /* Move if we gain throughput */ |
Suresh Siddha | 7fd0d2d | 2007-09-05 14:32:48 +0200 | [diff] [blame] | 3189 | if (pwr_move > pwr_now) |
| 3190 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | } |
| 3192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | return busiest; |
| 3194 | |
| 3195 | out_balanced: |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3196 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3197 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3198 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3200 | if (this == group_leader && group_leader != group_min) { |
| 3201 | *imbalance = min_load_per_task; |
| 3202 | return group_min; |
| 3203 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3204 | #endif |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3205 | ret: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | *imbalance = 0; |
| 3207 | return NULL; |
| 3208 | } |
| 3209 | |
| 3210 | /* |
| 3211 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 3212 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3213 | static struct rq * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3214 | find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3215 | unsigned long imbalance, const cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3216 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3217 | struct rq *busiest = NULL, *rq; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3218 | unsigned long max_load = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | int i; |
| 3220 | |
| 3221 | for_each_cpu_mask(i, group->cpumask) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3222 | unsigned long wl; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3223 | |
| 3224 | if (!cpu_isset(i, *cpus)) |
| 3225 | continue; |
| 3226 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3227 | rq = cpu_rq(i); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3228 | wl = weighted_cpuload(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3230 | if (rq->nr_running == 1 && wl > imbalance) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3231 | continue; |
| 3232 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3233 | if (wl > max_load) { |
| 3234 | max_load = wl; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3235 | busiest = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | return busiest; |
| 3240 | } |
| 3241 | |
| 3242 | /* |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 3243 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 3244 | * so long as it is large enough. |
| 3245 | */ |
| 3246 | #define MAX_PINNED_INTERVAL 512 |
| 3247 | |
| 3248 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3249 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3250 | * tasks if there is an imbalance. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3252 | static int load_balance(int this_cpu, struct rq *this_rq, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3253 | struct sched_domain *sd, enum cpu_idle_type idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3254 | int *balance, cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | { |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3256 | int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | struct sched_group *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | unsigned long imbalance; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3259 | struct rq *busiest; |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3260 | unsigned long flags; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3261 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3262 | cpus_setall(*cpus); |
| 3263 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3264 | /* |
| 3265 | * When power savings policy is enabled for the parent domain, idle |
| 3266 | * sibling can pick up load irrespective of busy siblings. In this case, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3267 | * 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] | 3268 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3269 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3270 | if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3271 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3272 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3274 | schedstat_inc(sd, lb_count[idle]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3276 | redo: |
| 3277 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3278 | cpus, balance); |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3279 | |
Chen, Kenneth W | 0606671 | 2006-12-10 02:20:35 -0800 | [diff] [blame] | 3280 | if (*balance == 0) |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3281 | goto out_balanced; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | if (!group) { |
| 3284 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 3285 | goto out_balanced; |
| 3286 | } |
| 3287 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3288 | busiest = find_busiest_queue(group, idle, imbalance, cpus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | if (!busiest) { |
| 3290 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 3291 | goto out_balanced; |
| 3292 | } |
| 3293 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3294 | BUG_ON(busiest == this_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | |
| 3296 | schedstat_add(sd, lb_imbalance[idle], imbalance); |
| 3297 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3298 | ld_moved = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | if (busiest->nr_running > 1) { |
| 3300 | /* |
| 3301 | * Attempt to move tasks. If find_busiest_group has found |
| 3302 | * an imbalance but busiest->nr_running <= 1, the group is |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3303 | * still unbalanced. ld_moved simply stays zero, so it is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | * correctly treated as an imbalance. |
| 3305 | */ |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3306 | local_irq_save(flags); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 3307 | double_rq_lock(this_rq, busiest); |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3308 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3309 | imbalance, sd, idle, &all_pinned); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 3310 | double_rq_unlock(this_rq, busiest); |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3311 | local_irq_restore(flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3312 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3313 | /* |
| 3314 | * some other cpu did the load balance for us. |
| 3315 | */ |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3316 | if (ld_moved && this_cpu != smp_processor_id()) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3317 | resched_cpu(this_cpu); |
| 3318 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3319 | /* All tasks on this runqueue were pinned by CPU affinity */ |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3320 | if (unlikely(all_pinned)) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3321 | cpu_clear(cpu_of(busiest), *cpus); |
| 3322 | if (!cpus_empty(*cpus)) |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3323 | goto redo; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3324 | goto out_balanced; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3325 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3327 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3328 | if (!ld_moved) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | schedstat_inc(sd, lb_failed[idle]); |
| 3330 | sd->nr_balance_failed++; |
| 3331 | |
| 3332 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3334 | spin_lock_irqsave(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3335 | |
| 3336 | /* don't kick the migration_thread, if the curr |
| 3337 | * task on busiest cpu can't be moved to this_cpu |
| 3338 | */ |
| 3339 | if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3340 | spin_unlock_irqrestore(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3341 | all_pinned = 1; |
| 3342 | goto out_one_pinned; |
| 3343 | } |
| 3344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | if (!busiest->active_balance) { |
| 3346 | busiest->active_balance = 1; |
| 3347 | busiest->push_cpu = this_cpu; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3348 | active_balance = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | } |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3350 | spin_unlock_irqrestore(&busiest->lock, flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3351 | if (active_balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | wake_up_process(busiest->migration_thread); |
| 3353 | |
| 3354 | /* |
| 3355 | * We've kicked active balancing, reset the failure |
| 3356 | * counter. |
| 3357 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3358 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3360 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | sd->nr_balance_failed = 0; |
| 3362 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3363 | if (likely(!active_balance)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | /* We were unbalanced, so reset the balancing interval */ |
| 3365 | sd->balance_interval = sd->min_interval; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3366 | } else { |
| 3367 | /* |
| 3368 | * If we've begun active balancing, start to back off. This |
| 3369 | * case may not be covered by the all_pinned logic if there |
| 3370 | * is only 1 task on the busy runqueue (because we don't call |
| 3371 | * move_tasks). |
| 3372 | */ |
| 3373 | if (sd->balance_interval < sd->max_interval) |
| 3374 | sd->balance_interval *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | } |
| 3376 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3377 | if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3378 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 3379 | return -1; |
| 3380 | return ld_moved; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | |
| 3382 | out_balanced: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | schedstat_inc(sd, lb_balanced[idle]); |
| 3384 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3385 | sd->nr_balance_failed = 0; |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3386 | |
| 3387 | out_one_pinned: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | /* tune up the balancing interval */ |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 3389 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
| 3390 | (sd->balance_interval < sd->max_interval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | sd->balance_interval *= 2; |
| 3392 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3393 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3394 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 3395 | return -1; |
| 3396 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | /* |
| 3400 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3401 | * tasks if there is an imbalance. |
| 3402 | * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3403 | * 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] | 3404 | * this_rq is locked. |
| 3405 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3406 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3407 | load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd, |
| 3408 | cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | { |
| 3410 | struct sched_group *group; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3411 | struct rq *busiest = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | unsigned long imbalance; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3413 | int ld_moved = 0; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3414 | int sd_idle = 0; |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3415 | int all_pinned = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3416 | |
| 3417 | cpus_setall(*cpus); |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3418 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3419 | /* |
| 3420 | * When power savings policy is enabled for the parent domain, idle |
| 3421 | * sibling can pick up load irrespective of busy siblings. In this case, |
| 3422 | * let the state of idle sibling percolate up as IDLE, instead of |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3423 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3424 | */ |
| 3425 | if (sd->flags & SD_SHARE_CPUPOWER && |
| 3426 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3427 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3429 | schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3430 | redo: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3431 | group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3432 | &sd_idle, cpus, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | if (!group) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3434 | schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3435 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | } |
| 3437 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3438 | busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus); |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3439 | if (!busiest) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3440 | schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3441 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | } |
| 3443 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3444 | BUG_ON(busiest == this_rq); |
| 3445 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3446 | schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3447 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3448 | ld_moved = 0; |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3449 | if (busiest->nr_running > 1) { |
| 3450 | /* Attempt to move tasks */ |
| 3451 | double_lock_balance(this_rq, busiest); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3452 | /* this_rq->clock is already updated */ |
| 3453 | update_rq_clock(busiest); |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3454 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3455 | imbalance, sd, CPU_NEWLY_IDLE, |
| 3456 | &all_pinned); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3457 | spin_unlock(&busiest->lock); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3458 | |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3459 | if (unlikely(all_pinned)) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3460 | cpu_clear(cpu_of(busiest), *cpus); |
| 3461 | if (!cpus_empty(*cpus)) |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3462 | goto redo; |
| 3463 | } |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3464 | } |
| 3465 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3466 | if (!ld_moved) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3467 | schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3468 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 3469 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3470 | return -1; |
| 3471 | } else |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3472 | sd->nr_balance_failed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3474 | return ld_moved; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3475 | |
| 3476 | out_balanced: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3477 | schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3478 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3479 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3480 | return -1; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3481 | sd->nr_balance_failed = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3482 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3483 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | /* |
| 3487 | * idle_balance is called by schedule() if this_cpu is about to become |
| 3488 | * idle. Attempts to pull tasks from other CPUs. |
| 3489 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3490 | static void idle_balance(int this_cpu, struct rq *this_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | { |
| 3492 | struct sched_domain *sd; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3493 | int pulled_task = -1; |
| 3494 | unsigned long next_balance = jiffies + HZ; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3495 | cpumask_t tmpmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | |
| 3497 | for_each_domain(this_cpu, sd) { |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 3498 | unsigned long interval; |
| 3499 | |
| 3500 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 3501 | continue; |
| 3502 | |
| 3503 | if (sd->flags & SD_BALANCE_NEWIDLE) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3504 | /* If we've pulled tasks over stop searching: */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3505 | pulled_task = load_balance_newidle(this_cpu, this_rq, |
| 3506 | sd, &tmpmask); |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 3507 | |
| 3508 | interval = msecs_to_jiffies(sd->balance_interval); |
| 3509 | if (time_after(next_balance, sd->last_balance + interval)) |
| 3510 | next_balance = sd->last_balance + interval; |
| 3511 | if (pulled_task) |
| 3512 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3514 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 3515 | /* |
| 3516 | * We are going idle. next_balance may be set based on |
| 3517 | * a busy processor. So reset next_balance. |
| 3518 | */ |
| 3519 | this_rq->next_balance = next_balance; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | /* |
| 3524 | * active_load_balance is run by migration threads. It pushes running tasks |
| 3525 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be |
| 3526 | * running on each physical CPU where possible, and avoids physical / |
| 3527 | * logical imbalances. |
| 3528 | * |
| 3529 | * Called with busiest_rq locked. |
| 3530 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3531 | static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3533 | int target_cpu = busiest_rq->push_cpu; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3534 | struct sched_domain *sd; |
| 3535 | struct rq *target_rq; |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3536 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3537 | /* Is there any task to move? */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3538 | if (busiest_rq->nr_running <= 1) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3539 | return; |
| 3540 | |
| 3541 | target_rq = cpu_rq(target_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3542 | |
| 3543 | /* |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3544 | * This condition is "impossible", if it occurs |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3545 | * we need to fix it. Originally reported by |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3546 | * Bjorn Helgaas on a 128-cpu setup. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3548 | BUG_ON(busiest_rq == target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3550 | /* move a task from busiest_rq to target_rq */ |
| 3551 | double_lock_balance(busiest_rq, target_rq); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3552 | update_rq_clock(busiest_rq); |
| 3553 | update_rq_clock(target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3555 | /* Search for an sd spanning us and the target CPU. */ |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3556 | for_each_domain(target_cpu, sd) { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3557 | if ((sd->flags & SD_LOAD_BALANCE) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3558 | cpu_isset(busiest_cpu, sd->span)) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3559 | break; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3560 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3562 | if (likely(sd)) { |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3563 | schedstat_inc(sd, alb_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3565 | if (move_one_task(target_rq, target_cpu, busiest_rq, |
| 3566 | sd, CPU_IDLE)) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3567 | schedstat_inc(sd, alb_pushed); |
| 3568 | else |
| 3569 | schedstat_inc(sd, alb_failed); |
| 3570 | } |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3571 | spin_unlock(&target_rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | } |
| 3573 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3574 | #ifdef CONFIG_NO_HZ |
| 3575 | static struct { |
| 3576 | atomic_t load_balancer; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3577 | cpumask_t cpu_mask; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3578 | } nohz ____cacheline_aligned = { |
| 3579 | .load_balancer = ATOMIC_INIT(-1), |
| 3580 | .cpu_mask = CPU_MASK_NONE, |
| 3581 | }; |
| 3582 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3583 | /* |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3584 | * This routine will try to nominate the ilb (idle load balancing) |
| 3585 | * owner among the cpus whose ticks are stopped. ilb owner will do the idle |
| 3586 | * load balancing on behalf of all those cpus. If all the cpus in the system |
| 3587 | * go into this tickless mode, then there will be no ilb owner (as there is |
| 3588 | * no need for one) and all the cpus will sleep till the next wakeup event |
| 3589 | * arrives... |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3590 | * |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3591 | * For the ilb owner, tick is not stopped. And this tick will be used |
| 3592 | * for idle load balancing. ilb owner will still be part of |
| 3593 | * nohz.cpu_mask.. |
| 3594 | * |
| 3595 | * While stopping the tick, this cpu will become the ilb owner if there |
| 3596 | * is no other owner. And will be the owner till that cpu becomes busy |
| 3597 | * or if all cpus in the system stop their ticks at which point |
| 3598 | * there is no need for ilb owner. |
| 3599 | * |
| 3600 | * When the ilb owner becomes busy, it nominates another owner, during the |
| 3601 | * next busy scheduler_tick() |
| 3602 | */ |
| 3603 | int select_nohz_load_balancer(int stop_tick) |
| 3604 | { |
| 3605 | int cpu = smp_processor_id(); |
| 3606 | |
| 3607 | if (stop_tick) { |
| 3608 | cpu_set(cpu, nohz.cpu_mask); |
| 3609 | cpu_rq(cpu)->in_nohz_recently = 1; |
| 3610 | |
| 3611 | /* |
| 3612 | * If we are going offline and still the leader, give up! |
| 3613 | */ |
| 3614 | if (cpu_is_offline(cpu) && |
| 3615 | atomic_read(&nohz.load_balancer) == cpu) { |
| 3616 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 3617 | BUG(); |
| 3618 | return 0; |
| 3619 | } |
| 3620 | |
| 3621 | /* time for ilb owner also to sleep */ |
| 3622 | if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 3623 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 3624 | atomic_set(&nohz.load_balancer, -1); |
| 3625 | return 0; |
| 3626 | } |
| 3627 | |
| 3628 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 3629 | /* make me the ilb owner */ |
| 3630 | if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) |
| 3631 | return 1; |
| 3632 | } else if (atomic_read(&nohz.load_balancer) == cpu) |
| 3633 | return 1; |
| 3634 | } else { |
| 3635 | if (!cpu_isset(cpu, nohz.cpu_mask)) |
| 3636 | return 0; |
| 3637 | |
| 3638 | cpu_clear(cpu, nohz.cpu_mask); |
| 3639 | |
| 3640 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 3641 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 3642 | BUG(); |
| 3643 | } |
| 3644 | return 0; |
| 3645 | } |
| 3646 | #endif |
| 3647 | |
| 3648 | static DEFINE_SPINLOCK(balancing); |
| 3649 | |
| 3650 | /* |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3651 | * It checks each scheduling domain to see if it is due to be balanced, |
| 3652 | * and initiates a balancing operation if so. |
| 3653 | * |
| 3654 | * Balancing parameters are set up in arch_init_sched_domains. |
| 3655 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 3656 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3657 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3658 | int balance = 1; |
| 3659 | struct rq *rq = cpu_rq(cpu); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3660 | unsigned long interval; |
| 3661 | struct sched_domain *sd; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3662 | /* Earliest time when we have to do rebalance again */ |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 3663 | unsigned long next_balance = jiffies + 60*HZ; |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3664 | int update_next_balance = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3665 | cpumask_t tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3667 | for_each_domain(cpu, sd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3668 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 3669 | continue; |
| 3670 | |
| 3671 | interval = sd->balance_interval; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3672 | if (idle != CPU_IDLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | interval *= sd->busy_factor; |
| 3674 | |
| 3675 | /* scale ms to jiffies */ |
| 3676 | interval = msecs_to_jiffies(interval); |
| 3677 | if (unlikely(!interval)) |
| 3678 | interval = 1; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3679 | if (interval > HZ*NR_CPUS/10) |
| 3680 | interval = HZ*NR_CPUS/10; |
| 3681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 3683 | if (sd->flags & SD_SERIALIZE) { |
| 3684 | if (!spin_trylock(&balancing)) |
| 3685 | goto out; |
| 3686 | } |
| 3687 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 3688 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 3689 | if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) { |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3690 | /* |
| 3691 | * We've pulled tasks over so either we're no |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3692 | * longer idle, or one of our SMT siblings is |
| 3693 | * not idle. |
| 3694 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3695 | idle = CPU_NOT_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | } |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 3697 | sd->last_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | } |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 3699 | if (sd->flags & SD_SERIALIZE) |
| 3700 | spin_unlock(&balancing); |
| 3701 | out: |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3702 | if (time_after(next_balance, sd->last_balance + interval)) { |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 3703 | next_balance = sd->last_balance + interval; |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3704 | update_next_balance = 1; |
| 3705 | } |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3706 | |
| 3707 | /* |
| 3708 | * Stop the load balance at this level. There is another |
| 3709 | * CPU in our sched group which is doing load balancing more |
| 3710 | * actively. |
| 3711 | */ |
| 3712 | if (!balance) |
| 3713 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | } |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3715 | |
| 3716 | /* |
| 3717 | * next_balance will be updated only when there is a need. |
| 3718 | * When the cpu is attached to null domain for ex, it will not be |
| 3719 | * updated. |
| 3720 | */ |
| 3721 | if (likely(update_next_balance)) |
| 3722 | rq->next_balance = next_balance; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | /* |
| 3726 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
| 3727 | * In CONFIG_NO_HZ case, the idle load balance owner will do the |
| 3728 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 3729 | */ |
| 3730 | static void run_rebalance_domains(struct softirq_action *h) |
| 3731 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3732 | int this_cpu = smp_processor_id(); |
| 3733 | struct rq *this_rq = cpu_rq(this_cpu); |
| 3734 | enum cpu_idle_type idle = this_rq->idle_at_tick ? |
| 3735 | CPU_IDLE : CPU_NOT_IDLE; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3736 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3737 | rebalance_domains(this_cpu, idle); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3738 | |
| 3739 | #ifdef CONFIG_NO_HZ |
| 3740 | /* |
| 3741 | * If this cpu is the owner for idle load balancing, then do the |
| 3742 | * balancing on behalf of the other idle cpus whose ticks are |
| 3743 | * stopped. |
| 3744 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3745 | if (this_rq->idle_at_tick && |
| 3746 | atomic_read(&nohz.load_balancer) == this_cpu) { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3747 | cpumask_t cpus = nohz.cpu_mask; |
| 3748 | struct rq *rq; |
| 3749 | int balance_cpu; |
| 3750 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3751 | cpu_clear(this_cpu, cpus); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3752 | for_each_cpu_mask(balance_cpu, cpus) { |
| 3753 | /* |
| 3754 | * If this cpu gets work to do, stop the load balancing |
| 3755 | * work being done for other cpus. Next load |
| 3756 | * balancing owner will pick it up. |
| 3757 | */ |
| 3758 | if (need_resched()) |
| 3759 | break; |
| 3760 | |
Oleg Nesterov | de0cf89 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 3761 | rebalance_domains(balance_cpu, CPU_IDLE); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3762 | |
| 3763 | rq = cpu_rq(balance_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3764 | if (time_after(this_rq->next_balance, rq->next_balance)) |
| 3765 | this_rq->next_balance = rq->next_balance; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3766 | } |
| 3767 | } |
| 3768 | #endif |
| 3769 | } |
| 3770 | |
| 3771 | /* |
| 3772 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. |
| 3773 | * |
| 3774 | * In case of CONFIG_NO_HZ, this is the place where we nominate a new |
| 3775 | * idle load balancing owner or decide to stop the periodic load balancing, |
| 3776 | * if the whole system is idle. |
| 3777 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3778 | static inline void trigger_load_balance(struct rq *rq, int cpu) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3779 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3780 | #ifdef CONFIG_NO_HZ |
| 3781 | /* |
| 3782 | * If we were in the nohz mode recently and busy at the current |
| 3783 | * scheduler tick, then check if we need to nominate new idle |
| 3784 | * load balancer. |
| 3785 | */ |
| 3786 | if (rq->in_nohz_recently && !rq->idle_at_tick) { |
| 3787 | rq->in_nohz_recently = 0; |
| 3788 | |
| 3789 | if (atomic_read(&nohz.load_balancer) == cpu) { |
| 3790 | cpu_clear(cpu, nohz.cpu_mask); |
| 3791 | atomic_set(&nohz.load_balancer, -1); |
| 3792 | } |
| 3793 | |
| 3794 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 3795 | /* |
| 3796 | * simple selection for now: Nominate the |
| 3797 | * first cpu in the nohz list to be the next |
| 3798 | * ilb owner. |
| 3799 | * |
| 3800 | * TBD: Traverse the sched domains and nominate |
| 3801 | * the nearest cpu in the nohz.cpu_mask. |
| 3802 | */ |
| 3803 | int ilb = first_cpu(nohz.cpu_mask); |
| 3804 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 3805 | if (ilb < nr_cpu_ids) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3806 | resched_cpu(ilb); |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | /* |
| 3811 | * If this cpu is idle and doing idle load balancing for all the |
| 3812 | * cpus with ticks stopped, is it time for that to stop? |
| 3813 | */ |
| 3814 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && |
| 3815 | cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 3816 | resched_cpu(cpu); |
| 3817 | return; |
| 3818 | } |
| 3819 | |
| 3820 | /* |
| 3821 | * If this cpu is idle and the idle load balancing is done by |
| 3822 | * someone else, then no need raise the SCHED_SOFTIRQ |
| 3823 | */ |
| 3824 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && |
| 3825 | cpu_isset(cpu, nohz.cpu_mask)) |
| 3826 | return; |
| 3827 | #endif |
| 3828 | if (time_after_eq(jiffies, rq->next_balance)) |
| 3829 | raise_softirq(SCHED_SOFTIRQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3831 | |
| 3832 | #else /* CONFIG_SMP */ |
| 3833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | /* |
| 3835 | * on UP we do not need to balance between CPUs: |
| 3836 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3837 | static inline void idle_balance(int cpu, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | { |
| 3839 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3841 | #endif |
| 3842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
| 3844 | |
| 3845 | EXPORT_PER_CPU_SYMBOL(kstat); |
| 3846 | |
| 3847 | /* |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3848 | * Return p->sum_exec_runtime plus any more ns on the sched_clock |
| 3849 | * 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] | 3850 | */ |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3851 | unsigned long long task_sched_runtime(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3852 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | unsigned long flags; |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3854 | u64 ns, delta_exec; |
| 3855 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3856 | |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3857 | rq = task_rq_lock(p, &flags); |
| 3858 | ns = p->se.sum_exec_runtime; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 3859 | if (task_current(rq, p)) { |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 3860 | update_rq_clock(rq); |
| 3861 | delta_exec = rq->clock - p->se.exec_start; |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3862 | if ((s64)delta_exec > 0) |
| 3863 | ns += delta_exec; |
| 3864 | } |
| 3865 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | return ns; |
| 3868 | } |
| 3869 | |
| 3870 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | * Account user cpu time to a process. |
| 3872 | * @p: the process that the cpu time gets accounted to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | * @cputime: the cpu time spent in user space since the last update |
| 3874 | */ |
| 3875 | void account_user_time(struct task_struct *p, cputime_t cputime) |
| 3876 | { |
| 3877 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3878 | cputime64_t tmp; |
| 3879 | |
| 3880 | p->utime = cputime_add(p->utime, cputime); |
| 3881 | |
| 3882 | /* Add user time to cpustat. */ |
| 3883 | tmp = cputime_to_cputime64(cputime); |
| 3884 | if (TASK_NICE(p) > 0) |
| 3885 | cpustat->nice = cputime64_add(cpustat->nice, tmp); |
| 3886 | else |
| 3887 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 3888 | } |
| 3889 | |
| 3890 | /* |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 3891 | * Account guest cpu time to a process. |
| 3892 | * @p: the process that the cpu time gets accounted to |
| 3893 | * @cputime: the cpu time spent in virtual machine since the last update |
| 3894 | */ |
Adrian Bunk | f7402e0 | 2007-10-29 21:18:10 +0100 | [diff] [blame] | 3895 | static void account_guest_time(struct task_struct *p, cputime_t cputime) |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 3896 | { |
| 3897 | cputime64_t tmp; |
| 3898 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3899 | |
| 3900 | tmp = cputime_to_cputime64(cputime); |
| 3901 | |
| 3902 | p->utime = cputime_add(p->utime, cputime); |
| 3903 | p->gtime = cputime_add(p->gtime, cputime); |
| 3904 | |
| 3905 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 3906 | cpustat->guest = cputime64_add(cpustat->guest, tmp); |
| 3907 | } |
| 3908 | |
| 3909 | /* |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 3910 | * Account scaled user cpu time to a process. |
| 3911 | * @p: the process that the cpu time gets accounted to |
| 3912 | * @cputime: the cpu time spent in user space since the last update |
| 3913 | */ |
| 3914 | void account_user_time_scaled(struct task_struct *p, cputime_t cputime) |
| 3915 | { |
| 3916 | p->utimescaled = cputime_add(p->utimescaled, cputime); |
| 3917 | } |
| 3918 | |
| 3919 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | * Account system cpu time to a process. |
| 3921 | * @p: the process that the cpu time gets accounted to |
| 3922 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 3923 | * @cputime: the cpu time spent in kernel space since the last update |
| 3924 | */ |
| 3925 | void account_system_time(struct task_struct *p, int hardirq_offset, |
| 3926 | cputime_t cputime) |
| 3927 | { |
| 3928 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3929 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | cputime64_t tmp; |
| 3931 | |
Harvey Harrison | 983ed7a | 2008-04-24 18:17:55 -0700 | [diff] [blame] | 3932 | if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) { |
| 3933 | account_guest_time(p, cputime); |
| 3934 | return; |
| 3935 | } |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 3936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | p->stime = cputime_add(p->stime, cputime); |
| 3938 | |
| 3939 | /* Add system time to cpustat. */ |
| 3940 | tmp = cputime_to_cputime64(cputime); |
| 3941 | if (hardirq_count() - hardirq_offset) |
| 3942 | cpustat->irq = cputime64_add(cpustat->irq, tmp); |
| 3943 | else if (softirq_count()) |
| 3944 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 3945 | else if (p != rq->idle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | cpustat->system = cputime64_add(cpustat->system, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 3947 | else if (atomic_read(&rq->nr_iowait) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 3949 | else |
| 3950 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 3951 | /* Account for system time used */ |
| 3952 | acct_update_integrals(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | /* |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 3956 | * Account scaled system cpu time to a process. |
| 3957 | * @p: the process that the cpu time gets accounted to |
| 3958 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 3959 | * @cputime: the cpu time spent in kernel space since the last update |
| 3960 | */ |
| 3961 | void account_system_time_scaled(struct task_struct *p, cputime_t cputime) |
| 3962 | { |
| 3963 | p->stimescaled = cputime_add(p->stimescaled, cputime); |
| 3964 | } |
| 3965 | |
| 3966 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | * Account for involuntary wait time. |
| 3968 | * @p: the process from which the cpu time has been stolen |
| 3969 | * @steal: the cpu time spent in involuntary wait |
| 3970 | */ |
| 3971 | void account_steal_time(struct task_struct *p, cputime_t steal) |
| 3972 | { |
| 3973 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3974 | cputime64_t tmp = cputime_to_cputime64(steal); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3975 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | |
| 3977 | if (p == rq->idle) { |
| 3978 | p->stime = cputime_add(p->stime, steal); |
| 3979 | if (atomic_read(&rq->nr_iowait) > 0) |
| 3980 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 3981 | else |
| 3982 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 3983 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | cpustat->steal = cputime64_add(cpustat->steal, tmp); |
| 3985 | } |
| 3986 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3987 | /* |
| 3988 | * This function gets called by the timer code, with HZ frequency. |
| 3989 | * We call it with interrupts disabled. |
| 3990 | * |
| 3991 | * It also gets called by the fork code, when changing the parent's |
| 3992 | * timeslices. |
| 3993 | */ |
| 3994 | void scheduler_tick(void) |
| 3995 | { |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3996 | int cpu = smp_processor_id(); |
| 3997 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3998 | struct task_struct *curr = rq->curr; |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 3999 | |
| 4000 | sched_clock_tick(); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4001 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4002 | spin_lock(&rq->lock); |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 4003 | update_rq_clock(rq); |
Ingo Molnar | f1a438d | 2007-08-09 11:16:45 +0200 | [diff] [blame] | 4004 | update_cpu_load(rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4005 | curr->sched_class->task_tick(rq, curr, 0); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4006 | spin_unlock(&rq->lock); |
| 4007 | |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 4008 | #ifdef CONFIG_SMP |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4009 | rq->idle_at_tick = idle_cpu(cpu); |
| 4010 | trigger_load_balance(rq, cpu); |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 4011 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | } |
| 4013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) |
| 4015 | |
Srinivasa Ds | 4362758 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 4016 | void __kprobes add_preempt_count(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | { |
| 4018 | /* |
| 4019 | * Underflow? |
| 4020 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 4021 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
| 4022 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | preempt_count() += val; |
| 4024 | /* |
| 4025 | * Spinlock count overflowing soon? |
| 4026 | */ |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 4027 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
| 4028 | PREEMPT_MASK - 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | } |
| 4030 | EXPORT_SYMBOL(add_preempt_count); |
| 4031 | |
Srinivasa Ds | 4362758 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 4032 | void __kprobes sub_preempt_count(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | { |
| 4034 | /* |
| 4035 | * Underflow? |
| 4036 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 4037 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
| 4038 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 | /* |
| 4040 | * Is the spinlock portion underflowing? |
| 4041 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 4042 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
| 4043 | !(preempt_count() & PREEMPT_MASK))) |
| 4044 | return; |
| 4045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4046 | preempt_count() -= val; |
| 4047 | } |
| 4048 | EXPORT_SYMBOL(sub_preempt_count); |
| 4049 | |
| 4050 | #endif |
| 4051 | |
| 4052 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4053 | * Print scheduling while atomic bug: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4055 | static noinline void __schedule_bug(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | { |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 4057 | struct pt_regs *regs = get_irq_regs(); |
| 4058 | |
| 4059 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
| 4060 | prev->comm, prev->pid, preempt_count()); |
| 4061 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4062 | debug_show_held_locks(prev); |
| 4063 | if (irqs_disabled()) |
| 4064 | print_irqtrace_events(prev); |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 4065 | |
| 4066 | if (regs) |
| 4067 | show_regs(regs); |
| 4068 | else |
| 4069 | dump_stack(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4070 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4072 | /* |
| 4073 | * Various schedule()-time debugging checks and statistics: |
| 4074 | */ |
| 4075 | static inline void schedule_debug(struct task_struct *prev) |
| 4076 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4077 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4078 | * Test if we are atomic. Since do_exit() needs to call into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | * schedule() atomically, we ignore that path for now. |
| 4080 | * Otherwise, whine if we are scheduling when we should not be. |
| 4081 | */ |
Roel Kluin | 3f33a7c | 2008-05-13 23:44:11 +0200 | [diff] [blame] | 4082 | if (unlikely(in_atomic_preempt_off() && !prev->exit_state)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4083 | __schedule_bug(prev); |
| 4084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
| 4086 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4087 | schedstat_inc(this_rq(), sched_count); |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 4088 | #ifdef CONFIG_SCHEDSTATS |
| 4089 | if (unlikely(prev->lock_depth >= 0)) { |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4090 | schedstat_inc(this_rq(), bkl_count); |
| 4091 | schedstat_inc(prev, sched_info.bkl_count); |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 4092 | } |
| 4093 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | /* |
| 4097 | * Pick up the highest-prio task: |
| 4098 | */ |
| 4099 | static inline struct task_struct * |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4100 | pick_next_task(struct rq *rq, struct task_struct *prev) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4101 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4102 | const struct sched_class *class; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4103 | struct task_struct *p; |
| 4104 | |
| 4105 | /* |
| 4106 | * Optimization: we know that if all tasks are in |
| 4107 | * the fair class we can call that function directly: |
| 4108 | */ |
| 4109 | if (likely(rq->nr_running == rq->cfs.nr_running)) { |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 4110 | p = fair_sched_class.pick_next_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4111 | if (likely(p)) |
| 4112 | return p; |
| 4113 | } |
| 4114 | |
| 4115 | class = sched_class_highest; |
| 4116 | for ( ; ; ) { |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 4117 | p = class->pick_next_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4118 | if (p) |
| 4119 | return p; |
| 4120 | /* |
| 4121 | * Will never be NULL as the idle class always |
| 4122 | * returns a non-NULL p: |
| 4123 | */ |
| 4124 | class = class->next; |
| 4125 | } |
| 4126 | } |
| 4127 | |
| 4128 | /* |
| 4129 | * schedule() is the main scheduler function. |
| 4130 | */ |
| 4131 | asmlinkage void __sched schedule(void) |
| 4132 | { |
| 4133 | struct task_struct *prev, *next; |
Harvey Harrison | 67ca7bd | 2008-02-15 09:56:36 -0800 | [diff] [blame] | 4134 | unsigned long *switch_count; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4135 | struct rq *rq; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4136 | int cpu; |
| 4137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | need_resched: |
| 4139 | preempt_disable(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4140 | cpu = smp_processor_id(); |
| 4141 | rq = cpu_rq(cpu); |
| 4142 | rcu_qsctr_inc(cpu); |
| 4143 | prev = rq->curr; |
| 4144 | switch_count = &prev->nivcsw; |
| 4145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | release_kernel_lock(prev); |
| 4147 | need_resched_nonpreemptible: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4149 | schedule_debug(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4151 | hrtick_clear(rq); |
| 4152 | |
Ingo Molnar | 1e81995 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4153 | /* |
| 4154 | * Do the rq-clock update outside the rq lock: |
| 4155 | */ |
| 4156 | local_irq_disable(); |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 4157 | update_rq_clock(rq); |
Ingo Molnar | 1e81995 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4158 | spin_lock(&rq->lock); |
| 4159 | clear_tsk_need_resched(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4161 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
| 4162 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && |
Roel Kluin | 23e3c3c | 2008-03-13 17:41:59 +0100 | [diff] [blame] | 4163 | signal_pending(prev))) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4164 | prev->state = TASK_RUNNING; |
| 4165 | } else { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4166 | deactivate_task(rq, prev, 1); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4167 | } |
| 4168 | switch_count = &prev->nvcsw; |
| 4169 | } |
| 4170 | |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4171 | #ifdef CONFIG_SMP |
| 4172 | if (prev->sched_class->pre_schedule) |
| 4173 | prev->sched_class->pre_schedule(rq, prev); |
| 4174 | #endif |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 4175 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4176 | if (unlikely(!rq->nr_running)) |
| 4177 | idle_balance(cpu, rq); |
| 4178 | |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4179 | prev->sched_class->put_prev_task(rq, prev); |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4180 | next = pick_next_task(rq, prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | if (likely(prev != next)) { |
David Simner | 673a90a | 2008-04-29 10:08:59 +0100 | [diff] [blame] | 4183 | sched_info_switch(prev, next); |
| 4184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | rq->nr_switches++; |
| 4186 | rq->curr = next; |
| 4187 | ++*switch_count; |
| 4188 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4189 | context_switch(rq, prev, next); /* unlocks the rq */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4190 | /* |
| 4191 | * the context switch might have flipped the stack from under |
| 4192 | * us, hence refresh the local variables. |
| 4193 | */ |
| 4194 | cpu = smp_processor_id(); |
| 4195 | rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | } else |
| 4197 | spin_unlock_irq(&rq->lock); |
| 4198 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4199 | hrtick_set(rq); |
| 4200 | |
| 4201 | if (unlikely(reacquire_kernel_lock(current) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | goto need_resched_nonpreemptible; |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | preempt_enable_no_resched(); |
| 4205 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 4206 | goto need_resched; |
| 4207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | EXPORT_SYMBOL(schedule); |
| 4209 | |
| 4210 | #ifdef CONFIG_PREEMPT |
| 4211 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4212 | * this is the entry point to schedule() from in-kernel preemption |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4213 | * off of preempt_enable. Kernel preemptions off return from interrupt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4214 | * occur there and call schedule directly. |
| 4215 | */ |
| 4216 | asmlinkage void __sched preempt_schedule(void) |
| 4217 | { |
| 4218 | struct thread_info *ti = current_thread_info(); |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 4219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | /* |
| 4221 | * If there is a non-zero preempt_count or interrupts are disabled, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4222 | * we do not want to preempt the current task. Just return.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | */ |
Nick Piggin | beed33a | 2006-10-11 01:21:52 -0700 | [diff] [blame] | 4224 | if (likely(ti->preempt_count || irqs_disabled())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | return; |
| 4226 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4227 | do { |
| 4228 | add_preempt_count(PREEMPT_ACTIVE); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4229 | schedule(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4230 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4231 | |
| 4232 | /* |
| 4233 | * Check again in case we missed a preemption opportunity |
| 4234 | * between schedule and now. |
| 4235 | */ |
| 4236 | barrier(); |
| 4237 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | EXPORT_SYMBOL(preempt_schedule); |
| 4240 | |
| 4241 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4242 | * this is the entry point to schedule() from kernel preemption |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | * off of irq context. |
| 4244 | * Note, that this is called and return with irqs disabled. This will |
| 4245 | * protect us against recursive calling from irq. |
| 4246 | */ |
| 4247 | asmlinkage void __sched preempt_schedule_irq(void) |
| 4248 | { |
| 4249 | struct thread_info *ti = current_thread_info(); |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 4250 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4251 | /* Catch callers which need to be fixed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
| 4253 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4254 | do { |
| 4255 | add_preempt_count(PREEMPT_ACTIVE); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4256 | local_irq_enable(); |
| 4257 | schedule(); |
| 4258 | local_irq_disable(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4259 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4260 | |
| 4261 | /* |
| 4262 | * Check again in case we missed a preemption opportunity |
| 4263 | * between schedule and now. |
| 4264 | */ |
| 4265 | barrier(); |
| 4266 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | #endif /* CONFIG_PREEMPT */ |
| 4270 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4271 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, |
| 4272 | void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4274 | return try_to_wake_up(curr->private, mode, sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | EXPORT_SYMBOL(default_wake_function); |
| 4277 | |
| 4278 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4279 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just |
| 4280 | * 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] | 4281 | * number) then we wake all the non-exclusive tasks and one exclusive task. |
| 4282 | * |
| 4283 | * 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] | 4284 | * 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] | 4285 | * zero in this (rare) case, and we handle it by continuing to scan the queue. |
| 4286 | */ |
| 4287 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, |
| 4288 | int nr_exclusive, int sync, void *key) |
| 4289 | { |
Matthias Kaehlcke | 2e45874 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 4290 | wait_queue_t *curr, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | |
Matthias Kaehlcke | 2e45874 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 4292 | list_for_each_entry_safe(curr, next, &q->task_list, task_list) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4293 | unsigned flags = curr->flags; |
| 4294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | if (curr->func(curr, mode, sync, key) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4296 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | break; |
| 4298 | } |
| 4299 | } |
| 4300 | |
| 4301 | /** |
| 4302 | * __wake_up - wake up threads blocked on a waitqueue. |
| 4303 | * @q: the waitqueue |
| 4304 | * @mode: which threads |
| 4305 | * @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] | 4306 | * @key: is directly passed to the wakeup function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4308 | void __wake_up(wait_queue_head_t *q, unsigned int mode, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4309 | int nr_exclusive, void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | { |
| 4311 | unsigned long flags; |
| 4312 | |
| 4313 | spin_lock_irqsave(&q->lock, flags); |
| 4314 | __wake_up_common(q, mode, nr_exclusive, 0, key); |
| 4315 | spin_unlock_irqrestore(&q->lock, flags); |
| 4316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | EXPORT_SYMBOL(__wake_up); |
| 4318 | |
| 4319 | /* |
| 4320 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. |
| 4321 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4322 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | { |
| 4324 | __wake_up_common(q, mode, 1, 0, NULL); |
| 4325 | } |
| 4326 | |
| 4327 | /** |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 4328 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | * @q: the waitqueue |
| 4330 | * @mode: which threads |
| 4331 | * @nr_exclusive: how many wake-one or wake-many threads to wake up |
| 4332 | * |
| 4333 | * The sync wakeup differs that the waker knows that it will schedule |
| 4334 | * away soon, so while the target thread will be woken up, it will not |
| 4335 | * be migrated to another CPU - ie. the two threads are 'synchronized' |
| 4336 | * with each other. This can prevent needless bouncing between CPUs. |
| 4337 | * |
| 4338 | * On UP it can prevent extra preemption. |
| 4339 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4340 | void |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4341 | __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] | 4342 | { |
| 4343 | unsigned long flags; |
| 4344 | int sync = 1; |
| 4345 | |
| 4346 | if (unlikely(!q)) |
| 4347 | return; |
| 4348 | |
| 4349 | if (unlikely(!nr_exclusive)) |
| 4350 | sync = 0; |
| 4351 | |
| 4352 | spin_lock_irqsave(&q->lock, flags); |
| 4353 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); |
| 4354 | spin_unlock_irqrestore(&q->lock, flags); |
| 4355 | } |
| 4356 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ |
| 4357 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4358 | void complete(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | { |
| 4360 | unsigned long flags; |
| 4361 | |
| 4362 | spin_lock_irqsave(&x->wait.lock, flags); |
| 4363 | x->done++; |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 4364 | __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 4366 | } |
| 4367 | EXPORT_SYMBOL(complete); |
| 4368 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4369 | void complete_all(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | { |
| 4371 | unsigned long flags; |
| 4372 | |
| 4373 | spin_lock_irqsave(&x->wait.lock, flags); |
| 4374 | x->done += UINT_MAX/2; |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 4375 | __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 4377 | } |
| 4378 | EXPORT_SYMBOL(complete_all); |
| 4379 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4380 | static inline long __sched |
| 4381 | do_wait_for_common(struct completion *x, long timeout, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4382 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | if (!x->done) { |
| 4384 | DECLARE_WAITQUEUE(wait, current); |
| 4385 | |
| 4386 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 4387 | __add_wait_queue_tail(&x->wait, &wait); |
| 4388 | do { |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 4389 | if ((state == TASK_INTERRUPTIBLE && |
| 4390 | signal_pending(current)) || |
| 4391 | (state == TASK_KILLABLE && |
| 4392 | fatal_signal_pending(current))) { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4393 | __remove_wait_queue(&x->wait, &wait); |
| 4394 | return -ERESTARTSYS; |
| 4395 | } |
| 4396 | __set_current_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | spin_unlock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4398 | timeout = schedule_timeout(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | spin_lock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4400 | if (!timeout) { |
| 4401 | __remove_wait_queue(&x->wait, &wait); |
| 4402 | return timeout; |
| 4403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | } while (!x->done); |
| 4405 | __remove_wait_queue(&x->wait, &wait); |
| 4406 | } |
| 4407 | x->done--; |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4408 | return timeout; |
| 4409 | } |
| 4410 | |
| 4411 | static long __sched |
| 4412 | wait_for_common(struct completion *x, long timeout, int state) |
| 4413 | { |
| 4414 | might_sleep(); |
| 4415 | |
| 4416 | spin_lock_irq(&x->wait.lock); |
| 4417 | timeout = do_wait_for_common(x, timeout, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | spin_unlock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4419 | return timeout; |
| 4420 | } |
| 4421 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4422 | void __sched wait_for_completion(struct completion *x) |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4423 | { |
| 4424 | wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | } |
| 4426 | EXPORT_SYMBOL(wait_for_completion); |
| 4427 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4428 | unsigned long __sched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) |
| 4430 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4431 | return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | } |
| 4433 | EXPORT_SYMBOL(wait_for_completion_timeout); |
| 4434 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4435 | int __sched wait_for_completion_interruptible(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | { |
Andi Kleen | 51e9799 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 4437 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE); |
| 4438 | if (t == -ERESTARTSYS) |
| 4439 | return t; |
| 4440 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | } |
| 4442 | EXPORT_SYMBOL(wait_for_completion_interruptible); |
| 4443 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4444 | unsigned long __sched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | wait_for_completion_interruptible_timeout(struct completion *x, |
| 4446 | unsigned long timeout) |
| 4447 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4448 | return wait_for_common(x, timeout, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | } |
| 4450 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); |
| 4451 | |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 4452 | int __sched wait_for_completion_killable(struct completion *x) |
| 4453 | { |
| 4454 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE); |
| 4455 | if (t == -ERESTARTSYS) |
| 4456 | return t; |
| 4457 | return 0; |
| 4458 | } |
| 4459 | EXPORT_SYMBOL(wait_for_completion_killable); |
| 4460 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4461 | static long __sched |
| 4462 | sleep_on_common(wait_queue_head_t *q, int state, long timeout) |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4463 | { |
| 4464 | unsigned long flags; |
| 4465 | wait_queue_t wait; |
| 4466 | |
| 4467 | init_waitqueue_entry(&wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4469 | __set_current_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4471 | spin_lock_irqsave(&q->lock, flags); |
| 4472 | __add_wait_queue(q, &wait); |
| 4473 | spin_unlock(&q->lock); |
| 4474 | timeout = schedule_timeout(timeout); |
| 4475 | spin_lock_irq(&q->lock); |
| 4476 | __remove_wait_queue(q, &wait); |
| 4477 | spin_unlock_irqrestore(&q->lock, flags); |
| 4478 | |
| 4479 | return timeout; |
| 4480 | } |
| 4481 | |
| 4482 | void __sched interruptible_sleep_on(wait_queue_head_t *q) |
| 4483 | { |
| 4484 | sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | EXPORT_SYMBOL(interruptible_sleep_on); |
| 4487 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4488 | long __sched |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4489 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4491 | return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
| 4494 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4495 | void __sched sleep_on(wait_queue_head_t *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4497 | sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | EXPORT_SYMBOL(sleep_on); |
| 4500 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4501 | long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4503 | return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | EXPORT_SYMBOL(sleep_on_timeout); |
| 4506 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4507 | #ifdef CONFIG_RT_MUTEXES |
| 4508 | |
| 4509 | /* |
| 4510 | * rt_mutex_setprio - set the current priority of a task |
| 4511 | * @p: task |
| 4512 | * @prio: prio value (kernel-internal form) |
| 4513 | * |
| 4514 | * This function changes the 'effective' priority of a task. It does |
| 4515 | * not touch ->normal_prio like __setscheduler(). |
| 4516 | * |
| 4517 | * Used by the rt_mutex code to implement priority inheritance logic. |
| 4518 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4519 | void rt_mutex_setprio(struct task_struct *p, int prio) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4520 | { |
| 4521 | unsigned long flags; |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4522 | int oldprio, on_rq, running; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4523 | struct rq *rq; |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4524 | const struct sched_class *prev_class = p->sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4525 | |
| 4526 | BUG_ON(prio < 0 || prio > MAX_PRIO); |
| 4527 | |
| 4528 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4529 | update_rq_clock(rq); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4530 | |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4531 | oldprio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4532 | on_rq = p->se.on_rq; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 4533 | running = task_current(rq, p); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4534 | if (on_rq) |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4535 | dequeue_task(rq, p, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4536 | if (running) |
| 4537 | p->sched_class->put_prev_task(rq, p); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4538 | |
| 4539 | if (rt_prio(prio)) |
| 4540 | p->sched_class = &rt_sched_class; |
| 4541 | else |
| 4542 | p->sched_class = &fair_sched_class; |
| 4543 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4544 | p->prio = prio; |
| 4545 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4546 | if (running) |
| 4547 | p->sched_class->set_curr_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4548 | if (on_rq) { |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4549 | enqueue_task(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4550 | |
| 4551 | check_class_changed(rq, p, prev_class, oldprio, running); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4552 | } |
| 4553 | task_rq_unlock(rq, &flags); |
| 4554 | } |
| 4555 | |
| 4556 | #endif |
| 4557 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4558 | void set_user_nice(struct task_struct *p, long nice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4559 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4560 | int old_prio, delta, on_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4562 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | |
| 4564 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) |
| 4565 | return; |
| 4566 | /* |
| 4567 | * We have to be careful, if called from sys_setpriority(), |
| 4568 | * the task might be in the middle of scheduling on another CPU. |
| 4569 | */ |
| 4570 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4571 | update_rq_clock(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | /* |
| 4573 | * The RT priorities are set via sched_setscheduler(), but we still |
| 4574 | * allow the 'normal' nice value to be set - but as expected |
| 4575 | * it wont have any effect on scheduling until the task is |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4576 | * SCHED_FIFO/SCHED_RR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | */ |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4578 | if (task_has_rt_policy(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4579 | p->static_prio = NICE_TO_PRIO(nice); |
| 4580 | goto out_unlock; |
| 4581 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4582 | on_rq = p->se.on_rq; |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 4583 | if (on_rq) { |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4584 | dequeue_task(rq, p, 0); |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 4585 | dec_load(rq, p); |
| 4586 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | p->static_prio = NICE_TO_PRIO(nice); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 4589 | set_load_weight(p); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4590 | old_prio = p->prio; |
| 4591 | p->prio = effective_prio(p); |
| 4592 | delta = p->prio - old_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4594 | if (on_rq) { |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4595 | enqueue_task(rq, p, 0); |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 4596 | inc_load(rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | /* |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4598 | * If the task increased its priority or is running and |
| 4599 | * lowered its priority, then reschedule its CPU: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | */ |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4601 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | resched_task(rq->curr); |
| 4603 | } |
| 4604 | out_unlock: |
| 4605 | task_rq_unlock(rq, &flags); |
| 4606 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | EXPORT_SYMBOL(set_user_nice); |
| 4608 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4609 | /* |
| 4610 | * can_nice - check if a task can reduce its nice value |
| 4611 | * @p: task |
| 4612 | * @nice: nice value |
| 4613 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4614 | int can_nice(const struct task_struct *p, const int nice) |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4615 | { |
Matt Mackall | 024f474 | 2005-08-18 11:24:19 -0700 | [diff] [blame] | 4616 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
| 4617 | int nice_rlim = 20 - nice; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4618 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4619 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
| 4620 | capable(CAP_SYS_NICE)); |
| 4621 | } |
| 4622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | #ifdef __ARCH_WANT_SYS_NICE |
| 4624 | |
| 4625 | /* |
| 4626 | * sys_nice - change the priority of the current process. |
| 4627 | * @increment: priority increment |
| 4628 | * |
| 4629 | * sys_setpriority is a more generic, but much slower function that |
| 4630 | * does similar things. |
| 4631 | */ |
| 4632 | asmlinkage long sys_nice(int increment) |
| 4633 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4634 | long nice, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | |
| 4636 | /* |
| 4637 | * Setpriority might change our priority at the same moment. |
| 4638 | * We don't have to worry. Conceptually one call occurs first |
| 4639 | * and we have a single winner. |
| 4640 | */ |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4641 | if (increment < -40) |
| 4642 | increment = -40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | if (increment > 40) |
| 4644 | increment = 40; |
| 4645 | |
| 4646 | nice = PRIO_TO_NICE(current->static_prio) + increment; |
| 4647 | if (nice < -20) |
| 4648 | nice = -20; |
| 4649 | if (nice > 19) |
| 4650 | nice = 19; |
| 4651 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4652 | if (increment < 0 && !can_nice(current, nice)) |
| 4653 | return -EPERM; |
| 4654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | retval = security_task_setnice(current, nice); |
| 4656 | if (retval) |
| 4657 | return retval; |
| 4658 | |
| 4659 | set_user_nice(current, nice); |
| 4660 | return 0; |
| 4661 | } |
| 4662 | |
| 4663 | #endif |
| 4664 | |
| 4665 | /** |
| 4666 | * task_prio - return the priority value of a given task. |
| 4667 | * @p: the task in question. |
| 4668 | * |
| 4669 | * This is the priority value as seen by users in /proc. |
| 4670 | * RT tasks are offset by -200. Normal tasks are centered |
| 4671 | * around 0, value goes from -16 to +15. |
| 4672 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4673 | int task_prio(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | { |
| 4675 | return p->prio - MAX_RT_PRIO; |
| 4676 | } |
| 4677 | |
| 4678 | /** |
| 4679 | * task_nice - return the nice value of a given task. |
| 4680 | * @p: the task in question. |
| 4681 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4682 | int task_nice(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | { |
| 4684 | return TASK_NICE(p); |
| 4685 | } |
Pavel Roskin | 150d8be | 2008-03-05 16:56:37 -0500 | [diff] [blame] | 4686 | EXPORT_SYMBOL(task_nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | |
| 4688 | /** |
| 4689 | * idle_cpu - is a given cpu idle currently? |
| 4690 | * @cpu: the processor in question. |
| 4691 | */ |
| 4692 | int idle_cpu(int cpu) |
| 4693 | { |
| 4694 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; |
| 4695 | } |
| 4696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | /** |
| 4698 | * idle_task - return the idle task for a given cpu. |
| 4699 | * @cpu: the processor in question. |
| 4700 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4701 | struct task_struct *idle_task(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | { |
| 4703 | return cpu_rq(cpu)->idle; |
| 4704 | } |
| 4705 | |
| 4706 | /** |
| 4707 | * find_process_by_pid - find a process with a matching PID value. |
| 4708 | * @pid: the pid in question. |
| 4709 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4710 | static struct task_struct *find_process_by_pid(pid_t pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | { |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 4712 | return pid ? find_task_by_vpid(pid) : current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | /* Actually do priority change: must hold rq lock. */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4716 | static void |
| 4717 | __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4719 | BUG_ON(p->se.on_rq); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | p->policy = policy; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4722 | switch (p->policy) { |
| 4723 | case SCHED_NORMAL: |
| 4724 | case SCHED_BATCH: |
| 4725 | case SCHED_IDLE: |
| 4726 | p->sched_class = &fair_sched_class; |
| 4727 | break; |
| 4728 | case SCHED_FIFO: |
| 4729 | case SCHED_RR: |
| 4730 | p->sched_class = &rt_sched_class; |
| 4731 | break; |
| 4732 | } |
| 4733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | p->rt_priority = prio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4735 | p->normal_prio = normal_prio(p); |
| 4736 | /* we are holding p->pi_lock already */ |
| 4737 | p->prio = rt_mutex_getprio(p); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 4738 | set_load_weight(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 4742 | * 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] | 4743 | * @p: the task in question. |
| 4744 | * @policy: new policy. |
| 4745 | * @param: structure containing the new RT priority. |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4746 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 4747 | * NOTE that the task may be already dead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4749 | int sched_setscheduler(struct task_struct *p, int policy, |
| 4750 | struct sched_param *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4752 | int retval, oldprio, oldpolicy = -1, on_rq, running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | unsigned long flags; |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4754 | const struct sched_class *prev_class = p->sched_class; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4755 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | |
Steven Rostedt | 66e5393 | 2006-06-27 02:54:44 -0700 | [diff] [blame] | 4757 | /* may grab non-irq protected spin_locks */ |
| 4758 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4759 | recheck: |
| 4760 | /* double check policy once rq lock held */ |
| 4761 | if (policy < 0) |
| 4762 | policy = oldpolicy = p->policy; |
| 4763 | else if (policy != SCHED_FIFO && policy != SCHED_RR && |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4764 | policy != SCHED_NORMAL && policy != SCHED_BATCH && |
| 4765 | policy != SCHED_IDLE) |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4766 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | /* |
| 4768 | * Valid priorities for SCHED_FIFO and SCHED_RR are |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4769 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
| 4770 | * SCHED_BATCH and SCHED_IDLE is 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | */ |
| 4772 | if (param->sched_priority < 0 || |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4773 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
Steven Rostedt | d46523e | 2005-07-25 16:28:39 -0400 | [diff] [blame] | 4774 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | return -EINVAL; |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4776 | if (rt_policy(policy) != (param->sched_priority != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | return -EINVAL; |
| 4778 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4779 | /* |
| 4780 | * Allow unprivileged RT tasks to decrease priority: |
| 4781 | */ |
| 4782 | if (!capable(CAP_SYS_NICE)) { |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4783 | if (rt_policy(policy)) { |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4784 | unsigned long rlim_rtprio; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4785 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4786 | if (!lock_task_sighand(p, &flags)) |
| 4787 | return -ESRCH; |
| 4788 | rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur; |
| 4789 | unlock_task_sighand(p, &flags); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4790 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4791 | /* can't set/change the rt policy */ |
| 4792 | if (policy != p->policy && !rlim_rtprio) |
| 4793 | return -EPERM; |
| 4794 | |
| 4795 | /* can't increase priority */ |
| 4796 | if (param->sched_priority > p->rt_priority && |
| 4797 | param->sched_priority > rlim_rtprio) |
| 4798 | return -EPERM; |
| 4799 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4800 | /* |
| 4801 | * Like positive nice levels, dont allow tasks to |
| 4802 | * move out of SCHED_IDLE either: |
| 4803 | */ |
| 4804 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) |
| 4805 | return -EPERM; |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4806 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4807 | /* can't change other user's priorities */ |
| 4808 | if ((current->euid != p->euid) && |
| 4809 | (current->euid != p->uid)) |
| 4810 | return -EPERM; |
| 4811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 4813 | #ifdef CONFIG_RT_GROUP_SCHED |
| 4814 | /* |
| 4815 | * Do not allow realtime tasks into groups that have no runtime |
| 4816 | * assigned. |
| 4817 | */ |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 4818 | if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0) |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 4819 | return -EPERM; |
| 4820 | #endif |
| 4821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | retval = security_task_setscheduler(p, policy, param); |
| 4823 | if (retval) |
| 4824 | return retval; |
| 4825 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4826 | * make sure no PI-waiters arrive (or leave) while we are |
| 4827 | * changing the priority of the task: |
| 4828 | */ |
| 4829 | spin_lock_irqsave(&p->pi_lock, flags); |
| 4830 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | * To be able to change p->policy safely, the apropriate |
| 4832 | * runqueue lock must be held. |
| 4833 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4834 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | /* recheck policy now with rq lock held */ |
| 4836 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
| 4837 | policy = oldpolicy = -1; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4838 | __task_rq_unlock(rq); |
| 4839 | spin_unlock_irqrestore(&p->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | goto recheck; |
| 4841 | } |
Ingo Molnar | 2daa357 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 4842 | update_rq_clock(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4843 | on_rq = p->se.on_rq; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 4844 | running = task_current(rq, p); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4845 | if (on_rq) |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4846 | deactivate_task(rq, p, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4847 | if (running) |
| 4848 | p->sched_class->put_prev_task(rq, p); |
Dmitry Adamushko | f6b53205 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | oldprio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4851 | __setscheduler(rq, p, policy, param->sched_priority); |
Dmitry Adamushko | f6b53205 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4852 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4853 | if (running) |
| 4854 | p->sched_class->set_curr_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4855 | if (on_rq) { |
| 4856 | activate_task(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4857 | |
| 4858 | check_class_changed(rq, p, prev_class, oldprio, running); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4860 | __task_rq_unlock(rq); |
| 4861 | spin_unlock_irqrestore(&p->pi_lock, flags); |
| 4862 | |
Thomas Gleixner | 95e02ca | 2006-06-27 02:55:02 -0700 | [diff] [blame] | 4863 | rt_mutex_adjust_pi(p); |
| 4864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | return 0; |
| 4866 | } |
| 4867 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
| 4868 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4869 | static int |
| 4870 | 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] | 4871 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | struct sched_param lparam; |
| 4873 | struct task_struct *p; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4874 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | |
| 4876 | if (!param || pid < 0) |
| 4877 | return -EINVAL; |
| 4878 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) |
| 4879 | return -EFAULT; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4880 | |
| 4881 | rcu_read_lock(); |
| 4882 | retval = -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | p = find_process_by_pid(pid); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4884 | if (p != NULL) |
| 4885 | retval = sched_setscheduler(p, policy, &lparam); |
| 4886 | rcu_read_unlock(); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | return retval; |
| 4889 | } |
| 4890 | |
| 4891 | /** |
| 4892 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority |
| 4893 | * @pid: the pid in question. |
| 4894 | * @policy: new policy. |
| 4895 | * @param: structure containing the new RT priority. |
| 4896 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4897 | asmlinkage long |
| 4898 | 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] | 4899 | { |
Jason Baron | c21761f | 2006-01-18 17:43:03 -0800 | [diff] [blame] | 4900 | /* negative values for policy are not valid */ |
| 4901 | if (policy < 0) |
| 4902 | return -EINVAL; |
| 4903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | return do_sched_setscheduler(pid, policy, param); |
| 4905 | } |
| 4906 | |
| 4907 | /** |
| 4908 | * sys_sched_setparam - set/change the RT priority of a thread |
| 4909 | * @pid: the pid in question. |
| 4910 | * @param: structure containing the new RT priority. |
| 4911 | */ |
| 4912 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) |
| 4913 | { |
| 4914 | return do_sched_setscheduler(pid, -1, param); |
| 4915 | } |
| 4916 | |
| 4917 | /** |
| 4918 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread |
| 4919 | * @pid: the pid in question. |
| 4920 | */ |
| 4921 | asmlinkage long sys_sched_getscheduler(pid_t pid) |
| 4922 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4923 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4924 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | |
| 4926 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4927 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | |
| 4929 | retval = -ESRCH; |
| 4930 | read_lock(&tasklist_lock); |
| 4931 | p = find_process_by_pid(pid); |
| 4932 | if (p) { |
| 4933 | retval = security_task_getscheduler(p); |
| 4934 | if (!retval) |
| 4935 | retval = p->policy; |
| 4936 | } |
| 4937 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | return retval; |
| 4939 | } |
| 4940 | |
| 4941 | /** |
| 4942 | * sys_sched_getscheduler - get the RT priority of a thread |
| 4943 | * @pid: the pid in question. |
| 4944 | * @param: structure containing the RT priority. |
| 4945 | */ |
| 4946 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) |
| 4947 | { |
| 4948 | struct sched_param lp; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4949 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4950 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | |
| 4952 | if (!param || pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4953 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | |
| 4955 | read_lock(&tasklist_lock); |
| 4956 | p = find_process_by_pid(pid); |
| 4957 | retval = -ESRCH; |
| 4958 | if (!p) |
| 4959 | goto out_unlock; |
| 4960 | |
| 4961 | retval = security_task_getscheduler(p); |
| 4962 | if (retval) |
| 4963 | goto out_unlock; |
| 4964 | |
| 4965 | lp.sched_priority = p->rt_priority; |
| 4966 | read_unlock(&tasklist_lock); |
| 4967 | |
| 4968 | /* |
| 4969 | * This one might sleep, we cannot do it with a spinlock held ... |
| 4970 | */ |
| 4971 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; |
| 4972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | return retval; |
| 4974 | |
| 4975 | out_unlock: |
| 4976 | read_unlock(&tasklist_lock); |
| 4977 | return retval; |
| 4978 | } |
| 4979 | |
Mike Travis | b53e921 | 2008-04-04 18:11:08 -0700 | [diff] [blame] | 4980 | long sched_setaffinity(pid_t pid, const cpumask_t *in_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | cpumask_t cpus_allowed; |
Mike Travis | b53e921 | 2008-04-04 18:11:08 -0700 | [diff] [blame] | 4983 | cpumask_t new_mask = *in_mask; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4984 | struct task_struct *p; |
| 4985 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4987 | get_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | read_lock(&tasklist_lock); |
| 4989 | |
| 4990 | p = find_process_by_pid(pid); |
| 4991 | if (!p) { |
| 4992 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4993 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | return -ESRCH; |
| 4995 | } |
| 4996 | |
| 4997 | /* |
| 4998 | * It is not safe to call set_cpus_allowed with the |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4999 | * tasklist_lock held. We will bump the task_struct's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | * usage count and then drop tasklist_lock. |
| 5001 | */ |
| 5002 | get_task_struct(p); |
| 5003 | read_unlock(&tasklist_lock); |
| 5004 | |
| 5005 | retval = -EPERM; |
| 5006 | if ((current->euid != p->euid) && (current->euid != p->uid) && |
| 5007 | !capable(CAP_SYS_NICE)) |
| 5008 | goto out_unlock; |
| 5009 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 5010 | retval = security_task_setscheduler(p, 0, NULL); |
| 5011 | if (retval) |
| 5012 | goto out_unlock; |
| 5013 | |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 5014 | cpuset_cpus_allowed(p, &cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | cpus_and(new_mask, new_mask, cpus_allowed); |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 5016 | again: |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 5017 | retval = set_cpus_allowed_ptr(p, &new_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 5019 | if (!retval) { |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 5020 | cpuset_cpus_allowed(p, &cpus_allowed); |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 5021 | if (!cpus_subset(new_mask, cpus_allowed)) { |
| 5022 | /* |
| 5023 | * We must have raced with a concurrent cpuset |
| 5024 | * update. Just reset the cpus_allowed to the |
| 5025 | * cpuset's cpus_allowed |
| 5026 | */ |
| 5027 | new_mask = cpus_allowed; |
| 5028 | goto again; |
| 5029 | } |
| 5030 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | out_unlock: |
| 5032 | put_task_struct(p); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5033 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | return retval; |
| 5035 | } |
| 5036 | |
| 5037 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, |
| 5038 | cpumask_t *new_mask) |
| 5039 | { |
| 5040 | if (len < sizeof(cpumask_t)) { |
| 5041 | memset(new_mask, 0, sizeof(cpumask_t)); |
| 5042 | } else if (len > sizeof(cpumask_t)) { |
| 5043 | len = sizeof(cpumask_t); |
| 5044 | } |
| 5045 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
| 5046 | } |
| 5047 | |
| 5048 | /** |
| 5049 | * sys_sched_setaffinity - set the cpu affinity of a process |
| 5050 | * @pid: pid of the process |
| 5051 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 5052 | * @user_mask_ptr: user-space pointer to the new cpu mask |
| 5053 | */ |
| 5054 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, |
| 5055 | unsigned long __user *user_mask_ptr) |
| 5056 | { |
| 5057 | cpumask_t new_mask; |
| 5058 | int retval; |
| 5059 | |
| 5060 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); |
| 5061 | if (retval) |
| 5062 | return retval; |
| 5063 | |
Mike Travis | b53e921 | 2008-04-04 18:11:08 -0700 | [diff] [blame] | 5064 | return sched_setaffinity(pid, &new_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | } |
| 5066 | |
| 5067 | /* |
| 5068 | * Represents all cpu's present in the system |
| 5069 | * In systems capable of hotplug, this map could dynamically grow |
| 5070 | * as new cpu's are detected in the system via any platform specific |
| 5071 | * method, such as ACPI for e.g. |
| 5072 | */ |
| 5073 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 5074 | cpumask_t cpu_present_map __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | EXPORT_SYMBOL(cpu_present_map); |
| 5076 | |
| 5077 | #ifndef CONFIG_SMP |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 5078 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 5079 | EXPORT_SYMBOL(cpu_online_map); |
| 5080 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 5081 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 5082 | EXPORT_SYMBOL(cpu_possible_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | #endif |
| 5084 | |
| 5085 | long sched_getaffinity(pid_t pid, cpumask_t *mask) |
| 5086 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5087 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5090 | get_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | read_lock(&tasklist_lock); |
| 5092 | |
| 5093 | retval = -ESRCH; |
| 5094 | p = find_process_by_pid(pid); |
| 5095 | if (!p) |
| 5096 | goto out_unlock; |
| 5097 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 5098 | retval = security_task_getscheduler(p); |
| 5099 | if (retval) |
| 5100 | goto out_unlock; |
| 5101 | |
Jack Steiner | 2f7016d | 2006-02-01 03:05:18 -0800 | [diff] [blame] | 5102 | cpus_and(*mask, p->cpus_allowed, cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | |
| 5104 | out_unlock: |
| 5105 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5106 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | |
Ulrich Drepper | 9531b62 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5108 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | /** |
| 5112 | * sys_sched_getaffinity - get the cpu affinity of a process |
| 5113 | * @pid: pid of the process |
| 5114 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 5115 | * @user_mask_ptr: user-space pointer to hold the current cpu mask |
| 5116 | */ |
| 5117 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, |
| 5118 | unsigned long __user *user_mask_ptr) |
| 5119 | { |
| 5120 | int ret; |
| 5121 | cpumask_t mask; |
| 5122 | |
| 5123 | if (len < sizeof(cpumask_t)) |
| 5124 | return -EINVAL; |
| 5125 | |
| 5126 | ret = sched_getaffinity(pid, &mask); |
| 5127 | if (ret < 0) |
| 5128 | return ret; |
| 5129 | |
| 5130 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) |
| 5131 | return -EFAULT; |
| 5132 | |
| 5133 | return sizeof(cpumask_t); |
| 5134 | } |
| 5135 | |
| 5136 | /** |
| 5137 | * sys_sched_yield - yield the current processor to other threads. |
| 5138 | * |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5139 | * This function yields the current CPU to other tasks. If there are no |
| 5140 | * other threads running on this CPU then this function will return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | */ |
| 5142 | asmlinkage long sys_sched_yield(void) |
| 5143 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5144 | struct rq *rq = this_rq_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 5146 | schedstat_inc(rq, yld_count); |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5147 | current->sched_class->yield_task(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | |
| 5149 | /* |
| 5150 | * Since we are going to call schedule() anyway, there's |
| 5151 | * no need to preempt or enable interrupts: |
| 5152 | */ |
| 5153 | __release(rq->lock); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 5154 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | _raw_spin_unlock(&rq->lock); |
| 5156 | preempt_enable_no_resched(); |
| 5157 | |
| 5158 | schedule(); |
| 5159 | |
| 5160 | return 0; |
| 5161 | } |
| 5162 | |
Andrew Morton | e7b3840 | 2006-06-30 01:56:00 -0700 | [diff] [blame] | 5163 | static void __cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | { |
Ingo Molnar | 8e0a43d | 2006-06-23 02:05:23 -0700 | [diff] [blame] | 5165 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 5166 | __might_sleep(__FILE__, __LINE__); |
| 5167 | #endif |
Ingo Molnar | 5bbcfd9 | 2005-07-07 17:57:04 -0700 | [diff] [blame] | 5168 | /* |
| 5169 | * The BKS might be reacquired before we have dropped |
| 5170 | * PREEMPT_ACTIVE, which could trigger a second |
| 5171 | * cond_resched() call. |
| 5172 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | do { |
| 5174 | add_preempt_count(PREEMPT_ACTIVE); |
| 5175 | schedule(); |
| 5176 | sub_preempt_count(PREEMPT_ACTIVE); |
| 5177 | } while (need_resched()); |
| 5178 | } |
| 5179 | |
Herbert Xu | 02b67cc3 | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 5180 | int __sched _cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | { |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 5182 | if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) && |
| 5183 | system_state == SYSTEM_RUNNING) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | __cond_resched(); |
| 5185 | return 1; |
| 5186 | } |
| 5187 | return 0; |
| 5188 | } |
Herbert Xu | 02b67cc3 | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 5189 | EXPORT_SYMBOL(_cond_resched); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | |
| 5191 | /* |
| 5192 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, |
| 5193 | * call schedule, and on return reacquire the lock. |
| 5194 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5195 | * 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] | 5196 | * operations here to prevent schedule() from being called twice (once via |
| 5197 | * spin_unlock(), once by hand). |
| 5198 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5199 | int cond_resched_lock(spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | { |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5201 | int resched = need_resched() && system_state == SYSTEM_RUNNING; |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5202 | int ret = 0; |
| 5203 | |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5204 | if (spin_needbreak(lock) || resched) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | spin_unlock(lock); |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5206 | if (resched && need_resched()) |
| 5207 | __cond_resched(); |
| 5208 | else |
| 5209 | cpu_relax(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5210 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | spin_lock(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | } |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5213 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | EXPORT_SYMBOL(cond_resched_lock); |
| 5216 | |
| 5217 | int __sched cond_resched_softirq(void) |
| 5218 | { |
| 5219 | BUG_ON(!in_softirq()); |
| 5220 | |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 5221 | if (need_resched() && system_state == SYSTEM_RUNNING) { |
Thomas Gleixner | 98d82567 | 2007-05-23 13:58:18 -0700 | [diff] [blame] | 5222 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | __cond_resched(); |
| 5224 | local_bh_disable(); |
| 5225 | return 1; |
| 5226 | } |
| 5227 | return 0; |
| 5228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | EXPORT_SYMBOL(cond_resched_softirq); |
| 5230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | /** |
| 5232 | * yield - yield the current processor to other threads. |
| 5233 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 5234 | * This is a shortcut for kernel-space yielding - it marks the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | * thread runnable and calls sys_sched_yield(). |
| 5236 | */ |
| 5237 | void __sched yield(void) |
| 5238 | { |
| 5239 | set_current_state(TASK_RUNNING); |
| 5240 | sys_sched_yield(); |
| 5241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | EXPORT_SYMBOL(yield); |
| 5243 | |
| 5244 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5245 | * 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] | 5246 | * that process accounting knows that this is a task in IO wait state. |
| 5247 | * |
| 5248 | * But don't do that if it is a deliberate, throttling IO wait (this task |
| 5249 | * has set its backing_dev_info: the queue against which it should throttle) |
| 5250 | */ |
| 5251 | void __sched io_schedule(void) |
| 5252 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5253 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5255 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | atomic_inc(&rq->nr_iowait); |
| 5257 | schedule(); |
| 5258 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5259 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | EXPORT_SYMBOL(io_schedule); |
| 5262 | |
| 5263 | long __sched io_schedule_timeout(long timeout) |
| 5264 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5265 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | long ret; |
| 5267 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5268 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | atomic_inc(&rq->nr_iowait); |
| 5270 | ret = schedule_timeout(timeout); |
| 5271 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5272 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | return ret; |
| 5274 | } |
| 5275 | |
| 5276 | /** |
| 5277 | * sys_sched_get_priority_max - return maximum RT priority. |
| 5278 | * @policy: scheduling class. |
| 5279 | * |
| 5280 | * this syscall returns the maximum rt_priority that can be used |
| 5281 | * by a given scheduling class. |
| 5282 | */ |
| 5283 | asmlinkage long sys_sched_get_priority_max(int policy) |
| 5284 | { |
| 5285 | int ret = -EINVAL; |
| 5286 | |
| 5287 | switch (policy) { |
| 5288 | case SCHED_FIFO: |
| 5289 | case SCHED_RR: |
| 5290 | ret = MAX_USER_RT_PRIO-1; |
| 5291 | break; |
| 5292 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5293 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5294 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | ret = 0; |
| 5296 | break; |
| 5297 | } |
| 5298 | return ret; |
| 5299 | } |
| 5300 | |
| 5301 | /** |
| 5302 | * sys_sched_get_priority_min - return minimum RT priority. |
| 5303 | * @policy: scheduling class. |
| 5304 | * |
| 5305 | * this syscall returns the minimum rt_priority that can be used |
| 5306 | * by a given scheduling class. |
| 5307 | */ |
| 5308 | asmlinkage long sys_sched_get_priority_min(int policy) |
| 5309 | { |
| 5310 | int ret = -EINVAL; |
| 5311 | |
| 5312 | switch (policy) { |
| 5313 | case SCHED_FIFO: |
| 5314 | case SCHED_RR: |
| 5315 | ret = 1; |
| 5316 | break; |
| 5317 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5318 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5319 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | ret = 0; |
| 5321 | } |
| 5322 | return ret; |
| 5323 | } |
| 5324 | |
| 5325 | /** |
| 5326 | * sys_sched_rr_get_interval - return the default timeslice of a process. |
| 5327 | * @pid: pid of the process. |
| 5328 | * @interval: userspace pointer to the timeslice value. |
| 5329 | * |
| 5330 | * this syscall writes the default timeslice value of a given process |
| 5331 | * into the user-space timespec buffer. A value of '0' means infinity. |
| 5332 | */ |
| 5333 | asmlinkage |
| 5334 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) |
| 5335 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5336 | struct task_struct *p; |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5337 | unsigned int time_slice; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5338 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | struct timespec t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | |
| 5341 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5342 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | |
| 5344 | retval = -ESRCH; |
| 5345 | read_lock(&tasklist_lock); |
| 5346 | p = find_process_by_pid(pid); |
| 5347 | if (!p) |
| 5348 | goto out_unlock; |
| 5349 | |
| 5350 | retval = security_task_getscheduler(p); |
| 5351 | if (retval) |
| 5352 | goto out_unlock; |
| 5353 | |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5354 | /* |
| 5355 | * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER |
| 5356 | * tasks that are on an otherwise idle runqueue: |
| 5357 | */ |
| 5358 | time_slice = 0; |
| 5359 | if (p->policy == SCHED_RR) { |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5360 | time_slice = DEF_TIMESLICE; |
Miao Xie | 1868f95 | 2008-03-07 09:35:06 +0800 | [diff] [blame] | 5361 | } else if (p->policy != SCHED_FIFO) { |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5362 | struct sched_entity *se = &p->se; |
| 5363 | unsigned long flags; |
| 5364 | struct rq *rq; |
| 5365 | |
| 5366 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5367 | if (rq->cfs.load.weight) |
| 5368 | time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5369 | task_rq_unlock(rq, &flags); |
| 5370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | read_unlock(&tasklist_lock); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5372 | jiffies_to_timespec(time_slice, &t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | return retval; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | out_unlock: |
| 5377 | read_unlock(&tasklist_lock); |
| 5378 | return retval; |
| 5379 | } |
| 5380 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5381 | static const char stat_nam[] = "RSDTtZX"; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5382 | |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5383 | void sched_show_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | unsigned long free = 0; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5386 | unsigned state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | state = p->state ? __ffs(p->state) + 1 : 0; |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5389 | printk(KERN_INFO "%-13.13s %c", p->comm, |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5390 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5391 | #if BITS_PER_LONG == 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5392 | if (state == TASK_RUNNING) |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5393 | printk(KERN_CONT " running "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | else |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5395 | printk(KERN_CONT " %08lx ", thread_saved_pc(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | #else |
| 5397 | if (state == TASK_RUNNING) |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5398 | printk(KERN_CONT " running task "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | else |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5400 | printk(KERN_CONT " %016lx ", thread_saved_pc(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | #endif |
| 5402 | #ifdef CONFIG_DEBUG_STACK_USAGE |
| 5403 | { |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 5404 | unsigned long *n = end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | while (!*n) |
| 5406 | n++; |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 5407 | free = (unsigned long)n - (unsigned long)end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | } |
| 5409 | #endif |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5410 | printk(KERN_CONT "%5lu %5d %6d\n", free, |
Roland McGrath | fcfd50a | 2008-01-09 00:03:23 -0800 | [diff] [blame] | 5411 | task_pid_nr(p), task_pid_nr(p->real_parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | |
Nick Piggin | 5fb5e6d | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 5413 | show_stack(p, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | } |
| 5415 | |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 5416 | void show_state_filter(unsigned long state_filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5418 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5420 | #if BITS_PER_LONG == 32 |
| 5421 | printk(KERN_INFO |
| 5422 | " task PC stack pid father\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | #else |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5424 | printk(KERN_INFO |
| 5425 | " task PC stack pid father\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | #endif |
| 5427 | read_lock(&tasklist_lock); |
| 5428 | do_each_thread(g, p) { |
| 5429 | /* |
| 5430 | * reset the NMI-timeout, listing all files on a slow |
| 5431 | * console might take alot of time: |
| 5432 | */ |
| 5433 | touch_nmi_watchdog(); |
Ingo Molnar | 39bc89f | 2007-04-25 20:50:03 -0700 | [diff] [blame] | 5434 | if (!state_filter || (p->state & state_filter)) |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5435 | sched_show_task(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | } while_each_thread(g, p); |
| 5437 | |
Jeremy Fitzhardinge | 04c9167 | 2007-05-08 00:28:05 -0700 | [diff] [blame] | 5438 | touch_all_softlockup_watchdogs(); |
| 5439 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5440 | #ifdef CONFIG_SCHED_DEBUG |
| 5441 | sysrq_sched_debug_show(); |
| 5442 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | read_unlock(&tasklist_lock); |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 5444 | /* |
| 5445 | * Only show locks if all tasks are dumped: |
| 5446 | */ |
| 5447 | if (state_filter == -1) |
| 5448 | debug_show_all_locks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | } |
| 5450 | |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5451 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) |
| 5452 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5453 | idle->sched_class = &idle_sched_class; |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5454 | } |
| 5455 | |
Ingo Molnar | f340c0d | 2005-06-28 16:40:42 +0200 | [diff] [blame] | 5456 | /** |
| 5457 | * init_idle - set up an idle thread for a given CPU |
| 5458 | * @idle: task in question |
| 5459 | * @cpu: cpu the idle task belongs to |
| 5460 | * |
| 5461 | * NOTE: this function does not set the idle thread's NEED_RESCHED |
| 5462 | * flag, to make booting more robust. |
| 5463 | */ |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 5464 | void __cpuinit init_idle(struct task_struct *idle, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5466 | struct rq *rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | unsigned long flags; |
| 5468 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5469 | __sched_fork(idle); |
| 5470 | idle->se.exec_start = sched_clock(); |
| 5471 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5472 | idle->prio = idle->normal_prio = MAX_PRIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | idle->cpus_allowed = cpumask_of_cpu(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5474 | __set_task_cpu(idle, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | |
| 5476 | spin_lock_irqsave(&rq->lock, flags); |
| 5477 | rq->curr = rq->idle = idle; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 5478 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
| 5479 | idle->oncpu = 1; |
| 5480 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5482 | |
| 5483 | /* Set the preempt count _outside_ the spinlocks! */ |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 5484 | #if defined(CONFIG_PREEMPT) |
| 5485 | task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0); |
| 5486 | #else |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 5487 | task_thread_info(idle)->preempt_count = 0; |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 5488 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5489 | /* |
| 5490 | * The idle tasks have their own, simple scheduling class: |
| 5491 | */ |
| 5492 | idle->sched_class = &idle_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | /* |
| 5496 | * In a system that switches off the HZ timer nohz_cpu_mask |
| 5497 | * indicates which cpus entered this state. This is used |
| 5498 | * in the rcu update to wait only for active cpus. For system |
| 5499 | * which do not switch off the HZ timer nohz_cpu_mask should |
| 5500 | * always be CPU_MASK_NONE. |
| 5501 | */ |
| 5502 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; |
| 5503 | |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 5504 | /* |
| 5505 | * Increase the granularity value when there are more CPUs, |
| 5506 | * because with more CPUs the 'effective latency' as visible |
| 5507 | * to users decreases. But the relationship is not linear, |
| 5508 | * so pick a second-best guess by going with the log2 of the |
| 5509 | * number of CPUs. |
| 5510 | * |
| 5511 | * This idea comes from the SD scheduler of Con Kolivas: |
| 5512 | */ |
| 5513 | static inline void sched_init_granularity(void) |
| 5514 | { |
| 5515 | unsigned int factor = 1 + ilog2(num_online_cpus()); |
| 5516 | const unsigned long limit = 200000000; |
| 5517 | |
| 5518 | sysctl_sched_min_granularity *= factor; |
| 5519 | if (sysctl_sched_min_granularity > limit) |
| 5520 | sysctl_sched_min_granularity = limit; |
| 5521 | |
| 5522 | sysctl_sched_latency *= factor; |
| 5523 | if (sysctl_sched_latency > limit) |
| 5524 | sysctl_sched_latency = limit; |
| 5525 | |
| 5526 | sysctl_sched_wakeup_granularity *= factor; |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 5527 | } |
| 5528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | #ifdef CONFIG_SMP |
| 5530 | /* |
| 5531 | * This is how migration works: |
| 5532 | * |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5533 | * 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] | 5534 | * runqueue and wake up that CPU's migration thread. |
| 5535 | * 2) we down() the locked semaphore => thread blocks. |
| 5536 | * 3) migration thread wakes up (implicitly it forces the migrated |
| 5537 | * thread off the CPU) |
| 5538 | * 4) it gets the migration request and checks whether the migrated |
| 5539 | * task is still in the wrong runqueue. |
| 5540 | * 5) if it's in the wrong runqueue then the migration thread removes |
| 5541 | * it and puts it into the right queue. |
| 5542 | * 6) migration thread up()s the semaphore. |
| 5543 | * 7) we wake up and the migration is done. |
| 5544 | */ |
| 5545 | |
| 5546 | /* |
| 5547 | * Change a given task's CPU affinity. Migrate the thread to a |
| 5548 | * proper CPU and schedule it away if the CPU it's executing on |
| 5549 | * is removed from the allowed bitmask. |
| 5550 | * |
| 5551 | * NOTE: the caller must have a valid reference to the task, the |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5552 | * task must not exit() & deallocate itself prematurely. The |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | * call is not atomic; no spinlocks may be held. |
| 5554 | */ |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5555 | 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] | 5556 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5557 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5558 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5559 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5560 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | |
| 5562 | rq = task_rq_lock(p, &flags); |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5563 | if (!cpus_intersects(*new_mask, cpu_online_map)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5564 | ret = -EINVAL; |
| 5565 | goto out; |
| 5566 | } |
| 5567 | |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 5568 | if (p->sched_class->set_cpus_allowed) |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5569 | p->sched_class->set_cpus_allowed(p, new_mask); |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 5570 | else { |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5571 | p->cpus_allowed = *new_mask; |
| 5572 | p->rt.nr_cpus_allowed = cpus_weight(*new_mask); |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 5573 | } |
| 5574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5575 | /* 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] | 5576 | if (cpu_isset(task_cpu(p), *new_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | goto out; |
| 5578 | |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5579 | if (migrate_task(p, any_online_cpu(*new_mask), &req)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | /* Need help from migration thread: drop lock and wait. */ |
| 5581 | task_rq_unlock(rq, &flags); |
| 5582 | wake_up_process(rq->migration_thread); |
| 5583 | wait_for_completion(&req.done); |
| 5584 | tlb_migrate_finish(p->mm); |
| 5585 | return 0; |
| 5586 | } |
| 5587 | out: |
| 5588 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | return ret; |
| 5591 | } |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 5592 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5593 | |
| 5594 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5595 | * 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] | 5596 | * this because either it can't run here any more (set_cpus_allowed() |
| 5597 | * away from this CPU, or CPU going down), or because we're |
| 5598 | * attempting to rebalance this task on exec (sched_exec). |
| 5599 | * |
| 5600 | * So we race with normal scheduler movements, but that's OK, as long |
| 5601 | * as the task is no longer on this CPU. |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5602 | * |
| 5603 | * Returns non-zero if task was successfully migrated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | */ |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5605 | 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] | 5606 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5607 | struct rq *rq_dest, *rq_src; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5608 | int ret = 0, on_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | |
| 5610 | if (unlikely(cpu_is_offline(dest_cpu))) |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5611 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | |
| 5613 | rq_src = cpu_rq(src_cpu); |
| 5614 | rq_dest = cpu_rq(dest_cpu); |
| 5615 | |
| 5616 | double_rq_lock(rq_src, rq_dest); |
| 5617 | /* Already moved. */ |
| 5618 | if (task_cpu(p) != src_cpu) |
| 5619 | goto out; |
| 5620 | /* Affinity changed (again). */ |
| 5621 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) |
| 5622 | goto out; |
| 5623 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5624 | on_rq = p->se.on_rq; |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5625 | if (on_rq) |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5626 | deactivate_task(rq_src, p, 0); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | set_task_cpu(p, dest_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5629 | if (on_rq) { |
| 5630 | activate_task(rq_dest, p, 0); |
| 5631 | check_preempt_curr(rq_dest, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5632 | } |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5633 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | out: |
| 5635 | double_rq_unlock(rq_src, rq_dest); |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5636 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | } |
| 5638 | |
| 5639 | /* |
| 5640 | * migration_thread - this is a highprio system thread that performs |
| 5641 | * thread migration by bumping thread off CPU then 'pushing' onto |
| 5642 | * another runqueue. |
| 5643 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5644 | static int migration_thread(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | int cpu = (long)data; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5647 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | |
| 5649 | rq = cpu_rq(cpu); |
| 5650 | BUG_ON(rq->migration_thread != current); |
| 5651 | |
| 5652 | set_current_state(TASK_INTERRUPTIBLE); |
| 5653 | while (!kthread_should_stop()) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5654 | struct migration_req *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | spin_lock_irq(&rq->lock); |
| 5658 | |
| 5659 | if (cpu_is_offline(cpu)) { |
| 5660 | spin_unlock_irq(&rq->lock); |
| 5661 | goto wait_to_die; |
| 5662 | } |
| 5663 | |
| 5664 | if (rq->active_balance) { |
| 5665 | active_load_balance(rq, cpu); |
| 5666 | rq->active_balance = 0; |
| 5667 | } |
| 5668 | |
| 5669 | head = &rq->migration_queue; |
| 5670 | |
| 5671 | if (list_empty(head)) { |
| 5672 | spin_unlock_irq(&rq->lock); |
| 5673 | schedule(); |
| 5674 | set_current_state(TASK_INTERRUPTIBLE); |
| 5675 | continue; |
| 5676 | } |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5677 | req = list_entry(head->next, struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | list_del_init(head->next); |
| 5679 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 5680 | spin_unlock(&rq->lock); |
| 5681 | __migrate_task(req->task, cpu, req->dest_cpu); |
| 5682 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | |
| 5684 | complete(&req->done); |
| 5685 | } |
| 5686 | __set_current_state(TASK_RUNNING); |
| 5687 | return 0; |
| 5688 | |
| 5689 | wait_to_die: |
| 5690 | /* Wait for kthread_stop */ |
| 5691 | set_current_state(TASK_INTERRUPTIBLE); |
| 5692 | while (!kthread_should_stop()) { |
| 5693 | schedule(); |
| 5694 | set_current_state(TASK_INTERRUPTIBLE); |
| 5695 | } |
| 5696 | __set_current_state(TASK_RUNNING); |
| 5697 | return 0; |
| 5698 | } |
| 5699 | |
| 5700 | #ifdef CONFIG_HOTPLUG_CPU |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5701 | |
| 5702 | static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu) |
| 5703 | { |
| 5704 | int ret; |
| 5705 | |
| 5706 | local_irq_disable(); |
| 5707 | ret = __migrate_task(p, src_cpu, dest_cpu); |
| 5708 | local_irq_enable(); |
| 5709 | return ret; |
| 5710 | } |
| 5711 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5712 | /* |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 5713 | * 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] | 5714 | * NOTE: interrupts should be disabled by the caller |
| 5715 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5716 | 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] | 5717 | { |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5718 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5719 | cpumask_t mask; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5720 | struct rq *rq; |
| 5721 | int dest_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5723 | do { |
| 5724 | /* On same node? */ |
| 5725 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); |
| 5726 | cpus_and(mask, mask, p->cpus_allowed); |
| 5727 | dest_cpu = any_online_cpu(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5729 | /* On any allowed CPU? */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 5730 | if (dest_cpu >= nr_cpu_ids) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5731 | dest_cpu = any_online_cpu(p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5733 | /* No more Mr. Nice Guy. */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 5734 | if (dest_cpu >= nr_cpu_ids) { |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 5735 | cpumask_t cpus_allowed; |
| 5736 | |
| 5737 | cpuset_cpus_allowed_locked(p, &cpus_allowed); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5738 | /* |
| 5739 | * Try to stay on the same cpuset, where the |
| 5740 | * current cpuset may be a subset of all cpus. |
| 5741 | * The cpuset_cpus_allowed_locked() variant of |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5742 | * cpuset_cpus_allowed() will not block. It must be |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5743 | * called within calls to cpuset_lock/cpuset_unlock. |
| 5744 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5745 | rq = task_rq_lock(p, &flags); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5746 | p->cpus_allowed = cpus_allowed; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5747 | dest_cpu = any_online_cpu(p->cpus_allowed); |
| 5748 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5750 | /* |
| 5751 | * Don't tell them about moving exiting tasks or |
| 5752 | * kernel threads (both mm NULL), since they never |
| 5753 | * leave kernel. |
| 5754 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5755 | if (p->mm && printk_ratelimit()) { |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5756 | printk(KERN_INFO "process %d (%s) no " |
| 5757 | "longer affine to cpu%d\n", |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5758 | task_pid_nr(p), p->comm, dead_cpu); |
| 5759 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5760 | } |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5761 | } while (!__migrate_task_irq(p, dead_cpu, dest_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | /* |
| 5765 | * While a dead CPU has no uninterruptible tasks queued at this point, |
| 5766 | * it might still have a nonzero ->nr_uninterruptible counter, because |
| 5767 | * for performance reasons the counter is not stricly tracking tasks to |
| 5768 | * their home CPUs. So we just add the counter to another CPU's counter, |
| 5769 | * to keep the global sum constant after CPU-down: |
| 5770 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5771 | static void migrate_nr_uninterruptible(struct rq *rq_src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5772 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 5773 | 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] | 5774 | unsigned long flags; |
| 5775 | |
| 5776 | local_irq_save(flags); |
| 5777 | double_rq_lock(rq_src, rq_dest); |
| 5778 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; |
| 5779 | rq_src->nr_uninterruptible = 0; |
| 5780 | double_rq_unlock(rq_src, rq_dest); |
| 5781 | local_irq_restore(flags); |
| 5782 | } |
| 5783 | |
| 5784 | /* Run through task list and migrate tasks from the dead cpu. */ |
| 5785 | static void migrate_live_tasks(int src_cpu) |
| 5786 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5787 | struct task_struct *p, *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5789 | read_lock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5791 | do_each_thread(t, p) { |
| 5792 | if (p == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5793 | continue; |
| 5794 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5795 | if (task_cpu(p) == src_cpu) |
| 5796 | move_task_off_dead_cpu(src_cpu, p); |
| 5797 | } while_each_thread(t, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5798 | |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5799 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | } |
| 5801 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5802 | /* |
| 5803 | * Schedules idle task to be the next runnable task on current CPU. |
Dmitry Adamushko | 94bc9a7 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 5804 | * It does so by boosting its priority to highest possible. |
| 5805 | * Used by CPU offline code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5806 | */ |
| 5807 | void sched_idle_next(void) |
| 5808 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5809 | int this_cpu = smp_processor_id(); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5810 | struct rq *rq = cpu_rq(this_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | struct task_struct *p = rq->idle; |
| 5812 | unsigned long flags; |
| 5813 | |
| 5814 | /* cpu has to be offline */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5815 | BUG_ON(cpu_online(this_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5816 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5817 | /* |
| 5818 | * Strictly not necessary since rest of the CPUs are stopped by now |
| 5819 | * and interrupts disabled on the current cpu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | */ |
| 5821 | spin_lock_irqsave(&rq->lock, flags); |
| 5822 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5823 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5824 | |
Dmitry Adamushko | 94bc9a7 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 5825 | update_rq_clock(rq); |
| 5826 | activate_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | |
| 5828 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5829 | } |
| 5830 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5831 | /* |
| 5832 | * 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] | 5833 | * offline. |
| 5834 | */ |
| 5835 | void idle_task_exit(void) |
| 5836 | { |
| 5837 | struct mm_struct *mm = current->active_mm; |
| 5838 | |
| 5839 | BUG_ON(cpu_online(smp_processor_id())); |
| 5840 | |
| 5841 | if (mm != &init_mm) |
| 5842 | switch_mm(mm, &init_mm, current); |
| 5843 | mmdrop(mm); |
| 5844 | } |
| 5845 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5846 | /* called under rq->lock with disabled interrupts */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5847 | static void migrate_dead(unsigned int dead_cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5849 | struct rq *rq = cpu_rq(dead_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | |
| 5851 | /* Must be exiting, otherwise would be on tasklist. */ |
Eugene Teo | 270f722 | 2007-10-18 23:40:38 -0700 | [diff] [blame] | 5852 | BUG_ON(!p->exit_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5853 | |
| 5854 | /* Cannot have done final schedule yet: would have vanished. */ |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 5855 | BUG_ON(p->state == TASK_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5857 | get_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | |
| 5859 | /* |
| 5860 | * Drop lock around migration; if someone else moves it, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5861 | * 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] | 5862 | * fine. |
| 5863 | */ |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5864 | spin_unlock_irq(&rq->lock); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5865 | move_task_off_dead_cpu(dead_cpu, p); |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5866 | spin_lock_irq(&rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5868 | put_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | } |
| 5870 | |
| 5871 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ |
| 5872 | static void migrate_dead_tasks(unsigned int dead_cpu) |
| 5873 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5874 | struct rq *rq = cpu_rq(dead_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5875 | struct task_struct *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5877 | for ( ; ; ) { |
| 5878 | if (!rq->nr_running) |
| 5879 | break; |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 5880 | update_rq_clock(rq); |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5881 | next = pick_next_task(rq, rq->curr); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5882 | if (!next) |
| 5883 | break; |
| 5884 | migrate_dead(dead_cpu, next); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | } |
| 5887 | } |
| 5888 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 5889 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5890 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
| 5891 | |
| 5892 | static struct ctl_table sd_ctl_dir[] = { |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5893 | { |
| 5894 | .procname = "sched_domain", |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5895 | .mode = 0555, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5896 | }, |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 5897 | {0, }, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5898 | }; |
| 5899 | |
| 5900 | static struct ctl_table sd_ctl_root[] = { |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5901 | { |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5902 | .ctl_name = CTL_KERN, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5903 | .procname = "kernel", |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5904 | .mode = 0555, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5905 | .child = sd_ctl_dir, |
| 5906 | }, |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 5907 | {0, }, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5908 | }; |
| 5909 | |
| 5910 | static struct ctl_table *sd_alloc_ctl_entry(int n) |
| 5911 | { |
| 5912 | struct ctl_table *entry = |
Milton Miller | 5cf9f06 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5913 | kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5914 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5915 | return entry; |
| 5916 | } |
| 5917 | |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5918 | static void sd_free_ctl_entry(struct ctl_table **tablep) |
| 5919 | { |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5920 | struct ctl_table *entry; |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5921 | |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5922 | /* |
| 5923 | * In the intermediate directories, both the child directory and |
| 5924 | * procname are dynamically allocated and could fail but the mode |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5925 | * will always be set. In the lowest directory the names are |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5926 | * static strings and all have proc handlers. |
| 5927 | */ |
| 5928 | for (entry = *tablep; entry->mode; entry++) { |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5929 | if (entry->child) |
| 5930 | sd_free_ctl_entry(&entry->child); |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5931 | if (entry->proc_handler == NULL) |
| 5932 | kfree(entry->procname); |
| 5933 | } |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5934 | |
| 5935 | kfree(*tablep); |
| 5936 | *tablep = NULL; |
| 5937 | } |
| 5938 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5939 | static void |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5940 | set_table_entry(struct ctl_table *entry, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5941 | const char *procname, void *data, int maxlen, |
| 5942 | mode_t mode, proc_handler *proc_handler) |
| 5943 | { |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5944 | entry->procname = procname; |
| 5945 | entry->data = data; |
| 5946 | entry->maxlen = maxlen; |
| 5947 | entry->mode = mode; |
| 5948 | entry->proc_handler = proc_handler; |
| 5949 | } |
| 5950 | |
| 5951 | static struct ctl_table * |
| 5952 | sd_alloc_ctl_domain_table(struct sched_domain *sd) |
| 5953 | { |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5954 | struct ctl_table *table = sd_alloc_ctl_entry(12); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5955 | |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5956 | if (table == NULL) |
| 5957 | return NULL; |
| 5958 | |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5959 | set_table_entry(&table[0], "min_interval", &sd->min_interval, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5960 | sizeof(long), 0644, proc_doulongvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5961 | set_table_entry(&table[1], "max_interval", &sd->max_interval, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5962 | sizeof(long), 0644, proc_doulongvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5963 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5964 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5965 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5966 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5967 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5968 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5969 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5970 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5971 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5972 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5973 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5974 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5975 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5976 | sizeof(int), 0644, proc_dointvec_minmax); |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5977 | set_table_entry(&table[9], "cache_nice_tries", |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5978 | &sd->cache_nice_tries, |
| 5979 | sizeof(int), 0644, proc_dointvec_minmax); |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5980 | set_table_entry(&table[10], "flags", &sd->flags, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5981 | sizeof(int), 0644, proc_dointvec_minmax); |
Milton Miller | 6323469f | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5982 | /* &table[11] is terminator */ |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5983 | |
| 5984 | return table; |
| 5985 | } |
| 5986 | |
Ingo Molnar | 9a4e715 | 2007-11-28 15:52:56 +0100 | [diff] [blame] | 5987 | static ctl_table *sd_alloc_ctl_cpu_table(int cpu) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5988 | { |
| 5989 | struct ctl_table *entry, *table; |
| 5990 | struct sched_domain *sd; |
| 5991 | int domain_num = 0, i; |
| 5992 | char buf[32]; |
| 5993 | |
| 5994 | for_each_domain(cpu, sd) |
| 5995 | domain_num++; |
| 5996 | entry = table = sd_alloc_ctl_entry(domain_num + 1); |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5997 | if (table == NULL) |
| 5998 | return NULL; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5999 | |
| 6000 | i = 0; |
| 6001 | for_each_domain(cpu, sd) { |
| 6002 | snprintf(buf, 32, "domain%d", i); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6003 | entry->procname = kstrdup(buf, GFP_KERNEL); |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6004 | entry->mode = 0555; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6005 | entry->child = sd_alloc_ctl_domain_table(sd); |
| 6006 | entry++; |
| 6007 | i++; |
| 6008 | } |
| 6009 | return table; |
| 6010 | } |
| 6011 | |
| 6012 | static struct ctl_table_header *sd_sysctl_header; |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6013 | static void register_sched_domain_sysctl(void) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6014 | { |
| 6015 | int i, cpu_num = num_online_cpus(); |
| 6016 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); |
| 6017 | char buf[32]; |
| 6018 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6019 | WARN_ON(sd_ctl_dir[0].child); |
| 6020 | sd_ctl_dir[0].child = entry; |
| 6021 | |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6022 | if (entry == NULL) |
| 6023 | return; |
| 6024 | |
Milton Miller | 97b6ea7 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6025 | for_each_online_cpu(i) { |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6026 | snprintf(buf, 32, "cpu%d", i); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6027 | entry->procname = kstrdup(buf, GFP_KERNEL); |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 6028 | entry->mode = 0555; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6029 | entry->child = sd_alloc_ctl_cpu_table(i); |
Milton Miller | 97b6ea7 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6030 | entry++; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6031 | } |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6032 | |
| 6033 | WARN_ON(sd_sysctl_header); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6034 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); |
| 6035 | } |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6036 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6037 | /* may be called multiple times per register */ |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6038 | static void unregister_sched_domain_sysctl(void) |
| 6039 | { |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6040 | if (sd_sysctl_header) |
| 6041 | unregister_sysctl_table(sd_sysctl_header); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6042 | sd_sysctl_header = NULL; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6043 | if (sd_ctl_dir[0].child) |
| 6044 | sd_free_ctl_entry(&sd_ctl_dir[0].child); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6045 | } |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6046 | #else |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6047 | static void register_sched_domain_sysctl(void) |
| 6048 | { |
| 6049 | } |
| 6050 | static void unregister_sched_domain_sysctl(void) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 6051 | { |
| 6052 | } |
| 6053 | #endif |
| 6054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | /* |
| 6056 | * migration_call - callback that gets triggered when a CPU is added. |
| 6057 | * Here we can start up the necessary migration thread for the new CPU. |
| 6058 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6059 | static int __cpuinit |
| 6060 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | struct task_struct *p; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6063 | int cpu = (long)hcpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6065 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | |
| 6067 | switch (action) { |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 6068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6070 | case CPU_UP_PREPARE_FROZEN: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6071 | p = kthread_create(migration_thread, hcpu, "migration/%d", cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | if (IS_ERR(p)) |
| 6073 | return NOTIFY_BAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | kthread_bind(p, cpu); |
| 6075 | /* Must be high prio: stop_machine expects to yield to it. */ |
| 6076 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6077 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | task_rq_unlock(rq, &flags); |
| 6079 | cpu_rq(cpu)->migration_thread = p; |
| 6080 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6083 | case CPU_ONLINE_FROZEN: |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 6084 | /* Strictly unnecessary, as first user will wake it. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | wake_up_process(cpu_rq(cpu)->migration_thread); |
Gregory Haskins | 1f94ef5 | 2008-03-10 16:52:41 -0400 | [diff] [blame] | 6086 | |
| 6087 | /* Update our root-domain */ |
| 6088 | rq = cpu_rq(cpu); |
| 6089 | spin_lock_irqsave(&rq->lock, flags); |
| 6090 | if (rq->rd) { |
| 6091 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); |
| 6092 | cpu_set(cpu, rq->rd->online); |
| 6093 | } |
| 6094 | spin_unlock_irqrestore(&rq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | #ifdef CONFIG_HOTPLUG_CPU |
| 6098 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6099 | case CPU_UP_CANCELED_FROZEN: |
Heiko Carstens | fc75cdf | 2006-06-25 05:49:10 -0700 | [diff] [blame] | 6100 | if (!cpu_rq(cpu)->migration_thread) |
| 6101 | break; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6102 | /* Unbind it from offline cpu so it can run. Fall thru. */ |
Heiko Carstens | a4c4af7 | 2005-11-07 00:58:38 -0800 | [diff] [blame] | 6103 | kthread_bind(cpu_rq(cpu)->migration_thread, |
| 6104 | any_online_cpu(cpu_online_map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | kthread_stop(cpu_rq(cpu)->migration_thread); |
| 6106 | cpu_rq(cpu)->migration_thread = NULL; |
| 6107 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6110 | case CPU_DEAD_FROZEN: |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6111 | cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | migrate_live_tasks(cpu); |
| 6113 | rq = cpu_rq(cpu); |
| 6114 | kthread_stop(rq->migration_thread); |
| 6115 | rq->migration_thread = NULL; |
| 6116 | /* Idle task back to normal (off runqueue, low prio) */ |
Oleg Nesterov | d2da272 | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6117 | spin_lock_irq(&rq->lock); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 6118 | update_rq_clock(rq); |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 6119 | deactivate_task(rq, rq->idle, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | rq->idle->static_prio = MAX_PRIO; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6121 | __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); |
| 6122 | rq->idle->sched_class = &idle_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | migrate_dead_tasks(cpu); |
Oleg Nesterov | d2da272 | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 6124 | spin_unlock_irq(&rq->lock); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 6125 | cpuset_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6126 | migrate_nr_uninterruptible(rq); |
| 6127 | BUG_ON(rq->nr_running != 0); |
| 6128 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6129 | /* |
| 6130 | * No need to migrate the tasks: it was best-effort if |
| 6131 | * they didn't take sched_hotcpu_mutex. Just wake up |
| 6132 | * the requestors. |
| 6133 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6134 | spin_lock_irq(&rq->lock); |
| 6135 | while (!list_empty(&rq->migration_queue)) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6136 | struct migration_req *req; |
| 6137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6138 | req = list_entry(rq->migration_queue.next, |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6139 | struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6140 | list_del_init(&req->list); |
| 6141 | complete(&req->done); |
| 6142 | } |
| 6143 | spin_unlock_irq(&rq->lock); |
| 6144 | break; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6145 | |
Gregory Haskins | 08f503b | 2008-03-10 17:59:11 -0400 | [diff] [blame] | 6146 | case CPU_DYING: |
| 6147 | case CPU_DYING_FROZEN: |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6148 | /* Update our root-domain */ |
| 6149 | rq = cpu_rq(cpu); |
| 6150 | spin_lock_irqsave(&rq->lock, flags); |
| 6151 | if (rq->rd) { |
| 6152 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); |
| 6153 | cpu_clear(cpu, rq->rd->online); |
| 6154 | } |
| 6155 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6156 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6157 | #endif |
| 6158 | } |
| 6159 | return NOTIFY_OK; |
| 6160 | } |
| 6161 | |
| 6162 | /* Register at highest priority so that task migration (migrate_all_tasks) |
| 6163 | * happens before everything else. |
| 6164 | */ |
Chandra Seetharaman | 26c2143 | 2006-06-27 02:54:10 -0700 | [diff] [blame] | 6165 | static struct notifier_block __cpuinitdata migration_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | .notifier_call = migration_call, |
| 6167 | .priority = 10 |
| 6168 | }; |
| 6169 | |
Adrian Bunk | e6fe664 | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 6170 | void __init migration_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6171 | { |
| 6172 | void *cpu = (void *)(long)smp_processor_id(); |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 6173 | int err; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6174 | |
| 6175 | /* Start one for the boot CPU: */ |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 6176 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
| 6177 | BUG_ON(err == NOTIFY_BAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
| 6179 | register_cpu_notifier(&migration_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6180 | } |
| 6181 | #endif |
| 6182 | |
| 6183 | #ifdef CONFIG_SMP |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 6184 | |
Ingo Molnar | 3e9830d | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 6185 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6186 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6187 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, |
| 6188 | cpumask_t *groupmask) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6189 | { |
| 6190 | struct sched_group *group = sd->groups; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6191 | char str[256]; |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6192 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6193 | cpulist_scnprintf(str, sizeof(str), sd->span); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6194 | cpus_clear(*groupmask); |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6195 | |
| 6196 | printk(KERN_DEBUG "%*s domain %d: ", level, "", level); |
| 6197 | |
| 6198 | if (!(sd->flags & SD_LOAD_BALANCE)) { |
| 6199 | printk("does not load-balance\n"); |
| 6200 | if (sd->parent) |
| 6201 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" |
| 6202 | " has parent"); |
| 6203 | return -1; |
| 6204 | } |
| 6205 | |
| 6206 | printk(KERN_CONT "span %s\n", str); |
| 6207 | |
| 6208 | if (!cpu_isset(cpu, sd->span)) { |
| 6209 | printk(KERN_ERR "ERROR: domain->span does not contain " |
| 6210 | "CPU%d\n", cpu); |
| 6211 | } |
| 6212 | if (!cpu_isset(cpu, group->cpumask)) { |
| 6213 | printk(KERN_ERR "ERROR: domain->groups does not contain" |
| 6214 | " CPU%d\n", cpu); |
| 6215 | } |
| 6216 | |
| 6217 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); |
| 6218 | do { |
| 6219 | if (!group) { |
| 6220 | printk("\n"); |
| 6221 | printk(KERN_ERR "ERROR: group is NULL\n"); |
| 6222 | break; |
| 6223 | } |
| 6224 | |
| 6225 | if (!group->__cpu_power) { |
| 6226 | printk(KERN_CONT "\n"); |
| 6227 | printk(KERN_ERR "ERROR: domain->cpu_power not " |
| 6228 | "set\n"); |
| 6229 | break; |
| 6230 | } |
| 6231 | |
| 6232 | if (!cpus_weight(group->cpumask)) { |
| 6233 | printk(KERN_CONT "\n"); |
| 6234 | printk(KERN_ERR "ERROR: empty group\n"); |
| 6235 | break; |
| 6236 | } |
| 6237 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6238 | if (cpus_intersects(*groupmask, group->cpumask)) { |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6239 | printk(KERN_CONT "\n"); |
| 6240 | printk(KERN_ERR "ERROR: repeated CPUs\n"); |
| 6241 | break; |
| 6242 | } |
| 6243 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6244 | cpus_or(*groupmask, *groupmask, group->cpumask); |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6245 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6246 | cpulist_scnprintf(str, sizeof(str), group->cpumask); |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6247 | printk(KERN_CONT " %s", str); |
| 6248 | |
| 6249 | group = group->next; |
| 6250 | } while (group != sd->groups); |
| 6251 | printk(KERN_CONT "\n"); |
| 6252 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6253 | if (!cpus_equal(sd->span, *groupmask)) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6254 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
| 6255 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6256 | if (sd->parent && !cpus_subset(*groupmask, sd->parent->span)) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6257 | printk(KERN_ERR "ERROR: parent span is not a superset " |
| 6258 | "of domain->span\n"); |
| 6259 | return 0; |
| 6260 | } |
| 6261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6262 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
| 6263 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6264 | cpumask_t *groupmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6265 | int level = 0; |
| 6266 | |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 6267 | if (!sd) { |
| 6268 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); |
| 6269 | return; |
| 6270 | } |
| 6271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6272 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
| 6273 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6274 | groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL); |
| 6275 | if (!groupmask) { |
| 6276 | printk(KERN_DEBUG "Cannot load-balance (out of memory)\n"); |
| 6277 | return; |
| 6278 | } |
| 6279 | |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6280 | for (;;) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6281 | if (sched_domain_debug_one(sd, cpu, level, groupmask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | level++; |
| 6284 | sd = sd->parent; |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 6285 | if (!sd) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6286 | break; |
| 6287 | } |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6288 | kfree(groupmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6289 | } |
| 6290 | #else |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6291 | # define sched_domain_debug(sd, cpu) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6292 | #endif |
| 6293 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6294 | static int sd_degenerate(struct sched_domain *sd) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6295 | { |
| 6296 | if (cpus_weight(sd->span) == 1) |
| 6297 | return 1; |
| 6298 | |
| 6299 | /* Following flags need at least 2 groups */ |
| 6300 | if (sd->flags & (SD_LOAD_BALANCE | |
| 6301 | SD_BALANCE_NEWIDLE | |
| 6302 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6303 | SD_BALANCE_EXEC | |
| 6304 | SD_SHARE_CPUPOWER | |
| 6305 | SD_SHARE_PKG_RESOURCES)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6306 | if (sd->groups != sd->groups->next) |
| 6307 | return 0; |
| 6308 | } |
| 6309 | |
| 6310 | /* Following flags don't use groups */ |
| 6311 | if (sd->flags & (SD_WAKE_IDLE | |
| 6312 | SD_WAKE_AFFINE | |
| 6313 | SD_WAKE_BALANCE)) |
| 6314 | return 0; |
| 6315 | |
| 6316 | return 1; |
| 6317 | } |
| 6318 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6319 | static int |
| 6320 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6321 | { |
| 6322 | unsigned long cflags = sd->flags, pflags = parent->flags; |
| 6323 | |
| 6324 | if (sd_degenerate(parent)) |
| 6325 | return 1; |
| 6326 | |
| 6327 | if (!cpus_equal(sd->span, parent->span)) |
| 6328 | return 0; |
| 6329 | |
| 6330 | /* Does parent contain flags not in child? */ |
| 6331 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ |
| 6332 | if (cflags & SD_WAKE_AFFINE) |
| 6333 | pflags &= ~SD_WAKE_BALANCE; |
| 6334 | /* Flags needing groups don't count if only 1 group in parent */ |
| 6335 | if (parent->groups == parent->groups->next) { |
| 6336 | pflags &= ~(SD_LOAD_BALANCE | |
| 6337 | SD_BALANCE_NEWIDLE | |
| 6338 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6339 | SD_BALANCE_EXEC | |
| 6340 | SD_SHARE_CPUPOWER | |
| 6341 | SD_SHARE_PKG_RESOURCES); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6342 | } |
| 6343 | if (~cflags & pflags) |
| 6344 | return 0; |
| 6345 | |
| 6346 | return 1; |
| 6347 | } |
| 6348 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6349 | static void rq_attach_root(struct rq *rq, struct root_domain *rd) |
| 6350 | { |
| 6351 | unsigned long flags; |
| 6352 | const struct sched_class *class; |
| 6353 | |
| 6354 | spin_lock_irqsave(&rq->lock, flags); |
| 6355 | |
| 6356 | if (rq->rd) { |
| 6357 | struct root_domain *old_rd = rq->rd; |
| 6358 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6359 | for (class = sched_class_highest; class; class = class->next) { |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6360 | if (class->leave_domain) |
| 6361 | class->leave_domain(rq); |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6362 | } |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6363 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6364 | cpu_clear(rq->cpu, old_rd->span); |
| 6365 | cpu_clear(rq->cpu, old_rd->online); |
| 6366 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6367 | if (atomic_dec_and_test(&old_rd->refcount)) |
| 6368 | kfree(old_rd); |
| 6369 | } |
| 6370 | |
| 6371 | atomic_inc(&rd->refcount); |
| 6372 | rq->rd = rd; |
| 6373 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6374 | cpu_set(rq->cpu, rd->span); |
Gregory Haskins | 1f94ef5 | 2008-03-10 16:52:41 -0400 | [diff] [blame] | 6375 | if (cpu_isset(rq->cpu, cpu_online_map)) |
| 6376 | cpu_set(rq->cpu, rd->online); |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6377 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6378 | for (class = sched_class_highest; class; class = class->next) { |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6379 | if (class->join_domain) |
| 6380 | class->join_domain(rq); |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6381 | } |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6382 | |
| 6383 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6384 | } |
| 6385 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6386 | static void init_rootdomain(struct root_domain *rd) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6387 | { |
| 6388 | memset(rd, 0, sizeof(*rd)); |
| 6389 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6390 | cpus_clear(rd->span); |
| 6391 | cpus_clear(rd->online); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6392 | } |
| 6393 | |
| 6394 | static void init_defrootdomain(void) |
| 6395 | { |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6396 | init_rootdomain(&def_root_domain); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6397 | atomic_set(&def_root_domain.refcount, 1); |
| 6398 | } |
| 6399 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6400 | static struct root_domain *alloc_rootdomain(void) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6401 | { |
| 6402 | struct root_domain *rd; |
| 6403 | |
| 6404 | rd = kmalloc(sizeof(*rd), GFP_KERNEL); |
| 6405 | if (!rd) |
| 6406 | return NULL; |
| 6407 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6408 | init_rootdomain(rd); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6409 | |
| 6410 | return rd; |
| 6411 | } |
| 6412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6413 | /* |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6414 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6415 | * hold the hotplug lock. |
| 6416 | */ |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6417 | static void |
| 6418 | 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] | 6419 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6420 | struct rq *rq = cpu_rq(cpu); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6421 | struct sched_domain *tmp; |
| 6422 | |
| 6423 | /* Remove the sched domains which do not contribute to scheduling. */ |
| 6424 | for (tmp = sd; tmp; tmp = tmp->parent) { |
| 6425 | struct sched_domain *parent = tmp->parent; |
| 6426 | if (!parent) |
| 6427 | break; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6428 | if (sd_parent_degenerate(tmp, parent)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6429 | tmp->parent = parent->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6430 | if (parent->parent) |
| 6431 | parent->parent->child = tmp; |
| 6432 | } |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6433 | } |
| 6434 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6435 | if (sd && sd_degenerate(sd)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6436 | sd = sd->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6437 | if (sd) |
| 6438 | sd->child = NULL; |
| 6439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6440 | |
| 6441 | sched_domain_debug(sd, cpu); |
| 6442 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6443 | rq_attach_root(rq, rd); |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 6444 | rcu_assign_pointer(rq->sd, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6445 | } |
| 6446 | |
| 6447 | /* cpus with isolated domains */ |
Tim Chen | 67af63a | 2006-12-22 01:07:50 -0800 | [diff] [blame] | 6448 | static cpumask_t cpu_isolated_map = CPU_MASK_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6449 | |
| 6450 | /* Setup the mask of cpus configured for isolated domains */ |
| 6451 | static int __init isolated_cpu_setup(char *str) |
| 6452 | { |
| 6453 | int ints[NR_CPUS], i; |
| 6454 | |
| 6455 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 6456 | cpus_clear(cpu_isolated_map); |
| 6457 | for (i = 1; i <= ints[0]; i++) |
| 6458 | if (ints[i] < NR_CPUS) |
| 6459 | cpu_set(ints[i], cpu_isolated_map); |
| 6460 | return 1; |
| 6461 | } |
| 6462 | |
Ingo Molnar | 8927f49 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 6463 | __setup("isolcpus=", isolated_cpu_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6464 | |
| 6465 | /* |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6466 | * init_sched_build_groups takes the cpumask we wish to span, and a pointer |
| 6467 | * to a function which identifies what group(along with sched group) a CPU |
| 6468 | * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS |
| 6469 | * (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] | 6470 | * |
| 6471 | * init_sched_build_groups will build a circular linked list of the groups |
| 6472 | * covered by the given span, and will set each group's ->cpumask correctly, |
| 6473 | * and ->cpu_power to 0. |
| 6474 | */ |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6475 | static void |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6476 | 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] | 6477 | int (*group_fn)(int cpu, const cpumask_t *cpu_map, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6478 | struct sched_group **sg, |
| 6479 | cpumask_t *tmpmask), |
| 6480 | cpumask_t *covered, cpumask_t *tmpmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6481 | { |
| 6482 | struct sched_group *first = NULL, *last = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6483 | int i; |
| 6484 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6485 | cpus_clear(*covered); |
| 6486 | |
| 6487 | for_each_cpu_mask(i, *span) { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6488 | struct sched_group *sg; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6489 | int group = group_fn(i, cpu_map, &sg, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6490 | int j; |
| 6491 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6492 | if (cpu_isset(i, *covered)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6493 | continue; |
| 6494 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6495 | cpus_clear(sg->cpumask); |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6496 | sg->__cpu_power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6497 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6498 | for_each_cpu_mask(j, *span) { |
| 6499 | if (group_fn(j, cpu_map, NULL, tmpmask) != group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | continue; |
| 6501 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6502 | cpu_set(j, *covered); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | cpu_set(j, sg->cpumask); |
| 6504 | } |
| 6505 | if (!first) |
| 6506 | first = sg; |
| 6507 | if (last) |
| 6508 | last->next = sg; |
| 6509 | last = sg; |
| 6510 | } |
| 6511 | last->next = first; |
| 6512 | } |
| 6513 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6514 | #define SD_NODES_PER_DOMAIN 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6515 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6516 | #ifdef CONFIG_NUMA |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 6517 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6518 | /** |
| 6519 | * find_next_best_node - find the next node to include in a sched_domain |
| 6520 | * @node: node whose sched_domain we're building |
| 6521 | * @used_nodes: nodes already in the sched_domain |
| 6522 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6523 | * Find the next node to include in a given scheduling domain. Simply |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6524 | * finds the closest node not already in the @used_nodes map. |
| 6525 | * |
| 6526 | * Should use nodemask_t. |
| 6527 | */ |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6528 | static int find_next_best_node(int node, nodemask_t *used_nodes) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6529 | { |
| 6530 | int i, n, val, min_val, best_node = 0; |
| 6531 | |
| 6532 | min_val = INT_MAX; |
| 6533 | |
| 6534 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6535 | /* Start at @node */ |
| 6536 | n = (node + i) % MAX_NUMNODES; |
| 6537 | |
| 6538 | if (!nr_cpus_node(n)) |
| 6539 | continue; |
| 6540 | |
| 6541 | /* Skip already used nodes */ |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6542 | if (node_isset(n, *used_nodes)) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6543 | continue; |
| 6544 | |
| 6545 | /* Simple min distance search */ |
| 6546 | val = node_distance(node, n); |
| 6547 | |
| 6548 | if (val < min_val) { |
| 6549 | min_val = val; |
| 6550 | best_node = n; |
| 6551 | } |
| 6552 | } |
| 6553 | |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6554 | node_set(best_node, *used_nodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6555 | return best_node; |
| 6556 | } |
| 6557 | |
| 6558 | /** |
| 6559 | * sched_domain_node_span - get a cpumask for a node's sched_domain |
| 6560 | * @node: node whose cpumask we're constructing |
Randy Dunlap | 7348672 | 2008-04-22 10:07:22 -0700 | [diff] [blame] | 6561 | * @span: resulting cpumask |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6562 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6563 | * 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] | 6564 | * should be one that prevents unnecessary balancing, but also spreads tasks |
| 6565 | * out optimally. |
| 6566 | */ |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 6567 | static void sched_domain_node_span(int node, cpumask_t *span) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6568 | { |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6569 | nodemask_t used_nodes; |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6570 | node_to_cpumask_ptr(nodemask, node); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6571 | int i; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6572 | |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 6573 | cpus_clear(*span); |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6574 | nodes_clear(used_nodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6575 | |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 6576 | cpus_or(*span, *span, *nodemask); |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6577 | node_set(node, used_nodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6578 | |
| 6579 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6580 | int next_node = find_next_best_node(node, &used_nodes); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6581 | |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 6582 | node_to_cpumask_ptr_next(nodemask, next_node); |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 6583 | cpus_or(*span, *span, *nodemask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6584 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6585 | } |
| 6586 | #endif |
| 6587 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6588 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6589 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6590 | /* |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6591 | * SMT sched-domains: |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6592 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6593 | #ifdef CONFIG_SCHED_SMT |
| 6594 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6595 | static DEFINE_PER_CPU(struct sched_group, sched_group_cpus); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6596 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6597 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6598 | cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 6599 | cpumask_t *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6600 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6601 | if (sg) |
| 6602 | *sg = &per_cpu(sched_group_cpus, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6603 | return cpu; |
| 6604 | } |
| 6605 | #endif |
| 6606 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6607 | /* |
| 6608 | * multi-core sched-domains: |
| 6609 | */ |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6610 | #ifdef CONFIG_SCHED_MC |
| 6611 | static DEFINE_PER_CPU(struct sched_domain, core_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6612 | static DEFINE_PER_CPU(struct sched_group, sched_group_core); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6613 | #endif |
| 6614 | |
| 6615 | #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6616 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6617 | cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 6618 | cpumask_t *mask) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6619 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6620 | int group; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6621 | |
| 6622 | *mask = per_cpu(cpu_sibling_map, cpu); |
| 6623 | cpus_and(*mask, *mask, *cpu_map); |
| 6624 | group = first_cpu(*mask); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6625 | if (sg) |
| 6626 | *sg = &per_cpu(sched_group_core, group); |
| 6627 | return group; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6628 | } |
| 6629 | #elif defined(CONFIG_SCHED_MC) |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6630 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6631 | cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 6632 | cpumask_t *unused) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6633 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6634 | if (sg) |
| 6635 | *sg = &per_cpu(sched_group_core, cpu); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6636 | return cpu; |
| 6637 | } |
| 6638 | #endif |
| 6639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6640 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6641 | static DEFINE_PER_CPU(struct sched_group, sched_group_phys); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6642 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6643 | static int |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6644 | cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
| 6645 | cpumask_t *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6646 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6647 | int group; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6648 | #ifdef CONFIG_SCHED_MC |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6649 | *mask = cpu_coregroup_map(cpu); |
| 6650 | cpus_and(*mask, *mask, *cpu_map); |
| 6651 | group = first_cpu(*mask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6652 | #elif defined(CONFIG_SCHED_SMT) |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6653 | *mask = per_cpu(cpu_sibling_map, cpu); |
| 6654 | cpus_and(*mask, *mask, *cpu_map); |
| 6655 | group = first_cpu(*mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | #else |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6657 | group = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6658 | #endif |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6659 | if (sg) |
| 6660 | *sg = &per_cpu(sched_group_phys, group); |
| 6661 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | } |
| 6663 | |
| 6664 | #ifdef CONFIG_NUMA |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6665 | /* |
| 6666 | * The init_sched_build_groups can't handle what we want to do with node |
| 6667 | * groups, so roll our own. Now each node has its own list of groups which |
| 6668 | * gets dynamically allocated. |
| 6669 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6670 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 6671 | static struct sched_group ***sched_group_nodes_bycpu; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6672 | |
| 6673 | static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6674 | static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6675 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6676 | 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] | 6677 | struct sched_group **sg, cpumask_t *nodemask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6678 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6679 | int group; |
| 6680 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6681 | *nodemask = node_to_cpumask(cpu_to_node(cpu)); |
| 6682 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 6683 | group = first_cpu(*nodemask); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6684 | |
| 6685 | if (sg) |
| 6686 | *sg = &per_cpu(sched_group_allnodes, group); |
| 6687 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6688 | } |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6689 | |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6690 | static void init_numa_sched_groups_power(struct sched_group *group_head) |
| 6691 | { |
| 6692 | struct sched_group *sg = group_head; |
| 6693 | int j; |
| 6694 | |
| 6695 | if (!sg) |
| 6696 | return; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6697 | do { |
| 6698 | for_each_cpu_mask(j, sg->cpumask) { |
| 6699 | struct sched_domain *sd; |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6700 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6701 | sd = &per_cpu(phys_domains, j); |
| 6702 | if (j != first_cpu(sd->groups->cpumask)) { |
| 6703 | /* |
| 6704 | * Only add "power" once for each |
| 6705 | * physical package. |
| 6706 | */ |
| 6707 | continue; |
| 6708 | } |
| 6709 | |
| 6710 | sg_inc_cpu_power(sg, sd->groups->__cpu_power); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6711 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6712 | sg = sg->next; |
| 6713 | } while (sg != group_head); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6715 | #endif |
| 6716 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6717 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6718 | /* Free memory allocated for various sched_group structures */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6719 | 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] | 6720 | { |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6721 | int cpu, i; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6722 | |
| 6723 | for_each_cpu_mask(cpu, *cpu_map) { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6724 | struct sched_group **sched_group_nodes |
| 6725 | = sched_group_nodes_bycpu[cpu]; |
| 6726 | |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6727 | if (!sched_group_nodes) |
| 6728 | continue; |
| 6729 | |
| 6730 | for (i = 0; i < MAX_NUMNODES; i++) { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6731 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; |
| 6732 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6733 | *nodemask = node_to_cpumask(i); |
| 6734 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 6735 | if (cpus_empty(*nodemask)) |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6736 | continue; |
| 6737 | |
| 6738 | if (sg == NULL) |
| 6739 | continue; |
| 6740 | sg = sg->next; |
| 6741 | next_sg: |
| 6742 | oldsg = sg; |
| 6743 | sg = sg->next; |
| 6744 | kfree(oldsg); |
| 6745 | if (oldsg != sched_group_nodes[i]) |
| 6746 | goto next_sg; |
| 6747 | } |
| 6748 | kfree(sched_group_nodes); |
| 6749 | sched_group_nodes_bycpu[cpu] = NULL; |
| 6750 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6751 | } |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6752 | #else |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6753 | 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] | 6754 | { |
| 6755 | } |
| 6756 | #endif |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6758 | /* |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6759 | * Initialize sched groups cpu_power. |
| 6760 | * |
| 6761 | * cpu_power indicates the capacity of sched group, which is used while |
| 6762 | * distributing the load between different sched groups in a sched domain. |
| 6763 | * Typically cpu_power for all the groups in a sched domain will be same unless |
| 6764 | * there are asymmetries in the topology. If there are asymmetries, group |
| 6765 | * having more cpu_power will pickup more load compared to the group having |
| 6766 | * less cpu_power. |
| 6767 | * |
| 6768 | * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents |
| 6769 | * the maximum number of tasks a group can handle in the presence of other idle |
| 6770 | * or lightly loaded groups in the same sched domain. |
| 6771 | */ |
| 6772 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) |
| 6773 | { |
| 6774 | struct sched_domain *child; |
| 6775 | struct sched_group *group; |
| 6776 | |
| 6777 | WARN_ON(!sd || !sd->groups); |
| 6778 | |
| 6779 | if (cpu != first_cpu(sd->groups->cpumask)) |
| 6780 | return; |
| 6781 | |
| 6782 | child = sd->child; |
| 6783 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6784 | sd->groups->__cpu_power = 0; |
| 6785 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6786 | /* |
| 6787 | * For perf policy, if the groups in child domain share resources |
| 6788 | * (for example cores sharing some portions of the cache hierarchy |
| 6789 | * or SMT), then set this domain groups cpu_power such that each group |
| 6790 | * can handle only one task, when there are other idle groups in the |
| 6791 | * same sched domain. |
| 6792 | */ |
| 6793 | if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) && |
| 6794 | (child->flags & |
| 6795 | (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6796 | sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6797 | return; |
| 6798 | } |
| 6799 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6800 | /* |
| 6801 | * add cpu_power of each child group to this groups cpu_power |
| 6802 | */ |
| 6803 | group = child->groups; |
| 6804 | do { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6805 | sg_inc_cpu_power(sd->groups, group->__cpu_power); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6806 | group = group->next; |
| 6807 | } while (group != child->groups); |
| 6808 | } |
| 6809 | |
| 6810 | /* |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6811 | * Initializers for schedule domains |
| 6812 | * Non-inlined to reduce accumulated stack pressure in build_sched_domains() |
| 6813 | */ |
| 6814 | |
| 6815 | #define SD_INIT(sd, type) sd_init_##type(sd) |
| 6816 | #define SD_INIT_FUNC(type) \ |
| 6817 | static noinline void sd_init_##type(struct sched_domain *sd) \ |
| 6818 | { \ |
| 6819 | memset(sd, 0, sizeof(*sd)); \ |
| 6820 | *sd = SD_##type##_INIT; \ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 6821 | sd->level = SD_LV_##type; \ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6822 | } |
| 6823 | |
| 6824 | SD_INIT_FUNC(CPU) |
| 6825 | #ifdef CONFIG_NUMA |
| 6826 | SD_INIT_FUNC(ALLNODES) |
| 6827 | SD_INIT_FUNC(NODE) |
| 6828 | #endif |
| 6829 | #ifdef CONFIG_SCHED_SMT |
| 6830 | SD_INIT_FUNC(SIBLING) |
| 6831 | #endif |
| 6832 | #ifdef CONFIG_SCHED_MC |
| 6833 | SD_INIT_FUNC(MC) |
| 6834 | #endif |
| 6835 | |
| 6836 | /* |
| 6837 | * To minimize stack usage kmalloc room for cpumasks and share the |
| 6838 | * space as the usage in build_sched_domains() dictates. Used only |
| 6839 | * if the amount of space is significant. |
| 6840 | */ |
| 6841 | struct allmasks { |
| 6842 | cpumask_t tmpmask; /* make this one first */ |
| 6843 | union { |
| 6844 | cpumask_t nodemask; |
| 6845 | cpumask_t this_sibling_map; |
| 6846 | cpumask_t this_core_map; |
| 6847 | }; |
| 6848 | cpumask_t send_covered; |
| 6849 | |
| 6850 | #ifdef CONFIG_NUMA |
| 6851 | cpumask_t domainspan; |
| 6852 | cpumask_t covered; |
| 6853 | cpumask_t notcovered; |
| 6854 | #endif |
| 6855 | }; |
| 6856 | |
| 6857 | #if NR_CPUS > 128 |
| 6858 | #define SCHED_CPUMASK_ALLOC 1 |
| 6859 | #define SCHED_CPUMASK_FREE(v) kfree(v) |
| 6860 | #define SCHED_CPUMASK_DECLARE(v) struct allmasks *v |
| 6861 | #else |
| 6862 | #define SCHED_CPUMASK_ALLOC 0 |
| 6863 | #define SCHED_CPUMASK_FREE(v) |
| 6864 | #define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v |
| 6865 | #endif |
| 6866 | |
| 6867 | #define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \ |
| 6868 | ((unsigned long)(a) + offsetof(struct allmasks, v)) |
| 6869 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 6870 | static int default_relax_domain_level = -1; |
| 6871 | |
| 6872 | static int __init setup_relax_domain_level(char *str) |
| 6873 | { |
| 6874 | default_relax_domain_level = simple_strtoul(str, NULL, 0); |
| 6875 | return 1; |
| 6876 | } |
| 6877 | __setup("relax_domain_level=", setup_relax_domain_level); |
| 6878 | |
| 6879 | static void set_domain_attribute(struct sched_domain *sd, |
| 6880 | struct sched_domain_attr *attr) |
| 6881 | { |
| 6882 | int request; |
| 6883 | |
| 6884 | if (!attr || attr->relax_domain_level < 0) { |
| 6885 | if (default_relax_domain_level < 0) |
| 6886 | return; |
| 6887 | else |
| 6888 | request = default_relax_domain_level; |
| 6889 | } else |
| 6890 | request = attr->relax_domain_level; |
| 6891 | if (request < sd->level) { |
| 6892 | /* turn off idle balance on this domain */ |
| 6893 | sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE); |
| 6894 | } else { |
| 6895 | /* turn on idle balance on this domain */ |
| 6896 | sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE); |
| 6897 | } |
| 6898 | } |
| 6899 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6900 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6901 | * Build sched domains for a given set of cpus and attach the sched domains |
| 6902 | * to the individual cpus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 6904 | static int __build_sched_domains(const cpumask_t *cpu_map, |
| 6905 | struct sched_domain_attr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | { |
| 6907 | int i; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6908 | struct root_domain *rd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6909 | SCHED_CPUMASK_DECLARE(allmasks); |
| 6910 | cpumask_t *tmpmask; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6911 | #ifdef CONFIG_NUMA |
| 6912 | struct sched_group **sched_group_nodes = NULL; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6913 | int sd_allnodes = 0; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6914 | |
| 6915 | /* |
| 6916 | * Allocate the per-node list of sched groups |
| 6917 | */ |
Milton Miller | 5cf9f06 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6918 | sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *), |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6919 | GFP_KERNEL); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6920 | if (!sched_group_nodes) { |
| 6921 | printk(KERN_WARNING "Can not alloc sched group node list\n"); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6922 | return -ENOMEM; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6923 | } |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6924 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6925 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6926 | rd = alloc_rootdomain(); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6927 | if (!rd) { |
| 6928 | printk(KERN_WARNING "Cannot alloc root domain\n"); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6929 | #ifdef CONFIG_NUMA |
| 6930 | kfree(sched_group_nodes); |
| 6931 | #endif |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6932 | return -ENOMEM; |
| 6933 | } |
| 6934 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6935 | #if SCHED_CPUMASK_ALLOC |
| 6936 | /* get space for all scratch cpumask variables */ |
| 6937 | allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL); |
| 6938 | if (!allmasks) { |
| 6939 | printk(KERN_WARNING "Cannot alloc cpumask array\n"); |
| 6940 | kfree(rd); |
| 6941 | #ifdef CONFIG_NUMA |
| 6942 | kfree(sched_group_nodes); |
| 6943 | #endif |
| 6944 | return -ENOMEM; |
| 6945 | } |
| 6946 | #endif |
| 6947 | tmpmask = (cpumask_t *)allmasks; |
| 6948 | |
| 6949 | |
| 6950 | #ifdef CONFIG_NUMA |
| 6951 | sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; |
| 6952 | #endif |
| 6953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6954 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6955 | * Set up domains for cpus specified by the cpu_map. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6956 | */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6957 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6958 | struct sched_domain *sd = NULL, *p; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6959 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6961 | *nodemask = node_to_cpumask(cpu_to_node(i)); |
| 6962 | cpus_and(*nodemask, *nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | |
| 6964 | #ifdef CONFIG_NUMA |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6965 | if (cpus_weight(*cpu_map) > |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6966 | SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6967 | sd = &per_cpu(allnodes_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6968 | SD_INIT(sd, ALLNODES); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 6969 | set_domain_attribute(sd, attr); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6970 | sd->span = *cpu_map; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6971 | cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6972 | p = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6973 | sd_allnodes = 1; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6974 | } else |
| 6975 | p = NULL; |
| 6976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6977 | sd = &per_cpu(node_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6978 | SD_INIT(sd, NODE); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 6979 | set_domain_attribute(sd, attr); |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 6980 | sched_domain_node_span(cpu_to_node(i), &sd->span); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6981 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6982 | if (p) |
| 6983 | p->child = sd; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6984 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6985 | #endif |
| 6986 | |
| 6987 | p = sd; |
| 6988 | sd = &per_cpu(phys_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6989 | SD_INIT(sd, CPU); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 6990 | set_domain_attribute(sd, attr); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6991 | sd->span = *nodemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6992 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6993 | if (p) |
| 6994 | p->child = sd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 6995 | cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6997 | #ifdef CONFIG_SCHED_MC |
| 6998 | p = sd; |
| 6999 | sd = &per_cpu(core_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7000 | SD_INIT(sd, MC); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7001 | set_domain_attribute(sd, attr); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7002 | sd->span = cpu_coregroup_map(i); |
| 7003 | cpus_and(sd->span, sd->span, *cpu_map); |
| 7004 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 7005 | p->child = sd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7006 | cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7007 | #endif |
| 7008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7009 | #ifdef CONFIG_SCHED_SMT |
| 7010 | p = sd; |
| 7011 | sd = &per_cpu(cpu_domains, i); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7012 | SD_INIT(sd, SIBLING); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7013 | set_domain_attribute(sd, attr); |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 7014 | sd->span = per_cpu(cpu_sibling_map, i); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7015 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7016 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 7017 | p->child = sd; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7018 | cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7019 | #endif |
| 7020 | } |
| 7021 | |
| 7022 | #ifdef CONFIG_SCHED_SMT |
| 7023 | /* Set up CPU (sibling) groups */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7024 | for_each_cpu_mask(i, *cpu_map) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7025 | SCHED_CPUMASK_VAR(this_sibling_map, allmasks); |
| 7026 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
| 7027 | |
| 7028 | *this_sibling_map = per_cpu(cpu_sibling_map, i); |
| 7029 | cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map); |
| 7030 | if (i != first_cpu(*this_sibling_map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7031 | continue; |
| 7032 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7033 | init_sched_build_groups(this_sibling_map, cpu_map, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7034 | &cpu_to_cpu_group, |
| 7035 | send_covered, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7036 | } |
| 7037 | #endif |
| 7038 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7039 | #ifdef CONFIG_SCHED_MC |
| 7040 | /* Set up multi-core groups */ |
| 7041 | for_each_cpu_mask(i, *cpu_map) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7042 | SCHED_CPUMASK_VAR(this_core_map, allmasks); |
| 7043 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
| 7044 | |
| 7045 | *this_core_map = cpu_coregroup_map(i); |
| 7046 | cpus_and(*this_core_map, *this_core_map, *cpu_map); |
| 7047 | if (i != first_cpu(*this_core_map)) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7048 | continue; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7049 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7050 | init_sched_build_groups(this_core_map, cpu_map, |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7051 | &cpu_to_core_group, |
| 7052 | send_covered, tmpmask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7053 | } |
| 7054 | #endif |
| 7055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7056 | /* Set up physical groups */ |
| 7057 | for (i = 0; i < MAX_NUMNODES; i++) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7058 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
| 7059 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7061 | *nodemask = node_to_cpumask(i); |
| 7062 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 7063 | if (cpus_empty(*nodemask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7064 | continue; |
| 7065 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7066 | init_sched_build_groups(nodemask, cpu_map, |
| 7067 | &cpu_to_phys_group, |
| 7068 | send_covered, tmpmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7069 | } |
| 7070 | |
| 7071 | #ifdef CONFIG_NUMA |
| 7072 | /* Set up node groups */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7073 | if (sd_allnodes) { |
| 7074 | SCHED_CPUMASK_VAR(send_covered, allmasks); |
| 7075 | |
| 7076 | init_sched_build_groups(cpu_map, cpu_map, |
| 7077 | &cpu_to_allnodes_group, |
| 7078 | send_covered, tmpmask); |
| 7079 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7080 | |
| 7081 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 7082 | /* Set up node groups */ |
| 7083 | struct sched_group *sg, *prev; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7084 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
| 7085 | SCHED_CPUMASK_VAR(domainspan, allmasks); |
| 7086 | SCHED_CPUMASK_VAR(covered, allmasks); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7087 | int j; |
| 7088 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7089 | *nodemask = node_to_cpumask(i); |
| 7090 | cpus_clear(*covered); |
| 7091 | |
| 7092 | cpus_and(*nodemask, *nodemask, *cpu_map); |
| 7093 | if (cpus_empty(*nodemask)) { |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7094 | sched_group_nodes[i] = NULL; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7095 | continue; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7096 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7097 | |
Mike Travis | 4bdbaad3 | 2008-04-15 16:35:52 -0700 | [diff] [blame] | 7098 | sched_domain_node_span(i, domainspan); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7099 | cpus_and(*domainspan, *domainspan, *cpu_map); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7100 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 7101 | sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7102 | if (!sg) { |
| 7103 | printk(KERN_WARNING "Can not alloc domain group for " |
| 7104 | "node %d\n", i); |
| 7105 | goto error; |
| 7106 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7107 | sched_group_nodes[i] = sg; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7108 | for_each_cpu_mask(j, *nodemask) { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7109 | struct sched_domain *sd; |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 7110 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7111 | sd = &per_cpu(node_domains, j); |
| 7112 | sd->groups = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7113 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7114 | sg->__cpu_power = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7115 | sg->cpumask = *nodemask; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7116 | sg->next = sg; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7117 | cpus_or(*covered, *covered, *nodemask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7118 | prev = sg; |
| 7119 | |
| 7120 | for (j = 0; j < MAX_NUMNODES; j++) { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7121 | SCHED_CPUMASK_VAR(notcovered, allmasks); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7122 | int n = (i + j) % MAX_NUMNODES; |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 7123 | node_to_cpumask_ptr(pnodemask, n); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7124 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7125 | cpus_complement(*notcovered, *covered); |
| 7126 | cpus_and(*tmpmask, *notcovered, *cpu_map); |
| 7127 | cpus_and(*tmpmask, *tmpmask, *domainspan); |
| 7128 | if (cpus_empty(*tmpmask)) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7129 | break; |
| 7130 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7131 | cpus_and(*tmpmask, *tmpmask, *pnodemask); |
| 7132 | if (cpus_empty(*tmpmask)) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7133 | continue; |
| 7134 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 7135 | sg = kmalloc_node(sizeof(struct sched_group), |
| 7136 | GFP_KERNEL, i); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7137 | if (!sg) { |
| 7138 | printk(KERN_WARNING |
| 7139 | "Can not alloc domain group for node %d\n", j); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7140 | goto error; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7141 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 7142 | sg->__cpu_power = 0; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7143 | sg->cpumask = *tmpmask; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7144 | sg->next = prev->next; |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7145 | cpus_or(*covered, *covered, *tmpmask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7146 | prev->next = sg; |
| 7147 | prev = sg; |
| 7148 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | #endif |
| 7151 | |
| 7152 | /* Calculate CPU power for physical packages and nodes */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7153 | #ifdef CONFIG_SCHED_SMT |
| 7154 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7155 | struct sched_domain *sd = &per_cpu(cpu_domains, i); |
| 7156 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7157 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7158 | } |
| 7159 | #endif |
| 7160 | #ifdef CONFIG_SCHED_MC |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7161 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7162 | struct sched_domain *sd = &per_cpu(core_domains, i); |
| 7163 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7164 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7165 | } |
| 7166 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7167 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7168 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7169 | struct sched_domain *sd = &per_cpu(phys_domains, i); |
| 7170 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 7171 | init_sched_groups_power(i, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7172 | } |
| 7173 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7174 | #ifdef CONFIG_NUMA |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 7175 | for (i = 0; i < MAX_NUMNODES; i++) |
| 7176 | init_numa_sched_groups_power(sched_group_nodes[i]); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7177 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 7178 | if (sd_allnodes) { |
| 7179 | struct sched_group *sg; |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 7180 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7181 | cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg, |
| 7182 | tmpmask); |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 7183 | init_numa_sched_groups_power(sg); |
| 7184 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7185 | #endif |
| 7186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7187 | /* Attach the domains */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7188 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7189 | struct sched_domain *sd; |
| 7190 | #ifdef CONFIG_SCHED_SMT |
| 7191 | sd = &per_cpu(cpu_domains, i); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 7192 | #elif defined(CONFIG_SCHED_MC) |
| 7193 | sd = &per_cpu(core_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | #else |
| 7195 | sd = &per_cpu(phys_domains, i); |
| 7196 | #endif |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7197 | cpu_attach_domain(sd, rd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7198 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7199 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7200 | SCHED_CPUMASK_FREE((void *)allmasks); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7201 | return 0; |
| 7202 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7203 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7204 | error: |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7205 | free_sched_groups(cpu_map, tmpmask); |
| 7206 | SCHED_CPUMASK_FREE((void *)allmasks); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7207 | return -ENOMEM; |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 7208 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7209 | } |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7210 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7211 | static int build_sched_domains(const cpumask_t *cpu_map) |
| 7212 | { |
| 7213 | return __build_sched_domains(cpu_map, NULL); |
| 7214 | } |
| 7215 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7216 | static cpumask_t *doms_cur; /* current sched domains */ |
| 7217 | static int ndoms_cur; /* number of sched domains in 'doms_cur' */ |
Ingo Molnar | 4285f594 | 2008-05-16 17:47:14 +0200 | [diff] [blame] | 7218 | static struct sched_domain_attr *dattr_cur; |
| 7219 | /* attribues of custom domains in 'doms_cur' */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7220 | |
| 7221 | /* |
| 7222 | * Special case: If a kmalloc of a doms_cur partition (array of |
| 7223 | * cpumask_t) fails, then fallback to a single sched domain, |
| 7224 | * as determined by the single cpumask_t fallback_doms. |
| 7225 | */ |
| 7226 | static cpumask_t fallback_doms; |
| 7227 | |
Heiko Carstens | 22e52b0 | 2008-03-12 18:31:59 +0100 | [diff] [blame] | 7228 | void __attribute__((weak)) arch_update_cpu_topology(void) |
| 7229 | { |
| 7230 | } |
| 7231 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7232 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7233 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7234 | * For now this just excludes isolated cpus, but could be used to |
| 7235 | * exclude other special cases in the future. |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7236 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 7237 | static int arch_init_sched_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7238 | { |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7239 | int err; |
| 7240 | |
Heiko Carstens | 22e52b0 | 2008-03-12 18:31:59 +0100 | [diff] [blame] | 7241 | arch_update_cpu_topology(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7242 | ndoms_cur = 1; |
| 7243 | doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL); |
| 7244 | if (!doms_cur) |
| 7245 | doms_cur = &fallback_doms; |
| 7246 | cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7247 | dattr_cur = NULL; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7248 | err = build_sched_domains(doms_cur); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 7249 | register_sched_domain_sysctl(); |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7250 | |
| 7251 | return err; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7252 | } |
| 7253 | |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7254 | static void arch_destroy_sched_domains(const cpumask_t *cpu_map, |
| 7255 | cpumask_t *tmpmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7256 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7257 | free_sched_groups(cpu_map, tmpmask); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 7258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7259 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7260 | /* |
| 7261 | * Detach sched domains from a group of cpus specified in cpu_map |
| 7262 | * These cpus will now be attached to the NULL domain |
| 7263 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 7264 | static void detach_destroy_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7265 | { |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7266 | cpumask_t tmpmask; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7267 | int i; |
| 7268 | |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 7269 | unregister_sched_domain_sysctl(); |
| 7270 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7271 | for_each_cpu_mask(i, *cpu_map) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7272 | cpu_attach_domain(NULL, &def_root_domain, i); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7273 | synchronize_sched(); |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7274 | arch_destroy_sched_domains(cpu_map, &tmpmask); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7275 | } |
| 7276 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7277 | /* handle null as "default" */ |
| 7278 | static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, |
| 7279 | struct sched_domain_attr *new, int idx_new) |
| 7280 | { |
| 7281 | struct sched_domain_attr tmp; |
| 7282 | |
| 7283 | /* fast path */ |
| 7284 | if (!new && !cur) |
| 7285 | return 1; |
| 7286 | |
| 7287 | tmp = SD_ATTR_INIT; |
| 7288 | return !memcmp(cur ? (cur + idx_cur) : &tmp, |
| 7289 | new ? (new + idx_new) : &tmp, |
| 7290 | sizeof(struct sched_domain_attr)); |
| 7291 | } |
| 7292 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7293 | /* |
| 7294 | * Partition sched domains as specified by the 'ndoms_new' |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7295 | * cpumasks in the array doms_new[] of cpumasks. This compares |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7296 | * doms_new[] to the current sched domain partitioning, doms_cur[]. |
| 7297 | * It destroys each deleted domain and builds each new domain. |
| 7298 | * |
| 7299 | * '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] | 7300 | * The masks don't intersect (don't overlap.) We should setup one |
| 7301 | * sched domain for each mask. CPUs not in any of the cpumasks will |
| 7302 | * not be load balanced. If the same cpumask appears both in the |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7303 | * current 'doms_cur' domains and in the new 'doms_new', we can leave |
| 7304 | * it as it is. |
| 7305 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7306 | * The passed in 'doms_new' should be kmalloc'd. This routine takes |
| 7307 | * 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] | 7308 | * failed the kmalloc call, then it can pass in doms_new == NULL, |
| 7309 | * and partition_sched_domains() will fallback to the single partition |
| 7310 | * 'fallback_doms'. |
| 7311 | * |
| 7312 | * Call with hotplug lock held |
| 7313 | */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7314 | void partition_sched_domains(int ndoms_new, cpumask_t *doms_new, |
| 7315 | struct sched_domain_attr *dattr_new) |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7316 | { |
| 7317 | int i, j; |
| 7318 | |
Heiko Carstens | 712555e | 2008-04-28 11:33:07 +0200 | [diff] [blame] | 7319 | mutex_lock(&sched_domains_mutex); |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7320 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7321 | /* always unregister in case we don't destroy any domains */ |
| 7322 | unregister_sched_domain_sysctl(); |
| 7323 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7324 | if (doms_new == NULL) { |
| 7325 | ndoms_new = 1; |
| 7326 | doms_new = &fallback_doms; |
| 7327 | cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7328 | dattr_new = NULL; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7329 | } |
| 7330 | |
| 7331 | /* Destroy deleted domains */ |
| 7332 | for (i = 0; i < ndoms_cur; i++) { |
| 7333 | for (j = 0; j < ndoms_new; j++) { |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7334 | if (cpus_equal(doms_cur[i], doms_new[j]) |
| 7335 | && dattrs_equal(dattr_cur, i, dattr_new, j)) |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7336 | goto match1; |
| 7337 | } |
| 7338 | /* no match - a current sched domain not in new doms_new[] */ |
| 7339 | detach_destroy_domains(doms_cur + i); |
| 7340 | match1: |
| 7341 | ; |
| 7342 | } |
| 7343 | |
| 7344 | /* Build new domains */ |
| 7345 | for (i = 0; i < ndoms_new; i++) { |
| 7346 | for (j = 0; j < ndoms_cur; j++) { |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7347 | if (cpus_equal(doms_new[i], doms_cur[j]) |
| 7348 | && dattrs_equal(dattr_new, i, dattr_cur, j)) |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7349 | goto match2; |
| 7350 | } |
| 7351 | /* no match - add a new doms_new */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7352 | __build_sched_domains(doms_new + i, |
| 7353 | dattr_new ? dattr_new + i : NULL); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7354 | match2: |
| 7355 | ; |
| 7356 | } |
| 7357 | |
| 7358 | /* Remember the new sched domains */ |
| 7359 | if (doms_cur != &fallback_doms) |
| 7360 | kfree(doms_cur); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7361 | kfree(dattr_cur); /* kfree(NULL) is safe */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7362 | doms_cur = doms_new; |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 7363 | dattr_cur = dattr_new; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7364 | ndoms_cur = ndoms_new; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7365 | |
| 7366 | register_sched_domain_sysctl(); |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7367 | |
Heiko Carstens | 712555e | 2008-04-28 11:33:07 +0200 | [diff] [blame] | 7368 | mutex_unlock(&sched_domains_mutex); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7369 | } |
| 7370 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7371 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Heiko Carstens | 9aefd0a | 2008-03-12 18:31:58 +0100 | [diff] [blame] | 7372 | int arch_reinit_sched_domains(void) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7373 | { |
| 7374 | int err; |
| 7375 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7376 | get_online_cpus(); |
Heiko Carstens | 712555e | 2008-04-28 11:33:07 +0200 | [diff] [blame] | 7377 | mutex_lock(&sched_domains_mutex); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7378 | detach_destroy_domains(&cpu_online_map); |
| 7379 | err = arch_init_sched_domains(&cpu_online_map); |
Heiko Carstens | 712555e | 2008-04-28 11:33:07 +0200 | [diff] [blame] | 7380 | mutex_unlock(&sched_domains_mutex); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7381 | put_online_cpus(); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7382 | |
| 7383 | return err; |
| 7384 | } |
| 7385 | |
| 7386 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) |
| 7387 | { |
| 7388 | int ret; |
| 7389 | |
| 7390 | if (buf[0] != '0' && buf[0] != '1') |
| 7391 | return -EINVAL; |
| 7392 | |
| 7393 | if (smt) |
| 7394 | sched_smt_power_savings = (buf[0] == '1'); |
| 7395 | else |
| 7396 | sched_mc_power_savings = (buf[0] == '1'); |
| 7397 | |
| 7398 | ret = arch_reinit_sched_domains(); |
| 7399 | |
| 7400 | return ret ? ret : count; |
| 7401 | } |
| 7402 | |
Adrian Bunk | 6707de00 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 7403 | #ifdef CONFIG_SCHED_MC |
| 7404 | static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page) |
| 7405 | { |
| 7406 | return sprintf(page, "%u\n", sched_mc_power_savings); |
| 7407 | } |
| 7408 | static ssize_t sched_mc_power_savings_store(struct sys_device *dev, |
| 7409 | const char *buf, size_t count) |
| 7410 | { |
| 7411 | return sched_power_savings_store(buf, count, 0); |
| 7412 | } |
| 7413 | static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show, |
| 7414 | sched_mc_power_savings_store); |
| 7415 | #endif |
| 7416 | |
| 7417 | #ifdef CONFIG_SCHED_SMT |
| 7418 | static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page) |
| 7419 | { |
| 7420 | return sprintf(page, "%u\n", sched_smt_power_savings); |
| 7421 | } |
| 7422 | static ssize_t sched_smt_power_savings_store(struct sys_device *dev, |
| 7423 | const char *buf, size_t count) |
| 7424 | { |
| 7425 | return sched_power_savings_store(buf, count, 1); |
| 7426 | } |
| 7427 | static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show, |
| 7428 | sched_smt_power_savings_store); |
| 7429 | #endif |
| 7430 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7431 | int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) |
| 7432 | { |
| 7433 | int err = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7434 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7435 | #ifdef CONFIG_SCHED_SMT |
| 7436 | if (smt_capable()) |
| 7437 | err = sysfs_create_file(&cls->kset.kobj, |
| 7438 | &attr_sched_smt_power_savings.attr); |
| 7439 | #endif |
| 7440 | #ifdef CONFIG_SCHED_MC |
| 7441 | if (!err && mc_capable()) |
| 7442 | err = sysfs_create_file(&cls->kset.kobj, |
| 7443 | &attr_sched_mc_power_savings.attr); |
| 7444 | #endif |
| 7445 | return err; |
| 7446 | } |
| 7447 | #endif |
| 7448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7449 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7450 | * Force a reinitialization of the sched domains hierarchy. The domains |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | * and groups cannot be updated in place without racing with the balancing |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 7452 | * code, so we temporarily attach all running cpus to the NULL domain |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | * which will prevent rebalancing while the sched domains are recalculated. |
| 7454 | */ |
| 7455 | static int update_sched_domains(struct notifier_block *nfb, |
| 7456 | unsigned long action, void *hcpu) |
| 7457 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | switch (action) { |
| 7459 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7460 | case CPU_UP_PREPARE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7461 | case CPU_DOWN_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7462 | case CPU_DOWN_PREPARE_FROZEN: |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7463 | detach_destroy_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7464 | return NOTIFY_OK; |
| 7465 | |
| 7466 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7467 | case CPU_UP_CANCELED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7468 | case CPU_DOWN_FAILED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7469 | case CPU_DOWN_FAILED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7470 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7471 | case CPU_ONLINE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7473 | case CPU_DEAD_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7474 | /* |
| 7475 | * Fall through and re-initialise the domains. |
| 7476 | */ |
| 7477 | break; |
| 7478 | default: |
| 7479 | return NOTIFY_DONE; |
| 7480 | } |
| 7481 | |
| 7482 | /* The hotplug lock is already held by cpu_up/cpu_down */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7483 | arch_init_sched_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7484 | |
| 7485 | return NOTIFY_OK; |
| 7486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7487 | |
| 7488 | void __init sched_init_smp(void) |
| 7489 | { |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7490 | cpumask_t non_isolated_cpus; |
| 7491 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7492 | #if defined(CONFIG_NUMA) |
| 7493 | sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **), |
| 7494 | GFP_KERNEL); |
| 7495 | BUG_ON(sched_group_nodes_bycpu == NULL); |
| 7496 | #endif |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7497 | get_online_cpus(); |
Heiko Carstens | 712555e | 2008-04-28 11:33:07 +0200 | [diff] [blame] | 7498 | mutex_lock(&sched_domains_mutex); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7499 | arch_init_sched_domains(&cpu_online_map); |
Nathan Lynch | e5e5673 | 2007-01-10 23:15:28 -0800 | [diff] [blame] | 7500 | cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7501 | if (cpus_empty(non_isolated_cpus)) |
| 7502 | cpu_set(smp_processor_id(), non_isolated_cpus); |
Heiko Carstens | 712555e | 2008-04-28 11:33:07 +0200 | [diff] [blame] | 7503 | mutex_unlock(&sched_domains_mutex); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7504 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7505 | /* XXX: Theoretical race here - CPU may be hotplugged now */ |
| 7506 | hotcpu_notifier(update_sched_domains, 0); |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 7507 | init_hrtick(); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7508 | |
| 7509 | /* Move init over to a non-isolated CPU */ |
Mike Travis | 7c16ec5 | 2008-04-04 18:11:11 -0700 | [diff] [blame] | 7510 | if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0) |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7511 | BUG(); |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 7512 | sched_init_granularity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | } |
| 7514 | #else |
| 7515 | void __init sched_init_smp(void) |
| 7516 | { |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 7517 | sched_init_granularity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7518 | } |
| 7519 | #endif /* CONFIG_SMP */ |
| 7520 | |
| 7521 | int in_sched_functions(unsigned long addr) |
| 7522 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7523 | return in_lock_functions(addr) || |
| 7524 | (addr >= (unsigned long)__sched_text_start |
| 7525 | && addr < (unsigned long)__sched_text_end); |
| 7526 | } |
| 7527 | |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 7528 | 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] | 7529 | { |
| 7530 | cfs_rq->tasks_timeline = RB_ROOT; |
Peter Zijlstra | 4a55bd5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7531 | INIT_LIST_HEAD(&cfs_rq->tasks); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7532 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7533 | cfs_rq->rq = rq; |
| 7534 | #endif |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 7535 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7536 | } |
| 7537 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7538 | static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) |
| 7539 | { |
| 7540 | struct rt_prio_array *array; |
| 7541 | int i; |
| 7542 | |
| 7543 | array = &rt_rq->active; |
| 7544 | for (i = 0; i < MAX_RT_PRIO; i++) { |
| 7545 | INIT_LIST_HEAD(array->queue + i); |
| 7546 | __clear_bit(i, array->bitmap); |
| 7547 | } |
| 7548 | /* delimiter for bitsearch: */ |
| 7549 | __set_bit(MAX_RT_PRIO, array->bitmap); |
| 7550 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7551 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 7552 | rt_rq->highest_prio = MAX_RT_PRIO; |
| 7553 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7554 | #ifdef CONFIG_SMP |
| 7555 | rt_rq->rt_nr_migratory = 0; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7556 | rt_rq->overloaded = 0; |
| 7557 | #endif |
| 7558 | |
| 7559 | rt_rq->rt_time = 0; |
| 7560 | rt_rq->rt_throttled = 0; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 7561 | rt_rq->rt_runtime = 0; |
| 7562 | spin_lock_init(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7563 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7564 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7565 | rt_rq->rt_nr_boosted = 0; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7566 | rt_rq->rq = rq; |
| 7567 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7568 | } |
| 7569 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7570 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7571 | static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
| 7572 | struct sched_entity *se, int cpu, int add, |
| 7573 | struct sched_entity *parent) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7574 | { |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7575 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7576 | tg->cfs_rq[cpu] = cfs_rq; |
| 7577 | init_cfs_rq(cfs_rq, rq); |
| 7578 | cfs_rq->tg = tg; |
| 7579 | if (add) |
| 7580 | list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list); |
| 7581 | |
| 7582 | tg->se[cpu] = se; |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7583 | /* se could be NULL for init_task_group */ |
| 7584 | if (!se) |
| 7585 | return; |
| 7586 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7587 | if (!parent) |
| 7588 | se->cfs_rq = &rq->cfs; |
| 7589 | else |
| 7590 | se->cfs_rq = parent->my_q; |
| 7591 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7592 | se->my_q = cfs_rq; |
| 7593 | se->load.weight = tg->shares; |
Peter Zijlstra | e05510d | 2008-05-05 23:56:17 +0200 | [diff] [blame] | 7594 | se->load.inv_weight = 0; |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7595 | se->parent = parent; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7596 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7597 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7598 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7599 | #ifdef CONFIG_RT_GROUP_SCHED |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7600 | static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, |
| 7601 | struct sched_rt_entity *rt_se, int cpu, int add, |
| 7602 | struct sched_rt_entity *parent) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7603 | { |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7604 | struct rq *rq = cpu_rq(cpu); |
| 7605 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7606 | tg->rt_rq[cpu] = rt_rq; |
| 7607 | init_rt_rq(rt_rq, rq); |
| 7608 | rt_rq->tg = tg; |
| 7609 | rt_rq->rt_se = rt_se; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 7610 | rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7611 | if (add) |
| 7612 | list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list); |
| 7613 | |
| 7614 | tg->rt_se[cpu] = rt_se; |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7615 | if (!rt_se) |
| 7616 | return; |
| 7617 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7618 | if (!parent) |
| 7619 | rt_se->rt_rq = &rq->rt; |
| 7620 | else |
| 7621 | rt_se->rt_rq = parent->my_q; |
| 7622 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7623 | rt_se->rt_rq = &rq->rt; |
| 7624 | rt_se->my_q = rt_rq; |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7625 | rt_se->parent = parent; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7626 | INIT_LIST_HEAD(&rt_se->run_list); |
| 7627 | } |
| 7628 | #endif |
| 7629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7630 | void __init sched_init(void) |
| 7631 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7632 | int i, j; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7633 | unsigned long alloc_size = 0, ptr; |
| 7634 | |
| 7635 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7636 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); |
| 7637 | #endif |
| 7638 | #ifdef CONFIG_RT_GROUP_SCHED |
| 7639 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); |
| 7640 | #endif |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7641 | #ifdef CONFIG_USER_SCHED |
| 7642 | alloc_size *= 2; |
| 7643 | #endif |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7644 | /* |
| 7645 | * As sched_init() is called before page_alloc is setup, |
| 7646 | * we use alloc_bootmem(). |
| 7647 | */ |
| 7648 | if (alloc_size) { |
David Miller | 5a9d322 | 2008-04-24 20:46:20 -0700 | [diff] [blame] | 7649 | ptr = (unsigned long)alloc_bootmem(alloc_size); |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7650 | |
| 7651 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7652 | init_task_group.se = (struct sched_entity **)ptr; |
| 7653 | ptr += nr_cpu_ids * sizeof(void **); |
| 7654 | |
| 7655 | init_task_group.cfs_rq = (struct cfs_rq **)ptr; |
| 7656 | ptr += nr_cpu_ids * sizeof(void **); |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7657 | |
| 7658 | #ifdef CONFIG_USER_SCHED |
| 7659 | root_task_group.se = (struct sched_entity **)ptr; |
| 7660 | ptr += nr_cpu_ids * sizeof(void **); |
| 7661 | |
| 7662 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; |
| 7663 | ptr += nr_cpu_ids * sizeof(void **); |
| 7664 | #endif |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7665 | #endif |
| 7666 | #ifdef CONFIG_RT_GROUP_SCHED |
| 7667 | init_task_group.rt_se = (struct sched_rt_entity **)ptr; |
| 7668 | ptr += nr_cpu_ids * sizeof(void **); |
| 7669 | |
| 7670 | init_task_group.rt_rq = (struct rt_rq **)ptr; |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7671 | ptr += nr_cpu_ids * sizeof(void **); |
| 7672 | |
| 7673 | #ifdef CONFIG_USER_SCHED |
| 7674 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; |
| 7675 | ptr += nr_cpu_ids * sizeof(void **); |
| 7676 | |
| 7677 | root_task_group.rt_rq = (struct rt_rq **)ptr; |
| 7678 | ptr += nr_cpu_ids * sizeof(void **); |
| 7679 | #endif |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7680 | #endif |
| 7681 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7682 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7683 | #ifdef CONFIG_SMP |
| 7684 | init_defrootdomain(); |
| 7685 | #endif |
| 7686 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 7687 | init_rt_bandwidth(&def_rt_bandwidth, |
| 7688 | global_rt_period(), global_rt_runtime()); |
| 7689 | |
| 7690 | #ifdef CONFIG_RT_GROUP_SCHED |
| 7691 | init_rt_bandwidth(&init_task_group.rt_bandwidth, |
| 7692 | global_rt_period(), global_rt_runtime()); |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7693 | #ifdef CONFIG_USER_SCHED |
| 7694 | init_rt_bandwidth(&root_task_group.rt_bandwidth, |
| 7695 | global_rt_period(), RUNTIME_INF); |
| 7696 | #endif |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 7697 | #endif |
| 7698 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7699 | #ifdef CONFIG_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7700 | list_add(&init_task_group.list, &task_groups); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7701 | INIT_LIST_HEAD(&init_task_group.children); |
| 7702 | |
| 7703 | #ifdef CONFIG_USER_SCHED |
| 7704 | INIT_LIST_HEAD(&root_task_group.children); |
| 7705 | init_task_group.parent = &root_task_group; |
| 7706 | list_add(&init_task_group.siblings, &root_task_group.children); |
| 7707 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7708 | #endif |
| 7709 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 7710 | for_each_possible_cpu(i) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 7711 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7712 | |
| 7713 | rq = cpu_rq(i); |
| 7714 | spin_lock_init(&rq->lock); |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 7715 | lockdep_set_class(&rq->lock, &rq->rq_lock_key); |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 7716 | rq->nr_running = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7717 | init_cfs_rq(&rq->cfs, rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7718 | init_rt_rq(&rq->rt, rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7719 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7720 | init_task_group.shares = init_task_group_load; |
| 7721 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7722 | #ifdef CONFIG_CGROUP_SCHED |
| 7723 | /* |
| 7724 | * How much cpu bandwidth does init_task_group get? |
| 7725 | * |
| 7726 | * In case of task-groups formed thr' the cgroup filesystem, it |
| 7727 | * gets 100% of the cpu resources in the system. This overall |
| 7728 | * system cpu resource is divided among the tasks of |
| 7729 | * init_task_group and its child task-groups in a fair manner, |
| 7730 | * based on each entity's (task or task-group's) weight |
| 7731 | * (se->load.weight). |
| 7732 | * |
| 7733 | * In other words, if init_task_group has 10 tasks of weight |
| 7734 | * 1024) and two child groups A0 and A1 (of weight 1024 each), |
| 7735 | * then A0's share of the cpu resource is: |
| 7736 | * |
| 7737 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% |
| 7738 | * |
| 7739 | * We achieve this by letting init_task_group's tasks sit |
| 7740 | * directly in rq->cfs (i.e init_task_group->se[] = NULL). |
| 7741 | */ |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7742 | 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] | 7743 | #elif defined CONFIG_USER_SCHED |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7744 | root_task_group.shares = NICE_0_LOAD; |
| 7745 | 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] | 7746 | /* |
| 7747 | * In case of task-groups formed thr' the user id of tasks, |
| 7748 | * init_task_group represents tasks belonging to root user. |
| 7749 | * Hence it forms a sibling of all subsequent groups formed. |
| 7750 | * In this case, init_task_group gets only a fraction of overall |
| 7751 | * system cpu resource, based on the weight assigned to root |
| 7752 | * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished |
| 7753 | * by letting tasks of init_task_group sit in a separate cfs_rq |
| 7754 | * (init_cfs_rq) and having one entity represent this group of |
| 7755 | * tasks in rq->cfs (i.e init_task_group->se[] != NULL). |
| 7756 | */ |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7757 | init_tg_cfs_entry(&init_task_group, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7758 | &per_cpu(init_cfs_rq, i), |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7759 | &per_cpu(init_sched_entity, i), i, 1, |
| 7760 | root_task_group.se[i]); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7761 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7762 | #endif |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7763 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 7764 | |
| 7765 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7766 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7767 | INIT_LIST_HEAD(&rq->leaf_rt_rq_list); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7768 | #ifdef CONFIG_CGROUP_SCHED |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7769 | 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] | 7770 | #elif defined CONFIG_USER_SCHED |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7771 | 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] | 7772 | init_tg_rt_entry(&init_task_group, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7773 | &per_cpu(init_rt_rq, i), |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 7774 | &per_cpu(init_sched_rt_entity, i), i, 1, |
| 7775 | root_task_group.rt_se[i]); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7776 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7777 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7778 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7779 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) |
| 7780 | rq->cpu_load[j] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7781 | #ifdef CONFIG_SMP |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 7782 | rq->sd = NULL; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7783 | rq->rd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7784 | rq->active_balance = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7785 | rq->next_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7786 | rq->push_cpu = 0; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 7787 | rq->cpu = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7788 | rq->migration_thread = NULL; |
| 7789 | INIT_LIST_HEAD(&rq->migration_queue); |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 7790 | rq_attach_root(rq, &def_root_domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7791 | #endif |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7792 | init_rq_hrtick(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7793 | atomic_set(&rq->nr_iowait, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7794 | } |
| 7795 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 7796 | set_load_weight(&init_task); |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 7797 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 7798 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 7799 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); |
| 7800 | #endif |
| 7801 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 7802 | #ifdef CONFIG_SMP |
| 7803 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL); |
| 7804 | #endif |
| 7805 | |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 7806 | #ifdef CONFIG_RT_MUTEXES |
| 7807 | plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); |
| 7808 | #endif |
| 7809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7810 | /* |
| 7811 | * The boot idle thread does lazy MMU switching as well: |
| 7812 | */ |
| 7813 | atomic_inc(&init_mm.mm_count); |
| 7814 | enter_lazy_tlb(&init_mm, current); |
| 7815 | |
| 7816 | /* |
| 7817 | * Make us the idle thread. Technically, schedule() should not be |
| 7818 | * called from this thread, however somewhere below it might be, |
| 7819 | * but because we are the idle thread, we just pick up running again |
| 7820 | * when this runqueue becomes "idle". |
| 7821 | */ |
| 7822 | init_idle(current, smp_processor_id()); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7823 | /* |
| 7824 | * During early bootup we pretend to be a normal task: |
| 7825 | */ |
| 7826 | current->sched_class = &fair_sched_class; |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 7827 | |
| 7828 | scheduler_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7829 | } |
| 7830 | |
| 7831 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 7832 | void __might_sleep(char *file, int line) |
| 7833 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7834 | #ifdef in_atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7835 | static unsigned long prev_jiffy; /* ratelimiting */ |
| 7836 | |
| 7837 | if ((in_atomic() || irqs_disabled()) && |
| 7838 | system_state == SYSTEM_RUNNING && !oops_in_progress) { |
| 7839 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 7840 | return; |
| 7841 | prev_jiffy = jiffies; |
Ingo Molnar | 91368d7 | 2006-03-23 03:00:54 -0800 | [diff] [blame] | 7842 | printk(KERN_ERR "BUG: sleeping function called from invalid" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7843 | " context at %s:%d\n", file, line); |
| 7844 | printk("in_atomic():%d, irqs_disabled():%d\n", |
| 7845 | in_atomic(), irqs_disabled()); |
Peter Zijlstra | a4c410f | 2006-12-06 20:37:21 -0800 | [diff] [blame] | 7846 | debug_show_held_locks(current); |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 7847 | if (irqs_disabled()) |
| 7848 | print_irqtrace_events(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7849 | dump_stack(); |
| 7850 | } |
| 7851 | #endif |
| 7852 | } |
| 7853 | EXPORT_SYMBOL(__might_sleep); |
| 7854 | #endif |
| 7855 | |
| 7856 | #ifdef CONFIG_MAGIC_SYSRQ |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 7857 | static void normalize_task(struct rq *rq, struct task_struct *p) |
| 7858 | { |
| 7859 | int on_rq; |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 7860 | |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 7861 | update_rq_clock(rq); |
| 7862 | on_rq = p->se.on_rq; |
| 7863 | if (on_rq) |
| 7864 | deactivate_task(rq, p, 0); |
| 7865 | __setscheduler(rq, p, SCHED_NORMAL, 0); |
| 7866 | if (on_rq) { |
| 7867 | activate_task(rq, p, 0); |
| 7868 | resched_task(rq->curr); |
| 7869 | } |
| 7870 | } |
| 7871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7872 | void normalize_rt_tasks(void) |
| 7873 | { |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 7874 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7875 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 7876 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7877 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7878 | read_lock_irqsave(&tasklist_lock, flags); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 7879 | do_each_thread(g, p) { |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 7880 | /* |
| 7881 | * Only normalize user tasks: |
| 7882 | */ |
| 7883 | if (!p->mm) |
| 7884 | continue; |
| 7885 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7886 | p->se.exec_start = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 7887 | #ifdef CONFIG_SCHEDSTATS |
| 7888 | p->se.wait_start = 0; |
| 7889 | p->se.sleep_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7890 | p->se.block_start = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 7891 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7892 | |
| 7893 | if (!rt_task(p)) { |
| 7894 | /* |
| 7895 | * Renice negative nice level userspace |
| 7896 | * tasks back to 0: |
| 7897 | */ |
| 7898 | if (TASK_NICE(p) < 0 && p->mm) |
| 7899 | set_user_nice(p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7900 | continue; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7901 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7902 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7903 | spin_lock(&p->pi_lock); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 7904 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7905 | |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 7906 | normalize_task(rq, p); |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 7907 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 7908 | __task_rq_unlock(rq); |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7909 | spin_unlock(&p->pi_lock); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 7910 | } while_each_thread(g, p); |
| 7911 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7912 | read_unlock_irqrestore(&tasklist_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7913 | } |
| 7914 | |
| 7915 | #endif /* CONFIG_MAGIC_SYSRQ */ |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 7916 | |
| 7917 | #ifdef CONFIG_IA64 |
| 7918 | /* |
| 7919 | * These functions are only useful for the IA64 MCA handling. |
| 7920 | * |
| 7921 | * They can only be called when the whole system has been |
| 7922 | * stopped - every CPU needs to be quiescent, and no scheduling |
| 7923 | * activity can take place. Using them for anything else would |
| 7924 | * be a serious bug, and as a result, they aren't even visible |
| 7925 | * under any other configuration. |
| 7926 | */ |
| 7927 | |
| 7928 | /** |
| 7929 | * curr_task - return the current task for a given cpu. |
| 7930 | * @cpu: the processor in question. |
| 7931 | * |
| 7932 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 7933 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 7934 | struct task_struct *curr_task(int cpu) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 7935 | { |
| 7936 | return cpu_curr(cpu); |
| 7937 | } |
| 7938 | |
| 7939 | /** |
| 7940 | * set_curr_task - set the current task for a given cpu. |
| 7941 | * @cpu: the processor in question. |
| 7942 | * @p: the task pointer to set. |
| 7943 | * |
| 7944 | * Description: This function must only be used when non-maskable interrupts |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7945 | * are serviced on a separate stack. It allows the architecture to switch the |
| 7946 | * 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] | 7947 | * must be called with all CPU's synchronized, and interrupts disabled, the |
| 7948 | * and caller must save the original value of the current task (see |
| 7949 | * curr_task() above) and restore that value before reenabling interrupts and |
| 7950 | * re-starting the system. |
| 7951 | * |
| 7952 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 7953 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 7954 | void set_curr_task(int cpu, struct task_struct *p) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 7955 | { |
| 7956 | cpu_curr(cpu) = p; |
| 7957 | } |
| 7958 | |
| 7959 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7960 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7961 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7962 | static void free_fair_sched_group(struct task_group *tg) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7963 | { |
| 7964 | int i; |
| 7965 | |
| 7966 | for_each_possible_cpu(i) { |
| 7967 | if (tg->cfs_rq) |
| 7968 | kfree(tg->cfs_rq[i]); |
| 7969 | if (tg->se) |
| 7970 | kfree(tg->se[i]); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7971 | } |
| 7972 | |
| 7973 | kfree(tg->cfs_rq); |
| 7974 | kfree(tg->se); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7975 | } |
| 7976 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7977 | static |
| 7978 | 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] | 7979 | { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7980 | struct cfs_rq *cfs_rq; |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 7981 | struct sched_entity *se, *parent_se; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7982 | struct rq *rq; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7983 | int i; |
| 7984 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7985 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7986 | if (!tg->cfs_rq) |
| 7987 | goto err; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 7988 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7989 | if (!tg->se) |
| 7990 | goto err; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7991 | |
| 7992 | tg->shares = NICE_0_LOAD; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7993 | |
| 7994 | for_each_possible_cpu(i) { |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7995 | rq = cpu_rq(i); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7996 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7997 | cfs_rq = kmalloc_node(sizeof(struct cfs_rq), |
| 7998 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7999 | if (!cfs_rq) |
| 8000 | goto err; |
| 8001 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8002 | se = kmalloc_node(sizeof(struct sched_entity), |
| 8003 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8004 | if (!se) |
| 8005 | goto err; |
| 8006 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8007 | parent_se = parent ? parent->se[i] : NULL; |
| 8008 | init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8009 | } |
| 8010 | |
| 8011 | return 1; |
| 8012 | |
| 8013 | err: |
| 8014 | return 0; |
| 8015 | } |
| 8016 | |
| 8017 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) |
| 8018 | { |
| 8019 | list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list, |
| 8020 | &cpu_rq(cpu)->leaf_cfs_rq_list); |
| 8021 | } |
| 8022 | |
| 8023 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 8024 | { |
| 8025 | list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list); |
| 8026 | } |
| 8027 | #else |
| 8028 | static inline void free_fair_sched_group(struct task_group *tg) |
| 8029 | { |
| 8030 | } |
| 8031 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8032 | static inline |
| 8033 | 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] | 8034 | { |
| 8035 | return 1; |
| 8036 | } |
| 8037 | |
| 8038 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) |
| 8039 | { |
| 8040 | } |
| 8041 | |
| 8042 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 8043 | { |
| 8044 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8045 | #endif |
| 8046 | |
| 8047 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8048 | static void free_rt_sched_group(struct task_group *tg) |
| 8049 | { |
| 8050 | int i; |
| 8051 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8052 | destroy_rt_bandwidth(&tg->rt_bandwidth); |
| 8053 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8054 | for_each_possible_cpu(i) { |
| 8055 | if (tg->rt_rq) |
| 8056 | kfree(tg->rt_rq[i]); |
| 8057 | if (tg->rt_se) |
| 8058 | kfree(tg->rt_se[i]); |
| 8059 | } |
| 8060 | |
| 8061 | kfree(tg->rt_rq); |
| 8062 | kfree(tg->rt_se); |
| 8063 | } |
| 8064 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8065 | static |
| 8066 | 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] | 8067 | { |
| 8068 | struct rt_rq *rt_rq; |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8069 | struct sched_rt_entity *rt_se, *parent_se; |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8070 | struct rq *rq; |
| 8071 | int i; |
| 8072 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8073 | tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8074 | if (!tg->rt_rq) |
| 8075 | goto err; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8076 | tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8077 | if (!tg->rt_se) |
| 8078 | goto err; |
| 8079 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8080 | init_rt_bandwidth(&tg->rt_bandwidth, |
| 8081 | ktime_to_ns(def_rt_bandwidth.rt_period), 0); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8082 | |
| 8083 | for_each_possible_cpu(i) { |
| 8084 | rq = cpu_rq(i); |
| 8085 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8086 | rt_rq = kmalloc_node(sizeof(struct rt_rq), |
| 8087 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
| 8088 | if (!rt_rq) |
| 8089 | goto err; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8090 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8091 | rt_se = kmalloc_node(sizeof(struct sched_rt_entity), |
| 8092 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
| 8093 | if (!rt_se) |
| 8094 | goto err; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8095 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8096 | parent_se = parent ? parent->rt_se[i] : NULL; |
| 8097 | 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] | 8098 | } |
| 8099 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8100 | return 1; |
| 8101 | |
| 8102 | err: |
| 8103 | return 0; |
| 8104 | } |
| 8105 | |
| 8106 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) |
| 8107 | { |
| 8108 | list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list, |
| 8109 | &cpu_rq(cpu)->leaf_rt_rq_list); |
| 8110 | } |
| 8111 | |
| 8112 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) |
| 8113 | { |
| 8114 | list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list); |
| 8115 | } |
| 8116 | #else |
| 8117 | static inline void free_rt_sched_group(struct task_group *tg) |
| 8118 | { |
| 8119 | } |
| 8120 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8121 | static inline |
| 8122 | 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] | 8123 | { |
| 8124 | return 1; |
| 8125 | } |
| 8126 | |
| 8127 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) |
| 8128 | { |
| 8129 | } |
| 8130 | |
| 8131 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) |
| 8132 | { |
| 8133 | } |
| 8134 | #endif |
| 8135 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8136 | #ifdef CONFIG_GROUP_SCHED |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8137 | static void free_sched_group(struct task_group *tg) |
| 8138 | { |
| 8139 | free_fair_sched_group(tg); |
| 8140 | free_rt_sched_group(tg); |
| 8141 | kfree(tg); |
| 8142 | } |
| 8143 | |
| 8144 | /* allocate runqueue etc for a new task group */ |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8145 | struct task_group *sched_create_group(struct task_group *parent) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8146 | { |
| 8147 | struct task_group *tg; |
| 8148 | unsigned long flags; |
| 8149 | int i; |
| 8150 | |
| 8151 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); |
| 8152 | if (!tg) |
| 8153 | return ERR_PTR(-ENOMEM); |
| 8154 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8155 | if (!alloc_fair_sched_group(tg, parent)) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8156 | goto err; |
| 8157 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8158 | if (!alloc_rt_sched_group(tg, parent)) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8159 | goto err; |
| 8160 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8161 | spin_lock_irqsave(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8162 | for_each_possible_cpu(i) { |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8163 | register_fair_sched_group(tg, i); |
| 8164 | register_rt_sched_group(tg, i); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8165 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8166 | list_add_rcu(&tg->list, &task_groups); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8167 | |
| 8168 | WARN_ON(!parent); /* root should already exist */ |
| 8169 | |
| 8170 | tg->parent = parent; |
| 8171 | list_add_rcu(&tg->siblings, &parent->children); |
| 8172 | INIT_LIST_HEAD(&tg->children); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8173 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8174 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8175 | return tg; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8176 | |
| 8177 | err: |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8178 | free_sched_group(tg); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8179 | return ERR_PTR(-ENOMEM); |
| 8180 | } |
| 8181 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8182 | /* rcu callback to free various structures associated with a task group */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8183 | static void free_sched_group_rcu(struct rcu_head *rhp) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8184 | { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8185 | /* now it should be safe to free those cfs_rqs */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8186 | free_sched_group(container_of(rhp, struct task_group, rcu)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8187 | } |
| 8188 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8189 | /* Destroy runqueue etc associated with a task group */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8190 | void sched_destroy_group(struct task_group *tg) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8191 | { |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8192 | unsigned long flags; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8193 | int i; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8194 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8195 | spin_lock_irqsave(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8196 | for_each_possible_cpu(i) { |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8197 | unregister_fair_sched_group(tg, i); |
| 8198 | unregister_rt_sched_group(tg, i); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8199 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8200 | list_del_rcu(&tg->list); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8201 | list_del_rcu(&tg->siblings); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8202 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8203 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8204 | /* wait for possible concurrent references to cfs_rqs complete */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8205 | call_rcu(&tg->rcu, free_sched_group_rcu); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8206 | } |
| 8207 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8208 | /* change task's runqueue when it moves between groups. |
Ingo Molnar | 3a25201 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 8209 | * The caller of this function should have put the task in its new group |
| 8210 | * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to |
| 8211 | * reflect its new group. |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8212 | */ |
| 8213 | void sched_move_task(struct task_struct *tsk) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8214 | { |
| 8215 | int on_rq, running; |
| 8216 | unsigned long flags; |
| 8217 | struct rq *rq; |
| 8218 | |
| 8219 | rq = task_rq_lock(tsk, &flags); |
| 8220 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8221 | update_rq_clock(rq); |
| 8222 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 8223 | running = task_current(rq, tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8224 | on_rq = tsk->se.on_rq; |
| 8225 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 8226 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8227 | dequeue_task(rq, tsk, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 8228 | if (unlikely(running)) |
| 8229 | tsk->sched_class->put_prev_task(rq, tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8230 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8231 | set_task_rq(tsk, task_cpu(tsk)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8232 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 8233 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8234 | if (tsk->sched_class->moved_group) |
| 8235 | tsk->sched_class->moved_group(tsk); |
| 8236 | #endif |
| 8237 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 8238 | if (unlikely(running)) |
| 8239 | tsk->sched_class->set_curr_task(rq); |
| 8240 | if (on_rq) |
Dmitry Adamushko | 7074bad | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8241 | enqueue_task(rq, tsk, 0); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8242 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8243 | task_rq_unlock(rq, &flags); |
| 8244 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8245 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8246 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8247 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 8248 | static void set_se_shares(struct sched_entity *se, unsigned long shares) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8249 | { |
| 8250 | struct cfs_rq *cfs_rq = se->cfs_rq; |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 8251 | struct rq *rq = cfs_rq->rq; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8252 | int on_rq; |
| 8253 | |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 8254 | spin_lock_irq(&rq->lock); |
| 8255 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8256 | on_rq = se->on_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8257 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8258 | dequeue_entity(cfs_rq, se, 0); |
| 8259 | |
| 8260 | se->load.weight = shares; |
Peter Zijlstra | e05510d | 2008-05-05 23:56:17 +0200 | [diff] [blame] | 8261 | se->load.inv_weight = 0; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8262 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8263 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8264 | enqueue_entity(cfs_rq, se, 0); |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8265 | |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 8266 | spin_unlock_irq(&rq->lock); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8267 | } |
| 8268 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8269 | static DEFINE_MUTEX(shares_mutex); |
| 8270 | |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8271 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8272 | { |
| 8273 | int i; |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8274 | unsigned long flags; |
Ingo Molnar | c61935f | 2008-01-22 11:24:58 +0100 | [diff] [blame] | 8275 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8276 | /* |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8277 | * We can't change the weight of the root cgroup. |
| 8278 | */ |
| 8279 | if (!tg->se[0]) |
| 8280 | return -EINVAL; |
| 8281 | |
Peter Zijlstra | 18d95a2 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8282 | if (shares < MIN_SHARES) |
| 8283 | shares = MIN_SHARES; |
Miao Xie | cb4ad1f | 2008-04-28 12:54:56 +0800 | [diff] [blame] | 8284 | else if (shares > MAX_SHARES) |
| 8285 | shares = MAX_SHARES; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 8286 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8287 | mutex_lock(&shares_mutex); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8288 | if (tg->shares == shares) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8289 | goto done; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8290 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8291 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8292 | for_each_possible_cpu(i) |
| 8293 | unregister_fair_sched_group(tg, i); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8294 | list_del_rcu(&tg->siblings); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8295 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 8296 | |
| 8297 | /* wait for any ongoing reference to this group to finish */ |
| 8298 | synchronize_sched(); |
| 8299 | |
| 8300 | /* |
| 8301 | * Now we are free to modify the group's share on each cpu |
| 8302 | * w/o tripping rebalance_share or load_balance_fair. |
| 8303 | */ |
| 8304 | tg->shares = shares; |
Ingo Molnar | 6363ca5 | 2008-05-29 11:28:57 +0200 | [diff] [blame] | 8305 | for_each_possible_cpu(i) |
Miao Xie | cb4ad1f | 2008-04-28 12:54:56 +0800 | [diff] [blame] | 8306 | set_se_shares(tg->se[i], shares); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 8307 | |
| 8308 | /* |
| 8309 | * Enable load balance activity on this group, by inserting it back on |
| 8310 | * each cpu's rq->leaf_cfs_rq_list. |
| 8311 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8312 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8313 | for_each_possible_cpu(i) |
| 8314 | register_fair_sched_group(tg, i); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8315 | list_add_rcu(&tg->siblings, &tg->parent->children); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8316 | spin_unlock_irqrestore(&task_group_lock, flags); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8317 | done: |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8318 | mutex_unlock(&shares_mutex); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 8319 | return 0; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 8320 | } |
| 8321 | |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8322 | unsigned long sched_group_shares(struct task_group *tg) |
| 8323 | { |
| 8324 | return tg->shares; |
| 8325 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8326 | #endif |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8327 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8328 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8329 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8330 | * Ensure that the real time constraints are schedulable. |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8331 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8332 | static DEFINE_MUTEX(rt_constraints_mutex); |
| 8333 | |
| 8334 | static unsigned long to_ratio(u64 period, u64 runtime) |
| 8335 | { |
| 8336 | if (runtime == RUNTIME_INF) |
| 8337 | return 1ULL << 16; |
| 8338 | |
Roman Zippel | 6f6d6a1 | 2008-05-01 04:34:28 -0700 | [diff] [blame] | 8339 | return div64_u64(runtime << 16, period); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8340 | } |
| 8341 | |
Peter Zijlstra | b40b2e8 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8342 | #ifdef CONFIG_CGROUP_SCHED |
| 8343 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
| 8344 | { |
| 8345 | struct task_group *tgi, *parent = tg->parent; |
| 8346 | unsigned long total = 0; |
| 8347 | |
| 8348 | if (!parent) { |
| 8349 | if (global_rt_period() < period) |
| 8350 | return 0; |
| 8351 | |
| 8352 | return to_ratio(period, runtime) < |
| 8353 | to_ratio(global_rt_period(), global_rt_runtime()); |
| 8354 | } |
| 8355 | |
| 8356 | if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period) |
| 8357 | return 0; |
| 8358 | |
| 8359 | rcu_read_lock(); |
| 8360 | list_for_each_entry_rcu(tgi, &parent->children, siblings) { |
| 8361 | if (tgi == tg) |
| 8362 | continue; |
| 8363 | |
| 8364 | total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period), |
| 8365 | tgi->rt_bandwidth.rt_runtime); |
| 8366 | } |
| 8367 | rcu_read_unlock(); |
| 8368 | |
| 8369 | return total + to_ratio(period, runtime) < |
| 8370 | to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period), |
| 8371 | parent->rt_bandwidth.rt_runtime); |
| 8372 | } |
| 8373 | #elif defined CONFIG_USER_SCHED |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8374 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8375 | { |
| 8376 | struct task_group *tgi; |
| 8377 | unsigned long total = 0; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8378 | unsigned long global_ratio = |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8379 | to_ratio(global_rt_period(), global_rt_runtime()); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8380 | |
| 8381 | rcu_read_lock(); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8382 | list_for_each_entry_rcu(tgi, &task_groups, list) { |
| 8383 | if (tgi == tg) |
| 8384 | continue; |
| 8385 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8386 | total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period), |
| 8387 | tgi->rt_bandwidth.rt_runtime); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8388 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8389 | rcu_read_unlock(); |
| 8390 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8391 | return total + to_ratio(period, runtime) < global_ratio; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8392 | } |
Peter Zijlstra | b40b2e8 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8393 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8394 | |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8395 | /* Must be called with tasklist_lock held */ |
| 8396 | static inline int tg_has_rt_tasks(struct task_group *tg) |
| 8397 | { |
| 8398 | struct task_struct *g, *p; |
| 8399 | do_each_thread(g, p) { |
| 8400 | if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg) |
| 8401 | return 1; |
| 8402 | } while_each_thread(g, p); |
| 8403 | return 0; |
| 8404 | } |
| 8405 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8406 | static int tg_set_bandwidth(struct task_group *tg, |
| 8407 | u64 rt_period, u64 rt_runtime) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8408 | { |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8409 | int i, err = 0; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8410 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8411 | mutex_lock(&rt_constraints_mutex); |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8412 | read_lock(&tasklist_lock); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8413 | if (rt_runtime == 0 && tg_has_rt_tasks(tg)) { |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8414 | err = -EBUSY; |
| 8415 | goto unlock; |
| 8416 | } |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8417 | if (!__rt_schedulable(tg, rt_period, rt_runtime)) { |
| 8418 | err = -EINVAL; |
| 8419 | goto unlock; |
| 8420 | } |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8421 | |
| 8422 | spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock); |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8423 | tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period); |
| 8424 | tg->rt_bandwidth.rt_runtime = rt_runtime; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8425 | |
| 8426 | for_each_possible_cpu(i) { |
| 8427 | struct rt_rq *rt_rq = tg->rt_rq[i]; |
| 8428 | |
| 8429 | spin_lock(&rt_rq->rt_runtime_lock); |
| 8430 | rt_rq->rt_runtime = rt_runtime; |
| 8431 | spin_unlock(&rt_rq->rt_runtime_lock); |
| 8432 | } |
| 8433 | spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8434 | unlock: |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 8435 | read_unlock(&tasklist_lock); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8436 | mutex_unlock(&rt_constraints_mutex); |
| 8437 | |
| 8438 | return err; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8439 | } |
| 8440 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8441 | int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) |
| 8442 | { |
| 8443 | u64 rt_runtime, rt_period; |
| 8444 | |
| 8445 | rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period); |
| 8446 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; |
| 8447 | if (rt_runtime_us < 0) |
| 8448 | rt_runtime = RUNTIME_INF; |
| 8449 | |
| 8450 | return tg_set_bandwidth(tg, rt_period, rt_runtime); |
| 8451 | } |
| 8452 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8453 | long sched_group_rt_runtime(struct task_group *tg) |
| 8454 | { |
| 8455 | u64 rt_runtime_us; |
| 8456 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8457 | if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF) |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8458 | return -1; |
| 8459 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8460 | rt_runtime_us = tg->rt_bandwidth.rt_runtime; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8461 | do_div(rt_runtime_us, NSEC_PER_USEC); |
| 8462 | return rt_runtime_us; |
| 8463 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8464 | |
| 8465 | int sched_group_set_rt_period(struct task_group *tg, long rt_period_us) |
| 8466 | { |
| 8467 | u64 rt_runtime, rt_period; |
| 8468 | |
| 8469 | rt_period = (u64)rt_period_us * NSEC_PER_USEC; |
| 8470 | rt_runtime = tg->rt_bandwidth.rt_runtime; |
| 8471 | |
| 8472 | return tg_set_bandwidth(tg, rt_period, rt_runtime); |
| 8473 | } |
| 8474 | |
| 8475 | long sched_group_rt_period(struct task_group *tg) |
| 8476 | { |
| 8477 | u64 rt_period_us; |
| 8478 | |
| 8479 | rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period); |
| 8480 | do_div(rt_period_us, NSEC_PER_USEC); |
| 8481 | return rt_period_us; |
| 8482 | } |
| 8483 | |
| 8484 | static int sched_rt_global_constraints(void) |
| 8485 | { |
| 8486 | int ret = 0; |
| 8487 | |
| 8488 | mutex_lock(&rt_constraints_mutex); |
| 8489 | if (!__rt_schedulable(NULL, 1, 0)) |
| 8490 | ret = -EINVAL; |
| 8491 | mutex_unlock(&rt_constraints_mutex); |
| 8492 | |
| 8493 | return ret; |
| 8494 | } |
| 8495 | #else |
| 8496 | static int sched_rt_global_constraints(void) |
| 8497 | { |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 8498 | unsigned long flags; |
| 8499 | int i; |
| 8500 | |
| 8501 | spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); |
| 8502 | for_each_possible_cpu(i) { |
| 8503 | struct rt_rq *rt_rq = &cpu_rq(i)->rt; |
| 8504 | |
| 8505 | spin_lock(&rt_rq->rt_runtime_lock); |
| 8506 | rt_rq->rt_runtime = global_rt_runtime(); |
| 8507 | spin_unlock(&rt_rq->rt_runtime_lock); |
| 8508 | } |
| 8509 | spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags); |
| 8510 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8511 | return 0; |
| 8512 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8513 | #endif |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8514 | |
| 8515 | int sched_rt_handler(struct ctl_table *table, int write, |
| 8516 | struct file *filp, void __user *buffer, size_t *lenp, |
| 8517 | loff_t *ppos) |
| 8518 | { |
| 8519 | int ret; |
| 8520 | int old_period, old_runtime; |
| 8521 | static DEFINE_MUTEX(mutex); |
| 8522 | |
| 8523 | mutex_lock(&mutex); |
| 8524 | old_period = sysctl_sched_rt_period; |
| 8525 | old_runtime = sysctl_sched_rt_runtime; |
| 8526 | |
| 8527 | ret = proc_dointvec(table, write, filp, buffer, lenp, ppos); |
| 8528 | |
| 8529 | if (!ret && write) { |
| 8530 | ret = sched_rt_global_constraints(); |
| 8531 | if (ret) { |
| 8532 | sysctl_sched_rt_period = old_period; |
| 8533 | sysctl_sched_rt_runtime = old_runtime; |
| 8534 | } else { |
| 8535 | def_rt_bandwidth.rt_runtime = global_rt_runtime(); |
| 8536 | def_rt_bandwidth.rt_period = |
| 8537 | ns_to_ktime(global_rt_period()); |
| 8538 | } |
| 8539 | } |
| 8540 | mutex_unlock(&mutex); |
| 8541 | |
| 8542 | return ret; |
| 8543 | } |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8544 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8545 | #ifdef CONFIG_CGROUP_SCHED |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8546 | |
| 8547 | /* return corresponding task_group object of a cgroup */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8548 | static inline struct task_group *cgroup_tg(struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8549 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8550 | return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id), |
| 8551 | struct task_group, css); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8552 | } |
| 8553 | |
| 8554 | static struct cgroup_subsys_state * |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8555 | cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8556 | { |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8557 | struct task_group *tg, *parent; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8558 | |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8559 | if (!cgrp->parent) { |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8560 | /* This is early initialization for the top cgroup */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8561 | init_task_group.css.cgroup = cgrp; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8562 | return &init_task_group.css; |
| 8563 | } |
| 8564 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8565 | parent = cgroup_tg(cgrp->parent); |
| 8566 | tg = sched_create_group(parent); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8567 | if (IS_ERR(tg)) |
| 8568 | return ERR_PTR(-ENOMEM); |
| 8569 | |
| 8570 | /* Bind the cgroup to task_group object we just created */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8571 | tg->css.cgroup = cgrp; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8572 | |
| 8573 | return &tg->css; |
| 8574 | } |
| 8575 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8576 | static void |
| 8577 | cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8578 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8579 | struct task_group *tg = cgroup_tg(cgrp); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8580 | |
| 8581 | sched_destroy_group(tg); |
| 8582 | } |
| 8583 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8584 | static int |
| 8585 | cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
| 8586 | struct task_struct *tsk) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8587 | { |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8588 | #ifdef CONFIG_RT_GROUP_SCHED |
| 8589 | /* 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] | 8590 | if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0) |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8591 | return -EINVAL; |
| 8592 | #else |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8593 | /* We don't support RT-tasks being in separate groups */ |
| 8594 | if (tsk->sched_class != &fair_sched_class) |
| 8595 | return -EINVAL; |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8596 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8597 | |
| 8598 | return 0; |
| 8599 | } |
| 8600 | |
| 8601 | static void |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8602 | cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8603 | struct cgroup *old_cont, struct task_struct *tsk) |
| 8604 | { |
| 8605 | sched_move_task(tsk); |
| 8606 | } |
| 8607 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8608 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 8609 | static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype, |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8610 | u64 shareval) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8611 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8612 | return sched_group_set_shares(cgroup_tg(cgrp), shareval); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8613 | } |
| 8614 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 8615 | static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8616 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8617 | struct task_group *tg = cgroup_tg(cgrp); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8618 | |
| 8619 | return (u64) tg->shares; |
| 8620 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8621 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8622 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8623 | #ifdef CONFIG_RT_GROUP_SCHED |
Mirco Tischler | 0c70814 | 2008-05-14 16:05:46 -0700 | [diff] [blame] | 8624 | static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | 06ecb27 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 8625 | s64 val) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8626 | { |
Paul Menage | 06ecb27 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 8627 | return sched_group_set_rt_runtime(cgroup_tg(cgrp), val); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8628 | } |
| 8629 | |
Paul Menage | 06ecb27 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 8630 | static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8631 | { |
Paul Menage | 06ecb27 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 8632 | return sched_group_rt_runtime(cgroup_tg(cgrp)); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8633 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8634 | |
| 8635 | static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype, |
| 8636 | u64 rt_period_us) |
| 8637 | { |
| 8638 | return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us); |
| 8639 | } |
| 8640 | |
| 8641 | static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft) |
| 8642 | { |
| 8643 | return sched_group_rt_period(cgroup_tg(cgrp)); |
| 8644 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8645 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8646 | |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8647 | static struct cftype cpu_files[] = { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8648 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8649 | { |
| 8650 | .name = "shares", |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 8651 | .read_u64 = cpu_shares_read_u64, |
| 8652 | .write_u64 = cpu_shares_write_u64, |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8653 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8654 | #endif |
| 8655 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8656 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8657 | .name = "rt_runtime_us", |
Paul Menage | 06ecb27 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 8658 | .read_s64 = cpu_rt_runtime_read, |
| 8659 | .write_s64 = cpu_rt_runtime_write, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8660 | }, |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8661 | { |
| 8662 | .name = "rt_period_us", |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 8663 | .read_u64 = cpu_rt_period_read_uint, |
| 8664 | .write_u64 = cpu_rt_period_write_uint, |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8665 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8666 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8667 | }; |
| 8668 | |
| 8669 | static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont) |
| 8670 | { |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8671 | return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files)); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8672 | } |
| 8673 | |
| 8674 | struct cgroup_subsys cpu_cgroup_subsys = { |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8675 | .name = "cpu", |
| 8676 | .create = cpu_cgroup_create, |
| 8677 | .destroy = cpu_cgroup_destroy, |
| 8678 | .can_attach = cpu_cgroup_can_attach, |
| 8679 | .attach = cpu_cgroup_attach, |
| 8680 | .populate = cpu_cgroup_populate, |
| 8681 | .subsys_id = cpu_cgroup_subsys_id, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8682 | .early_init = 1, |
| 8683 | }; |
| 8684 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8685 | #endif /* CONFIG_CGROUP_SCHED */ |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8686 | |
| 8687 | #ifdef CONFIG_CGROUP_CPUACCT |
| 8688 | |
| 8689 | /* |
| 8690 | * CPU accounting code for task groups. |
| 8691 | * |
| 8692 | * Based on the work by Paul Menage (menage@google.com) and Balbir Singh |
| 8693 | * (balbir@in.ibm.com). |
| 8694 | */ |
| 8695 | |
| 8696 | /* track cpu usage of a group of tasks */ |
| 8697 | struct cpuacct { |
| 8698 | struct cgroup_subsys_state css; |
| 8699 | /* cpuusage holds pointer to a u64-type object on every cpu */ |
| 8700 | u64 *cpuusage; |
| 8701 | }; |
| 8702 | |
| 8703 | struct cgroup_subsys cpuacct_subsys; |
| 8704 | |
| 8705 | /* return cpu accounting group corresponding to this container */ |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 8706 | static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8707 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 8708 | return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id), |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8709 | struct cpuacct, css); |
| 8710 | } |
| 8711 | |
| 8712 | /* return cpu accounting group to which this task belongs */ |
| 8713 | static inline struct cpuacct *task_ca(struct task_struct *tsk) |
| 8714 | { |
| 8715 | return container_of(task_subsys_state(tsk, cpuacct_subsys_id), |
| 8716 | struct cpuacct, css); |
| 8717 | } |
| 8718 | |
| 8719 | /* create a new cpu accounting group */ |
| 8720 | static struct cgroup_subsys_state *cpuacct_create( |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 8721 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8722 | { |
| 8723 | struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL); |
| 8724 | |
| 8725 | if (!ca) |
| 8726 | return ERR_PTR(-ENOMEM); |
| 8727 | |
| 8728 | ca->cpuusage = alloc_percpu(u64); |
| 8729 | if (!ca->cpuusage) { |
| 8730 | kfree(ca); |
| 8731 | return ERR_PTR(-ENOMEM); |
| 8732 | } |
| 8733 | |
| 8734 | return &ca->css; |
| 8735 | } |
| 8736 | |
| 8737 | /* destroy an existing cpu accounting group */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8738 | static void |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 8739 | cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8740 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 8741 | struct cpuacct *ca = cgroup_ca(cgrp); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8742 | |
| 8743 | free_percpu(ca->cpuusage); |
| 8744 | kfree(ca); |
| 8745 | } |
| 8746 | |
| 8747 | /* return total cpu usage (in nanoseconds) of a group */ |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 8748 | static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8749 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 8750 | struct cpuacct *ca = cgroup_ca(cgrp); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8751 | u64 totalcpuusage = 0; |
| 8752 | int i; |
| 8753 | |
| 8754 | for_each_possible_cpu(i) { |
| 8755 | u64 *cpuusage = percpu_ptr(ca->cpuusage, i); |
| 8756 | |
| 8757 | /* |
| 8758 | * Take rq->lock to make 64-bit addition safe on 32-bit |
| 8759 | * platforms. |
| 8760 | */ |
| 8761 | spin_lock_irq(&cpu_rq(i)->lock); |
| 8762 | totalcpuusage += *cpuusage; |
| 8763 | spin_unlock_irq(&cpu_rq(i)->lock); |
| 8764 | } |
| 8765 | |
| 8766 | return totalcpuusage; |
| 8767 | } |
| 8768 | |
Dhaval Giani | 0297b80 | 2008-02-29 10:02:44 +0530 | [diff] [blame] | 8769 | static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype, |
| 8770 | u64 reset) |
| 8771 | { |
| 8772 | struct cpuacct *ca = cgroup_ca(cgrp); |
| 8773 | int err = 0; |
| 8774 | int i; |
| 8775 | |
| 8776 | if (reset) { |
| 8777 | err = -EINVAL; |
| 8778 | goto out; |
| 8779 | } |
| 8780 | |
| 8781 | for_each_possible_cpu(i) { |
| 8782 | u64 *cpuusage = percpu_ptr(ca->cpuusage, i); |
| 8783 | |
| 8784 | spin_lock_irq(&cpu_rq(i)->lock); |
| 8785 | *cpuusage = 0; |
| 8786 | spin_unlock_irq(&cpu_rq(i)->lock); |
| 8787 | } |
| 8788 | out: |
| 8789 | return err; |
| 8790 | } |
| 8791 | |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8792 | static struct cftype files[] = { |
| 8793 | { |
| 8794 | .name = "usage", |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 8795 | .read_u64 = cpuusage_read, |
| 8796 | .write_u64 = cpuusage_write, |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8797 | }, |
| 8798 | }; |
| 8799 | |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 8800 | static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8801 | { |
Dhaval Giani | 32cd756 | 2008-02-29 10:02:43 +0530 | [diff] [blame] | 8802 | return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files)); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8803 | } |
| 8804 | |
| 8805 | /* |
| 8806 | * charge this task's execution time to its accounting group. |
| 8807 | * |
| 8808 | * called with rq->lock held. |
| 8809 | */ |
| 8810 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime) |
| 8811 | { |
| 8812 | struct cpuacct *ca; |
| 8813 | |
| 8814 | if (!cpuacct_subsys.active) |
| 8815 | return; |
| 8816 | |
| 8817 | ca = task_ca(tsk); |
| 8818 | if (ca) { |
| 8819 | u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk)); |
| 8820 | |
| 8821 | *cpuusage += cputime; |
| 8822 | } |
| 8823 | } |
| 8824 | |
| 8825 | struct cgroup_subsys cpuacct_subsys = { |
| 8826 | .name = "cpuacct", |
| 8827 | .create = cpuacct_create, |
| 8828 | .destroy = cpuacct_destroy, |
| 8829 | .populate = cpuacct_populate, |
| 8830 | .subsys_id = cpuacct_subsys_id, |
| 8831 | }; |
| 8832 | #endif /* CONFIG_CGROUP_CPUACCT */ |