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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 71 | #include <asm/tlb.h> |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 72 | #include <asm/irq_regs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | /* |
Alexey Dobriyan | b035b6d | 2007-02-10 01:45:10 -0800 | [diff] [blame] | 75 | * Scheduler clock - returns current time in nanosec units. |
| 76 | * This is default implementation. |
| 77 | * Architectures and sub-architectures can override this. |
| 78 | */ |
| 79 | unsigned long long __attribute__((weak)) sched_clock(void) |
| 80 | { |
Eric Dumazet | d6322fa | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 81 | return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ); |
Alexey Dobriyan | b035b6d | 2007-02-10 01:45:10 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | * Convert user-nice values [ -20 ... 0 ... 19 ] |
| 86 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], |
| 87 | * and back. |
| 88 | */ |
| 89 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) |
| 90 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) |
| 91 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) |
| 92 | |
| 93 | /* |
| 94 | * 'User priority' is the nice value converted to something we |
| 95 | * can work with better when scaling various scheduler parameters, |
| 96 | * it's a [ 0 ... 39 ] range. |
| 97 | */ |
| 98 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) |
| 99 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) |
| 100 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) |
| 101 | |
| 102 | /* |
Ingo Molnar | d7876a0 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 103 | * Helpers for converting nanosecond timing to jiffy resolution |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | */ |
Eric Dumazet | d6322fa | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 105 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 107 | #define NICE_0_LOAD SCHED_LOAD_SCALE |
| 108 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* |
| 111 | * These are the 'tuning knobs' of the scheduler: |
| 112 | * |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 113 | * default timeslice is 100 msecs (used only for SCHED_RR tasks). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | * Timeslices get refilled after they expire. |
| 115 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | #define DEF_TIMESLICE (100 * HZ / 1000) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 117 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 118 | #ifdef CONFIG_SMP |
| 119 | /* |
| 120 | * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) |
| 121 | * Since cpu_power is a 'constant', we can use a reciprocal divide. |
| 122 | */ |
| 123 | static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load) |
| 124 | { |
| 125 | return reciprocal_divide(load, sg->reciprocal_cpu_power); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Each time a sched group cpu_power is changed, |
| 130 | * we must compute its reciprocal value |
| 131 | */ |
| 132 | static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val) |
| 133 | { |
| 134 | sg->__cpu_power += val; |
| 135 | sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power); |
| 136 | } |
| 137 | #endif |
| 138 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 139 | static inline int rt_policy(int policy) |
| 140 | { |
| 141 | if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR)) |
| 142 | return 1; |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static inline int task_has_rt_policy(struct task_struct *p) |
| 147 | { |
| 148 | return rt_policy(p->policy); |
| 149 | } |
| 150 | |
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 | * This is the priority-queue data structure of the RT scheduling class: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | */ |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 154 | struct rt_prio_array { |
| 155 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ |
| 156 | struct list_head queue[MAX_RT_PRIO]; |
| 157 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 159 | #ifdef CONFIG_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 160 | |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 161 | #include <linux/cgroup.h> |
| 162 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 163 | struct cfs_rq; |
| 164 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 165 | static LIST_HEAD(task_groups); |
| 166 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 167 | /* task group related information */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 168 | struct task_group { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 169 | #ifdef CONFIG_CGROUP_SCHED |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 170 | struct cgroup_subsys_state css; |
| 171 | #endif |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 172 | |
| 173 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 174 | /* schedulable entities of this group on each cpu */ |
| 175 | struct sched_entity **se; |
| 176 | /* runqueue "owned" by this group on each cpu */ |
| 177 | struct cfs_rq **cfs_rq; |
| 178 | unsigned long shares; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 179 | #endif |
| 180 | |
| 181 | #ifdef CONFIG_RT_GROUP_SCHED |
| 182 | struct sched_rt_entity **rt_se; |
| 183 | struct rt_rq **rt_rq; |
| 184 | |
| 185 | u64 rt_runtime; |
| 186 | #endif |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 187 | |
Srivatsa Vaddagiri | ae8393e | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 188 | struct rcu_head rcu; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 189 | struct list_head list; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 190 | }; |
| 191 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 192 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 193 | /* Default task group's sched entity on each cpu */ |
| 194 | static DEFINE_PER_CPU(struct sched_entity, init_sched_entity); |
| 195 | /* Default task group's cfs_rq on each cpu */ |
| 196 | static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp; |
| 197 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 198 | static struct sched_entity *init_sched_entity_p[NR_CPUS]; |
| 199 | static struct cfs_rq *init_cfs_rq_p[NR_CPUS]; |
| 200 | #endif |
| 201 | |
| 202 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 203 | static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity); |
| 204 | static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp; |
| 205 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 206 | static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS]; |
| 207 | static struct rt_rq *init_rt_rq_p[NR_CPUS]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 208 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 209 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 210 | /* 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] | 211 | * a task group's cpu shares. |
| 212 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 213 | static DEFINE_SPINLOCK(task_group_lock); |
Srivatsa Vaddagiri | ec2c507 | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 214 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 215 | /* doms_cur_mutex serializes access to doms_cur[] array */ |
| 216 | static DEFINE_MUTEX(doms_cur_mutex); |
| 217 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 218 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 219 | #ifdef CONFIG_USER_SCHED |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 220 | # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD) |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 221 | #else |
Srivatsa Vaddagiri | 93f992c | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 222 | # define INIT_TASK_GROUP_LOAD NICE_0_LOAD |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 223 | #endif |
| 224 | |
Srivatsa Vaddagiri | 93f992c | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 225 | static int init_task_group_load = INIT_TASK_GROUP_LOAD; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 226 | #endif |
| 227 | |
| 228 | /* Default task group. |
| 229 | * Every task in system belong to this group at bootup. |
| 230 | */ |
| 231 | struct task_group init_task_group = { |
| 232 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 233 | .se = init_sched_entity_p, |
| 234 | .cfs_rq = init_cfs_rq_p, |
| 235 | #endif |
| 236 | |
| 237 | #ifdef CONFIG_RT_GROUP_SCHED |
| 238 | .rt_se = init_sched_rt_entity_p, |
| 239 | .rt_rq = init_rt_rq_p, |
| 240 | #endif |
| 241 | }; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 242 | |
| 243 | /* return group to which a task belongs */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 244 | static inline struct task_group *task_group(struct task_struct *p) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 245 | { |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 246 | struct task_group *tg; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 247 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 248 | #ifdef CONFIG_USER_SCHED |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 249 | tg = p->user->tg; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 250 | #elif defined(CONFIG_CGROUP_SCHED) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 251 | tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id), |
| 252 | struct task_group, css); |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 253 | #else |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 254 | tg = &init_task_group; |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 255 | #endif |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 256 | return tg; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* 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] | 260 | 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] | 261 | { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 262 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Dmitry Adamushko | ce96b5a | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 263 | p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; |
| 264 | p->se.parent = task_group(p)->se[cpu]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 265 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 266 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 267 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 268 | p->rt.rt_rq = task_group(p)->rt_rq[cpu]; |
| 269 | p->rt.parent = task_group(p)->rt_se[cpu]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 270 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 273 | static inline void lock_doms_cur(void) |
| 274 | { |
| 275 | mutex_lock(&doms_cur_mutex); |
| 276 | } |
| 277 | |
| 278 | static inline void unlock_doms_cur(void) |
| 279 | { |
| 280 | mutex_unlock(&doms_cur_mutex); |
| 281 | } |
| 282 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 283 | #else |
| 284 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 285 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 286 | static inline void lock_doms_cur(void) { } |
| 287 | static inline void unlock_doms_cur(void) { } |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 288 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 289 | #endif /* CONFIG_GROUP_SCHED */ |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 290 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 291 | /* CFS-related fields in a runqueue */ |
| 292 | struct cfs_rq { |
| 293 | struct load_weight load; |
| 294 | unsigned long nr_running; |
| 295 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 296 | u64 exec_clock; |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 297 | u64 min_vruntime; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 298 | |
| 299 | struct rb_root tasks_timeline; |
| 300 | struct rb_node *rb_leftmost; |
| 301 | struct rb_node *rb_load_balance_curr; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 302 | /* 'curr' points to currently running entity on this cfs_rq. |
| 303 | * It is set to NULL otherwise (i.e when none are currently running). |
| 304 | */ |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 305 | struct sched_entity *curr, *next; |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 306 | |
| 307 | unsigned long nr_spread_over; |
| 308 | |
Ingo Molnar | 62160e3 | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 309 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 310 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ |
| 311 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 312 | /* |
| 313 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 314 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities |
| 315 | * (like users, containers etc.) |
| 316 | * |
| 317 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This |
| 318 | * list is used during load balance. |
| 319 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 320 | struct list_head leaf_cfs_rq_list; |
| 321 | struct task_group *tg; /* group that "owns" this runqueue */ |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 322 | #endif |
| 323 | }; |
| 324 | |
| 325 | /* Real-Time classes' related field in a runqueue: */ |
| 326 | struct rt_rq { |
| 327 | struct rt_prio_array active; |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 328 | unsigned long rt_nr_running; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 329 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 330 | int highest_prio; /* highest queued rt task prio */ |
| 331 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 332 | #ifdef CONFIG_SMP |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 333 | unsigned long rt_nr_migratory; |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 334 | int overloaded; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 335 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 336 | int rt_throttled; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 337 | u64 rt_time; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 338 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 339 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 340 | unsigned long rt_nr_boosted; |
| 341 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 342 | struct rq *rq; |
| 343 | struct list_head leaf_rt_rq_list; |
| 344 | struct task_group *tg; |
| 345 | struct sched_rt_entity *rt_se; |
| 346 | #endif |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 347 | }; |
| 348 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 349 | #ifdef CONFIG_SMP |
| 350 | |
| 351 | /* |
| 352 | * 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] | 353 | * variables. Each exclusive cpuset essentially defines an island domain by |
| 354 | * fully partitioning the member cpus from any other cpuset. Whenever a new |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 355 | * exclusive cpuset is created, we also create and attach a new root-domain |
| 356 | * object. |
| 357 | * |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 358 | */ |
| 359 | struct root_domain { |
| 360 | atomic_t refcount; |
| 361 | cpumask_t span; |
| 362 | cpumask_t online; |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 363 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 364 | /* |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 365 | * The "RT overload" flag: it gets set if a CPU has more than |
| 366 | * one runnable RT task. |
| 367 | */ |
| 368 | cpumask_t rto_mask; |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 369 | atomic_t rto_count; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 370 | }; |
| 371 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 372 | /* |
| 373 | * By default the system creates a single root-domain with all cpus as |
| 374 | * members (mimicking the global state we have today). |
| 375 | */ |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 376 | static struct root_domain def_root_domain; |
| 377 | |
| 378 | #endif |
| 379 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 380 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | * This is the main, per-CPU runqueue data structure. |
| 382 | * |
| 383 | * Locking rule: those places that want to lock multiple runqueues |
| 384 | * (such as the load balancing or the thread migration code), lock |
| 385 | * acquire operations must be ordered by ascending &runqueue. |
| 386 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 387 | struct rq { |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 388 | /* runqueue lock: */ |
| 389 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | /* |
| 392 | * nr_running and cpu_load should be in the same cacheline because |
| 393 | * remote CPUs use both these fields when doing load calculation. |
| 394 | */ |
| 395 | unsigned long nr_running; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 396 | #define CPU_LOAD_IDX_MAX 5 |
| 397 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; |
Siddha, Suresh B | bdecea3 | 2007-05-08 00:32:48 -0700 | [diff] [blame] | 398 | unsigned char idle_at_tick; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 399 | #ifdef CONFIG_NO_HZ |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 400 | unsigned long last_tick_seen; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 401 | unsigned char in_nohz_recently; |
| 402 | #endif |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 403 | /* capture load from *all* tasks on this cpu: */ |
| 404 | struct load_weight load; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 405 | unsigned long nr_load_updates; |
| 406 | u64 nr_switches; |
| 407 | |
| 408 | struct cfs_rq cfs; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 409 | struct rt_rq rt; |
| 410 | u64 rt_period_expire; |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 411 | int rt_throttled; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 412 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 413 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 414 | /* list of leaf cfs_rq on this cpu: */ |
| 415 | struct list_head leaf_cfs_rq_list; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 416 | #endif |
| 417 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 418 | struct list_head leaf_rt_rq_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * This is part of a global counter where only the total sum |
| 423 | * over all CPUs matters. A task can increase this counter on |
| 424 | * one CPU and if it got migrated afterwards it may decrease |
| 425 | * it on another CPU. Always updated under the runqueue lock: |
| 426 | */ |
| 427 | unsigned long nr_uninterruptible; |
| 428 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 429 | struct task_struct *curr, *idle; |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 430 | unsigned long next_balance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | struct mm_struct *prev_mm; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 432 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 433 | u64 clock, prev_clock_raw; |
| 434 | s64 clock_max_delta; |
| 435 | |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 436 | unsigned int clock_warps, clock_overflows, clock_underflows; |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 437 | u64 idle_clock; |
| 438 | unsigned int clock_deep_idle_events; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 439 | u64 tick_timestamp; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | atomic_t nr_iowait; |
| 442 | |
| 443 | #ifdef CONFIG_SMP |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 444 | struct root_domain *rd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | struct sched_domain *sd; |
| 446 | |
| 447 | /* For active balancing */ |
| 448 | int active_balance; |
| 449 | int push_cpu; |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 450 | /* cpu of this runqueue: */ |
| 451 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 453 | struct task_struct *migration_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | struct list_head migration_queue; |
| 455 | #endif |
| 456 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 457 | #ifdef CONFIG_SCHED_HRTICK |
| 458 | unsigned long hrtick_flags; |
| 459 | ktime_t hrtick_expire; |
| 460 | struct hrtimer hrtick_timer; |
| 461 | #endif |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | #ifdef CONFIG_SCHEDSTATS |
| 464 | /* latency stats */ |
| 465 | struct sched_info rq_sched_info; |
| 466 | |
| 467 | /* sys_sched_yield() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 468 | unsigned int yld_exp_empty; |
| 469 | unsigned int yld_act_empty; |
| 470 | unsigned int yld_both_empty; |
| 471 | unsigned int yld_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | /* schedule() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 474 | unsigned int sched_switch; |
| 475 | unsigned int sched_count; |
| 476 | unsigned int sched_goidle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | /* try_to_wake_up() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 479 | unsigned int ttwu_count; |
| 480 | unsigned int ttwu_local; |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 481 | |
| 482 | /* BKL stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 483 | unsigned int bkl_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | #endif |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 485 | struct lock_class_key rq_lock_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | }; |
| 487 | |
Fenghua Yu | f34e3b6 | 2007-07-19 01:48:13 -0700 | [diff] [blame] | 488 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 490 | static inline void check_preempt_curr(struct rq *rq, struct task_struct *p) |
| 491 | { |
| 492 | rq->curr->sched_class->check_preempt_curr(rq, p); |
| 493 | } |
| 494 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 495 | static inline int cpu_of(struct rq *rq) |
| 496 | { |
| 497 | #ifdef CONFIG_SMP |
| 498 | return rq->cpu; |
| 499 | #else |
| 500 | return 0; |
| 501 | #endif |
| 502 | } |
| 503 | |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 504 | #ifdef CONFIG_NO_HZ |
| 505 | static inline bool nohz_on(int cpu) |
| 506 | { |
| 507 | return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE; |
| 508 | } |
| 509 | |
| 510 | static inline u64 max_skipped_ticks(struct rq *rq) |
| 511 | { |
| 512 | return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1; |
| 513 | } |
| 514 | |
| 515 | static inline void update_last_tick_seen(struct rq *rq) |
| 516 | { |
| 517 | rq->last_tick_seen = jiffies; |
| 518 | } |
| 519 | #else |
| 520 | static inline u64 max_skipped_ticks(struct rq *rq) |
| 521 | { |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | static inline void update_last_tick_seen(struct rq *rq) |
| 526 | { |
| 527 | } |
| 528 | #endif |
| 529 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 530 | /* |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 531 | * Update the per-runqueue clock, as finegrained as the platform can give |
| 532 | * us, but without assuming monotonicity, etc.: |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 533 | */ |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 534 | static void __update_rq_clock(struct rq *rq) |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 535 | { |
| 536 | u64 prev_raw = rq->prev_clock_raw; |
| 537 | u64 now = sched_clock(); |
| 538 | s64 delta = now - prev_raw; |
| 539 | u64 clock = rq->clock; |
| 540 | |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 541 | #ifdef CONFIG_SCHED_DEBUG |
| 542 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 543 | #endif |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 544 | /* |
| 545 | * Protect against sched_clock() occasionally going backwards: |
| 546 | */ |
| 547 | if (unlikely(delta < 0)) { |
| 548 | clock++; |
| 549 | rq->clock_warps++; |
| 550 | } else { |
| 551 | /* |
| 552 | * Catch too large forward jumps too: |
| 553 | */ |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 554 | u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC; |
| 555 | u64 max_time = rq->tick_timestamp + max_jump; |
| 556 | |
| 557 | if (unlikely(clock + delta > max_time)) { |
| 558 | if (clock < max_time) |
| 559 | clock = max_time; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 560 | else |
| 561 | clock++; |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 562 | rq->clock_overflows++; |
| 563 | } else { |
| 564 | if (unlikely(delta > rq->clock_max_delta)) |
| 565 | rq->clock_max_delta = delta; |
| 566 | clock += delta; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | rq->prev_clock_raw = now; |
| 571 | rq->clock = clock; |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 572 | } |
| 573 | |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 574 | static void update_rq_clock(struct rq *rq) |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 575 | { |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 576 | if (likely(smp_processor_id() == cpu_of(rq))) |
| 577 | __update_rq_clock(rq); |
| 578 | } |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 579 | |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 580 | /* |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 581 | * 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] | 582 | * See detach_destroy_domains: synchronize_sched for details. |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 583 | * |
| 584 | * The domain tree of any CPU may only be accessed from within |
| 585 | * preempt-disabled sections. |
| 586 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 587 | #define for_each_domain(cpu, __sd) \ |
| 588 | for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
| 591 | #define this_rq() (&__get_cpu_var(runqueues)) |
| 592 | #define task_rq(p) cpu_rq(task_cpu(p)) |
| 593 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) |
| 594 | |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 595 | unsigned long rt_needs_cpu(int cpu) |
| 596 | { |
| 597 | struct rq *rq = cpu_rq(cpu); |
| 598 | u64 delta; |
| 599 | |
| 600 | if (!rq->rt_throttled) |
| 601 | return 0; |
| 602 | |
| 603 | if (rq->clock > rq->rt_period_expire) |
| 604 | return 1; |
| 605 | |
| 606 | delta = rq->rt_period_expire - rq->clock; |
| 607 | do_div(delta, NSEC_PER_SEC / HZ); |
| 608 | |
| 609 | return (unsigned long)delta; |
| 610 | } |
| 611 | |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 612 | /* |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 613 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: |
| 614 | */ |
| 615 | #ifdef CONFIG_SCHED_DEBUG |
| 616 | # define const_debug __read_mostly |
| 617 | #else |
| 618 | # define const_debug static const |
| 619 | #endif |
| 620 | |
| 621 | /* |
| 622 | * Debugging: various feature bits |
| 623 | */ |
| 624 | enum { |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 625 | SCHED_FEAT_NEW_FAIR_SLEEPERS = 1, |
Ingo Molnar | 9612633 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 626 | SCHED_FEAT_WAKEUP_PREEMPT = 2, |
| 627 | SCHED_FEAT_START_DEBIT = 4, |
Ingo Molnar | d25ce4c | 2008-03-17 09:36:53 +0100 | [diff] [blame^] | 628 | SCHED_FEAT_AFFINE_WAKEUPS = 8, |
| 629 | SCHED_FEAT_CACHE_HOT_BUDDY = 16, |
| 630 | SCHED_FEAT_HRTICK = 32, |
| 631 | SCHED_FEAT_DOUBLE_TICK = 64, |
| 632 | SCHED_FEAT_SYNC_WAKEUPS = 128, |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 633 | }; |
| 634 | |
| 635 | const_debug unsigned int sysctl_sched_features = |
Ingo Molnar | 8401f77 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 636 | SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 | |
Ingo Molnar | 9612633 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 637 | SCHED_FEAT_WAKEUP_PREEMPT * 1 | |
Ingo Molnar | 8401f77 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 638 | SCHED_FEAT_START_DEBIT * 1 | |
Ingo Molnar | d25ce4c | 2008-03-17 09:36:53 +0100 | [diff] [blame^] | 639 | SCHED_FEAT_AFFINE_WAKEUPS * 1 | |
| 640 | SCHED_FEAT_CACHE_HOT_BUDDY * 1 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 641 | SCHED_FEAT_HRTICK * 1 | |
Ingo Molnar | b85d066 | 2008-03-16 20:03:22 +0100 | [diff] [blame] | 642 | SCHED_FEAT_DOUBLE_TICK * 0 | |
Ingo Molnar | d25ce4c | 2008-03-17 09:36:53 +0100 | [diff] [blame^] | 643 | SCHED_FEAT_SYNC_WAKEUPS * 0; |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 644 | |
| 645 | #define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x) |
| 646 | |
| 647 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 648 | * Number of tasks to iterate in a single balance run. |
| 649 | * Limited because this is done with IRQs disabled. |
| 650 | */ |
| 651 | const_debug unsigned int sysctl_sched_nr_migrate = 32; |
| 652 | |
| 653 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 654 | * period over which we measure -rt task cpu usage in us. |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 655 | * default: 1s |
| 656 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 657 | unsigned int sysctl_sched_rt_period = 1000000; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 658 | |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 659 | static __read_mostly int scheduler_running; |
| 660 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 661 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 662 | * part of the period that we allow rt tasks to run in us. |
| 663 | * default: 0.95s |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 664 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 665 | int sysctl_sched_rt_runtime = 950000; |
| 666 | |
| 667 | /* |
| 668 | * single value that denotes runtime == period, ie unlimited time. |
| 669 | */ |
| 670 | #define RUNTIME_INF ((u64)~0ULL) |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 671 | |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 672 | static const unsigned long long time_sync_thresh = 100000; |
| 673 | |
| 674 | static DEFINE_PER_CPU(unsigned long long, time_offset); |
| 675 | static DEFINE_PER_CPU(unsigned long long, prev_cpu_time); |
| 676 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 677 | /* |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 678 | * Global lock which we take every now and then to synchronize |
| 679 | * the CPUs time. This method is not warp-safe, but it's good |
| 680 | * enough to synchronize slowly diverging time sources and thus |
| 681 | * it's good enough for tracing: |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 682 | */ |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 683 | static DEFINE_SPINLOCK(time_sync_lock); |
| 684 | static unsigned long long prev_global_time; |
| 685 | |
| 686 | static unsigned long long __sync_cpu_clock(cycles_t time, int cpu) |
| 687 | { |
| 688 | unsigned long flags; |
| 689 | |
| 690 | spin_lock_irqsave(&time_sync_lock, flags); |
| 691 | |
| 692 | if (time < prev_global_time) { |
| 693 | per_cpu(time_offset, cpu) += prev_global_time - time; |
| 694 | time = prev_global_time; |
| 695 | } else { |
| 696 | prev_global_time = time; |
| 697 | } |
| 698 | |
| 699 | spin_unlock_irqrestore(&time_sync_lock, flags); |
| 700 | |
| 701 | return time; |
| 702 | } |
| 703 | |
| 704 | static unsigned long long __cpu_clock(int cpu) |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 705 | { |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 706 | unsigned long long now; |
| 707 | unsigned long flags; |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 708 | struct rq *rq; |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 709 | |
Ingo Molnar | 8ced5f6 | 2007-12-07 19:02:47 +0100 | [diff] [blame] | 710 | /* |
| 711 | * Only call sched_clock() if the scheduler has already been |
| 712 | * initialized (some code might call cpu_clock() very early): |
| 713 | */ |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 714 | if (unlikely(!scheduler_running)) |
| 715 | return 0; |
| 716 | |
| 717 | local_irq_save(flags); |
| 718 | rq = cpu_rq(cpu); |
| 719 | update_rq_clock(rq); |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 720 | now = rq->clock; |
Ingo Molnar | 2cd4d0e | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 721 | local_irq_restore(flags); |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 722 | |
| 723 | return now; |
| 724 | } |
Ingo Molnar | 27ec440 | 2008-02-28 21:00:21 +0100 | [diff] [blame] | 725 | |
| 726 | /* |
| 727 | * For kernel-internal use: high-speed (but slightly incorrect) per-cpu |
| 728 | * clock constructed from sched_clock(): |
| 729 | */ |
| 730 | unsigned long long cpu_clock(int cpu) |
| 731 | { |
| 732 | unsigned long long prev_cpu_time, time, delta_time; |
| 733 | |
| 734 | prev_cpu_time = per_cpu(prev_cpu_time, cpu); |
| 735 | time = __cpu_clock(cpu) + per_cpu(time_offset, cpu); |
| 736 | delta_time = time-prev_cpu_time; |
| 737 | |
| 738 | if (unlikely(delta_time > time_sync_thresh)) |
| 739 | time = __sync_cpu_clock(time, cpu); |
| 740 | |
| 741 | return time; |
| 742 | } |
Paul E. McKenney | a58f6f2 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 743 | EXPORT_SYMBOL_GPL(cpu_clock); |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | #ifndef prepare_arch_switch |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 746 | # define prepare_arch_switch(next) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | #endif |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 748 | #ifndef finish_arch_switch |
| 749 | # define finish_arch_switch(prev) do { } while (0) |
| 750 | #endif |
| 751 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 752 | static inline int task_current(struct rq *rq, struct task_struct *p) |
| 753 | { |
| 754 | return rq->curr == p; |
| 755 | } |
| 756 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 757 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 758 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 759 | { |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 760 | return task_current(rq, p); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 763 | 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] | 764 | { |
| 765 | } |
| 766 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 767 | 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] | 768 | { |
Ingo Molnar | da04c03 | 2005-09-13 11:17:59 +0200 | [diff] [blame] | 769 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 770 | /* this is a valid case when another task releases the spinlock */ |
| 771 | rq->lock.owner = current; |
| 772 | #endif |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 773 | /* |
| 774 | * If we are tracking spinlock dependencies then we have to |
| 775 | * fix up the runqueue lock - which gets 'carried over' from |
| 776 | * prev into current: |
| 777 | */ |
| 778 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); |
| 779 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 780 | spin_unlock_irq(&rq->lock); |
| 781 | } |
| 782 | |
| 783 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 784 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 785 | { |
| 786 | #ifdef CONFIG_SMP |
| 787 | return p->oncpu; |
| 788 | #else |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 789 | return task_current(rq, p); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 790 | #endif |
| 791 | } |
| 792 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 793 | 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] | 794 | { |
| 795 | #ifdef CONFIG_SMP |
| 796 | /* |
| 797 | * We can optimise this out completely for !SMP, because the |
| 798 | * SMP rebalancing from interrupt is the only thing that cares |
| 799 | * here. |
| 800 | */ |
| 801 | next->oncpu = 1; |
| 802 | #endif |
| 803 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 804 | spin_unlock_irq(&rq->lock); |
| 805 | #else |
| 806 | spin_unlock(&rq->lock); |
| 807 | #endif |
| 808 | } |
| 809 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 810 | 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] | 811 | { |
| 812 | #ifdef CONFIG_SMP |
| 813 | /* |
| 814 | * After ->oncpu is cleared, the task can be moved to a different CPU. |
| 815 | * We must ensure this doesn't happen until the switch is completely |
| 816 | * finished. |
| 817 | */ |
| 818 | smp_wmb(); |
| 819 | prev->oncpu = 0; |
| 820 | #endif |
| 821 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 822 | local_irq_enable(); |
| 823 | #endif |
| 824 | } |
| 825 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
| 827 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 828 | * __task_rq_lock - lock the runqueue a given task resides on. |
| 829 | * Must be called interrupts disabled. |
| 830 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 831 | static inline struct rq *__task_rq_lock(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 832 | __acquires(rq->lock) |
| 833 | { |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 834 | for (;;) { |
| 835 | struct rq *rq = task_rq(p); |
| 836 | spin_lock(&rq->lock); |
| 837 | if (likely(rq == task_rq(p))) |
| 838 | return rq; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 839 | spin_unlock(&rq->lock); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 840 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | * 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] | 845 | * interrupts. Note the ordering: we can safely lookup the task_rq without |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | * explicitly disabling preemption. |
| 847 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 848 | 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] | 849 | __acquires(rq->lock) |
| 850 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 851 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 853 | for (;;) { |
| 854 | local_irq_save(*flags); |
| 855 | rq = task_rq(p); |
| 856 | spin_lock(&rq->lock); |
| 857 | if (likely(rq == task_rq(p))) |
| 858 | return rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | spin_unlock_irqrestore(&rq->lock, *flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 863 | static void __task_rq_unlock(struct rq *rq) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 864 | __releases(rq->lock) |
| 865 | { |
| 866 | spin_unlock(&rq->lock); |
| 867 | } |
| 868 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 869 | static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | __releases(rq->lock) |
| 871 | { |
| 872 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 873 | } |
| 874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | /* |
Robert P. J. Day | cc2a73b | 2006-12-10 02:20:00 -0800 | [diff] [blame] | 876 | * this_rq_lock - lock this runqueue and disable interrupts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 878 | static struct rq *this_rq_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | __acquires(rq->lock) |
| 880 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 881 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
| 883 | local_irq_disable(); |
| 884 | rq = this_rq(); |
| 885 | spin_lock(&rq->lock); |
| 886 | |
| 887 | return rq; |
| 888 | } |
| 889 | |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 890 | /* |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 891 | * We are going deep-idle (irqs are disabled): |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 892 | */ |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 893 | void sched_clock_idle_sleep_event(void) |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 894 | { |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 895 | struct rq *rq = cpu_rq(smp_processor_id()); |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 896 | |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 897 | spin_lock(&rq->lock); |
| 898 | __update_rq_clock(rq); |
| 899 | spin_unlock(&rq->lock); |
| 900 | rq->clock_deep_idle_events++; |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 901 | } |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 902 | EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event); |
| 903 | |
| 904 | /* |
| 905 | * We just idled delta nanoseconds (called with irqs disabled): |
| 906 | */ |
| 907 | void sched_clock_idle_wakeup_event(u64 delta_ns) |
| 908 | { |
| 909 | struct rq *rq = cpu_rq(smp_processor_id()); |
| 910 | u64 now = sched_clock(); |
| 911 | |
| 912 | rq->idle_clock += delta_ns; |
| 913 | /* |
| 914 | * Override the previous timestamp and ignore all |
| 915 | * sched_clock() deltas that occured while we idled, |
| 916 | * and use the PM-provided delta_ns to advance the |
| 917 | * rq clock: |
| 918 | */ |
| 919 | spin_lock(&rq->lock); |
| 920 | rq->prev_clock_raw = now; |
| 921 | rq->clock += delta_ns; |
| 922 | spin_unlock(&rq->lock); |
Guillaume Chazarain | 782daee | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 923 | touch_softlockup_watchdog(); |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 924 | } |
| 925 | EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event); |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 926 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 927 | static void __resched_task(struct task_struct *p, int tif_bit); |
| 928 | |
| 929 | static inline void resched_task(struct task_struct *p) |
| 930 | { |
| 931 | __resched_task(p, TIF_NEED_RESCHED); |
| 932 | } |
| 933 | |
| 934 | #ifdef CONFIG_SCHED_HRTICK |
| 935 | /* |
| 936 | * Use HR-timers to deliver accurate preemption points. |
| 937 | * |
| 938 | * Its all a bit involved since we cannot program an hrt while holding the |
| 939 | * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a |
| 940 | * reschedule event. |
| 941 | * |
| 942 | * When we get rescheduled we reprogram the hrtick_timer outside of the |
| 943 | * rq->lock. |
| 944 | */ |
| 945 | static inline void resched_hrt(struct task_struct *p) |
| 946 | { |
| 947 | __resched_task(p, TIF_HRTICK_RESCHED); |
| 948 | } |
| 949 | |
| 950 | static inline void resched_rq(struct rq *rq) |
| 951 | { |
| 952 | unsigned long flags; |
| 953 | |
| 954 | spin_lock_irqsave(&rq->lock, flags); |
| 955 | resched_task(rq->curr); |
| 956 | spin_unlock_irqrestore(&rq->lock, flags); |
| 957 | } |
| 958 | |
| 959 | enum { |
| 960 | HRTICK_SET, /* re-programm hrtick_timer */ |
| 961 | HRTICK_RESET, /* not a new slice */ |
| 962 | }; |
| 963 | |
| 964 | /* |
| 965 | * Use hrtick when: |
| 966 | * - enabled by features |
| 967 | * - hrtimer is actually high res |
| 968 | */ |
| 969 | static inline int hrtick_enabled(struct rq *rq) |
| 970 | { |
| 971 | if (!sched_feat(HRTICK)) |
| 972 | return 0; |
| 973 | return hrtimer_is_hres_active(&rq->hrtick_timer); |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * Called to set the hrtick timer state. |
| 978 | * |
| 979 | * called with rq->lock held and irqs disabled |
| 980 | */ |
| 981 | static void hrtick_start(struct rq *rq, u64 delay, int reset) |
| 982 | { |
| 983 | assert_spin_locked(&rq->lock); |
| 984 | |
| 985 | /* |
| 986 | * preempt at: now + delay |
| 987 | */ |
| 988 | rq->hrtick_expire = |
| 989 | ktime_add_ns(rq->hrtick_timer.base->get_time(), delay); |
| 990 | /* |
| 991 | * indicate we need to program the timer |
| 992 | */ |
| 993 | __set_bit(HRTICK_SET, &rq->hrtick_flags); |
| 994 | if (reset) |
| 995 | __set_bit(HRTICK_RESET, &rq->hrtick_flags); |
| 996 | |
| 997 | /* |
| 998 | * New slices are called from the schedule path and don't need a |
| 999 | * forced reschedule. |
| 1000 | */ |
| 1001 | if (reset) |
| 1002 | resched_hrt(rq->curr); |
| 1003 | } |
| 1004 | |
| 1005 | static void hrtick_clear(struct rq *rq) |
| 1006 | { |
| 1007 | if (hrtimer_active(&rq->hrtick_timer)) |
| 1008 | hrtimer_cancel(&rq->hrtick_timer); |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * Update the timer from the possible pending state. |
| 1013 | */ |
| 1014 | static void hrtick_set(struct rq *rq) |
| 1015 | { |
| 1016 | ktime_t time; |
| 1017 | int set, reset; |
| 1018 | unsigned long flags; |
| 1019 | |
| 1020 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 1021 | |
| 1022 | spin_lock_irqsave(&rq->lock, flags); |
| 1023 | set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags); |
| 1024 | reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags); |
| 1025 | time = rq->hrtick_expire; |
| 1026 | clear_thread_flag(TIF_HRTICK_RESCHED); |
| 1027 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1028 | |
| 1029 | if (set) { |
| 1030 | hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS); |
| 1031 | if (reset && !hrtimer_active(&rq->hrtick_timer)) |
| 1032 | resched_rq(rq); |
| 1033 | } else |
| 1034 | hrtick_clear(rq); |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * High-resolution timer tick. |
| 1039 | * Runs from hardirq context with interrupts disabled. |
| 1040 | */ |
| 1041 | static enum hrtimer_restart hrtick(struct hrtimer *timer) |
| 1042 | { |
| 1043 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); |
| 1044 | |
| 1045 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 1046 | |
| 1047 | spin_lock(&rq->lock); |
| 1048 | __update_rq_clock(rq); |
| 1049 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
| 1050 | spin_unlock(&rq->lock); |
| 1051 | |
| 1052 | return HRTIMER_NORESTART; |
| 1053 | } |
| 1054 | |
| 1055 | static inline void init_rq_hrtick(struct rq *rq) |
| 1056 | { |
| 1057 | rq->hrtick_flags = 0; |
| 1058 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1059 | rq->hrtick_timer.function = hrtick; |
| 1060 | rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; |
| 1061 | } |
| 1062 | |
| 1063 | void hrtick_resched(void) |
| 1064 | { |
| 1065 | struct rq *rq; |
| 1066 | unsigned long flags; |
| 1067 | |
| 1068 | if (!test_thread_flag(TIF_HRTICK_RESCHED)) |
| 1069 | return; |
| 1070 | |
| 1071 | local_irq_save(flags); |
| 1072 | rq = cpu_rq(smp_processor_id()); |
| 1073 | hrtick_set(rq); |
| 1074 | local_irq_restore(flags); |
| 1075 | } |
| 1076 | #else |
| 1077 | static inline void hrtick_clear(struct rq *rq) |
| 1078 | { |
| 1079 | } |
| 1080 | |
| 1081 | static inline void hrtick_set(struct rq *rq) |
| 1082 | { |
| 1083 | } |
| 1084 | |
| 1085 | static inline void init_rq_hrtick(struct rq *rq) |
| 1086 | { |
| 1087 | } |
| 1088 | |
| 1089 | void hrtick_resched(void) |
| 1090 | { |
| 1091 | } |
| 1092 | #endif |
| 1093 | |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1094 | /* |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1095 | * resched_task - mark a task 'to be rescheduled now'. |
| 1096 | * |
| 1097 | * On UP this means the setting of the need_resched flag, on SMP it |
| 1098 | * might also involve a cross-CPU call to trigger the scheduler on |
| 1099 | * the target CPU. |
| 1100 | */ |
| 1101 | #ifdef CONFIG_SMP |
| 1102 | |
| 1103 | #ifndef tsk_is_polling |
| 1104 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) |
| 1105 | #endif |
| 1106 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1107 | static void __resched_task(struct task_struct *p, int tif_bit) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1108 | { |
| 1109 | int cpu; |
| 1110 | |
| 1111 | assert_spin_locked(&task_rq(p)->lock); |
| 1112 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1113 | if (unlikely(test_tsk_thread_flag(p, tif_bit))) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1114 | return; |
| 1115 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1116 | set_tsk_thread_flag(p, tif_bit); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1117 | |
| 1118 | cpu = task_cpu(p); |
| 1119 | if (cpu == smp_processor_id()) |
| 1120 | return; |
| 1121 | |
| 1122 | /* NEED_RESCHED must be visible before we test polling */ |
| 1123 | smp_mb(); |
| 1124 | if (!tsk_is_polling(p)) |
| 1125 | smp_send_reschedule(cpu); |
| 1126 | } |
| 1127 | |
| 1128 | static void resched_cpu(int cpu) |
| 1129 | { |
| 1130 | struct rq *rq = cpu_rq(cpu); |
| 1131 | unsigned long flags; |
| 1132 | |
| 1133 | if (!spin_trylock_irqsave(&rq->lock, flags)) |
| 1134 | return; |
| 1135 | resched_task(cpu_curr(cpu)); |
| 1136 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1137 | } |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 1138 | |
| 1139 | #ifdef CONFIG_NO_HZ |
| 1140 | /* |
| 1141 | * When add_timer_on() enqueues a timer into the timer wheel of an |
| 1142 | * idle CPU then this timer might expire before the next timer event |
| 1143 | * which is scheduled to wake up that CPU. In case of a completely |
| 1144 | * idle system the next event might even be infinite time into the |
| 1145 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and |
| 1146 | * leaves the inner idle loop so the newly added timer is taken into |
| 1147 | * account when the CPU goes back to idle and evaluates the timer |
| 1148 | * wheel for the next timer event. |
| 1149 | */ |
| 1150 | void wake_up_idle_cpu(int cpu) |
| 1151 | { |
| 1152 | struct rq *rq = cpu_rq(cpu); |
| 1153 | |
| 1154 | if (cpu == smp_processor_id()) |
| 1155 | return; |
| 1156 | |
| 1157 | /* |
| 1158 | * This is safe, as this function is called with the timer |
| 1159 | * wheel base lock of (cpu) held. When the CPU is on the way |
| 1160 | * to idle and has not yet set rq->curr to idle then it will |
| 1161 | * be serialized on the timer wheel base lock and take the new |
| 1162 | * timer into account automatically. |
| 1163 | */ |
| 1164 | if (rq->curr != rq->idle) |
| 1165 | return; |
| 1166 | |
| 1167 | /* |
| 1168 | * We can set TIF_RESCHED on the idle task of the other CPU |
| 1169 | * lockless. The worst case is that the other CPU runs the |
| 1170 | * idle task through an additional NOOP schedule() |
| 1171 | */ |
| 1172 | set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED); |
| 1173 | |
| 1174 | /* NEED_RESCHED must be visible before we test polling */ |
| 1175 | smp_mb(); |
| 1176 | if (!tsk_is_polling(rq->idle)) |
| 1177 | smp_send_reschedule(cpu); |
| 1178 | } |
| 1179 | #endif |
| 1180 | |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1181 | #else |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1182 | static void __resched_task(struct task_struct *p, int tif_bit) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1183 | { |
| 1184 | assert_spin_locked(&task_rq(p)->lock); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1185 | set_tsk_thread_flag(p, tif_bit); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1186 | } |
| 1187 | #endif |
| 1188 | |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1189 | #if BITS_PER_LONG == 32 |
| 1190 | # define WMULT_CONST (~0UL) |
| 1191 | #else |
| 1192 | # define WMULT_CONST (1UL << 32) |
| 1193 | #endif |
| 1194 | |
| 1195 | #define WMULT_SHIFT 32 |
| 1196 | |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1197 | /* |
| 1198 | * Shift right and round: |
| 1199 | */ |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1200 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1201 | |
Ingo Molnar | cb1c4fc | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1202 | static unsigned long |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1203 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, |
| 1204 | struct load_weight *lw) |
| 1205 | { |
| 1206 | u64 tmp; |
| 1207 | |
| 1208 | if (unlikely(!lw->inv_weight)) |
Ingo Molnar | 27d1172 | 2008-03-14 22:20:01 +0100 | [diff] [blame] | 1209 | lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1210 | |
| 1211 | tmp = (u64)delta_exec * weight; |
| 1212 | /* |
| 1213 | * Check whether we'd overflow the 64-bit multiplication: |
| 1214 | */ |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1215 | if (unlikely(tmp > WMULT_CONST)) |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1216 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1217 | WMULT_SHIFT/2); |
| 1218 | else |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1219 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1220 | |
Ingo Molnar | ecf691d | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1221 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | static inline unsigned long |
| 1225 | calc_delta_fair(unsigned long delta_exec, struct load_weight *lw) |
| 1226 | { |
| 1227 | return calc_delta_mine(delta_exec, NICE_0_LOAD, lw); |
| 1228 | } |
| 1229 | |
Ingo Molnar | 1091985 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1230 | 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] | 1231 | { |
| 1232 | lw->weight += inc; |
Ingo Molnar | e89996a | 2008-03-14 23:48:28 +0100 | [diff] [blame] | 1233 | lw->inv_weight = 0; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1234 | } |
| 1235 | |
Ingo Molnar | 1091985 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1236 | 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] | 1237 | { |
| 1238 | lw->weight -= dec; |
Ingo Molnar | e89996a | 2008-03-14 23:48:28 +0100 | [diff] [blame] | 1239 | lw->inv_weight = 0; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1243 | * To aid in avoiding the subversion of "niceness" due to uneven distribution |
| 1244 | * of tasks with abnormal "nice" values across CPUs the contribution that |
| 1245 | * 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] | 1246 | * 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] | 1247 | * scaled version of the new time slice allocation that they receive on time |
| 1248 | * slice expiry etc. |
| 1249 | */ |
| 1250 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1251 | #define WEIGHT_IDLEPRIO 2 |
| 1252 | #define WMULT_IDLEPRIO (1 << 31) |
| 1253 | |
| 1254 | /* |
| 1255 | * Nice levels are multiplicative, with a gentle 10% change for every |
| 1256 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to |
| 1257 | * nice 1, it will get ~10% less CPU time than another CPU-bound task |
| 1258 | * that remained on nice 0. |
| 1259 | * |
| 1260 | * The "10% effect" is relative and cumulative: from _any_ nice level, |
| 1261 | * 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] | 1262 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. |
| 1263 | * If a task goes up by ~10% and another task goes down by ~10% then |
| 1264 | * the relative distance between them is ~25%.) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1265 | */ |
| 1266 | static const int prio_to_weight[40] = { |
Ingo Molnar | 254753d | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1267 | /* -20 */ 88761, 71755, 56483, 46273, 36291, |
| 1268 | /* -15 */ 29154, 23254, 18705, 14949, 11916, |
| 1269 | /* -10 */ 9548, 7620, 6100, 4904, 3906, |
| 1270 | /* -5 */ 3121, 2501, 1991, 1586, 1277, |
| 1271 | /* 0 */ 1024, 820, 655, 526, 423, |
| 1272 | /* 5 */ 335, 272, 215, 172, 137, |
| 1273 | /* 10 */ 110, 87, 70, 56, 45, |
| 1274 | /* 15 */ 36, 29, 23, 18, 15, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1275 | }; |
| 1276 | |
Ingo Molnar | 5714d2d | 2007-07-16 09:46:31 +0200 | [diff] [blame] | 1277 | /* |
| 1278 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. |
| 1279 | * |
| 1280 | * In cases where the weight does not change often, we can use the |
| 1281 | * precalculated inverse to speed up arithmetics by turning divisions |
| 1282 | * into multiplications: |
| 1283 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1284 | static const u32 prio_to_wmult[40] = { |
Ingo Molnar | 254753d | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1285 | /* -20 */ 48388, 59856, 76040, 92818, 118348, |
| 1286 | /* -15 */ 147320, 184698, 229616, 287308, 360437, |
| 1287 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, |
| 1288 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, |
| 1289 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, |
| 1290 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, |
| 1291 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, |
| 1292 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1293 | }; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1294 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1295 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup); |
| 1296 | |
| 1297 | /* |
| 1298 | * runqueue iterator, to support SMP load-balancing between different |
| 1299 | * scheduling classes, without having to expose their internal data |
| 1300 | * structures to the load-balancing proper: |
| 1301 | */ |
| 1302 | struct rq_iterator { |
| 1303 | void *arg; |
| 1304 | struct task_struct *(*start)(void *); |
| 1305 | struct task_struct *(*next)(void *); |
| 1306 | }; |
| 1307 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1308 | #ifdef CONFIG_SMP |
| 1309 | static unsigned long |
| 1310 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1311 | unsigned long max_load_move, struct sched_domain *sd, |
| 1312 | enum cpu_idle_type idle, int *all_pinned, |
| 1313 | int *this_best_prio, struct rq_iterator *iterator); |
| 1314 | |
| 1315 | static int |
| 1316 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1317 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 1318 | struct rq_iterator *iterator); |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1319 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1320 | |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 1321 | #ifdef CONFIG_CGROUP_CPUACCT |
| 1322 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime); |
| 1323 | #else |
| 1324 | static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} |
| 1325 | #endif |
| 1326 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1327 | #ifdef CONFIG_SMP |
| 1328 | static unsigned long source_load(int cpu, int type); |
| 1329 | static unsigned long target_load(int cpu, int type); |
| 1330 | static unsigned long cpu_avg_load_per_task(int cpu); |
| 1331 | static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd); |
| 1332 | #endif /* CONFIG_SMP */ |
| 1333 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1334 | #include "sched_stats.h" |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1335 | #include "sched_idletask.c" |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1336 | #include "sched_fair.c" |
| 1337 | #include "sched_rt.c" |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1338 | #ifdef CONFIG_SCHED_DEBUG |
| 1339 | # include "sched_debug.c" |
| 1340 | #endif |
| 1341 | |
| 1342 | #define sched_class_highest (&rt_sched_class) |
| 1343 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1344 | static inline void inc_load(struct rq *rq, const struct task_struct *p) |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1345 | { |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1346 | update_load_add(&rq->load, p->se.load.weight); |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1349 | static inline void dec_load(struct rq *rq, const struct task_struct *p) |
| 1350 | { |
| 1351 | update_load_sub(&rq->load, p->se.load.weight); |
| 1352 | } |
| 1353 | |
| 1354 | static void inc_nr_running(struct task_struct *p, struct rq *rq) |
| 1355 | { |
| 1356 | rq->nr_running++; |
| 1357 | inc_load(rq, p); |
| 1358 | } |
| 1359 | |
| 1360 | static void dec_nr_running(struct task_struct *p, struct rq *rq) |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1361 | { |
| 1362 | rq->nr_running--; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1363 | dec_load(rq, p); |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1364 | } |
| 1365 | |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1366 | static void set_load_weight(struct task_struct *p) |
| 1367 | { |
| 1368 | if (task_has_rt_policy(p)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1369 | p->se.load.weight = prio_to_weight[0] * 2; |
| 1370 | p->se.load.inv_weight = prio_to_wmult[0] >> 1; |
| 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | /* |
| 1375 | * SCHED_IDLE tasks get minimal weight: |
| 1376 | */ |
| 1377 | if (p->policy == SCHED_IDLE) { |
| 1378 | p->se.load.weight = WEIGHT_IDLEPRIO; |
| 1379 | p->se.load.inv_weight = WMULT_IDLEPRIO; |
| 1380 | return; |
| 1381 | } |
| 1382 | |
| 1383 | p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO]; |
| 1384 | 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] | 1385 | } |
| 1386 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1387 | 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] | 1388 | { |
| 1389 | sched_info_queued(p); |
Ingo Molnar | fd390f6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1390 | p->sched_class->enqueue_task(rq, p, wakeup); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1391 | p->se.on_rq = 1; |
| 1392 | } |
| 1393 | |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1394 | 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] | 1395 | { |
Ingo Molnar | f02231e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1396 | p->sched_class->dequeue_task(rq, p, sleep); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1397 | p->se.on_rq = 0; |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1401 | * __normal_prio - return the priority that is based on the static prio |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1402 | */ |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1403 | static inline int __normal_prio(struct task_struct *p) |
| 1404 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1405 | return p->static_prio; |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1409 | * Calculate the expected normal priority: i.e. priority |
| 1410 | * without taking RT-inheritance into account. Might be |
| 1411 | * boosted by interactivity modifiers. Changes upon fork, |
| 1412 | * setprio syscalls, and whenever the interactivity |
| 1413 | * estimator recalculates. |
| 1414 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1415 | static inline int normal_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1416 | { |
| 1417 | int prio; |
| 1418 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1419 | if (task_has_rt_policy(p)) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1420 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
| 1421 | else |
| 1422 | prio = __normal_prio(p); |
| 1423 | return prio; |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | * Calculate the current priority, i.e. the priority |
| 1428 | * taken into account by the scheduler. This value might |
| 1429 | * be boosted by RT tasks, or might be boosted by |
| 1430 | * interactivity modifiers. Will be RT if the task got |
| 1431 | * RT-boosted. If not then it returns p->normal_prio. |
| 1432 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1433 | static int effective_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1434 | { |
| 1435 | p->normal_prio = normal_prio(p); |
| 1436 | /* |
| 1437 | * If we are RT tasks or we were boosted to RT priority, |
| 1438 | * keep the priority unchanged. Otherwise, update priority |
| 1439 | * to the normal priority: |
| 1440 | */ |
| 1441 | if (!rt_prio(p->prio)) |
| 1442 | return p->normal_prio; |
| 1443 | return p->prio; |
| 1444 | } |
| 1445 | |
| 1446 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1447 | * activate_task - move a task to the runqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1449 | 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] | 1450 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 1451 | if (task_contributes_to_load(p)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1452 | rq->nr_uninterruptible--; |
| 1453 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1454 | enqueue_task(rq, p, wakeup); |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1455 | inc_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | * deactivate_task - remove a task from the runqueue. |
| 1460 | */ |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1461 | 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] | 1462 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 1463 | if (task_contributes_to_load(p)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1464 | rq->nr_uninterruptible++; |
| 1465 | |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1466 | dequeue_task(rq, p, sleep); |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1467 | dec_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | /** |
| 1471 | * task_curr - is this task currently executing on a CPU? |
| 1472 | * @p: the task in question. |
| 1473 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1474 | inline int task_curr(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | { |
| 1476 | return cpu_curr(task_cpu(p)) == p; |
| 1477 | } |
| 1478 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1479 | /* Used instead of source_load when we know the type == 0 */ |
| 1480 | unsigned long weighted_cpuload(const int cpu) |
| 1481 | { |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1482 | return cpu_rq(cpu)->load.weight; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) |
| 1486 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1487 | set_task_rq(p, cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1488 | #ifdef CONFIG_SMP |
Dmitry Adamushko | ce96b5a | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 1489 | /* |
| 1490 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be |
| 1491 | * successfuly executed on another CPU. We must ensure that updates of |
| 1492 | * per-task data have been completed by this moment. |
| 1493 | */ |
| 1494 | smp_wmb(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1495 | task_thread_info(p)->cpu = cpu; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1496 | #endif |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1499 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
| 1500 | const struct sched_class *prev_class, |
| 1501 | int oldprio, int running) |
| 1502 | { |
| 1503 | if (prev_class != p->sched_class) { |
| 1504 | if (prev_class->switched_from) |
| 1505 | prev_class->switched_from(rq, p, running); |
| 1506 | p->sched_class->switched_to(rq, p, running); |
| 1507 | } else |
| 1508 | p->sched_class->prio_changed(rq, p, oldprio, running); |
| 1509 | } |
| 1510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | #ifdef CONFIG_SMP |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1512 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1513 | /* |
| 1514 | * Is this task likely cache-hot: |
| 1515 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1516 | static int |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1517 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) |
| 1518 | { |
| 1519 | s64 delta; |
| 1520 | |
Ingo Molnar | f540a60 | 2008-03-15 17:10:34 +0100 | [diff] [blame] | 1521 | /* |
| 1522 | * Buddy candidates are cache hot: |
| 1523 | */ |
Ingo Molnar | d25ce4c | 2008-03-17 09:36:53 +0100 | [diff] [blame^] | 1524 | 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] | 1525 | return 1; |
| 1526 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1527 | if (p->sched_class != &fair_sched_class) |
| 1528 | return 0; |
| 1529 | |
Ingo Molnar | 6bc1665 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1530 | if (sysctl_sched_migration_cost == -1) |
| 1531 | return 1; |
| 1532 | if (sysctl_sched_migration_cost == 0) |
| 1533 | return 0; |
| 1534 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1535 | delta = now - p->se.exec_start; |
| 1536 | |
| 1537 | return delta < (s64)sysctl_sched_migration_cost; |
| 1538 | } |
| 1539 | |
| 1540 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1541 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1542 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1543 | int old_cpu = task_cpu(p); |
| 1544 | 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] | 1545 | struct cfs_rq *old_cfsrq = task_cfs_rq(p), |
| 1546 | *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu); |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1547 | u64 clock_offset; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1548 | |
| 1549 | clock_offset = old_rq->clock - new_rq->clock; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1550 | |
| 1551 | #ifdef CONFIG_SCHEDSTATS |
| 1552 | if (p->se.wait_start) |
| 1553 | p->se.wait_start -= clock_offset; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1554 | if (p->se.sleep_start) |
| 1555 | p->se.sleep_start -= clock_offset; |
| 1556 | if (p->se.block_start) |
| 1557 | p->se.block_start -= clock_offset; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1558 | if (old_cpu != new_cpu) { |
| 1559 | schedstat_inc(p, se.nr_migrations); |
| 1560 | if (task_hot(p, old_rq->clock, NULL)) |
| 1561 | schedstat_inc(p, se.nr_forced2_migrations); |
| 1562 | } |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1563 | #endif |
Srivatsa Vaddagiri | 2830cf8 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1564 | p->se.vruntime -= old_cfsrq->min_vruntime - |
| 1565 | new_cfsrq->min_vruntime; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1566 | |
| 1567 | __set_task_cpu(p, new_cpu); |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1568 | } |
| 1569 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1570 | struct migration_req { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1573 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | int dest_cpu; |
| 1575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | struct completion done; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1577 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | |
| 1579 | /* |
| 1580 | * The task's runqueue lock must be held. |
| 1581 | * Returns true if you have to wait for migration thread. |
| 1582 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1583 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1584 | 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] | 1585 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1586 | struct rq *rq = task_rq(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
| 1588 | /* |
| 1589 | * If the task is not on a runqueue (and not running), then |
| 1590 | * it is sufficient to simply update the task's cpu field. |
| 1591 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1592 | if (!p->se.on_rq && !task_running(rq, p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | set_task_cpu(p, dest_cpu); |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | init_completion(&req->done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | req->task = p; |
| 1599 | req->dest_cpu = dest_cpu; |
| 1600 | list_add(&req->list, &rq->migration_queue); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 1601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | return 1; |
| 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * wait_task_inactive - wait for a thread to unschedule. |
| 1607 | * |
| 1608 | * The caller must ensure that the task *will* unschedule sometime soon, |
| 1609 | * else this function might spin for a *long* time. This function can't |
| 1610 | * be called with interrupts off, or it may introduce deadlock with |
| 1611 | * smp_call_function() if an IPI is sent by the same process we are |
| 1612 | * waiting to become inactive. |
| 1613 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1614 | void wait_task_inactive(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | { |
| 1616 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1617 | int running, on_rq; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1618 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1620 | for (;;) { |
| 1621 | /* |
| 1622 | * We do the initial early heuristics without holding |
| 1623 | * any task-queue locks at all. We'll only try to get |
| 1624 | * the runqueue lock when things look like they will |
| 1625 | * work out! |
| 1626 | */ |
| 1627 | rq = task_rq(p); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 1628 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1629 | /* |
| 1630 | * If the task is actively running on another CPU |
| 1631 | * still, just relax and busy-wait without holding |
| 1632 | * any locks. |
| 1633 | * |
| 1634 | * NOTE! Since we don't hold any locks, it's not |
| 1635 | * even sure that "rq" stays as the right runqueue! |
| 1636 | * But we don't care, since "task_running()" will |
| 1637 | * return false if the runqueue has changed and p |
| 1638 | * is actually now running somewhere else! |
| 1639 | */ |
| 1640 | while (task_running(rq, p)) |
| 1641 | cpu_relax(); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 1642 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1643 | /* |
| 1644 | * Ok, time to look more closely! We need the rq |
| 1645 | * lock now, to be *sure*. If we're wrong, we'll |
| 1646 | * just go back and repeat. |
| 1647 | */ |
| 1648 | rq = task_rq_lock(p, &flags); |
| 1649 | running = task_running(rq, p); |
| 1650 | on_rq = p->se.on_rq; |
| 1651 | task_rq_unlock(rq, &flags); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 1652 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1653 | /* |
| 1654 | * Was it really running after all now that we |
| 1655 | * checked with the proper locks actually held? |
| 1656 | * |
| 1657 | * Oops. Go back and try again.. |
| 1658 | */ |
| 1659 | if (unlikely(running)) { |
| 1660 | cpu_relax(); |
| 1661 | continue; |
| 1662 | } |
| 1663 | |
| 1664 | /* |
| 1665 | * It's not enough that it's not actively running, |
| 1666 | * it must be off the runqueue _entirely_, and not |
| 1667 | * preempted! |
| 1668 | * |
| 1669 | * So if it wa still runnable (but just not actively |
| 1670 | * running right now), it's preempted, and we should |
| 1671 | * yield - it could be a while. |
| 1672 | */ |
| 1673 | if (unlikely(on_rq)) { |
| 1674 | schedule_timeout_uninterruptible(1); |
| 1675 | continue; |
| 1676 | } |
| 1677 | |
| 1678 | /* |
| 1679 | * Ahh, all good. It wasn't running, and it wasn't |
| 1680 | * runnable, which means that it will never become |
| 1681 | * running in the future either. We're all done! |
| 1682 | */ |
| 1683 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /*** |
| 1688 | * kick_process - kick a running thread to enter/exit the kernel |
| 1689 | * @p: the to-be-kicked thread |
| 1690 | * |
| 1691 | * Cause a process which is running on another CPU to enter |
| 1692 | * kernel-mode, without any delay. (to get signals handled.) |
| 1693 | * |
| 1694 | * NOTE: this function doesnt have to take the runqueue lock, |
| 1695 | * because all it wants to ensure is that the remote task enters |
| 1696 | * the kernel. If the IPI races and the task has been migrated |
| 1697 | * to another CPU then no harm is done and the purpose has been |
| 1698 | * achieved as well. |
| 1699 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1700 | void kick_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | { |
| 1702 | int cpu; |
| 1703 | |
| 1704 | preempt_disable(); |
| 1705 | cpu = task_cpu(p); |
| 1706 | if ((cpu != smp_processor_id()) && task_curr(p)) |
| 1707 | smp_send_reschedule(cpu); |
| 1708 | preempt_enable(); |
| 1709 | } |
| 1710 | |
| 1711 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1712 | * Return a low guess at the load of a migration-source cpu weighted |
| 1713 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | * |
| 1715 | * We want to under-estimate the load of migration sources, to |
| 1716 | * balance conservatively. |
| 1717 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1718 | static unsigned long source_load(int cpu, int type) |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 1719 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1720 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1721 | unsigned long total = weighted_cpuload(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 1722 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1723 | if (type == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1724 | return total; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1725 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1726 | return min(rq->cpu_load[type-1], total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1730 | * Return a high guess at the load of a migration-target cpu weighted |
| 1731 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1733 | static unsigned long target_load(int cpu, int type) |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 1734 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1735 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1736 | unsigned long total = weighted_cpuload(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 1737 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1738 | if (type == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1739 | return total; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1740 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1741 | return max(rq->cpu_load[type-1], total); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | /* |
| 1745 | * Return the average load per task on the cpu's run queue |
| 1746 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1747 | static unsigned long cpu_avg_load_per_task(int cpu) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1748 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1749 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1750 | unsigned long total = weighted_cpuload(cpu); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1751 | unsigned long n = rq->nr_running; |
| 1752 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1753 | return n ? total / n : SCHED_LOAD_SCALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1756 | /* |
| 1757 | * find_idlest_group finds and returns the least busy CPU group within the |
| 1758 | * domain. |
| 1759 | */ |
| 1760 | static struct sched_group * |
| 1761 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) |
| 1762 | { |
| 1763 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; |
| 1764 | unsigned long min_load = ULONG_MAX, this_load = 0; |
| 1765 | int load_idx = sd->forkexec_idx; |
| 1766 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
| 1767 | |
| 1768 | do { |
| 1769 | unsigned long load, avg_load; |
| 1770 | int local_group; |
| 1771 | int i; |
| 1772 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1773 | /* Skip over this group if it has no CPUs allowed */ |
| 1774 | if (!cpus_intersects(group->cpumask, p->cpus_allowed)) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1775 | continue; |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1776 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1777 | local_group = cpu_isset(this_cpu, group->cpumask); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1778 | |
| 1779 | /* Tally up the load of all CPUs in the group */ |
| 1780 | avg_load = 0; |
| 1781 | |
| 1782 | for_each_cpu_mask(i, group->cpumask) { |
| 1783 | /* Bias balancing toward cpus of our domain */ |
| 1784 | if (local_group) |
| 1785 | load = source_load(i, load_idx); |
| 1786 | else |
| 1787 | load = target_load(i, load_idx); |
| 1788 | |
| 1789 | avg_load += load; |
| 1790 | } |
| 1791 | |
| 1792 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 1793 | avg_load = sg_div_cpu_power(group, |
| 1794 | avg_load * SCHED_LOAD_SCALE); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1795 | |
| 1796 | if (local_group) { |
| 1797 | this_load = avg_load; |
| 1798 | this = group; |
| 1799 | } else if (avg_load < min_load) { |
| 1800 | min_load = avg_load; |
| 1801 | idlest = group; |
| 1802 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1803 | } while (group = group->next, group != sd->groups); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1804 | |
| 1805 | if (!idlest || 100*this_load < imbalance*min_load) |
| 1806 | return NULL; |
| 1807 | return idlest; |
| 1808 | } |
| 1809 | |
| 1810 | /* |
Satoru Takeuchi | 0feaece | 2006-10-03 01:14:10 -0700 | [diff] [blame] | 1811 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1812 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 1813 | static int |
| 1814 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1815 | { |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1816 | cpumask_t tmp; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1817 | unsigned long load, min_load = ULONG_MAX; |
| 1818 | int idlest = -1; |
| 1819 | int i; |
| 1820 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1821 | /* Traverse only the allowed CPUs */ |
| 1822 | cpus_and(tmp, group->cpumask, p->cpus_allowed); |
| 1823 | |
| 1824 | for_each_cpu_mask(i, tmp) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1825 | load = weighted_cpuload(i); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1826 | |
| 1827 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 1828 | min_load = load; |
| 1829 | idlest = i; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | return idlest; |
| 1834 | } |
| 1835 | |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1836 | /* |
| 1837 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 1838 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 1839 | * SD_BALANCE_EXEC. |
| 1840 | * |
| 1841 | * Balance, ie. select the least loaded group. |
| 1842 | * |
| 1843 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 1844 | * |
| 1845 | * preempt must be disabled. |
| 1846 | */ |
| 1847 | static int sched_balance_self(int cpu, int flag) |
| 1848 | { |
| 1849 | struct task_struct *t = current; |
| 1850 | struct sched_domain *tmp, *sd = NULL; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1851 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1852 | for_each_domain(cpu, tmp) { |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1853 | /* |
| 1854 | * If power savings logic is enabled for a domain, stop there. |
| 1855 | */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 1856 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) |
| 1857 | break; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1858 | if (tmp->flags & flag) |
| 1859 | sd = tmp; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1860 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1861 | |
| 1862 | while (sd) { |
| 1863 | cpumask_t span; |
| 1864 | struct sched_group *group; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1865 | int new_cpu, weight; |
| 1866 | |
| 1867 | if (!(sd->flags & flag)) { |
| 1868 | sd = sd->child; |
| 1869 | continue; |
| 1870 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1871 | |
| 1872 | span = sd->span; |
| 1873 | group = find_idlest_group(sd, t, cpu); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1874 | if (!group) { |
| 1875 | sd = sd->child; |
| 1876 | continue; |
| 1877 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1878 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1879 | new_cpu = find_idlest_cpu(group, t, cpu); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1880 | if (new_cpu == -1 || new_cpu == cpu) { |
| 1881 | /* Now try balancing at a lower domain level of cpu */ |
| 1882 | sd = sd->child; |
| 1883 | continue; |
| 1884 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1885 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1886 | /* Now try balancing at a lower domain level of new_cpu */ |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1887 | cpu = new_cpu; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1888 | sd = NULL; |
| 1889 | weight = cpus_weight(span); |
| 1890 | for_each_domain(cpu, tmp) { |
| 1891 | if (weight <= cpus_weight(tmp->span)) |
| 1892 | break; |
| 1893 | if (tmp->flags & flag) |
| 1894 | sd = tmp; |
| 1895 | } |
| 1896 | /* while loop will break here if sd == NULL */ |
| 1897 | } |
| 1898 | |
| 1899 | return cpu; |
| 1900 | } |
| 1901 | |
| 1902 | #endif /* CONFIG_SMP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | /*** |
| 1905 | * try_to_wake_up - wake up a thread |
| 1906 | * @p: the to-be-woken-up thread |
| 1907 | * @state: the mask of task states that can be woken |
| 1908 | * @sync: do a synchronous wakeup? |
| 1909 | * |
| 1910 | * Put it on the run-queue if it's not already there. The "current" |
| 1911 | * thread is always on the run-queue (except when the actual |
| 1912 | * re-schedule is in progress), and as such you're allowed to do |
| 1913 | * the simpler "current->state = TASK_RUNNING" to mark yourself |
| 1914 | * runnable without the overhead of this. |
| 1915 | * |
| 1916 | * returns failure only if the task is already active. |
| 1917 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1918 | 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] | 1919 | { |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1920 | int cpu, orig_cpu, this_cpu, success = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | unsigned long flags; |
| 1922 | long old_state; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1923 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | |
Ingo Molnar | b85d066 | 2008-03-16 20:03:22 +0100 | [diff] [blame] | 1925 | if (!sched_feat(SYNC_WAKEUPS)) |
| 1926 | sync = 0; |
| 1927 | |
Linus Torvalds | 04e2f17 | 2008-02-23 18:05:03 -0800 | [diff] [blame] | 1928 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | rq = task_rq_lock(p, &flags); |
| 1930 | old_state = p->state; |
| 1931 | if (!(old_state & state)) |
| 1932 | goto out; |
| 1933 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1934 | if (p->se.on_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | goto out_running; |
| 1936 | |
| 1937 | cpu = task_cpu(p); |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1938 | orig_cpu = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | this_cpu = smp_processor_id(); |
| 1940 | |
| 1941 | #ifdef CONFIG_SMP |
| 1942 | if (unlikely(task_running(rq, p))) |
| 1943 | goto out_activate; |
| 1944 | |
Dmitry Adamushko | 5d2f5a6 | 2008-01-25 21:08:21 +0100 | [diff] [blame] | 1945 | cpu = p->sched_class->select_task_rq(p, sync); |
| 1946 | if (cpu != orig_cpu) { |
| 1947 | set_task_cpu(p, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | task_rq_unlock(rq, &flags); |
| 1949 | /* might preempt at this point */ |
| 1950 | rq = task_rq_lock(p, &flags); |
| 1951 | old_state = p->state; |
| 1952 | if (!(old_state & state)) |
| 1953 | goto out; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1954 | if (p->se.on_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | goto out_running; |
| 1956 | |
| 1957 | this_cpu = smp_processor_id(); |
| 1958 | cpu = task_cpu(p); |
| 1959 | } |
| 1960 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1961 | #ifdef CONFIG_SCHEDSTATS |
| 1962 | schedstat_inc(rq, ttwu_count); |
| 1963 | if (cpu == this_cpu) |
| 1964 | schedstat_inc(rq, ttwu_local); |
| 1965 | else { |
| 1966 | struct sched_domain *sd; |
| 1967 | for_each_domain(this_cpu, sd) { |
| 1968 | if (cpu_isset(cpu, sd->span)) { |
| 1969 | schedstat_inc(sd, ttwu_wake_remote); |
| 1970 | break; |
| 1971 | } |
| 1972 | } |
| 1973 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1974 | #endif |
| 1975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | out_activate: |
| 1977 | #endif /* CONFIG_SMP */ |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1978 | schedstat_inc(p, se.nr_wakeups); |
| 1979 | if (sync) |
| 1980 | schedstat_inc(p, se.nr_wakeups_sync); |
| 1981 | if (orig_cpu != cpu) |
| 1982 | schedstat_inc(p, se.nr_wakeups_migrate); |
| 1983 | if (cpu == this_cpu) |
| 1984 | schedstat_inc(p, se.nr_wakeups_local); |
| 1985 | else |
| 1986 | schedstat_inc(p, se.nr_wakeups_remote); |
Ingo Molnar | 2daa357 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1987 | update_rq_clock(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1988 | activate_task(rq, p, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | success = 1; |
| 1990 | |
| 1991 | out_running: |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 1992 | check_preempt_curr(rq, p); |
| 1993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | p->state = TASK_RUNNING; |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1995 | #ifdef CONFIG_SMP |
| 1996 | if (p->sched_class->task_wake_up) |
| 1997 | p->sched_class->task_wake_up(rq, p); |
| 1998 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | out: |
| 2000 | task_rq_unlock(rq, &flags); |
| 2001 | |
| 2002 | return success; |
| 2003 | } |
| 2004 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2005 | int wake_up_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 2007 | return try_to_wake_up(p, TASK_ALL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | EXPORT_SYMBOL(wake_up_process); |
| 2010 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2011 | int wake_up_state(struct task_struct *p, unsigned int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | { |
| 2013 | return try_to_wake_up(p, state, 0); |
| 2014 | } |
| 2015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | /* |
| 2017 | * Perform scheduler related setup for a newly forked process p. |
| 2018 | * p is forked by current. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2019 | * |
| 2020 | * __sched_fork() is basic setup used by init_idle() too: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2022 | static void __sched_fork(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2024 | p->se.exec_start = 0; |
| 2025 | p->se.sum_exec_runtime = 0; |
Ingo Molnar | f6cf891 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 2026 | p->se.prev_sum_exec_runtime = 0; |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 2027 | p->se.last_wakeup = 0; |
| 2028 | p->se.avg_overlap = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2029 | |
| 2030 | #ifdef CONFIG_SCHEDSTATS |
| 2031 | p->se.wait_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2032 | p->se.sum_sleep_runtime = 0; |
| 2033 | p->se.sleep_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2034 | p->se.block_start = 0; |
| 2035 | p->se.sleep_max = 0; |
| 2036 | p->se.block_max = 0; |
| 2037 | p->se.exec_max = 0; |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 2038 | p->se.slice_max = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2039 | p->se.wait_max = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 2040 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2041 | |
Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 2042 | INIT_LIST_HEAD(&p->rt.run_list); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2043 | p->se.on_rq = 0; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2044 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2045 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 2046 | INIT_HLIST_HEAD(&p->preempt_notifiers); |
| 2047 | #endif |
| 2048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | /* |
| 2050 | * We mark the process as running here, but have not actually |
| 2051 | * inserted it onto the runqueue yet. This guarantees that |
| 2052 | * nobody will actually run it, and a signal or other external |
| 2053 | * event cannot wake it up and insert it on the runqueue either. |
| 2054 | */ |
| 2055 | p->state = TASK_RUNNING; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * fork()/clone()-time setup: |
| 2060 | */ |
| 2061 | void sched_fork(struct task_struct *p, int clone_flags) |
| 2062 | { |
| 2063 | int cpu = get_cpu(); |
| 2064 | |
| 2065 | __sched_fork(p); |
| 2066 | |
| 2067 | #ifdef CONFIG_SMP |
| 2068 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); |
| 2069 | #endif |
Ingo Molnar | 02e4bac2 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 2070 | set_task_cpu(p, cpu); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2071 | |
| 2072 | /* |
| 2073 | * Make sure we do not leak PI boosting priority to the child: |
| 2074 | */ |
| 2075 | p->prio = current->normal_prio; |
Hiroshi Shimamoto | 2ddbf95 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 2076 | if (!rt_prio(p->prio)) |
| 2077 | p->sched_class = &fair_sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2078 | |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 2079 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2080 | if (likely(sched_info_on())) |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 2081 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | #endif |
Chen, Kenneth W | d6077cb | 2006-02-14 13:53:10 -0800 | [diff] [blame] | 2083 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2084 | p->oncpu = 0; |
| 2085 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | #ifdef CONFIG_PREEMPT |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2087 | /* Want to start with kernel preemption disabled. */ |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 2088 | task_thread_info(p)->preempt_count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2090 | put_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * wake_up_new_task - wake up a newly created task for the first time. |
| 2095 | * |
| 2096 | * This function will do some initial scheduler statistics housekeeping |
| 2097 | * that must be done for every newly created context, then puts the task |
| 2098 | * on the runqueue and wakes it. |
| 2099 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2100 | 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] | 2101 | { |
| 2102 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2103 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | |
| 2105 | rq = task_rq_lock(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | BUG_ON(p->state != TASK_RUNNING); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 2107 | update_rq_clock(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | |
| 2109 | p->prio = effective_prio(p); |
| 2110 | |
Srivatsa Vaddagiri | b9dca1e | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2111 | if (!p->sched_class->task_new || !current->se.on_rq) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2112 | activate_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2115 | * Let the scheduling class do new task startup |
| 2116 | * management (if any): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | */ |
Ingo Molnar | ee0827d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2118 | p->sched_class->task_new(rq, p); |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 2119 | inc_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2121 | check_preempt_curr(rq, p); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2122 | #ifdef CONFIG_SMP |
| 2123 | if (p->sched_class->task_wake_up) |
| 2124 | p->sched_class->task_wake_up(rq, p); |
| 2125 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2126 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2129 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 2130 | |
| 2131 | /** |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2132 | * preempt_notifier_register - tell me when current is being being preempted & rescheduled |
| 2133 | * @notifier: notifier struct to register |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2134 | */ |
| 2135 | void preempt_notifier_register(struct preempt_notifier *notifier) |
| 2136 | { |
| 2137 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); |
| 2138 | } |
| 2139 | EXPORT_SYMBOL_GPL(preempt_notifier_register); |
| 2140 | |
| 2141 | /** |
| 2142 | * preempt_notifier_unregister - no longer interested in preemption notifications |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2143 | * @notifier: notifier struct to unregister |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2144 | * |
| 2145 | * This is safe to call from within a preemption notifier. |
| 2146 | */ |
| 2147 | void preempt_notifier_unregister(struct preempt_notifier *notifier) |
| 2148 | { |
| 2149 | hlist_del(¬ifier->link); |
| 2150 | } |
| 2151 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); |
| 2152 | |
| 2153 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 2154 | { |
| 2155 | struct preempt_notifier *notifier; |
| 2156 | struct hlist_node *node; |
| 2157 | |
| 2158 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) |
| 2159 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
| 2160 | } |
| 2161 | |
| 2162 | static void |
| 2163 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 2164 | struct task_struct *next) |
| 2165 | { |
| 2166 | struct preempt_notifier *notifier; |
| 2167 | struct hlist_node *node; |
| 2168 | |
| 2169 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) |
| 2170 | notifier->ops->sched_out(notifier, next); |
| 2171 | } |
| 2172 | |
| 2173 | #else |
| 2174 | |
| 2175 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 2176 | { |
| 2177 | } |
| 2178 | |
| 2179 | static void |
| 2180 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 2181 | struct task_struct *next) |
| 2182 | { |
| 2183 | } |
| 2184 | |
| 2185 | #endif |
| 2186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | /** |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2188 | * prepare_task_switch - prepare to switch tasks |
| 2189 | * @rq: the runqueue preparing to switch |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2190 | * @prev: the current task that is being switched out |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2191 | * @next: the task we are going to switch to. |
| 2192 | * |
| 2193 | * This is called with the rq lock held and interrupts off. It must |
| 2194 | * be paired with a subsequent finish_task_switch after the context |
| 2195 | * switch. |
| 2196 | * |
| 2197 | * prepare_task_switch sets up locking and calls architecture specific |
| 2198 | * hooks. |
| 2199 | */ |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2200 | static inline void |
| 2201 | prepare_task_switch(struct rq *rq, struct task_struct *prev, |
| 2202 | struct task_struct *next) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2203 | { |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2204 | fire_sched_out_preempt_notifiers(prev, next); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2205 | prepare_lock_switch(rq, next); |
| 2206 | prepare_arch_switch(next); |
| 2207 | } |
| 2208 | |
| 2209 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | * finish_task_switch - clean up after a task-switch |
Jeff Garzik | 344baba | 2005-09-07 01:15:17 -0400 | [diff] [blame] | 2211 | * @rq: runqueue associated with task-switch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | * @prev: the thread we just switched away from. |
| 2213 | * |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2214 | * finish_task_switch must be called after the context switch, paired |
| 2215 | * with a prepare_task_switch call before the context switch. |
| 2216 | * finish_task_switch will reconcile locking set up by prepare_task_switch, |
| 2217 | * and do any other architecture-specific cleanup actions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | * |
| 2219 | * 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] | 2220 | * so, we finish that here outside of the runqueue lock. (Doing it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | * with the lock held can cause deadlocks; see schedule() for |
| 2222 | * details.) |
| 2223 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2224 | static void finish_task_switch(struct rq *rq, struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | __releases(rq->lock) |
| 2226 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | struct mm_struct *mm = rq->prev_mm; |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 2228 | long prev_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | |
| 2230 | rq->prev_mm = NULL; |
| 2231 | |
| 2232 | /* |
| 2233 | * A task struct has one reference for the use as "current". |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2234 | * 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] | 2235 | * schedule one last time. The schedule call will never return, and |
| 2236 | * the scheduled task must drop that reference. |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2237 | * The test for TASK_DEAD must occur while the runqueue locks are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | * still held, otherwise prev could be scheduled on another cpu, die |
| 2239 | * there before we look at prev->state, and then the reference would |
| 2240 | * be dropped twice. |
| 2241 | * Manfred Spraul <manfred@colorfullife.com> |
| 2242 | */ |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 2243 | prev_state = prev->state; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2244 | finish_arch_switch(prev); |
| 2245 | finish_lock_switch(rq, prev); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2246 | #ifdef CONFIG_SMP |
| 2247 | if (current->sched_class->post_schedule) |
| 2248 | current->sched_class->post_schedule(rq); |
| 2249 | #endif |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2250 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2251 | fire_sched_in_preempt_notifiers(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | if (mm) |
| 2253 | mmdrop(mm); |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2254 | if (unlikely(prev_state == TASK_DEAD)) { |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2255 | /* |
| 2256 | * Remove function-return probe instances associated with this |
| 2257 | * task and put them back on the free list. |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2258 | */ |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2259 | kprobe_flush_task(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | put_task_struct(prev); |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | /** |
| 2265 | * schedule_tail - first thing a freshly forked thread must call. |
| 2266 | * @prev: the thread we just switched away from. |
| 2267 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2268 | asmlinkage void schedule_tail(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | __releases(rq->lock) |
| 2270 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2271 | struct rq *rq = this_rq(); |
| 2272 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2273 | finish_task_switch(rq, prev); |
| 2274 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW |
| 2275 | /* In this case, finish_task_switch does not reenable preemption */ |
| 2276 | preempt_enable(); |
| 2277 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | if (current->set_child_tid) |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2279 | put_user(task_pid_vnr(current), current->set_child_tid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | /* |
| 2283 | * context_switch - switch to the new MM and the new |
| 2284 | * thread's register state. |
| 2285 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2286 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2287 | context_switch(struct rq *rq, struct task_struct *prev, |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2288 | struct task_struct *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2290 | struct mm_struct *mm, *oldmm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2292 | prepare_task_switch(rq, prev, next); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2293 | mm = next->mm; |
| 2294 | oldmm = prev->active_mm; |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 2295 | /* |
| 2296 | * For paravirt, this is coupled with an exit in switch_to to |
| 2297 | * combine the page table reload and the switch backend into |
| 2298 | * one hypercall. |
| 2299 | */ |
| 2300 | arch_enter_lazy_cpu_mode(); |
| 2301 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2302 | if (unlikely(!mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | next->active_mm = oldmm; |
| 2304 | atomic_inc(&oldmm->mm_count); |
| 2305 | enter_lazy_tlb(oldmm, next); |
| 2306 | } else |
| 2307 | switch_mm(oldmm, mm, next); |
| 2308 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2309 | if (unlikely(!prev->mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | prev->active_mm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | rq->prev_mm = oldmm; |
| 2312 | } |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 2313 | /* |
| 2314 | * Since the runqueue lock will be released by the next |
| 2315 | * task (which is an invalid locking op but in the case |
| 2316 | * of the scheduler it's an obvious special-case), so we |
| 2317 | * do an early lockdep release here: |
| 2318 | */ |
| 2319 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 2320 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 2321 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | |
| 2323 | /* Here we just switch the register state and the stack. */ |
| 2324 | switch_to(prev, next, prev); |
| 2325 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2326 | barrier(); |
| 2327 | /* |
| 2328 | * this_rq must be evaluated again because prev may have moved |
| 2329 | * CPUs since it called schedule(), thus the 'rq' on its stack |
| 2330 | * frame will be invalid. |
| 2331 | */ |
| 2332 | finish_task_switch(this_rq(), prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * nr_running, nr_uninterruptible and nr_context_switches: |
| 2337 | * |
| 2338 | * externally visible scheduler statistics: current number of runnable |
| 2339 | * threads, current number of uninterruptible-sleeping threads, total |
| 2340 | * number of context switches performed since bootup. |
| 2341 | */ |
| 2342 | unsigned long nr_running(void) |
| 2343 | { |
| 2344 | unsigned long i, sum = 0; |
| 2345 | |
| 2346 | for_each_online_cpu(i) |
| 2347 | sum += cpu_rq(i)->nr_running; |
| 2348 | |
| 2349 | return sum; |
| 2350 | } |
| 2351 | |
| 2352 | unsigned long nr_uninterruptible(void) |
| 2353 | { |
| 2354 | unsigned long i, sum = 0; |
| 2355 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2356 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | sum += cpu_rq(i)->nr_uninterruptible; |
| 2358 | |
| 2359 | /* |
| 2360 | * Since we read the counters lockless, it might be slightly |
| 2361 | * inaccurate. Do not allow it to go below zero though: |
| 2362 | */ |
| 2363 | if (unlikely((long)sum < 0)) |
| 2364 | sum = 0; |
| 2365 | |
| 2366 | return sum; |
| 2367 | } |
| 2368 | |
| 2369 | unsigned long long nr_context_switches(void) |
| 2370 | { |
Steven Rostedt | cc94abf | 2006-06-27 02:54:31 -0700 | [diff] [blame] | 2371 | int i; |
| 2372 | unsigned long long sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2374 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | sum += cpu_rq(i)->nr_switches; |
| 2376 | |
| 2377 | return sum; |
| 2378 | } |
| 2379 | |
| 2380 | unsigned long nr_iowait(void) |
| 2381 | { |
| 2382 | unsigned long i, sum = 0; |
| 2383 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2384 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
| 2386 | |
| 2387 | return sum; |
| 2388 | } |
| 2389 | |
Jack Steiner | db1b1fe | 2006-03-31 02:31:21 -0800 | [diff] [blame] | 2390 | unsigned long nr_active(void) |
| 2391 | { |
| 2392 | unsigned long i, running = 0, uninterruptible = 0; |
| 2393 | |
| 2394 | for_each_online_cpu(i) { |
| 2395 | running += cpu_rq(i)->nr_running; |
| 2396 | uninterruptible += cpu_rq(i)->nr_uninterruptible; |
| 2397 | } |
| 2398 | |
| 2399 | if (unlikely((long)uninterruptible < 0)) |
| 2400 | uninterruptible = 0; |
| 2401 | |
| 2402 | return running + uninterruptible; |
| 2403 | } |
| 2404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2406 | * Update rq->cpu_load[] statistics. This function is usually called every |
| 2407 | * scheduler tick (TICK_NSEC). |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2408 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2409 | static void update_cpu_load(struct rq *this_rq) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2410 | { |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 2411 | unsigned long this_load = this_rq->load.weight; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2412 | int i, scale; |
| 2413 | |
| 2414 | this_rq->nr_load_updates++; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2415 | |
| 2416 | /* Update our load: */ |
| 2417 | for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { |
| 2418 | unsigned long old_load, new_load; |
| 2419 | |
| 2420 | /* scale is effectively 1 << i now, and >> i divides by scale */ |
| 2421 | |
| 2422 | old_load = this_rq->cpu_load[i]; |
| 2423 | new_load = this_load; |
Ingo Molnar | a25707f | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 2424 | /* |
| 2425 | * Round up the averaging division if load is increasing. This |
| 2426 | * prevents us from getting stuck on 9 if the load is 10, for |
| 2427 | * example. |
| 2428 | */ |
| 2429 | if (new_load > old_load) |
| 2430 | new_load += scale-1; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2431 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; |
| 2432 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2435 | #ifdef CONFIG_SMP |
| 2436 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2437 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | * double_rq_lock - safely lock two runqueues |
| 2439 | * |
| 2440 | * Note this does not disable interrupts like task_rq_lock, |
| 2441 | * you need to do so manually before calling. |
| 2442 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2443 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | __acquires(rq1->lock) |
| 2445 | __acquires(rq2->lock) |
| 2446 | { |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 2447 | BUG_ON(!irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | if (rq1 == rq2) { |
| 2449 | spin_lock(&rq1->lock); |
| 2450 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 2451 | } else { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2452 | if (rq1 < rq2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | spin_lock(&rq1->lock); |
| 2454 | spin_lock(&rq2->lock); |
| 2455 | } else { |
| 2456 | spin_lock(&rq2->lock); |
| 2457 | spin_lock(&rq1->lock); |
| 2458 | } |
| 2459 | } |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 2460 | update_rq_clock(rq1); |
| 2461 | update_rq_clock(rq2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | /* |
| 2465 | * double_rq_unlock - safely unlock two runqueues |
| 2466 | * |
| 2467 | * Note this does not restore interrupts like task_rq_unlock, |
| 2468 | * you need to do so manually after calling. |
| 2469 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2470 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | __releases(rq1->lock) |
| 2472 | __releases(rq2->lock) |
| 2473 | { |
| 2474 | spin_unlock(&rq1->lock); |
| 2475 | if (rq1 != rq2) |
| 2476 | spin_unlock(&rq2->lock); |
| 2477 | else |
| 2478 | __release(rq2->lock); |
| 2479 | } |
| 2480 | |
| 2481 | /* |
| 2482 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. |
| 2483 | */ |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2484 | static int double_lock_balance(struct rq *this_rq, struct rq *busiest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | __releases(this_rq->lock) |
| 2486 | __acquires(busiest->lock) |
| 2487 | __acquires(this_rq->lock) |
| 2488 | { |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2489 | int ret = 0; |
| 2490 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 2491 | if (unlikely(!irqs_disabled())) { |
| 2492 | /* printk() doesn't work good under rq->lock */ |
| 2493 | spin_unlock(&this_rq->lock); |
| 2494 | BUG_ON(1); |
| 2495 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | if (unlikely(!spin_trylock(&busiest->lock))) { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2497 | if (busiest < this_rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | spin_unlock(&this_rq->lock); |
| 2499 | spin_lock(&busiest->lock); |
| 2500 | spin_lock(&this_rq->lock); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2501 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | } else |
| 2503 | spin_lock(&busiest->lock); |
| 2504 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2505 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | * If dest_cpu is allowed for this process, migrate the task to it. |
| 2510 | * This is accomplished by forcing the cpu_allowed mask to only |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 2511 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | * the cpu_allowed mask is restored. |
| 2513 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2514 | static void sched_migrate_task(struct task_struct *p, int dest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2516 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2518 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | |
| 2520 | rq = task_rq_lock(p, &flags); |
| 2521 | if (!cpu_isset(dest_cpu, p->cpus_allowed) |
| 2522 | || unlikely(cpu_is_offline(dest_cpu))) |
| 2523 | goto out; |
| 2524 | |
| 2525 | /* force the process onto the specified CPU */ |
| 2526 | if (migrate_task(p, dest_cpu, &req)) { |
| 2527 | /* Need to wait for migration thread (might exit: take ref). */ |
| 2528 | struct task_struct *mt = rq->migration_thread; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | get_task_struct(mt); |
| 2531 | task_rq_unlock(rq, &flags); |
| 2532 | wake_up_process(mt); |
| 2533 | put_task_struct(mt); |
| 2534 | wait_for_completion(&req.done); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | return; |
| 2537 | } |
| 2538 | out: |
| 2539 | task_rq_unlock(rq, &flags); |
| 2540 | } |
| 2541 | |
| 2542 | /* |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2543 | * sched_exec - execve() is a valuable balancing opportunity, because at |
| 2544 | * this point the task has the smallest effective memory and cache footprint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | */ |
| 2546 | void sched_exec(void) |
| 2547 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | int new_cpu, this_cpu = get_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2549 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | put_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2551 | if (new_cpu != this_cpu) |
| 2552 | sched_migrate_task(current, new_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | /* |
| 2556 | * pull_task - move a task from a remote runqueue to the local runqueue. |
| 2557 | * Both runqueues must be locked. |
| 2558 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2559 | static void pull_task(struct rq *src_rq, struct task_struct *p, |
| 2560 | struct rq *this_rq, int this_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2562 | deactivate_task(src_rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | set_task_cpu(p, this_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2564 | activate_task(this_rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | /* |
| 2566 | * Note that idle threads have a prio of MAX_PRIO, for this test |
| 2567 | * to be always true for them. |
| 2568 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2569 | check_preempt_curr(this_rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | /* |
| 2573 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 2574 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 2575 | static |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2576 | 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] | 2577 | struct sched_domain *sd, enum cpu_idle_type idle, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 2578 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | { |
| 2580 | /* |
| 2581 | * We do not migrate tasks that are: |
| 2582 | * 1) running (obviously), or |
| 2583 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 2584 | * 3) are cache-hot on their current CPU. |
| 2585 | */ |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2586 | if (!cpu_isset(this_cpu, p->cpus_allowed)) { |
| 2587 | schedstat_inc(p, se.nr_failed_migrations_affine); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2589 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2590 | *all_pinned = 0; |
| 2591 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2592 | if (task_running(rq, p)) { |
| 2593 | schedstat_inc(p, se.nr_failed_migrations_running); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2594 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2595 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2597 | /* |
| 2598 | * Aggressive migration if: |
| 2599 | * 1) task is cache cold, or |
| 2600 | * 2) too many balance attempts have failed. |
| 2601 | */ |
| 2602 | |
Ingo Molnar | 6bc1665 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2603 | if (!task_hot(p, rq->clock, sd) || |
| 2604 | sd->nr_balance_failed > sd->cache_nice_tries) { |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2605 | #ifdef CONFIG_SCHEDSTATS |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2606 | if (task_hot(p, rq->clock, sd)) { |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2607 | schedstat_inc(sd, lb_hot_gained[idle]); |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2608 | schedstat_inc(p, se.nr_forced_migrations); |
| 2609 | } |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2610 | #endif |
| 2611 | return 1; |
| 2612 | } |
| 2613 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2614 | if (task_hot(p, rq->clock, sd)) { |
| 2615 | schedstat_inc(p, se.nr_failed_migrations_hot); |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2616 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | return 1; |
| 2619 | } |
| 2620 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2621 | static unsigned long |
| 2622 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2623 | unsigned long max_load_move, struct sched_domain *sd, |
| 2624 | enum cpu_idle_type idle, int *all_pinned, |
| 2625 | int *this_best_prio, struct rq_iterator *iterator) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2626 | { |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2627 | int loops = 0, pulled = 0, pinned = 0, skip_for_load; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2628 | struct task_struct *p; |
| 2629 | long rem_load_move = max_load_move; |
| 2630 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2631 | if (max_load_move == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2632 | goto out; |
| 2633 | |
| 2634 | pinned = 1; |
| 2635 | |
| 2636 | /* |
| 2637 | * Start the load-balancing iterator: |
| 2638 | */ |
| 2639 | p = iterator->start(iterator->arg); |
| 2640 | next: |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2641 | if (!p || loops++ > sysctl_sched_nr_migrate) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2642 | goto out; |
| 2643 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2644 | * To help distribute high priority tasks across CPUs we don't |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2645 | * skip a task if it will be the highest priority task (i.e. smallest |
| 2646 | * prio value) on its new queue regardless of its load weight |
| 2647 | */ |
| 2648 | skip_for_load = (p->se.load.weight >> 1) > rem_load_move + |
| 2649 | SCHED_LOAD_SCALE_FUZZ; |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2650 | if ((skip_for_load && p->prio >= *this_best_prio) || |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2651 | !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2652 | p = iterator->next(iterator->arg); |
| 2653 | goto next; |
| 2654 | } |
| 2655 | |
| 2656 | pull_task(busiest, p, this_rq, this_cpu); |
| 2657 | pulled++; |
| 2658 | rem_load_move -= p->se.load.weight; |
| 2659 | |
| 2660 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2661 | * 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] | 2662 | */ |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2663 | if (rem_load_move > 0) { |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2664 | if (p->prio < *this_best_prio) |
| 2665 | *this_best_prio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2666 | p = iterator->next(iterator->arg); |
| 2667 | goto next; |
| 2668 | } |
| 2669 | out: |
| 2670 | /* |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2671 | * 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] | 2672 | * so we can safely collect pull_task() stats here rather than |
| 2673 | * inside pull_task(). |
| 2674 | */ |
| 2675 | schedstat_add(sd, lb_gained[idle], pulled); |
| 2676 | |
| 2677 | if (all_pinned) |
| 2678 | *all_pinned = pinned; |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2679 | |
| 2680 | return max_load_move - rem_load_move; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2681 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | /* |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2684 | * move_tasks tries to move up to max_load_move weighted load from busiest to |
| 2685 | * this_rq, as part of a balancing operation within domain "sd". |
| 2686 | * Returns 1 if successful and 0 otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | * |
| 2688 | * Called with both runqueues locked. |
| 2689 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2690 | 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] | 2691 | unsigned long max_load_move, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2692 | struct sched_domain *sd, enum cpu_idle_type idle, |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2693 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2695 | const struct sched_class *class = sched_class_highest; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2696 | unsigned long total_load_moved = 0; |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2697 | int this_best_prio = this_rq->curr->prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2699 | do { |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2700 | total_load_moved += |
| 2701 | class->load_balance(this_rq, this_cpu, busiest, |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2702 | max_load_move - total_load_moved, |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2703 | sd, idle, all_pinned, &this_best_prio); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2704 | class = class->next; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2705 | } while (class && max_load_move > total_load_moved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2707 | return total_load_moved > 0; |
| 2708 | } |
| 2709 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2710 | static int |
| 2711 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2712 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 2713 | struct rq_iterator *iterator) |
| 2714 | { |
| 2715 | struct task_struct *p = iterator->start(iterator->arg); |
| 2716 | int pinned = 0; |
| 2717 | |
| 2718 | while (p) { |
| 2719 | if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
| 2720 | pull_task(busiest, p, this_rq, this_cpu); |
| 2721 | /* |
| 2722 | * Right now, this is only the second place pull_task() |
| 2723 | * is called, so we can safely collect pull_task() |
| 2724 | * stats here rather than inside pull_task(). |
| 2725 | */ |
| 2726 | schedstat_inc(sd, lb_gained[idle]); |
| 2727 | |
| 2728 | return 1; |
| 2729 | } |
| 2730 | p = iterator->next(iterator->arg); |
| 2731 | } |
| 2732 | |
| 2733 | return 0; |
| 2734 | } |
| 2735 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2736 | /* |
| 2737 | * move_one_task tries to move exactly one task from busiest to this_rq, as |
| 2738 | * part of active balancing operations within "domain". |
| 2739 | * Returns 1 if successful and 0 otherwise. |
| 2740 | * |
| 2741 | * Called with both runqueues locked. |
| 2742 | */ |
| 2743 | static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2744 | struct sched_domain *sd, enum cpu_idle_type idle) |
| 2745 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2746 | const struct sched_class *class; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2747 | |
| 2748 | for (class = sched_class_highest; class; class = class->next) |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2749 | if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle)) |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2750 | return 1; |
| 2751 | |
| 2752 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | /* |
| 2756 | * find_busiest_group finds and returns the busiest CPU group within the |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2757 | * domain. It calculates and returns the amount of weighted load which |
| 2758 | * should be moved to restore balance via the imbalance parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | */ |
| 2760 | static struct sched_group * |
| 2761 | find_busiest_group(struct sched_domain *sd, int this_cpu, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2762 | unsigned long *imbalance, enum cpu_idle_type idle, |
| 2763 | int *sd_idle, cpumask_t *cpus, int *balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | { |
| 2765 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; |
| 2766 | 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] | 2767 | unsigned long max_pull; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2768 | unsigned long busiest_load_per_task, busiest_nr_running; |
| 2769 | unsigned long this_load_per_task, this_nr_running; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2770 | int load_idx, group_imb = 0; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2771 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2772 | int power_savings_balance = 1; |
| 2773 | unsigned long leader_nr_running = 0, min_load_per_task = 0; |
| 2774 | unsigned long min_nr_running = ULONG_MAX; |
| 2775 | struct sched_group *group_min = NULL, *group_leader = NULL; |
| 2776 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | |
| 2778 | max_load = this_load = total_load = total_pwr = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2779 | busiest_load_per_task = busiest_nr_running = 0; |
| 2780 | this_load_per_task = this_nr_running = 0; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2781 | if (idle == CPU_NOT_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2782 | load_idx = sd->busy_idx; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2783 | else if (idle == CPU_NEWLY_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2784 | load_idx = sd->newidle_idx; |
| 2785 | else |
| 2786 | load_idx = sd->idle_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2787 | |
| 2788 | do { |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2789 | unsigned long load, group_capacity, max_cpu_load, min_cpu_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | int local_group; |
| 2791 | int i; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2792 | int __group_imb = 0; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2793 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2794 | unsigned long sum_nr_running, sum_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | |
| 2796 | local_group = cpu_isset(this_cpu, group->cpumask); |
| 2797 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2798 | if (local_group) |
| 2799 | balance_cpu = first_cpu(group->cpumask); |
| 2800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | /* Tally up the load of all CPUs in the group */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2802 | sum_weighted_load = sum_nr_running = avg_load = 0; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2803 | max_cpu_load = 0; |
| 2804 | min_cpu_load = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | |
| 2806 | for_each_cpu_mask(i, group->cpumask) { |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2807 | struct rq *rq; |
| 2808 | |
| 2809 | if (!cpu_isset(i, *cpus)) |
| 2810 | continue; |
| 2811 | |
| 2812 | rq = cpu_rq(i); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2813 | |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 2814 | if (*sd_idle && rq->nr_running) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2815 | *sd_idle = 0; |
| 2816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | /* Bias balancing toward cpus of our domain */ |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2818 | if (local_group) { |
| 2819 | if (idle_cpu(i) && !first_idle_cpu) { |
| 2820 | first_idle_cpu = 1; |
| 2821 | balance_cpu = i; |
| 2822 | } |
| 2823 | |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2824 | load = target_load(i, load_idx); |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2825 | } else { |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2826 | load = source_load(i, load_idx); |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2827 | if (load > max_cpu_load) |
| 2828 | max_cpu_load = load; |
| 2829 | if (min_cpu_load > load) |
| 2830 | min_cpu_load = load; |
| 2831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | |
| 2833 | avg_load += load; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2834 | sum_nr_running += rq->nr_running; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2835 | sum_weighted_load += weighted_cpuload(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | } |
| 2837 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2838 | /* |
| 2839 | * First idle cpu or the first cpu(busiest) in this sched group |
| 2840 | * is eligible for doing load balancing at this and above |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 2841 | * domains. In the newly idle case, we will allow all the cpu's |
| 2842 | * to do the newly idle load balance. |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2843 | */ |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 2844 | if (idle != CPU_NEWLY_IDLE && local_group && |
| 2845 | balance_cpu != this_cpu && balance) { |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2846 | *balance = 0; |
| 2847 | goto ret; |
| 2848 | } |
| 2849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | total_load += avg_load; |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2851 | total_pwr += group->__cpu_power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | |
| 2853 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2854 | avg_load = sg_div_cpu_power(group, |
| 2855 | avg_load * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2857 | if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE) |
| 2858 | __group_imb = 1; |
| 2859 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2860 | group_capacity = group->__cpu_power / SCHED_LOAD_SCALE; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | if (local_group) { |
| 2863 | this_load = avg_load; |
| 2864 | this = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2865 | this_nr_running = sum_nr_running; |
| 2866 | this_load_per_task = sum_weighted_load; |
| 2867 | } else if (avg_load > max_load && |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2868 | (sum_nr_running > group_capacity || __group_imb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | max_load = avg_load; |
| 2870 | busiest = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2871 | busiest_nr_running = sum_nr_running; |
| 2872 | busiest_load_per_task = sum_weighted_load; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2873 | group_imb = __group_imb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2875 | |
| 2876 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2877 | /* |
| 2878 | * Busy processors will not participate in power savings |
| 2879 | * balance. |
| 2880 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2881 | if (idle == CPU_NOT_IDLE || |
| 2882 | !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
| 2883 | goto group_next; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2884 | |
| 2885 | /* |
| 2886 | * If the local group is idle or completely loaded |
| 2887 | * no need to do power savings balance at this domain |
| 2888 | */ |
| 2889 | if (local_group && (this_nr_running >= group_capacity || |
| 2890 | !this_nr_running)) |
| 2891 | power_savings_balance = 0; |
| 2892 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2893 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2894 | * If a group is already running at full capacity or idle, |
| 2895 | * don't include that group in power savings calculations |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2896 | */ |
| 2897 | if (!power_savings_balance || sum_nr_running >= group_capacity |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2898 | || !sum_nr_running) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2899 | goto group_next; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2900 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2901 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2902 | * Calculate the group which has the least non-idle load. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2903 | * This is the group from where we need to pick up the load |
| 2904 | * for saving power |
| 2905 | */ |
| 2906 | if ((sum_nr_running < min_nr_running) || |
| 2907 | (sum_nr_running == min_nr_running && |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2908 | first_cpu(group->cpumask) < |
| 2909 | first_cpu(group_min->cpumask))) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2910 | group_min = group; |
| 2911 | min_nr_running = sum_nr_running; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2912 | min_load_per_task = sum_weighted_load / |
| 2913 | sum_nr_running; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2914 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2915 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2916 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2917 | * Calculate the group which is almost near its |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2918 | * capacity but still has some space to pick up some load |
| 2919 | * from other group and save more power |
| 2920 | */ |
| 2921 | if (sum_nr_running <= group_capacity - 1) { |
| 2922 | if (sum_nr_running > leader_nr_running || |
| 2923 | (sum_nr_running == leader_nr_running && |
| 2924 | first_cpu(group->cpumask) > |
| 2925 | first_cpu(group_leader->cpumask))) { |
| 2926 | group_leader = group; |
| 2927 | leader_nr_running = sum_nr_running; |
| 2928 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2929 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2930 | group_next: |
| 2931 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | group = group->next; |
| 2933 | } while (group != sd->groups); |
| 2934 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2935 | if (!busiest || this_load >= max_load || busiest_nr_running == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | goto out_balanced; |
| 2937 | |
| 2938 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; |
| 2939 | |
| 2940 | if (this_load >= avg_load || |
| 2941 | 100*max_load <= sd->imbalance_pct*this_load) |
| 2942 | goto out_balanced; |
| 2943 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2944 | busiest_load_per_task /= busiest_nr_running; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2945 | if (group_imb) |
| 2946 | busiest_load_per_task = min(busiest_load_per_task, avg_load); |
| 2947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | /* |
| 2949 | * We're trying to get all the cpus to the average_load, so we don't |
| 2950 | * want to push ourselves above the average load, nor do we wish to |
| 2951 | * reduce the max loaded cpu below the average load, as either of these |
| 2952 | * actions would just result in more rebalancing later, and ping-pong |
| 2953 | * tasks around. Thus we look for the minimum possible imbalance. |
| 2954 | * Negative imbalances (*we* are more loaded than anyone else) will |
| 2955 | * 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] | 2956 | * 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] | 2957 | * appear as very large values with unsigned longs. |
| 2958 | */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2959 | if (max_load <= busiest_load_per_task) |
| 2960 | goto out_balanced; |
| 2961 | |
| 2962 | /* |
| 2963 | * In the presence of smp nice balancing, certain scenarios can have |
| 2964 | * max load less than avg load(as we skip the groups at or below |
| 2965 | * its cpu_power, while calculating max_load..) |
| 2966 | */ |
| 2967 | if (max_load < avg_load) { |
| 2968 | *imbalance = 0; |
| 2969 | goto small_imbalance; |
| 2970 | } |
Siddha, Suresh B | 0c117f1 | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2971 | |
| 2972 | /* 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] | 2973 | 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] | 2974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | /* How much load to actually move to equalise the imbalance */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2976 | *imbalance = min(max_pull * busiest->__cpu_power, |
| 2977 | (avg_load - this_load) * this->__cpu_power) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | / SCHED_LOAD_SCALE; |
| 2979 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2980 | /* |
| 2981 | * if *imbalance is less than the average load per runnable task |
| 2982 | * there is no gaurantee that any tasks will be moved so we'll have |
| 2983 | * a think about bumping its value to force at least one task to be |
| 2984 | * moved |
| 2985 | */ |
Suresh Siddha | 7fd0d2d | 2007-09-05 14:32:48 +0200 | [diff] [blame] | 2986 | if (*imbalance < busiest_load_per_task) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2987 | unsigned long tmp, pwr_now, pwr_move; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2988 | unsigned int imbn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2990 | small_imbalance: |
| 2991 | pwr_move = pwr_now = 0; |
| 2992 | imbn = 2; |
| 2993 | if (this_nr_running) { |
| 2994 | this_load_per_task /= this_nr_running; |
| 2995 | if (busiest_load_per_task > this_load_per_task) |
| 2996 | imbn = 1; |
| 2997 | } else |
| 2998 | this_load_per_task = SCHED_LOAD_SCALE; |
| 2999 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3000 | if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >= |
| 3001 | busiest_load_per_task * imbn) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3002 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | return busiest; |
| 3004 | } |
| 3005 | |
| 3006 | /* |
| 3007 | * OK, we don't have enough imbalance to justify moving tasks, |
| 3008 | * however we may be able to increase total CPU power used by |
| 3009 | * moving them. |
| 3010 | */ |
| 3011 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3012 | pwr_now += busiest->__cpu_power * |
| 3013 | min(busiest_load_per_task, max_load); |
| 3014 | pwr_now += this->__cpu_power * |
| 3015 | min(this_load_per_task, this_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | pwr_now /= SCHED_LOAD_SCALE; |
| 3017 | |
| 3018 | /* Amount of load we'd subtract */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3019 | tmp = sg_div_cpu_power(busiest, |
| 3020 | busiest_load_per_task * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | if (max_load > tmp) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3022 | pwr_move += busiest->__cpu_power * |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3023 | min(busiest_load_per_task, max_load - tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | |
| 3025 | /* Amount of load we'd add */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3026 | if (max_load * busiest->__cpu_power < |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 3027 | busiest_load_per_task * SCHED_LOAD_SCALE) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3028 | tmp = sg_div_cpu_power(this, |
| 3029 | max_load * busiest->__cpu_power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | else |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 3031 | tmp = sg_div_cpu_power(this, |
| 3032 | busiest_load_per_task * SCHED_LOAD_SCALE); |
| 3033 | pwr_move += this->__cpu_power * |
| 3034 | min(this_load_per_task, this_load + tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | pwr_move /= SCHED_LOAD_SCALE; |
| 3036 | |
| 3037 | /* Move if we gain throughput */ |
Suresh Siddha | 7fd0d2d | 2007-09-05 14:32:48 +0200 | [diff] [blame] | 3038 | if (pwr_move > pwr_now) |
| 3039 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | } |
| 3041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | return busiest; |
| 3043 | |
| 3044 | out_balanced: |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3045 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3046 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3047 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3049 | if (this == group_leader && group_leader != group_min) { |
| 3050 | *imbalance = min_load_per_task; |
| 3051 | return group_min; |
| 3052 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 3053 | #endif |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3054 | ret: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | *imbalance = 0; |
| 3056 | return NULL; |
| 3057 | } |
| 3058 | |
| 3059 | /* |
| 3060 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 3061 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3062 | static struct rq * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3063 | find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3064 | unsigned long imbalance, cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3066 | struct rq *busiest = NULL, *rq; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3067 | unsigned long max_load = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | int i; |
| 3069 | |
| 3070 | for_each_cpu_mask(i, group->cpumask) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3071 | unsigned long wl; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3072 | |
| 3073 | if (!cpu_isset(i, *cpus)) |
| 3074 | continue; |
| 3075 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3076 | rq = cpu_rq(i); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3077 | wl = weighted_cpuload(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3079 | if (rq->nr_running == 1 && wl > imbalance) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3080 | continue; |
| 3081 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3082 | if (wl > max_load) { |
| 3083 | max_load = wl; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3084 | busiest = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | return busiest; |
| 3089 | } |
| 3090 | |
| 3091 | /* |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 3092 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 3093 | * so long as it is large enough. |
| 3094 | */ |
| 3095 | #define MAX_PINNED_INTERVAL 512 |
| 3096 | |
| 3097 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3099 | * tasks if there is an imbalance. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3101 | static int load_balance(int this_cpu, struct rq *this_rq, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3102 | struct sched_domain *sd, enum cpu_idle_type idle, |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3103 | int *balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | { |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3105 | int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | struct sched_group *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | unsigned long imbalance; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3108 | struct rq *busiest; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3109 | cpumask_t cpus = CPU_MASK_ALL; |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3110 | unsigned long flags; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3111 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3112 | /* |
| 3113 | * When power savings policy is enabled for the parent domain, idle |
| 3114 | * sibling can pick up load irrespective of busy siblings. In this case, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3115 | * 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] | 3116 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3117 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3118 | if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3119 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3120 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3122 | schedstat_inc(sd, lb_count[idle]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3124 | redo: |
| 3125 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3126 | &cpus, balance); |
| 3127 | |
Chen, Kenneth W | 0606671 | 2006-12-10 02:20:35 -0800 | [diff] [blame] | 3128 | if (*balance == 0) |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3129 | goto out_balanced; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | if (!group) { |
| 3132 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 3133 | goto out_balanced; |
| 3134 | } |
| 3135 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3136 | busiest = find_busiest_queue(group, idle, imbalance, &cpus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | if (!busiest) { |
| 3138 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 3139 | goto out_balanced; |
| 3140 | } |
| 3141 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3142 | BUG_ON(busiest == this_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | |
| 3144 | schedstat_add(sd, lb_imbalance[idle], imbalance); |
| 3145 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3146 | ld_moved = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | if (busiest->nr_running > 1) { |
| 3148 | /* |
| 3149 | * Attempt to move tasks. If find_busiest_group has found |
| 3150 | * an imbalance but busiest->nr_running <= 1, the group is |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3151 | * still unbalanced. ld_moved simply stays zero, so it is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | * correctly treated as an imbalance. |
| 3153 | */ |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3154 | local_irq_save(flags); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 3155 | double_rq_lock(this_rq, busiest); |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3156 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3157 | imbalance, sd, idle, &all_pinned); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 3158 | double_rq_unlock(this_rq, busiest); |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3159 | local_irq_restore(flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3160 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3161 | /* |
| 3162 | * some other cpu did the load balance for us. |
| 3163 | */ |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3164 | if (ld_moved && this_cpu != smp_processor_id()) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3165 | resched_cpu(this_cpu); |
| 3166 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3167 | /* All tasks on this runqueue were pinned by CPU affinity */ |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3168 | if (unlikely(all_pinned)) { |
| 3169 | cpu_clear(cpu_of(busiest), cpus); |
| 3170 | if (!cpus_empty(cpus)) |
| 3171 | goto redo; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3172 | goto out_balanced; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3175 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3176 | if (!ld_moved) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3177 | schedstat_inc(sd, lb_failed[idle]); |
| 3178 | sd->nr_balance_failed++; |
| 3179 | |
| 3180 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3182 | spin_lock_irqsave(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3183 | |
| 3184 | /* don't kick the migration_thread, if the curr |
| 3185 | * task on busiest cpu can't be moved to this_cpu |
| 3186 | */ |
| 3187 | if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3188 | spin_unlock_irqrestore(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3189 | all_pinned = 1; |
| 3190 | goto out_one_pinned; |
| 3191 | } |
| 3192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | if (!busiest->active_balance) { |
| 3194 | busiest->active_balance = 1; |
| 3195 | busiest->push_cpu = this_cpu; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3196 | active_balance = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | } |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3198 | spin_unlock_irqrestore(&busiest->lock, flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3199 | if (active_balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | wake_up_process(busiest->migration_thread); |
| 3201 | |
| 3202 | /* |
| 3203 | * We've kicked active balancing, reset the failure |
| 3204 | * counter. |
| 3205 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3206 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3208 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | sd->nr_balance_failed = 0; |
| 3210 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3211 | if (likely(!active_balance)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | /* We were unbalanced, so reset the balancing interval */ |
| 3213 | sd->balance_interval = sd->min_interval; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3214 | } else { |
| 3215 | /* |
| 3216 | * If we've begun active balancing, start to back off. This |
| 3217 | * case may not be covered by the all_pinned logic if there |
| 3218 | * is only 1 task on the busy runqueue (because we don't call |
| 3219 | * move_tasks). |
| 3220 | */ |
| 3221 | if (sd->balance_interval < sd->max_interval) |
| 3222 | sd->balance_interval *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | } |
| 3224 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3225 | if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3226 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3227 | return -1; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3228 | return ld_moved; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | |
| 3230 | out_balanced: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | schedstat_inc(sd, lb_balanced[idle]); |
| 3232 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3233 | sd->nr_balance_failed = 0; |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3234 | |
| 3235 | out_one_pinned: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | /* tune up the balancing interval */ |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 3237 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
| 3238 | (sd->balance_interval < sd->max_interval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | sd->balance_interval *= 2; |
| 3240 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3241 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3242 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3243 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | return 0; |
| 3245 | } |
| 3246 | |
| 3247 | /* |
| 3248 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3249 | * tasks if there is an imbalance. |
| 3250 | * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3251 | * 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] | 3252 | * this_rq is locked. |
| 3253 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3254 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3255 | load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3256 | { |
| 3257 | struct sched_group *group; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3258 | struct rq *busiest = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | unsigned long imbalance; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3260 | int ld_moved = 0; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3261 | int sd_idle = 0; |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3262 | int all_pinned = 0; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3263 | cpumask_t cpus = CPU_MASK_ALL; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3264 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3265 | /* |
| 3266 | * When power savings policy is enabled for the parent domain, idle |
| 3267 | * sibling can pick up load irrespective of busy siblings. In this case, |
| 3268 | * let the state of idle sibling percolate up as IDLE, instead of |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3269 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3270 | */ |
| 3271 | if (sd->flags & SD_SHARE_CPUPOWER && |
| 3272 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3273 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3275 | schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3276 | redo: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3277 | group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE, |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3278 | &sd_idle, &cpus, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | if (!group) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3280 | schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3281 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | } |
| 3283 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3284 | busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3285 | &cpus); |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3286 | if (!busiest) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3287 | schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3288 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | } |
| 3290 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3291 | BUG_ON(busiest == this_rq); |
| 3292 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3293 | schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3294 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3295 | ld_moved = 0; |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3296 | if (busiest->nr_running > 1) { |
| 3297 | /* Attempt to move tasks */ |
| 3298 | double_lock_balance(this_rq, busiest); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3299 | /* this_rq->clock is already updated */ |
| 3300 | update_rq_clock(busiest); |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3301 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3302 | imbalance, sd, CPU_NEWLY_IDLE, |
| 3303 | &all_pinned); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3304 | spin_unlock(&busiest->lock); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3305 | |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3306 | if (unlikely(all_pinned)) { |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3307 | cpu_clear(cpu_of(busiest), cpus); |
| 3308 | if (!cpus_empty(cpus)) |
| 3309 | goto redo; |
| 3310 | } |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3311 | } |
| 3312 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3313 | if (!ld_moved) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3314 | schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3315 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 3316 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3317 | return -1; |
| 3318 | } else |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3319 | sd->nr_balance_failed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3321 | return ld_moved; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3322 | |
| 3323 | out_balanced: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3324 | schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3325 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3326 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3327 | return -1; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3328 | sd->nr_balance_failed = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3329 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3330 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | /* |
| 3334 | * idle_balance is called by schedule() if this_cpu is about to become |
| 3335 | * idle. Attempts to pull tasks from other CPUs. |
| 3336 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3337 | static void idle_balance(int this_cpu, struct rq *this_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | { |
| 3339 | struct sched_domain *sd; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3340 | int pulled_task = -1; |
| 3341 | unsigned long next_balance = jiffies + HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | |
| 3343 | for_each_domain(this_cpu, sd) { |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 3344 | unsigned long interval; |
| 3345 | |
| 3346 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 3347 | continue; |
| 3348 | |
| 3349 | if (sd->flags & SD_BALANCE_NEWIDLE) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3350 | /* If we've pulled tasks over stop searching: */ |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 3351 | pulled_task = load_balance_newidle(this_cpu, |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 3352 | this_rq, sd); |
| 3353 | |
| 3354 | interval = msecs_to_jiffies(sd->balance_interval); |
| 3355 | if (time_after(next_balance, sd->last_balance + interval)) |
| 3356 | next_balance = sd->last_balance + interval; |
| 3357 | if (pulled_task) |
| 3358 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3360 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 3361 | /* |
| 3362 | * We are going idle. next_balance may be set based on |
| 3363 | * a busy processor. So reset next_balance. |
| 3364 | */ |
| 3365 | this_rq->next_balance = next_balance; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | /* |
| 3370 | * active_load_balance is run by migration threads. It pushes running tasks |
| 3371 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be |
| 3372 | * running on each physical CPU where possible, and avoids physical / |
| 3373 | * logical imbalances. |
| 3374 | * |
| 3375 | * Called with busiest_rq locked. |
| 3376 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3377 | static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3378 | { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3379 | int target_cpu = busiest_rq->push_cpu; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3380 | struct sched_domain *sd; |
| 3381 | struct rq *target_rq; |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3382 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3383 | /* Is there any task to move? */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3384 | if (busiest_rq->nr_running <= 1) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3385 | return; |
| 3386 | |
| 3387 | target_rq = cpu_rq(target_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | |
| 3389 | /* |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3390 | * This condition is "impossible", if it occurs |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3391 | * we need to fix it. Originally reported by |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3392 | * Bjorn Helgaas on a 128-cpu setup. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3394 | BUG_ON(busiest_rq == target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3396 | /* move a task from busiest_rq to target_rq */ |
| 3397 | double_lock_balance(busiest_rq, target_rq); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3398 | update_rq_clock(busiest_rq); |
| 3399 | update_rq_clock(target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3401 | /* Search for an sd spanning us and the target CPU. */ |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3402 | for_each_domain(target_cpu, sd) { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3403 | if ((sd->flags & SD_LOAD_BALANCE) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3404 | cpu_isset(busiest_cpu, sd->span)) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3405 | break; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3408 | if (likely(sd)) { |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3409 | schedstat_inc(sd, alb_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3411 | if (move_one_task(target_rq, target_cpu, busiest_rq, |
| 3412 | sd, CPU_IDLE)) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3413 | schedstat_inc(sd, alb_pushed); |
| 3414 | else |
| 3415 | schedstat_inc(sd, alb_failed); |
| 3416 | } |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3417 | spin_unlock(&target_rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | } |
| 3419 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3420 | #ifdef CONFIG_NO_HZ |
| 3421 | static struct { |
| 3422 | atomic_t load_balancer; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3423 | cpumask_t cpu_mask; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3424 | } nohz ____cacheline_aligned = { |
| 3425 | .load_balancer = ATOMIC_INIT(-1), |
| 3426 | .cpu_mask = CPU_MASK_NONE, |
| 3427 | }; |
| 3428 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3429 | /* |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3430 | * This routine will try to nominate the ilb (idle load balancing) |
| 3431 | * owner among the cpus whose ticks are stopped. ilb owner will do the idle |
| 3432 | * load balancing on behalf of all those cpus. If all the cpus in the system |
| 3433 | * go into this tickless mode, then there will be no ilb owner (as there is |
| 3434 | * no need for one) and all the cpus will sleep till the next wakeup event |
| 3435 | * arrives... |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3436 | * |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3437 | * For the ilb owner, tick is not stopped. And this tick will be used |
| 3438 | * for idle load balancing. ilb owner will still be part of |
| 3439 | * nohz.cpu_mask.. |
| 3440 | * |
| 3441 | * While stopping the tick, this cpu will become the ilb owner if there |
| 3442 | * is no other owner. And will be the owner till that cpu becomes busy |
| 3443 | * or if all cpus in the system stop their ticks at which point |
| 3444 | * there is no need for ilb owner. |
| 3445 | * |
| 3446 | * When the ilb owner becomes busy, it nominates another owner, during the |
| 3447 | * next busy scheduler_tick() |
| 3448 | */ |
| 3449 | int select_nohz_load_balancer(int stop_tick) |
| 3450 | { |
| 3451 | int cpu = smp_processor_id(); |
| 3452 | |
| 3453 | if (stop_tick) { |
| 3454 | cpu_set(cpu, nohz.cpu_mask); |
| 3455 | cpu_rq(cpu)->in_nohz_recently = 1; |
| 3456 | |
| 3457 | /* |
| 3458 | * If we are going offline and still the leader, give up! |
| 3459 | */ |
| 3460 | if (cpu_is_offline(cpu) && |
| 3461 | atomic_read(&nohz.load_balancer) == cpu) { |
| 3462 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 3463 | BUG(); |
| 3464 | return 0; |
| 3465 | } |
| 3466 | |
| 3467 | /* time for ilb owner also to sleep */ |
| 3468 | if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 3469 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 3470 | atomic_set(&nohz.load_balancer, -1); |
| 3471 | return 0; |
| 3472 | } |
| 3473 | |
| 3474 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 3475 | /* make me the ilb owner */ |
| 3476 | if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) |
| 3477 | return 1; |
| 3478 | } else if (atomic_read(&nohz.load_balancer) == cpu) |
| 3479 | return 1; |
| 3480 | } else { |
| 3481 | if (!cpu_isset(cpu, nohz.cpu_mask)) |
| 3482 | return 0; |
| 3483 | |
| 3484 | cpu_clear(cpu, nohz.cpu_mask); |
| 3485 | |
| 3486 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 3487 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 3488 | BUG(); |
| 3489 | } |
| 3490 | return 0; |
| 3491 | } |
| 3492 | #endif |
| 3493 | |
| 3494 | static DEFINE_SPINLOCK(balancing); |
| 3495 | |
| 3496 | /* |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3497 | * It checks each scheduling domain to see if it is due to be balanced, |
| 3498 | * and initiates a balancing operation if so. |
| 3499 | * |
| 3500 | * Balancing parameters are set up in arch_init_sched_domains. |
| 3501 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 3502 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3503 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3504 | int balance = 1; |
| 3505 | struct rq *rq = cpu_rq(cpu); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3506 | unsigned long interval; |
| 3507 | struct sched_domain *sd; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3508 | /* Earliest time when we have to do rebalance again */ |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 3509 | unsigned long next_balance = jiffies + 60*HZ; |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3510 | int update_next_balance = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3512 | for_each_domain(cpu, sd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 3514 | continue; |
| 3515 | |
| 3516 | interval = sd->balance_interval; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3517 | if (idle != CPU_IDLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | interval *= sd->busy_factor; |
| 3519 | |
| 3520 | /* scale ms to jiffies */ |
| 3521 | interval = msecs_to_jiffies(interval); |
| 3522 | if (unlikely(!interval)) |
| 3523 | interval = 1; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3524 | if (interval > HZ*NR_CPUS/10) |
| 3525 | interval = HZ*NR_CPUS/10; |
| 3526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 3528 | if (sd->flags & SD_SERIALIZE) { |
| 3529 | if (!spin_trylock(&balancing)) |
| 3530 | goto out; |
| 3531 | } |
| 3532 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 3533 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3534 | if (load_balance(cpu, rq, sd, idle, &balance)) { |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3535 | /* |
| 3536 | * We've pulled tasks over so either we're no |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3537 | * longer idle, or one of our SMT siblings is |
| 3538 | * not idle. |
| 3539 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3540 | idle = CPU_NOT_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | } |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 3542 | sd->last_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | } |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 3544 | if (sd->flags & SD_SERIALIZE) |
| 3545 | spin_unlock(&balancing); |
| 3546 | out: |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3547 | if (time_after(next_balance, sd->last_balance + interval)) { |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 3548 | next_balance = sd->last_balance + interval; |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3549 | update_next_balance = 1; |
| 3550 | } |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3551 | |
| 3552 | /* |
| 3553 | * Stop the load balance at this level. There is another |
| 3554 | * CPU in our sched group which is doing load balancing more |
| 3555 | * actively. |
| 3556 | */ |
| 3557 | if (!balance) |
| 3558 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | } |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3560 | |
| 3561 | /* |
| 3562 | * next_balance will be updated only when there is a need. |
| 3563 | * When the cpu is attached to null domain for ex, it will not be |
| 3564 | * updated. |
| 3565 | */ |
| 3566 | if (likely(update_next_balance)) |
| 3567 | rq->next_balance = next_balance; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | /* |
| 3571 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
| 3572 | * In CONFIG_NO_HZ case, the idle load balance owner will do the |
| 3573 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 3574 | */ |
| 3575 | static void run_rebalance_domains(struct softirq_action *h) |
| 3576 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3577 | int this_cpu = smp_processor_id(); |
| 3578 | struct rq *this_rq = cpu_rq(this_cpu); |
| 3579 | enum cpu_idle_type idle = this_rq->idle_at_tick ? |
| 3580 | CPU_IDLE : CPU_NOT_IDLE; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3581 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3582 | rebalance_domains(this_cpu, idle); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3583 | |
| 3584 | #ifdef CONFIG_NO_HZ |
| 3585 | /* |
| 3586 | * If this cpu is the owner for idle load balancing, then do the |
| 3587 | * balancing on behalf of the other idle cpus whose ticks are |
| 3588 | * stopped. |
| 3589 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3590 | if (this_rq->idle_at_tick && |
| 3591 | atomic_read(&nohz.load_balancer) == this_cpu) { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3592 | cpumask_t cpus = nohz.cpu_mask; |
| 3593 | struct rq *rq; |
| 3594 | int balance_cpu; |
| 3595 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3596 | cpu_clear(this_cpu, cpus); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3597 | for_each_cpu_mask(balance_cpu, cpus) { |
| 3598 | /* |
| 3599 | * If this cpu gets work to do, stop the load balancing |
| 3600 | * work being done for other cpus. Next load |
| 3601 | * balancing owner will pick it up. |
| 3602 | */ |
| 3603 | if (need_resched()) |
| 3604 | break; |
| 3605 | |
Oleg Nesterov | de0cf89 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 3606 | rebalance_domains(balance_cpu, CPU_IDLE); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3607 | |
| 3608 | rq = cpu_rq(balance_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3609 | if (time_after(this_rq->next_balance, rq->next_balance)) |
| 3610 | this_rq->next_balance = rq->next_balance; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3611 | } |
| 3612 | } |
| 3613 | #endif |
| 3614 | } |
| 3615 | |
| 3616 | /* |
| 3617 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. |
| 3618 | * |
| 3619 | * In case of CONFIG_NO_HZ, this is the place where we nominate a new |
| 3620 | * idle load balancing owner or decide to stop the periodic load balancing, |
| 3621 | * if the whole system is idle. |
| 3622 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3623 | static inline void trigger_load_balance(struct rq *rq, int cpu) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3624 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3625 | #ifdef CONFIG_NO_HZ |
| 3626 | /* |
| 3627 | * If we were in the nohz mode recently and busy at the current |
| 3628 | * scheduler tick, then check if we need to nominate new idle |
| 3629 | * load balancer. |
| 3630 | */ |
| 3631 | if (rq->in_nohz_recently && !rq->idle_at_tick) { |
| 3632 | rq->in_nohz_recently = 0; |
| 3633 | |
| 3634 | if (atomic_read(&nohz.load_balancer) == cpu) { |
| 3635 | cpu_clear(cpu, nohz.cpu_mask); |
| 3636 | atomic_set(&nohz.load_balancer, -1); |
| 3637 | } |
| 3638 | |
| 3639 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 3640 | /* |
| 3641 | * simple selection for now: Nominate the |
| 3642 | * first cpu in the nohz list to be the next |
| 3643 | * ilb owner. |
| 3644 | * |
| 3645 | * TBD: Traverse the sched domains and nominate |
| 3646 | * the nearest cpu in the nohz.cpu_mask. |
| 3647 | */ |
| 3648 | int ilb = first_cpu(nohz.cpu_mask); |
| 3649 | |
| 3650 | if (ilb != NR_CPUS) |
| 3651 | resched_cpu(ilb); |
| 3652 | } |
| 3653 | } |
| 3654 | |
| 3655 | /* |
| 3656 | * If this cpu is idle and doing idle load balancing for all the |
| 3657 | * cpus with ticks stopped, is it time for that to stop? |
| 3658 | */ |
| 3659 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && |
| 3660 | cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 3661 | resched_cpu(cpu); |
| 3662 | return; |
| 3663 | } |
| 3664 | |
| 3665 | /* |
| 3666 | * If this cpu is idle and the idle load balancing is done by |
| 3667 | * someone else, then no need raise the SCHED_SOFTIRQ |
| 3668 | */ |
| 3669 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && |
| 3670 | cpu_isset(cpu, nohz.cpu_mask)) |
| 3671 | return; |
| 3672 | #endif |
| 3673 | if (time_after_eq(jiffies, rq->next_balance)) |
| 3674 | raise_softirq(SCHED_SOFTIRQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3676 | |
| 3677 | #else /* CONFIG_SMP */ |
| 3678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | /* |
| 3680 | * on UP we do not need to balance between CPUs: |
| 3681 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3682 | static inline void idle_balance(int cpu, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | { |
| 3684 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | #endif |
| 3687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
| 3689 | |
| 3690 | EXPORT_PER_CPU_SYMBOL(kstat); |
| 3691 | |
| 3692 | /* |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3693 | * Return p->sum_exec_runtime plus any more ns on the sched_clock |
| 3694 | * 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] | 3695 | */ |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3696 | unsigned long long task_sched_runtime(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | unsigned long flags; |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3699 | u64 ns, delta_exec; |
| 3700 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3701 | |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3702 | rq = task_rq_lock(p, &flags); |
| 3703 | ns = p->se.sum_exec_runtime; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 3704 | if (task_current(rq, p)) { |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 3705 | update_rq_clock(rq); |
| 3706 | delta_exec = rq->clock - p->se.exec_start; |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3707 | if ((s64)delta_exec > 0) |
| 3708 | ns += delta_exec; |
| 3709 | } |
| 3710 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | return ns; |
| 3713 | } |
| 3714 | |
| 3715 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | * Account user cpu time to a process. |
| 3717 | * @p: the process that the cpu time gets accounted to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | * @cputime: the cpu time spent in user space since the last update |
| 3719 | */ |
| 3720 | void account_user_time(struct task_struct *p, cputime_t cputime) |
| 3721 | { |
| 3722 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3723 | cputime64_t tmp; |
| 3724 | |
| 3725 | p->utime = cputime_add(p->utime, cputime); |
| 3726 | |
| 3727 | /* Add user time to cpustat. */ |
| 3728 | tmp = cputime_to_cputime64(cputime); |
| 3729 | if (TASK_NICE(p) > 0) |
| 3730 | cpustat->nice = cputime64_add(cpustat->nice, tmp); |
| 3731 | else |
| 3732 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 3733 | } |
| 3734 | |
| 3735 | /* |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 3736 | * Account guest cpu time to a process. |
| 3737 | * @p: the process that the cpu time gets accounted to |
| 3738 | * @cputime: the cpu time spent in virtual machine since the last update |
| 3739 | */ |
Adrian Bunk | f7402e0 | 2007-10-29 21:18:10 +0100 | [diff] [blame] | 3740 | static void account_guest_time(struct task_struct *p, cputime_t cputime) |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 3741 | { |
| 3742 | cputime64_t tmp; |
| 3743 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3744 | |
| 3745 | tmp = cputime_to_cputime64(cputime); |
| 3746 | |
| 3747 | p->utime = cputime_add(p->utime, cputime); |
| 3748 | p->gtime = cputime_add(p->gtime, cputime); |
| 3749 | |
| 3750 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 3751 | cpustat->guest = cputime64_add(cpustat->guest, tmp); |
| 3752 | } |
| 3753 | |
| 3754 | /* |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 3755 | * Account scaled user cpu time to a process. |
| 3756 | * @p: the process that the cpu time gets accounted to |
| 3757 | * @cputime: the cpu time spent in user space since the last update |
| 3758 | */ |
| 3759 | void account_user_time_scaled(struct task_struct *p, cputime_t cputime) |
| 3760 | { |
| 3761 | p->utimescaled = cputime_add(p->utimescaled, cputime); |
| 3762 | } |
| 3763 | |
| 3764 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | * Account system cpu time to a process. |
| 3766 | * @p: the process that the cpu time gets accounted to |
| 3767 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 3768 | * @cputime: the cpu time spent in kernel space since the last update |
| 3769 | */ |
| 3770 | void account_system_time(struct task_struct *p, int hardirq_offset, |
| 3771 | cputime_t cputime) |
| 3772 | { |
| 3773 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3774 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | cputime64_t tmp; |
| 3776 | |
Christian Borntraeger | 9778385 | 2007-11-15 20:57:39 +0100 | [diff] [blame] | 3777 | if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) |
| 3778 | return account_guest_time(p, cputime); |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 3779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | p->stime = cputime_add(p->stime, cputime); |
| 3781 | |
| 3782 | /* Add system time to cpustat. */ |
| 3783 | tmp = cputime_to_cputime64(cputime); |
| 3784 | if (hardirq_count() - hardirq_offset) |
| 3785 | cpustat->irq = cputime64_add(cpustat->irq, tmp); |
| 3786 | else if (softirq_count()) |
| 3787 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 3788 | else if (p != rq->idle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | cpustat->system = cputime64_add(cpustat->system, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 3790 | else if (atomic_read(&rq->nr_iowait) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3791 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 3792 | else |
| 3793 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 3794 | /* Account for system time used */ |
| 3795 | acct_update_integrals(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | /* |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 3799 | * Account scaled system cpu time to a process. |
| 3800 | * @p: the process that the cpu time gets accounted to |
| 3801 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 3802 | * @cputime: the cpu time spent in kernel space since the last update |
| 3803 | */ |
| 3804 | void account_system_time_scaled(struct task_struct *p, cputime_t cputime) |
| 3805 | { |
| 3806 | p->stimescaled = cputime_add(p->stimescaled, cputime); |
| 3807 | } |
| 3808 | |
| 3809 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | * Account for involuntary wait time. |
| 3811 | * @p: the process from which the cpu time has been stolen |
| 3812 | * @steal: the cpu time spent in involuntary wait |
| 3813 | */ |
| 3814 | void account_steal_time(struct task_struct *p, cputime_t steal) |
| 3815 | { |
| 3816 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3817 | cputime64_t tmp = cputime_to_cputime64(steal); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3818 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | |
| 3820 | if (p == rq->idle) { |
| 3821 | p->stime = cputime_add(p->stime, steal); |
| 3822 | if (atomic_read(&rq->nr_iowait) > 0) |
| 3823 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 3824 | else |
| 3825 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 3826 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3827 | cpustat->steal = cputime64_add(cpustat->steal, tmp); |
| 3828 | } |
| 3829 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3830 | /* |
| 3831 | * This function gets called by the timer code, with HZ frequency. |
| 3832 | * We call it with interrupts disabled. |
| 3833 | * |
| 3834 | * It also gets called by the fork code, when changing the parent's |
| 3835 | * timeslices. |
| 3836 | */ |
| 3837 | void scheduler_tick(void) |
| 3838 | { |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3839 | int cpu = smp_processor_id(); |
| 3840 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3841 | struct task_struct *curr = rq->curr; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 3842 | u64 next_tick = rq->tick_timestamp + TICK_NSEC; |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3843 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3844 | spin_lock(&rq->lock); |
Ingo Molnar | 546fe3c | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3845 | __update_rq_clock(rq); |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 3846 | /* |
| 3847 | * Let rq->clock advance by at least TICK_NSEC: |
| 3848 | */ |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 3849 | if (unlikely(rq->clock < next_tick)) { |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 3850 | rq->clock = next_tick; |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 3851 | rq->clock_underflows++; |
| 3852 | } |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 3853 | rq->tick_timestamp = rq->clock; |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 3854 | update_last_tick_seen(rq); |
Ingo Molnar | f1a438d | 2007-08-09 11:16:45 +0200 | [diff] [blame] | 3855 | update_cpu_load(rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3856 | curr->sched_class->task_tick(rq, curr, 0); |
| 3857 | update_sched_rt_period(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3858 | spin_unlock(&rq->lock); |
| 3859 | |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 3860 | #ifdef CONFIG_SMP |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3861 | rq->idle_at_tick = idle_cpu(cpu); |
| 3862 | trigger_load_balance(rq, cpu); |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 3863 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | } |
| 3865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) |
| 3867 | |
Srinivasa Ds | 4362758 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 3868 | void __kprobes add_preempt_count(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3869 | { |
| 3870 | /* |
| 3871 | * Underflow? |
| 3872 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3873 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
| 3874 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | preempt_count() += val; |
| 3876 | /* |
| 3877 | * Spinlock count overflowing soon? |
| 3878 | */ |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 3879 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
| 3880 | PREEMPT_MASK - 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | } |
| 3882 | EXPORT_SYMBOL(add_preempt_count); |
| 3883 | |
Srinivasa Ds | 4362758 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 3884 | void __kprobes sub_preempt_count(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | { |
| 3886 | /* |
| 3887 | * Underflow? |
| 3888 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3889 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
| 3890 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | /* |
| 3892 | * Is the spinlock portion underflowing? |
| 3893 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3894 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
| 3895 | !(preempt_count() & PREEMPT_MASK))) |
| 3896 | return; |
| 3897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | preempt_count() -= val; |
| 3899 | } |
| 3900 | EXPORT_SYMBOL(sub_preempt_count); |
| 3901 | |
| 3902 | #endif |
| 3903 | |
| 3904 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3905 | * Print scheduling while atomic bug: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3907 | static noinline void __schedule_bug(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | { |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 3909 | struct pt_regs *regs = get_irq_regs(); |
| 3910 | |
| 3911 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
| 3912 | prev->comm, prev->pid, preempt_count()); |
| 3913 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3914 | debug_show_held_locks(prev); |
| 3915 | if (irqs_disabled()) |
| 3916 | print_irqtrace_events(prev); |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 3917 | |
| 3918 | if (regs) |
| 3919 | show_regs(regs); |
| 3920 | else |
| 3921 | dump_stack(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3922 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3924 | /* |
| 3925 | * Various schedule()-time debugging checks and statistics: |
| 3926 | */ |
| 3927 | static inline void schedule_debug(struct task_struct *prev) |
| 3928 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3929 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3930 | * Test if we are atomic. Since do_exit() needs to call into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | * schedule() atomically, we ignore that path for now. |
| 3932 | * Otherwise, whine if we are scheduling when we should not be. |
| 3933 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3934 | if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state)) |
| 3935 | __schedule_bug(prev); |
| 3936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
| 3938 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3939 | schedstat_inc(this_rq(), sched_count); |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 3940 | #ifdef CONFIG_SCHEDSTATS |
| 3941 | if (unlikely(prev->lock_depth >= 0)) { |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3942 | schedstat_inc(this_rq(), bkl_count); |
| 3943 | schedstat_inc(prev, sched_info.bkl_count); |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 3944 | } |
| 3945 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | /* |
| 3949 | * Pick up the highest-prio task: |
| 3950 | */ |
| 3951 | static inline struct task_struct * |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 3952 | pick_next_task(struct rq *rq, struct task_struct *prev) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3953 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3954 | const struct sched_class *class; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3955 | struct task_struct *p; |
| 3956 | |
| 3957 | /* |
| 3958 | * Optimization: we know that if all tasks are in |
| 3959 | * the fair class we can call that function directly: |
| 3960 | */ |
| 3961 | if (likely(rq->nr_running == rq->cfs.nr_running)) { |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3962 | p = fair_sched_class.pick_next_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3963 | if (likely(p)) |
| 3964 | return p; |
| 3965 | } |
| 3966 | |
| 3967 | class = sched_class_highest; |
| 3968 | for ( ; ; ) { |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3969 | p = class->pick_next_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3970 | if (p) |
| 3971 | return p; |
| 3972 | /* |
| 3973 | * Will never be NULL as the idle class always |
| 3974 | * returns a non-NULL p: |
| 3975 | */ |
| 3976 | class = class->next; |
| 3977 | } |
| 3978 | } |
| 3979 | |
| 3980 | /* |
| 3981 | * schedule() is the main scheduler function. |
| 3982 | */ |
| 3983 | asmlinkage void __sched schedule(void) |
| 3984 | { |
| 3985 | struct task_struct *prev, *next; |
Harvey Harrison | 67ca7bd | 2008-02-15 09:56:36 -0800 | [diff] [blame] | 3986 | unsigned long *switch_count; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3987 | struct rq *rq; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3988 | int cpu; |
| 3989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | need_resched: |
| 3991 | preempt_disable(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3992 | cpu = smp_processor_id(); |
| 3993 | rq = cpu_rq(cpu); |
| 3994 | rcu_qsctr_inc(cpu); |
| 3995 | prev = rq->curr; |
| 3996 | switch_count = &prev->nivcsw; |
| 3997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3998 | release_kernel_lock(prev); |
| 3999 | need_resched_nonpreemptible: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4001 | schedule_debug(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4003 | hrtick_clear(rq); |
| 4004 | |
Ingo Molnar | 1e81995 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4005 | /* |
| 4006 | * Do the rq-clock update outside the rq lock: |
| 4007 | */ |
| 4008 | local_irq_disable(); |
Ingo Molnar | c1b3da3 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4009 | __update_rq_clock(rq); |
Ingo Molnar | 1e81995 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4010 | spin_lock(&rq->lock); |
| 4011 | clear_tsk_need_resched(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4013 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
| 4014 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && |
Roel Kluin | 23e3c3c | 2008-03-13 17:41:59 +0100 | [diff] [blame] | 4015 | signal_pending(prev))) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4016 | prev->state = TASK_RUNNING; |
| 4017 | } else { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4018 | deactivate_task(rq, prev, 1); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4019 | } |
| 4020 | switch_count = &prev->nvcsw; |
| 4021 | } |
| 4022 | |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4023 | #ifdef CONFIG_SMP |
| 4024 | if (prev->sched_class->pre_schedule) |
| 4025 | prev->sched_class->pre_schedule(rq, prev); |
| 4026 | #endif |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 4027 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4028 | if (unlikely(!rq->nr_running)) |
| 4029 | idle_balance(cpu, rq); |
| 4030 | |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4031 | prev->sched_class->put_prev_task(rq, prev); |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4032 | next = pick_next_task(rq, prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | |
| 4034 | sched_info_switch(prev, next); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4036 | if (likely(prev != next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4037 | rq->nr_switches++; |
| 4038 | rq->curr = next; |
| 4039 | ++*switch_count; |
| 4040 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4041 | context_switch(rq, prev, next); /* unlocks the rq */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4042 | /* |
| 4043 | * the context switch might have flipped the stack from under |
| 4044 | * us, hence refresh the local variables. |
| 4045 | */ |
| 4046 | cpu = smp_processor_id(); |
| 4047 | rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4048 | } else |
| 4049 | spin_unlock_irq(&rq->lock); |
| 4050 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4051 | hrtick_set(rq); |
| 4052 | |
| 4053 | if (unlikely(reacquire_kernel_lock(current) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | goto need_resched_nonpreemptible; |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | preempt_enable_no_resched(); |
| 4057 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 4058 | goto need_resched; |
| 4059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | EXPORT_SYMBOL(schedule); |
| 4061 | |
| 4062 | #ifdef CONFIG_PREEMPT |
| 4063 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4064 | * this is the entry point to schedule() from in-kernel preemption |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4065 | * off of preempt_enable. Kernel preemptions off return from interrupt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | * occur there and call schedule directly. |
| 4067 | */ |
| 4068 | asmlinkage void __sched preempt_schedule(void) |
| 4069 | { |
| 4070 | struct thread_info *ti = current_thread_info(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | struct task_struct *task = current; |
| 4072 | int saved_lock_depth; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 4073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | /* |
| 4075 | * If there is a non-zero preempt_count or interrupts are disabled, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4076 | * we do not want to preempt the current task. Just return.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4077 | */ |
Nick Piggin | beed33a | 2006-10-11 01:21:52 -0700 | [diff] [blame] | 4078 | if (likely(ti->preempt_count || irqs_disabled())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | return; |
| 4080 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4081 | do { |
| 4082 | add_preempt_count(PREEMPT_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4084 | /* |
| 4085 | * We keep the big kernel semaphore locked, but we |
| 4086 | * clear ->lock_depth so that schedule() doesnt |
| 4087 | * auto-release the semaphore: |
| 4088 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4089 | saved_lock_depth = task->lock_depth; |
| 4090 | task->lock_depth = -1; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4091 | schedule(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4092 | task->lock_depth = saved_lock_depth; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4093 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4094 | |
| 4095 | /* |
| 4096 | * Check again in case we missed a preemption opportunity |
| 4097 | * between schedule and now. |
| 4098 | */ |
| 4099 | barrier(); |
| 4100 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | EXPORT_SYMBOL(preempt_schedule); |
| 4103 | |
| 4104 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4105 | * this is the entry point to schedule() from kernel preemption |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | * off of irq context. |
| 4107 | * Note, that this is called and return with irqs disabled. This will |
| 4108 | * protect us against recursive calling from irq. |
| 4109 | */ |
| 4110 | asmlinkage void __sched preempt_schedule_irq(void) |
| 4111 | { |
| 4112 | struct thread_info *ti = current_thread_info(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | struct task_struct *task = current; |
| 4114 | int saved_lock_depth; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 4115 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4116 | /* Catch callers which need to be fixed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
| 4118 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4119 | do { |
| 4120 | add_preempt_count(PREEMPT_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4122 | /* |
| 4123 | * We keep the big kernel semaphore locked, but we |
| 4124 | * clear ->lock_depth so that schedule() doesnt |
| 4125 | * auto-release the semaphore: |
| 4126 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4127 | saved_lock_depth = task->lock_depth; |
| 4128 | task->lock_depth = -1; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4129 | local_irq_enable(); |
| 4130 | schedule(); |
| 4131 | local_irq_disable(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4132 | task->lock_depth = saved_lock_depth; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4133 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4134 | |
| 4135 | /* |
| 4136 | * Check again in case we missed a preemption opportunity |
| 4137 | * between schedule and now. |
| 4138 | */ |
| 4139 | barrier(); |
| 4140 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | #endif /* CONFIG_PREEMPT */ |
| 4144 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4145 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, |
| 4146 | void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4148 | return try_to_wake_up(curr->private, mode, sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | EXPORT_SYMBOL(default_wake_function); |
| 4151 | |
| 4152 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4153 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just |
| 4154 | * 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] | 4155 | * number) then we wake all the non-exclusive tasks and one exclusive task. |
| 4156 | * |
| 4157 | * 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] | 4158 | * 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] | 4159 | * zero in this (rare) case, and we handle it by continuing to scan the queue. |
| 4160 | */ |
| 4161 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, |
| 4162 | int nr_exclusive, int sync, void *key) |
| 4163 | { |
Matthias Kaehlcke | 2e45874 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 4164 | wait_queue_t *curr, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | |
Matthias Kaehlcke | 2e45874 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 4166 | list_for_each_entry_safe(curr, next, &q->task_list, task_list) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4167 | unsigned flags = curr->flags; |
| 4168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | if (curr->func(curr, mode, sync, key) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4170 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | break; |
| 4172 | } |
| 4173 | } |
| 4174 | |
| 4175 | /** |
| 4176 | * __wake_up - wake up threads blocked on a waitqueue. |
| 4177 | * @q: the waitqueue |
| 4178 | * @mode: which threads |
| 4179 | * @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] | 4180 | * @key: is directly passed to the wakeup function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4182 | void __wake_up(wait_queue_head_t *q, unsigned int mode, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4183 | int nr_exclusive, void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | { |
| 4185 | unsigned long flags; |
| 4186 | |
| 4187 | spin_lock_irqsave(&q->lock, flags); |
| 4188 | __wake_up_common(q, mode, nr_exclusive, 0, key); |
| 4189 | spin_unlock_irqrestore(&q->lock, flags); |
| 4190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | EXPORT_SYMBOL(__wake_up); |
| 4192 | |
| 4193 | /* |
| 4194 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. |
| 4195 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4196 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | { |
| 4198 | __wake_up_common(q, mode, 1, 0, NULL); |
| 4199 | } |
| 4200 | |
| 4201 | /** |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 4202 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | * @q: the waitqueue |
| 4204 | * @mode: which threads |
| 4205 | * @nr_exclusive: how many wake-one or wake-many threads to wake up |
| 4206 | * |
| 4207 | * The sync wakeup differs that the waker knows that it will schedule |
| 4208 | * away soon, so while the target thread will be woken up, it will not |
| 4209 | * be migrated to another CPU - ie. the two threads are 'synchronized' |
| 4210 | * with each other. This can prevent needless bouncing between CPUs. |
| 4211 | * |
| 4212 | * On UP it can prevent extra preemption. |
| 4213 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4214 | void |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4215 | __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] | 4216 | { |
| 4217 | unsigned long flags; |
| 4218 | int sync = 1; |
| 4219 | |
| 4220 | if (unlikely(!q)) |
| 4221 | return; |
| 4222 | |
| 4223 | if (unlikely(!nr_exclusive)) |
| 4224 | sync = 0; |
| 4225 | |
| 4226 | spin_lock_irqsave(&q->lock, flags); |
| 4227 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); |
| 4228 | spin_unlock_irqrestore(&q->lock, flags); |
| 4229 | } |
| 4230 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ |
| 4231 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4232 | void complete(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | { |
| 4234 | unsigned long flags; |
| 4235 | |
| 4236 | spin_lock_irqsave(&x->wait.lock, flags); |
| 4237 | x->done++; |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 4238 | __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 4240 | } |
| 4241 | EXPORT_SYMBOL(complete); |
| 4242 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4243 | void complete_all(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | { |
| 4245 | unsigned long flags; |
| 4246 | |
| 4247 | spin_lock_irqsave(&x->wait.lock, flags); |
| 4248 | x->done += UINT_MAX/2; |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 4249 | __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 4251 | } |
| 4252 | EXPORT_SYMBOL(complete_all); |
| 4253 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4254 | static inline long __sched |
| 4255 | do_wait_for_common(struct completion *x, long timeout, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | if (!x->done) { |
| 4258 | DECLARE_WAITQUEUE(wait, current); |
| 4259 | |
| 4260 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 4261 | __add_wait_queue_tail(&x->wait, &wait); |
| 4262 | do { |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 4263 | if ((state == TASK_INTERRUPTIBLE && |
| 4264 | signal_pending(current)) || |
| 4265 | (state == TASK_KILLABLE && |
| 4266 | fatal_signal_pending(current))) { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4267 | __remove_wait_queue(&x->wait, &wait); |
| 4268 | return -ERESTARTSYS; |
| 4269 | } |
| 4270 | __set_current_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | spin_unlock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4272 | timeout = schedule_timeout(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | spin_lock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4274 | if (!timeout) { |
| 4275 | __remove_wait_queue(&x->wait, &wait); |
| 4276 | return timeout; |
| 4277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | } while (!x->done); |
| 4279 | __remove_wait_queue(&x->wait, &wait); |
| 4280 | } |
| 4281 | x->done--; |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4282 | return timeout; |
| 4283 | } |
| 4284 | |
| 4285 | static long __sched |
| 4286 | wait_for_common(struct completion *x, long timeout, int state) |
| 4287 | { |
| 4288 | might_sleep(); |
| 4289 | |
| 4290 | spin_lock_irq(&x->wait.lock); |
| 4291 | timeout = do_wait_for_common(x, timeout, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | spin_unlock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4293 | return timeout; |
| 4294 | } |
| 4295 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4296 | void __sched wait_for_completion(struct completion *x) |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4297 | { |
| 4298 | wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | } |
| 4300 | EXPORT_SYMBOL(wait_for_completion); |
| 4301 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4302 | unsigned long __sched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) |
| 4304 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4305 | return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | } |
| 4307 | EXPORT_SYMBOL(wait_for_completion_timeout); |
| 4308 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4309 | int __sched wait_for_completion_interruptible(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | { |
Andi Kleen | 51e9799 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 4311 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE); |
| 4312 | if (t == -ERESTARTSYS) |
| 4313 | return t; |
| 4314 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | } |
| 4316 | EXPORT_SYMBOL(wait_for_completion_interruptible); |
| 4317 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4318 | unsigned long __sched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | wait_for_completion_interruptible_timeout(struct completion *x, |
| 4320 | unsigned long timeout) |
| 4321 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4322 | return wait_for_common(x, timeout, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | } |
| 4324 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); |
| 4325 | |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 4326 | int __sched wait_for_completion_killable(struct completion *x) |
| 4327 | { |
| 4328 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE); |
| 4329 | if (t == -ERESTARTSYS) |
| 4330 | return t; |
| 4331 | return 0; |
| 4332 | } |
| 4333 | EXPORT_SYMBOL(wait_for_completion_killable); |
| 4334 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4335 | static long __sched |
| 4336 | sleep_on_common(wait_queue_head_t *q, int state, long timeout) |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4337 | { |
| 4338 | unsigned long flags; |
| 4339 | wait_queue_t wait; |
| 4340 | |
| 4341 | init_waitqueue_entry(&wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4343 | __set_current_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4345 | spin_lock_irqsave(&q->lock, flags); |
| 4346 | __add_wait_queue(q, &wait); |
| 4347 | spin_unlock(&q->lock); |
| 4348 | timeout = schedule_timeout(timeout); |
| 4349 | spin_lock_irq(&q->lock); |
| 4350 | __remove_wait_queue(q, &wait); |
| 4351 | spin_unlock_irqrestore(&q->lock, flags); |
| 4352 | |
| 4353 | return timeout; |
| 4354 | } |
| 4355 | |
| 4356 | void __sched interruptible_sleep_on(wait_queue_head_t *q) |
| 4357 | { |
| 4358 | sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4360 | EXPORT_SYMBOL(interruptible_sleep_on); |
| 4361 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4362 | long __sched |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4363 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4365 | return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
| 4368 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4369 | void __sched sleep_on(wait_queue_head_t *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4371 | sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | EXPORT_SYMBOL(sleep_on); |
| 4374 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4375 | long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4377 | return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | EXPORT_SYMBOL(sleep_on_timeout); |
| 4380 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4381 | #ifdef CONFIG_RT_MUTEXES |
| 4382 | |
| 4383 | /* |
| 4384 | * rt_mutex_setprio - set the current priority of a task |
| 4385 | * @p: task |
| 4386 | * @prio: prio value (kernel-internal form) |
| 4387 | * |
| 4388 | * This function changes the 'effective' priority of a task. It does |
| 4389 | * not touch ->normal_prio like __setscheduler(). |
| 4390 | * |
| 4391 | * Used by the rt_mutex code to implement priority inheritance logic. |
| 4392 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4393 | void rt_mutex_setprio(struct task_struct *p, int prio) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4394 | { |
| 4395 | unsigned long flags; |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4396 | int oldprio, on_rq, running; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4397 | struct rq *rq; |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4398 | const struct sched_class *prev_class = p->sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4399 | |
| 4400 | BUG_ON(prio < 0 || prio > MAX_PRIO); |
| 4401 | |
| 4402 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4403 | update_rq_clock(rq); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4404 | |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4405 | oldprio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4406 | on_rq = p->se.on_rq; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 4407 | running = task_current(rq, p); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4408 | if (on_rq) |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4409 | dequeue_task(rq, p, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4410 | if (running) |
| 4411 | p->sched_class->put_prev_task(rq, p); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4412 | |
| 4413 | if (rt_prio(prio)) |
| 4414 | p->sched_class = &rt_sched_class; |
| 4415 | else |
| 4416 | p->sched_class = &fair_sched_class; |
| 4417 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4418 | p->prio = prio; |
| 4419 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4420 | if (running) |
| 4421 | p->sched_class->set_curr_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4422 | if (on_rq) { |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4423 | enqueue_task(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4424 | |
| 4425 | check_class_changed(rq, p, prev_class, oldprio, running); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4426 | } |
| 4427 | task_rq_unlock(rq, &flags); |
| 4428 | } |
| 4429 | |
| 4430 | #endif |
| 4431 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4432 | void set_user_nice(struct task_struct *p, long nice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4434 | int old_prio, delta, on_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4436 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | |
| 4438 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) |
| 4439 | return; |
| 4440 | /* |
| 4441 | * We have to be careful, if called from sys_setpriority(), |
| 4442 | * the task might be in the middle of scheduling on another CPU. |
| 4443 | */ |
| 4444 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4445 | update_rq_clock(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | /* |
| 4447 | * The RT priorities are set via sched_setscheduler(), but we still |
| 4448 | * allow the 'normal' nice value to be set - but as expected |
| 4449 | * it wont have any effect on scheduling until the task is |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4450 | * SCHED_FIFO/SCHED_RR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | */ |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4452 | if (task_has_rt_policy(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | p->static_prio = NICE_TO_PRIO(nice); |
| 4454 | goto out_unlock; |
| 4455 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4456 | on_rq = p->se.on_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 4457 | if (on_rq) { |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4458 | dequeue_task(rq, p, 0); |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 4459 | dec_load(rq, p); |
| 4460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | p->static_prio = NICE_TO_PRIO(nice); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 4463 | set_load_weight(p); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4464 | old_prio = p->prio; |
| 4465 | p->prio = effective_prio(p); |
| 4466 | delta = p->prio - old_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4468 | if (on_rq) { |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4469 | enqueue_task(rq, p, 0); |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 4470 | inc_load(rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | /* |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4472 | * If the task increased its priority or is running and |
| 4473 | * lowered its priority, then reschedule its CPU: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | */ |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4475 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | resched_task(rq->curr); |
| 4477 | } |
| 4478 | out_unlock: |
| 4479 | task_rq_unlock(rq, &flags); |
| 4480 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | EXPORT_SYMBOL(set_user_nice); |
| 4482 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4483 | /* |
| 4484 | * can_nice - check if a task can reduce its nice value |
| 4485 | * @p: task |
| 4486 | * @nice: nice value |
| 4487 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4488 | int can_nice(const struct task_struct *p, const int nice) |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4489 | { |
Matt Mackall | 024f474 | 2005-08-18 11:24:19 -0700 | [diff] [blame] | 4490 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
| 4491 | int nice_rlim = 20 - nice; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4492 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4493 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
| 4494 | capable(CAP_SYS_NICE)); |
| 4495 | } |
| 4496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | #ifdef __ARCH_WANT_SYS_NICE |
| 4498 | |
| 4499 | /* |
| 4500 | * sys_nice - change the priority of the current process. |
| 4501 | * @increment: priority increment |
| 4502 | * |
| 4503 | * sys_setpriority is a more generic, but much slower function that |
| 4504 | * does similar things. |
| 4505 | */ |
| 4506 | asmlinkage long sys_nice(int increment) |
| 4507 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4508 | long nice, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | |
| 4510 | /* |
| 4511 | * Setpriority might change our priority at the same moment. |
| 4512 | * We don't have to worry. Conceptually one call occurs first |
| 4513 | * and we have a single winner. |
| 4514 | */ |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4515 | if (increment < -40) |
| 4516 | increment = -40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | if (increment > 40) |
| 4518 | increment = 40; |
| 4519 | |
| 4520 | nice = PRIO_TO_NICE(current->static_prio) + increment; |
| 4521 | if (nice < -20) |
| 4522 | nice = -20; |
| 4523 | if (nice > 19) |
| 4524 | nice = 19; |
| 4525 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4526 | if (increment < 0 && !can_nice(current, nice)) |
| 4527 | return -EPERM; |
| 4528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | retval = security_task_setnice(current, nice); |
| 4530 | if (retval) |
| 4531 | return retval; |
| 4532 | |
| 4533 | set_user_nice(current, nice); |
| 4534 | return 0; |
| 4535 | } |
| 4536 | |
| 4537 | #endif |
| 4538 | |
| 4539 | /** |
| 4540 | * task_prio - return the priority value of a given task. |
| 4541 | * @p: the task in question. |
| 4542 | * |
| 4543 | * This is the priority value as seen by users in /proc. |
| 4544 | * RT tasks are offset by -200. Normal tasks are centered |
| 4545 | * around 0, value goes from -16 to +15. |
| 4546 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4547 | int task_prio(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | { |
| 4549 | return p->prio - MAX_RT_PRIO; |
| 4550 | } |
| 4551 | |
| 4552 | /** |
| 4553 | * task_nice - return the nice value of a given task. |
| 4554 | * @p: the task in question. |
| 4555 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4556 | int task_nice(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | { |
| 4558 | return TASK_NICE(p); |
| 4559 | } |
Pavel Roskin | 150d8be | 2008-03-05 16:56:37 -0500 | [diff] [blame] | 4560 | EXPORT_SYMBOL(task_nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | |
| 4562 | /** |
| 4563 | * idle_cpu - is a given cpu idle currently? |
| 4564 | * @cpu: the processor in question. |
| 4565 | */ |
| 4566 | int idle_cpu(int cpu) |
| 4567 | { |
| 4568 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; |
| 4569 | } |
| 4570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | /** |
| 4572 | * idle_task - return the idle task for a given cpu. |
| 4573 | * @cpu: the processor in question. |
| 4574 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4575 | struct task_struct *idle_task(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | { |
| 4577 | return cpu_rq(cpu)->idle; |
| 4578 | } |
| 4579 | |
| 4580 | /** |
| 4581 | * find_process_by_pid - find a process with a matching PID value. |
| 4582 | * @pid: the pid in question. |
| 4583 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4584 | static struct task_struct *find_process_by_pid(pid_t pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | { |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 4586 | return pid ? find_task_by_vpid(pid) : current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | /* Actually do priority change: must hold rq lock. */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4590 | static void |
| 4591 | __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4593 | BUG_ON(p->se.on_rq); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | p->policy = policy; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4596 | switch (p->policy) { |
| 4597 | case SCHED_NORMAL: |
| 4598 | case SCHED_BATCH: |
| 4599 | case SCHED_IDLE: |
| 4600 | p->sched_class = &fair_sched_class; |
| 4601 | break; |
| 4602 | case SCHED_FIFO: |
| 4603 | case SCHED_RR: |
| 4604 | p->sched_class = &rt_sched_class; |
| 4605 | break; |
| 4606 | } |
| 4607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | p->rt_priority = prio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4609 | p->normal_prio = normal_prio(p); |
| 4610 | /* we are holding p->pi_lock already */ |
| 4611 | p->prio = rt_mutex_getprio(p); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 4612 | set_load_weight(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 4616 | * 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] | 4617 | * @p: the task in question. |
| 4618 | * @policy: new policy. |
| 4619 | * @param: structure containing the new RT priority. |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4620 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 4621 | * NOTE that the task may be already dead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4623 | int sched_setscheduler(struct task_struct *p, int policy, |
| 4624 | struct sched_param *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4626 | int retval, oldprio, oldpolicy = -1, on_rq, running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | unsigned long flags; |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4628 | const struct sched_class *prev_class = p->sched_class; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4629 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | |
Steven Rostedt | 66e5393 | 2006-06-27 02:54:44 -0700 | [diff] [blame] | 4631 | /* may grab non-irq protected spin_locks */ |
| 4632 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | recheck: |
| 4634 | /* double check policy once rq lock held */ |
| 4635 | if (policy < 0) |
| 4636 | policy = oldpolicy = p->policy; |
| 4637 | else if (policy != SCHED_FIFO && policy != SCHED_RR && |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4638 | policy != SCHED_NORMAL && policy != SCHED_BATCH && |
| 4639 | policy != SCHED_IDLE) |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4640 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | /* |
| 4642 | * Valid priorities for SCHED_FIFO and SCHED_RR are |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4643 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
| 4644 | * SCHED_BATCH and SCHED_IDLE is 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | */ |
| 4646 | if (param->sched_priority < 0 || |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4647 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
Steven Rostedt | d46523e | 2005-07-25 16:28:39 -0400 | [diff] [blame] | 4648 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | return -EINVAL; |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4650 | if (rt_policy(policy) != (param->sched_priority != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4651 | return -EINVAL; |
| 4652 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4653 | /* |
| 4654 | * Allow unprivileged RT tasks to decrease priority: |
| 4655 | */ |
| 4656 | if (!capable(CAP_SYS_NICE)) { |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4657 | if (rt_policy(policy)) { |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4658 | unsigned long rlim_rtprio; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4659 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4660 | if (!lock_task_sighand(p, &flags)) |
| 4661 | return -ESRCH; |
| 4662 | rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur; |
| 4663 | unlock_task_sighand(p, &flags); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4664 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4665 | /* can't set/change the rt policy */ |
| 4666 | if (policy != p->policy && !rlim_rtprio) |
| 4667 | return -EPERM; |
| 4668 | |
| 4669 | /* can't increase priority */ |
| 4670 | if (param->sched_priority > p->rt_priority && |
| 4671 | param->sched_priority > rlim_rtprio) |
| 4672 | return -EPERM; |
| 4673 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4674 | /* |
| 4675 | * Like positive nice levels, dont allow tasks to |
| 4676 | * move out of SCHED_IDLE either: |
| 4677 | */ |
| 4678 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) |
| 4679 | return -EPERM; |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4680 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4681 | /* can't change other user's priorities */ |
| 4682 | if ((current->euid != p->euid) && |
| 4683 | (current->euid != p->uid)) |
| 4684 | return -EPERM; |
| 4685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 4687 | #ifdef CONFIG_RT_GROUP_SCHED |
| 4688 | /* |
| 4689 | * Do not allow realtime tasks into groups that have no runtime |
| 4690 | * assigned. |
| 4691 | */ |
| 4692 | if (rt_policy(policy) && task_group(p)->rt_runtime == 0) |
| 4693 | return -EPERM; |
| 4694 | #endif |
| 4695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | retval = security_task_setscheduler(p, policy, param); |
| 4697 | if (retval) |
| 4698 | return retval; |
| 4699 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4700 | * make sure no PI-waiters arrive (or leave) while we are |
| 4701 | * changing the priority of the task: |
| 4702 | */ |
| 4703 | spin_lock_irqsave(&p->pi_lock, flags); |
| 4704 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4705 | * To be able to change p->policy safely, the apropriate |
| 4706 | * runqueue lock must be held. |
| 4707 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4708 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | /* recheck policy now with rq lock held */ |
| 4710 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
| 4711 | policy = oldpolicy = -1; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4712 | __task_rq_unlock(rq); |
| 4713 | spin_unlock_irqrestore(&p->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | goto recheck; |
| 4715 | } |
Ingo Molnar | 2daa357 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 4716 | update_rq_clock(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4717 | on_rq = p->se.on_rq; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 4718 | running = task_current(rq, p); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4719 | if (on_rq) |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4720 | deactivate_task(rq, p, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4721 | if (running) |
| 4722 | p->sched_class->put_prev_task(rq, p); |
Dmitry Adamushko | f6b53205 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | oldprio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4725 | __setscheduler(rq, p, policy, param->sched_priority); |
Dmitry Adamushko | f6b53205 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4726 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 4727 | if (running) |
| 4728 | p->sched_class->set_curr_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4729 | if (on_rq) { |
| 4730 | activate_task(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4731 | |
| 4732 | check_class_changed(rq, p, prev_class, oldprio, running); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4734 | __task_rq_unlock(rq); |
| 4735 | spin_unlock_irqrestore(&p->pi_lock, flags); |
| 4736 | |
Thomas Gleixner | 95e02ca | 2006-06-27 02:55:02 -0700 | [diff] [blame] | 4737 | rt_mutex_adjust_pi(p); |
| 4738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | return 0; |
| 4740 | } |
| 4741 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
| 4742 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4743 | static int |
| 4744 | 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] | 4745 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | struct sched_param lparam; |
| 4747 | struct task_struct *p; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4748 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | |
| 4750 | if (!param || pid < 0) |
| 4751 | return -EINVAL; |
| 4752 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) |
| 4753 | return -EFAULT; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4754 | |
| 4755 | rcu_read_lock(); |
| 4756 | retval = -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | p = find_process_by_pid(pid); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4758 | if (p != NULL) |
| 4759 | retval = sched_setscheduler(p, policy, &lparam); |
| 4760 | rcu_read_unlock(); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | return retval; |
| 4763 | } |
| 4764 | |
| 4765 | /** |
| 4766 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority |
| 4767 | * @pid: the pid in question. |
| 4768 | * @policy: new policy. |
| 4769 | * @param: structure containing the new RT priority. |
| 4770 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4771 | asmlinkage long |
| 4772 | 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] | 4773 | { |
Jason Baron | c21761f | 2006-01-18 17:43:03 -0800 | [diff] [blame] | 4774 | /* negative values for policy are not valid */ |
| 4775 | if (policy < 0) |
| 4776 | return -EINVAL; |
| 4777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4778 | return do_sched_setscheduler(pid, policy, param); |
| 4779 | } |
| 4780 | |
| 4781 | /** |
| 4782 | * sys_sched_setparam - set/change the RT priority of a thread |
| 4783 | * @pid: the pid in question. |
| 4784 | * @param: structure containing the new RT priority. |
| 4785 | */ |
| 4786 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) |
| 4787 | { |
| 4788 | return do_sched_setscheduler(pid, -1, param); |
| 4789 | } |
| 4790 | |
| 4791 | /** |
| 4792 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread |
| 4793 | * @pid: the pid in question. |
| 4794 | */ |
| 4795 | asmlinkage long sys_sched_getscheduler(pid_t pid) |
| 4796 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4797 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4798 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | |
| 4800 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4801 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4802 | |
| 4803 | retval = -ESRCH; |
| 4804 | read_lock(&tasklist_lock); |
| 4805 | p = find_process_by_pid(pid); |
| 4806 | if (p) { |
| 4807 | retval = security_task_getscheduler(p); |
| 4808 | if (!retval) |
| 4809 | retval = p->policy; |
| 4810 | } |
| 4811 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | return retval; |
| 4813 | } |
| 4814 | |
| 4815 | /** |
| 4816 | * sys_sched_getscheduler - get the RT priority of a thread |
| 4817 | * @pid: the pid in question. |
| 4818 | * @param: structure containing the RT priority. |
| 4819 | */ |
| 4820 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) |
| 4821 | { |
| 4822 | struct sched_param lp; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4823 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4824 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | |
| 4826 | if (!param || pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4827 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | |
| 4829 | read_lock(&tasklist_lock); |
| 4830 | p = find_process_by_pid(pid); |
| 4831 | retval = -ESRCH; |
| 4832 | if (!p) |
| 4833 | goto out_unlock; |
| 4834 | |
| 4835 | retval = security_task_getscheduler(p); |
| 4836 | if (retval) |
| 4837 | goto out_unlock; |
| 4838 | |
| 4839 | lp.sched_priority = p->rt_priority; |
| 4840 | read_unlock(&tasklist_lock); |
| 4841 | |
| 4842 | /* |
| 4843 | * This one might sleep, we cannot do it with a spinlock held ... |
| 4844 | */ |
| 4845 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; |
| 4846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | return retval; |
| 4848 | |
| 4849 | out_unlock: |
| 4850 | read_unlock(&tasklist_lock); |
| 4851 | return retval; |
| 4852 | } |
| 4853 | |
| 4854 | long sched_setaffinity(pid_t pid, cpumask_t new_mask) |
| 4855 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | cpumask_t cpus_allowed; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4857 | struct task_struct *p; |
| 4858 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4860 | get_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | read_lock(&tasklist_lock); |
| 4862 | |
| 4863 | p = find_process_by_pid(pid); |
| 4864 | if (!p) { |
| 4865 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4866 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4867 | return -ESRCH; |
| 4868 | } |
| 4869 | |
| 4870 | /* |
| 4871 | * It is not safe to call set_cpus_allowed with the |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4872 | * tasklist_lock held. We will bump the task_struct's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | * usage count and then drop tasklist_lock. |
| 4874 | */ |
| 4875 | get_task_struct(p); |
| 4876 | read_unlock(&tasklist_lock); |
| 4877 | |
| 4878 | retval = -EPERM; |
| 4879 | if ((current->euid != p->euid) && (current->euid != p->uid) && |
| 4880 | !capable(CAP_SYS_NICE)) |
| 4881 | goto out_unlock; |
| 4882 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 4883 | retval = security_task_setscheduler(p, 0, NULL); |
| 4884 | if (retval) |
| 4885 | goto out_unlock; |
| 4886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | cpus_allowed = cpuset_cpus_allowed(p); |
| 4888 | cpus_and(new_mask, new_mask, cpus_allowed); |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 4889 | again: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | retval = set_cpus_allowed(p, new_mask); |
| 4891 | |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 4892 | if (!retval) { |
| 4893 | cpus_allowed = cpuset_cpus_allowed(p); |
| 4894 | if (!cpus_subset(new_mask, cpus_allowed)) { |
| 4895 | /* |
| 4896 | * We must have raced with a concurrent cpuset |
| 4897 | * update. Just reset the cpus_allowed to the |
| 4898 | * cpuset's cpus_allowed |
| 4899 | */ |
| 4900 | new_mask = cpus_allowed; |
| 4901 | goto again; |
| 4902 | } |
| 4903 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | out_unlock: |
| 4905 | put_task_struct(p); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4906 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | return retval; |
| 4908 | } |
| 4909 | |
| 4910 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, |
| 4911 | cpumask_t *new_mask) |
| 4912 | { |
| 4913 | if (len < sizeof(cpumask_t)) { |
| 4914 | memset(new_mask, 0, sizeof(cpumask_t)); |
| 4915 | } else if (len > sizeof(cpumask_t)) { |
| 4916 | len = sizeof(cpumask_t); |
| 4917 | } |
| 4918 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
| 4919 | } |
| 4920 | |
| 4921 | /** |
| 4922 | * sys_sched_setaffinity - set the cpu affinity of a process |
| 4923 | * @pid: pid of the process |
| 4924 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 4925 | * @user_mask_ptr: user-space pointer to the new cpu mask |
| 4926 | */ |
| 4927 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, |
| 4928 | unsigned long __user *user_mask_ptr) |
| 4929 | { |
| 4930 | cpumask_t new_mask; |
| 4931 | int retval; |
| 4932 | |
| 4933 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); |
| 4934 | if (retval) |
| 4935 | return retval; |
| 4936 | |
| 4937 | return sched_setaffinity(pid, new_mask); |
| 4938 | } |
| 4939 | |
| 4940 | /* |
| 4941 | * Represents all cpu's present in the system |
| 4942 | * In systems capable of hotplug, this map could dynamically grow |
| 4943 | * as new cpu's are detected in the system via any platform specific |
| 4944 | * method, such as ACPI for e.g. |
| 4945 | */ |
| 4946 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4947 | cpumask_t cpu_present_map __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | EXPORT_SYMBOL(cpu_present_map); |
| 4949 | |
| 4950 | #ifndef CONFIG_SMP |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4951 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 4952 | EXPORT_SYMBOL(cpu_online_map); |
| 4953 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4954 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 4955 | EXPORT_SYMBOL(cpu_possible_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | #endif |
| 4957 | |
| 4958 | long sched_getaffinity(pid_t pid, cpumask_t *mask) |
| 4959 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4960 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4963 | get_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | read_lock(&tasklist_lock); |
| 4965 | |
| 4966 | retval = -ESRCH; |
| 4967 | p = find_process_by_pid(pid); |
| 4968 | if (!p) |
| 4969 | goto out_unlock; |
| 4970 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 4971 | retval = security_task_getscheduler(p); |
| 4972 | if (retval) |
| 4973 | goto out_unlock; |
| 4974 | |
Jack Steiner | 2f7016d | 2006-02-01 03:05:18 -0800 | [diff] [blame] | 4975 | cpus_and(*mask, p->cpus_allowed, cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | |
| 4977 | out_unlock: |
| 4978 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4979 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | |
Ulrich Drepper | 9531b62 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 4981 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | /** |
| 4985 | * sys_sched_getaffinity - get the cpu affinity of a process |
| 4986 | * @pid: pid of the process |
| 4987 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 4988 | * @user_mask_ptr: user-space pointer to hold the current cpu mask |
| 4989 | */ |
| 4990 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, |
| 4991 | unsigned long __user *user_mask_ptr) |
| 4992 | { |
| 4993 | int ret; |
| 4994 | cpumask_t mask; |
| 4995 | |
| 4996 | if (len < sizeof(cpumask_t)) |
| 4997 | return -EINVAL; |
| 4998 | |
| 4999 | ret = sched_getaffinity(pid, &mask); |
| 5000 | if (ret < 0) |
| 5001 | return ret; |
| 5002 | |
| 5003 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) |
| 5004 | return -EFAULT; |
| 5005 | |
| 5006 | return sizeof(cpumask_t); |
| 5007 | } |
| 5008 | |
| 5009 | /** |
| 5010 | * sys_sched_yield - yield the current processor to other threads. |
| 5011 | * |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5012 | * This function yields the current CPU to other tasks. If there are no |
| 5013 | * other threads running on this CPU then this function will return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | */ |
| 5015 | asmlinkage long sys_sched_yield(void) |
| 5016 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5017 | struct rq *rq = this_rq_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 5019 | schedstat_inc(rq, yld_count); |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5020 | current->sched_class->yield_task(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | |
| 5022 | /* |
| 5023 | * Since we are going to call schedule() anyway, there's |
| 5024 | * no need to preempt or enable interrupts: |
| 5025 | */ |
| 5026 | __release(rq->lock); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 5027 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | _raw_spin_unlock(&rq->lock); |
| 5029 | preempt_enable_no_resched(); |
| 5030 | |
| 5031 | schedule(); |
| 5032 | |
| 5033 | return 0; |
| 5034 | } |
| 5035 | |
Andrew Morton | e7b3840 | 2006-06-30 01:56:00 -0700 | [diff] [blame] | 5036 | static void __cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | { |
Ingo Molnar | 8e0a43d | 2006-06-23 02:05:23 -0700 | [diff] [blame] | 5038 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 5039 | __might_sleep(__FILE__, __LINE__); |
| 5040 | #endif |
Ingo Molnar | 5bbcfd9 | 2005-07-07 17:57:04 -0700 | [diff] [blame] | 5041 | /* |
| 5042 | * The BKS might be reacquired before we have dropped |
| 5043 | * PREEMPT_ACTIVE, which could trigger a second |
| 5044 | * cond_resched() call. |
| 5045 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | do { |
| 5047 | add_preempt_count(PREEMPT_ACTIVE); |
| 5048 | schedule(); |
| 5049 | sub_preempt_count(PREEMPT_ACTIVE); |
| 5050 | } while (need_resched()); |
| 5051 | } |
| 5052 | |
Herbert Xu | 02b67cc3 | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 5053 | #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY) |
| 5054 | int __sched _cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | { |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 5056 | if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) && |
| 5057 | system_state == SYSTEM_RUNNING) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5058 | __cond_resched(); |
| 5059 | return 1; |
| 5060 | } |
| 5061 | return 0; |
| 5062 | } |
Herbert Xu | 02b67cc3 | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 5063 | EXPORT_SYMBOL(_cond_resched); |
| 5064 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | |
| 5066 | /* |
| 5067 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, |
| 5068 | * call schedule, and on return reacquire the lock. |
| 5069 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5070 | * 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] | 5071 | * operations here to prevent schedule() from being called twice (once via |
| 5072 | * spin_unlock(), once by hand). |
| 5073 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5074 | int cond_resched_lock(spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | { |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5076 | int resched = need_resched() && system_state == SYSTEM_RUNNING; |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5077 | int ret = 0; |
| 5078 | |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5079 | if (spin_needbreak(lock) || resched) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | spin_unlock(lock); |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 5081 | if (resched && need_resched()) |
| 5082 | __cond_resched(); |
| 5083 | else |
| 5084 | cpu_relax(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5085 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | spin_lock(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | } |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 5088 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | EXPORT_SYMBOL(cond_resched_lock); |
| 5091 | |
| 5092 | int __sched cond_resched_softirq(void) |
| 5093 | { |
| 5094 | BUG_ON(!in_softirq()); |
| 5095 | |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 5096 | if (need_resched() && system_state == SYSTEM_RUNNING) { |
Thomas Gleixner | 98d82567 | 2007-05-23 13:58:18 -0700 | [diff] [blame] | 5097 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | __cond_resched(); |
| 5099 | local_bh_disable(); |
| 5100 | return 1; |
| 5101 | } |
| 5102 | return 0; |
| 5103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | EXPORT_SYMBOL(cond_resched_softirq); |
| 5105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | /** |
| 5107 | * yield - yield the current processor to other threads. |
| 5108 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 5109 | * This is a shortcut for kernel-space yielding - it marks the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | * thread runnable and calls sys_sched_yield(). |
| 5111 | */ |
| 5112 | void __sched yield(void) |
| 5113 | { |
| 5114 | set_current_state(TASK_RUNNING); |
| 5115 | sys_sched_yield(); |
| 5116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | EXPORT_SYMBOL(yield); |
| 5118 | |
| 5119 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5120 | * 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] | 5121 | * that process accounting knows that this is a task in IO wait state. |
| 5122 | * |
| 5123 | * But don't do that if it is a deliberate, throttling IO wait (this task |
| 5124 | * has set its backing_dev_info: the queue against which it should throttle) |
| 5125 | */ |
| 5126 | void __sched io_schedule(void) |
| 5127 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5128 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5129 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5130 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | atomic_inc(&rq->nr_iowait); |
| 5132 | schedule(); |
| 5133 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5134 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | EXPORT_SYMBOL(io_schedule); |
| 5137 | |
| 5138 | long __sched io_schedule_timeout(long timeout) |
| 5139 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5140 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | long ret; |
| 5142 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5143 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | atomic_inc(&rq->nr_iowait); |
| 5145 | ret = schedule_timeout(timeout); |
| 5146 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5147 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | return ret; |
| 5149 | } |
| 5150 | |
| 5151 | /** |
| 5152 | * sys_sched_get_priority_max - return maximum RT priority. |
| 5153 | * @policy: scheduling class. |
| 5154 | * |
| 5155 | * this syscall returns the maximum rt_priority that can be used |
| 5156 | * by a given scheduling class. |
| 5157 | */ |
| 5158 | asmlinkage long sys_sched_get_priority_max(int policy) |
| 5159 | { |
| 5160 | int ret = -EINVAL; |
| 5161 | |
| 5162 | switch (policy) { |
| 5163 | case SCHED_FIFO: |
| 5164 | case SCHED_RR: |
| 5165 | ret = MAX_USER_RT_PRIO-1; |
| 5166 | break; |
| 5167 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5168 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5169 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | ret = 0; |
| 5171 | break; |
| 5172 | } |
| 5173 | return ret; |
| 5174 | } |
| 5175 | |
| 5176 | /** |
| 5177 | * sys_sched_get_priority_min - return minimum RT priority. |
| 5178 | * @policy: scheduling class. |
| 5179 | * |
| 5180 | * this syscall returns the minimum rt_priority that can be used |
| 5181 | * by a given scheduling class. |
| 5182 | */ |
| 5183 | asmlinkage long sys_sched_get_priority_min(int policy) |
| 5184 | { |
| 5185 | int ret = -EINVAL; |
| 5186 | |
| 5187 | switch (policy) { |
| 5188 | case SCHED_FIFO: |
| 5189 | case SCHED_RR: |
| 5190 | ret = 1; |
| 5191 | break; |
| 5192 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5193 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5194 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | ret = 0; |
| 5196 | } |
| 5197 | return ret; |
| 5198 | } |
| 5199 | |
| 5200 | /** |
| 5201 | * sys_sched_rr_get_interval - return the default timeslice of a process. |
| 5202 | * @pid: pid of the process. |
| 5203 | * @interval: userspace pointer to the timeslice value. |
| 5204 | * |
| 5205 | * this syscall writes the default timeslice value of a given process |
| 5206 | * into the user-space timespec buffer. A value of '0' means infinity. |
| 5207 | */ |
| 5208 | asmlinkage |
| 5209 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) |
| 5210 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5211 | struct task_struct *p; |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5212 | unsigned int time_slice; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5213 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | struct timespec t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | |
| 5216 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5217 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | |
| 5219 | retval = -ESRCH; |
| 5220 | read_lock(&tasklist_lock); |
| 5221 | p = find_process_by_pid(pid); |
| 5222 | if (!p) |
| 5223 | goto out_unlock; |
| 5224 | |
| 5225 | retval = security_task_getscheduler(p); |
| 5226 | if (retval) |
| 5227 | goto out_unlock; |
| 5228 | |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5229 | /* |
| 5230 | * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER |
| 5231 | * tasks that are on an otherwise idle runqueue: |
| 5232 | */ |
| 5233 | time_slice = 0; |
| 5234 | if (p->policy == SCHED_RR) { |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5235 | time_slice = DEF_TIMESLICE; |
Miao Xie | 1868f95 | 2008-03-07 09:35:06 +0800 | [diff] [blame] | 5236 | } else if (p->policy != SCHED_FIFO) { |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5237 | struct sched_entity *se = &p->se; |
| 5238 | unsigned long flags; |
| 5239 | struct rq *rq; |
| 5240 | |
| 5241 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5242 | if (rq->cfs.load.weight) |
| 5243 | time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5244 | task_rq_unlock(rq, &flags); |
| 5245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | read_unlock(&tasklist_lock); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5247 | jiffies_to_timespec(time_slice, &t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | return retval; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | out_unlock: |
| 5252 | read_unlock(&tasklist_lock); |
| 5253 | return retval; |
| 5254 | } |
| 5255 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5256 | static const char stat_nam[] = "RSDTtZX"; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5257 | |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5258 | void sched_show_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | unsigned long free = 0; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5261 | unsigned state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | state = p->state ? __ffs(p->state) + 1 : 0; |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5264 | printk(KERN_INFO "%-13.13s %c", p->comm, |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5265 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5266 | #if BITS_PER_LONG == 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | if (state == TASK_RUNNING) |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5268 | printk(KERN_CONT " running "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | else |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5270 | printk(KERN_CONT " %08lx ", thread_saved_pc(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | #else |
| 5272 | if (state == TASK_RUNNING) |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5273 | printk(KERN_CONT " running task "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | else |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5275 | printk(KERN_CONT " %016lx ", thread_saved_pc(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | #endif |
| 5277 | #ifdef CONFIG_DEBUG_STACK_USAGE |
| 5278 | { |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 5279 | unsigned long *n = end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | while (!*n) |
| 5281 | n++; |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 5282 | free = (unsigned long)n - (unsigned long)end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | } |
| 5284 | #endif |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5285 | printk(KERN_CONT "%5lu %5d %6d\n", free, |
Roland McGrath | fcfd50a | 2008-01-09 00:03:23 -0800 | [diff] [blame] | 5286 | task_pid_nr(p), task_pid_nr(p->real_parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | |
Nick Piggin | 5fb5e6d | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 5288 | show_stack(p, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5289 | } |
| 5290 | |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 5291 | void show_state_filter(unsigned long state_filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5293 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5295 | #if BITS_PER_LONG == 32 |
| 5296 | printk(KERN_INFO |
| 5297 | " task PC stack pid father\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | #else |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5299 | printk(KERN_INFO |
| 5300 | " task PC stack pid father\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | #endif |
| 5302 | read_lock(&tasklist_lock); |
| 5303 | do_each_thread(g, p) { |
| 5304 | /* |
| 5305 | * reset the NMI-timeout, listing all files on a slow |
| 5306 | * console might take alot of time: |
| 5307 | */ |
| 5308 | touch_nmi_watchdog(); |
Ingo Molnar | 39bc89f | 2007-04-25 20:50:03 -0700 | [diff] [blame] | 5309 | if (!state_filter || (p->state & state_filter)) |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5310 | sched_show_task(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | } while_each_thread(g, p); |
| 5312 | |
Jeremy Fitzhardinge | 04c9167 | 2007-05-08 00:28:05 -0700 | [diff] [blame] | 5313 | touch_all_softlockup_watchdogs(); |
| 5314 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5315 | #ifdef CONFIG_SCHED_DEBUG |
| 5316 | sysrq_sched_debug_show(); |
| 5317 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | read_unlock(&tasklist_lock); |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 5319 | /* |
| 5320 | * Only show locks if all tasks are dumped: |
| 5321 | */ |
| 5322 | if (state_filter == -1) |
| 5323 | debug_show_all_locks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | } |
| 5325 | |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5326 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) |
| 5327 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5328 | idle->sched_class = &idle_sched_class; |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5329 | } |
| 5330 | |
Ingo Molnar | f340c0d | 2005-06-28 16:40:42 +0200 | [diff] [blame] | 5331 | /** |
| 5332 | * init_idle - set up an idle thread for a given CPU |
| 5333 | * @idle: task in question |
| 5334 | * @cpu: cpu the idle task belongs to |
| 5335 | * |
| 5336 | * NOTE: this function does not set the idle thread's NEED_RESCHED |
| 5337 | * flag, to make booting more robust. |
| 5338 | */ |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 5339 | void __cpuinit init_idle(struct task_struct *idle, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5341 | struct rq *rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | unsigned long flags; |
| 5343 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5344 | __sched_fork(idle); |
| 5345 | idle->se.exec_start = sched_clock(); |
| 5346 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5347 | idle->prio = idle->normal_prio = MAX_PRIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | idle->cpus_allowed = cpumask_of_cpu(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5349 | __set_task_cpu(idle, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | |
| 5351 | spin_lock_irqsave(&rq->lock, flags); |
| 5352 | rq->curr = rq->idle = idle; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 5353 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
| 5354 | idle->oncpu = 1; |
| 5355 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5357 | |
| 5358 | /* Set the preempt count _outside_ the spinlocks! */ |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 5359 | task_thread_info(idle)->preempt_count = 0; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 5360 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5361 | /* |
| 5362 | * The idle tasks have their own, simple scheduling class: |
| 5363 | */ |
| 5364 | idle->sched_class = &idle_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | } |
| 5366 | |
| 5367 | /* |
| 5368 | * In a system that switches off the HZ timer nohz_cpu_mask |
| 5369 | * indicates which cpus entered this state. This is used |
| 5370 | * in the rcu update to wait only for active cpus. For system |
| 5371 | * which do not switch off the HZ timer nohz_cpu_mask should |
| 5372 | * always be CPU_MASK_NONE. |
| 5373 | */ |
| 5374 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; |
| 5375 | |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 5376 | /* |
| 5377 | * Increase the granularity value when there are more CPUs, |
| 5378 | * because with more CPUs the 'effective latency' as visible |
| 5379 | * to users decreases. But the relationship is not linear, |
| 5380 | * so pick a second-best guess by going with the log2 of the |
| 5381 | * number of CPUs. |
| 5382 | * |
| 5383 | * This idea comes from the SD scheduler of Con Kolivas: |
| 5384 | */ |
| 5385 | static inline void sched_init_granularity(void) |
| 5386 | { |
| 5387 | unsigned int factor = 1 + ilog2(num_online_cpus()); |
| 5388 | const unsigned long limit = 200000000; |
| 5389 | |
| 5390 | sysctl_sched_min_granularity *= factor; |
| 5391 | if (sysctl_sched_min_granularity > limit) |
| 5392 | sysctl_sched_min_granularity = limit; |
| 5393 | |
| 5394 | sysctl_sched_latency *= factor; |
| 5395 | if (sysctl_sched_latency > limit) |
| 5396 | sysctl_sched_latency = limit; |
| 5397 | |
| 5398 | sysctl_sched_wakeup_granularity *= factor; |
| 5399 | sysctl_sched_batch_wakeup_granularity *= factor; |
| 5400 | } |
| 5401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | #ifdef CONFIG_SMP |
| 5403 | /* |
| 5404 | * This is how migration works: |
| 5405 | * |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5406 | * 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] | 5407 | * runqueue and wake up that CPU's migration thread. |
| 5408 | * 2) we down() the locked semaphore => thread blocks. |
| 5409 | * 3) migration thread wakes up (implicitly it forces the migrated |
| 5410 | * thread off the CPU) |
| 5411 | * 4) it gets the migration request and checks whether the migrated |
| 5412 | * task is still in the wrong runqueue. |
| 5413 | * 5) if it's in the wrong runqueue then the migration thread removes |
| 5414 | * it and puts it into the right queue. |
| 5415 | * 6) migration thread up()s the semaphore. |
| 5416 | * 7) we wake up and the migration is done. |
| 5417 | */ |
| 5418 | |
| 5419 | /* |
| 5420 | * Change a given task's CPU affinity. Migrate the thread to a |
| 5421 | * proper CPU and schedule it away if the CPU it's executing on |
| 5422 | * is removed from the allowed bitmask. |
| 5423 | * |
| 5424 | * NOTE: the caller must have a valid reference to the task, the |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5425 | * task must not exit() & deallocate itself prematurely. The |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | * call is not atomic; no spinlocks may be held. |
| 5427 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5428 | int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5430 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5432 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5433 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | |
| 5435 | rq = task_rq_lock(p, &flags); |
| 5436 | if (!cpus_intersects(new_mask, cpu_online_map)) { |
| 5437 | ret = -EINVAL; |
| 5438 | goto out; |
| 5439 | } |
| 5440 | |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 5441 | if (p->sched_class->set_cpus_allowed) |
| 5442 | p->sched_class->set_cpus_allowed(p, &new_mask); |
| 5443 | else { |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 5444 | p->cpus_allowed = new_mask; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 5445 | p->rt.nr_cpus_allowed = cpus_weight(new_mask); |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 5446 | } |
| 5447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | /* Can the task run on the task's current CPU? If so, we're done */ |
| 5449 | if (cpu_isset(task_cpu(p), new_mask)) |
| 5450 | goto out; |
| 5451 | |
| 5452 | if (migrate_task(p, any_online_cpu(new_mask), &req)) { |
| 5453 | /* Need help from migration thread: drop lock and wait. */ |
| 5454 | task_rq_unlock(rq, &flags); |
| 5455 | wake_up_process(rq->migration_thread); |
| 5456 | wait_for_completion(&req.done); |
| 5457 | tlb_migrate_finish(p->mm); |
| 5458 | return 0; |
| 5459 | } |
| 5460 | out: |
| 5461 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5463 | return ret; |
| 5464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | EXPORT_SYMBOL_GPL(set_cpus_allowed); |
| 5466 | |
| 5467 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5468 | * 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] | 5469 | * this because either it can't run here any more (set_cpus_allowed() |
| 5470 | * away from this CPU, or CPU going down), or because we're |
| 5471 | * attempting to rebalance this task on exec (sched_exec). |
| 5472 | * |
| 5473 | * So we race with normal scheduler movements, but that's OK, as long |
| 5474 | * as the task is no longer on this CPU. |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5475 | * |
| 5476 | * Returns non-zero if task was successfully migrated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | */ |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5478 | 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] | 5479 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5480 | struct rq *rq_dest, *rq_src; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5481 | int ret = 0, on_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5482 | |
| 5483 | if (unlikely(cpu_is_offline(dest_cpu))) |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5484 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | |
| 5486 | rq_src = cpu_rq(src_cpu); |
| 5487 | rq_dest = cpu_rq(dest_cpu); |
| 5488 | |
| 5489 | double_rq_lock(rq_src, rq_dest); |
| 5490 | /* Already moved. */ |
| 5491 | if (task_cpu(p) != src_cpu) |
| 5492 | goto out; |
| 5493 | /* Affinity changed (again). */ |
| 5494 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) |
| 5495 | goto out; |
| 5496 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5497 | on_rq = p->se.on_rq; |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5498 | if (on_rq) |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5499 | deactivate_task(rq_src, p, 0); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | set_task_cpu(p, dest_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5502 | if (on_rq) { |
| 5503 | activate_task(rq_dest, p, 0); |
| 5504 | check_preempt_curr(rq_dest, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | } |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5506 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | out: |
| 5508 | double_rq_unlock(rq_src, rq_dest); |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5509 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | /* |
| 5513 | * migration_thread - this is a highprio system thread that performs |
| 5514 | * thread migration by bumping thread off CPU then 'pushing' onto |
| 5515 | * another runqueue. |
| 5516 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5517 | static int migration_thread(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | int cpu = (long)data; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5520 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | |
| 5522 | rq = cpu_rq(cpu); |
| 5523 | BUG_ON(rq->migration_thread != current); |
| 5524 | |
| 5525 | set_current_state(TASK_INTERRUPTIBLE); |
| 5526 | while (!kthread_should_stop()) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5527 | struct migration_req *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | spin_lock_irq(&rq->lock); |
| 5531 | |
| 5532 | if (cpu_is_offline(cpu)) { |
| 5533 | spin_unlock_irq(&rq->lock); |
| 5534 | goto wait_to_die; |
| 5535 | } |
| 5536 | |
| 5537 | if (rq->active_balance) { |
| 5538 | active_load_balance(rq, cpu); |
| 5539 | rq->active_balance = 0; |
| 5540 | } |
| 5541 | |
| 5542 | head = &rq->migration_queue; |
| 5543 | |
| 5544 | if (list_empty(head)) { |
| 5545 | spin_unlock_irq(&rq->lock); |
| 5546 | schedule(); |
| 5547 | set_current_state(TASK_INTERRUPTIBLE); |
| 5548 | continue; |
| 5549 | } |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5550 | req = list_entry(head->next, struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | list_del_init(head->next); |
| 5552 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 5553 | spin_unlock(&rq->lock); |
| 5554 | __migrate_task(req->task, cpu, req->dest_cpu); |
| 5555 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | |
| 5557 | complete(&req->done); |
| 5558 | } |
| 5559 | __set_current_state(TASK_RUNNING); |
| 5560 | return 0; |
| 5561 | |
| 5562 | wait_to_die: |
| 5563 | /* Wait for kthread_stop */ |
| 5564 | set_current_state(TASK_INTERRUPTIBLE); |
| 5565 | while (!kthread_should_stop()) { |
| 5566 | schedule(); |
| 5567 | set_current_state(TASK_INTERRUPTIBLE); |
| 5568 | } |
| 5569 | __set_current_state(TASK_RUNNING); |
| 5570 | return 0; |
| 5571 | } |
| 5572 | |
| 5573 | #ifdef CONFIG_HOTPLUG_CPU |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5574 | |
| 5575 | static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu) |
| 5576 | { |
| 5577 | int ret; |
| 5578 | |
| 5579 | local_irq_disable(); |
| 5580 | ret = __migrate_task(p, src_cpu, dest_cpu); |
| 5581 | local_irq_enable(); |
| 5582 | return ret; |
| 5583 | } |
| 5584 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5585 | /* |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 5586 | * 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] | 5587 | * NOTE: interrupts should be disabled by the caller |
| 5588 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5589 | 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] | 5590 | { |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5591 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | cpumask_t mask; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5593 | struct rq *rq; |
| 5594 | int dest_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5596 | do { |
| 5597 | /* On same node? */ |
| 5598 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); |
| 5599 | cpus_and(mask, mask, p->cpus_allowed); |
| 5600 | dest_cpu = any_online_cpu(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5602 | /* On any allowed CPU? */ |
| 5603 | if (dest_cpu == NR_CPUS) |
| 5604 | dest_cpu = any_online_cpu(p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5606 | /* No more Mr. Nice Guy. */ |
| 5607 | if (dest_cpu == NR_CPUS) { |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5608 | cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p); |
| 5609 | /* |
| 5610 | * Try to stay on the same cpuset, where the |
| 5611 | * current cpuset may be a subset of all cpus. |
| 5612 | * The cpuset_cpus_allowed_locked() variant of |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5613 | * cpuset_cpus_allowed() will not block. It must be |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5614 | * called within calls to cpuset_lock/cpuset_unlock. |
| 5615 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5616 | rq = task_rq_lock(p, &flags); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5617 | p->cpus_allowed = cpus_allowed; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5618 | dest_cpu = any_online_cpu(p->cpus_allowed); |
| 5619 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5621 | /* |
| 5622 | * Don't tell them about moving exiting tasks or |
| 5623 | * kernel threads (both mm NULL), since they never |
| 5624 | * leave kernel. |
| 5625 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5626 | if (p->mm && printk_ratelimit()) { |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5627 | printk(KERN_INFO "process %d (%s) no " |
| 5628 | "longer affine to cpu%d\n", |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5629 | task_pid_nr(p), p->comm, dead_cpu); |
| 5630 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5631 | } |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5632 | } while (!__migrate_task_irq(p, dead_cpu, dest_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | /* |
| 5636 | * While a dead CPU has no uninterruptible tasks queued at this point, |
| 5637 | * it might still have a nonzero ->nr_uninterruptible counter, because |
| 5638 | * for performance reasons the counter is not stricly tracking tasks to |
| 5639 | * their home CPUs. So we just add the counter to another CPU's counter, |
| 5640 | * to keep the global sum constant after CPU-down: |
| 5641 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5642 | static void migrate_nr_uninterruptible(struct rq *rq_src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5644 | struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | unsigned long flags; |
| 5646 | |
| 5647 | local_irq_save(flags); |
| 5648 | double_rq_lock(rq_src, rq_dest); |
| 5649 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; |
| 5650 | rq_src->nr_uninterruptible = 0; |
| 5651 | double_rq_unlock(rq_src, rq_dest); |
| 5652 | local_irq_restore(flags); |
| 5653 | } |
| 5654 | |
| 5655 | /* Run through task list and migrate tasks from the dead cpu. */ |
| 5656 | static void migrate_live_tasks(int src_cpu) |
| 5657 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5658 | struct task_struct *p, *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5660 | read_lock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5662 | do_each_thread(t, p) { |
| 5663 | if (p == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | continue; |
| 5665 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5666 | if (task_cpu(p) == src_cpu) |
| 5667 | move_task_off_dead_cpu(src_cpu, p); |
| 5668 | } while_each_thread(t, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5670 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | } |
| 5672 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5673 | /* |
| 5674 | * Schedules idle task to be the next runnable task on current CPU. |
Dmitry Adamushko | 94bc9a7 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 5675 | * It does so by boosting its priority to highest possible. |
| 5676 | * Used by CPU offline code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5677 | */ |
| 5678 | void sched_idle_next(void) |
| 5679 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5680 | int this_cpu = smp_processor_id(); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5681 | struct rq *rq = cpu_rq(this_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | struct task_struct *p = rq->idle; |
| 5683 | unsigned long flags; |
| 5684 | |
| 5685 | /* cpu has to be offline */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5686 | BUG_ON(cpu_online(this_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5688 | /* |
| 5689 | * Strictly not necessary since rest of the CPUs are stopped by now |
| 5690 | * and interrupts disabled on the current cpu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | */ |
| 5692 | spin_lock_irqsave(&rq->lock, flags); |
| 5693 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5694 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5695 | |
Dmitry Adamushko | 94bc9a7 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 5696 | update_rq_clock(rq); |
| 5697 | activate_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5698 | |
| 5699 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5700 | } |
| 5701 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5702 | /* |
| 5703 | * 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] | 5704 | * offline. |
| 5705 | */ |
| 5706 | void idle_task_exit(void) |
| 5707 | { |
| 5708 | struct mm_struct *mm = current->active_mm; |
| 5709 | |
| 5710 | BUG_ON(cpu_online(smp_processor_id())); |
| 5711 | |
| 5712 | if (mm != &init_mm) |
| 5713 | switch_mm(mm, &init_mm, current); |
| 5714 | mmdrop(mm); |
| 5715 | } |
| 5716 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5717 | /* called under rq->lock with disabled interrupts */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5718 | static void migrate_dead(unsigned int dead_cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5719 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5720 | struct rq *rq = cpu_rq(dead_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | |
| 5722 | /* Must be exiting, otherwise would be on tasklist. */ |
Eugene Teo | 270f722 | 2007-10-18 23:40:38 -0700 | [diff] [blame] | 5723 | BUG_ON(!p->exit_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | |
| 5725 | /* Cannot have done final schedule yet: would have vanished. */ |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 5726 | BUG_ON(p->state == TASK_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5728 | get_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | |
| 5730 | /* |
| 5731 | * Drop lock around migration; if someone else moves it, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5732 | * 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] | 5733 | * fine. |
| 5734 | */ |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5735 | spin_unlock_irq(&rq->lock); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5736 | move_task_off_dead_cpu(dead_cpu, p); |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5737 | spin_lock_irq(&rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5739 | put_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ |
| 5743 | static void migrate_dead_tasks(unsigned int dead_cpu) |
| 5744 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5745 | struct rq *rq = cpu_rq(dead_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5746 | struct task_struct *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5748 | for ( ; ; ) { |
| 5749 | if (!rq->nr_running) |
| 5750 | break; |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 5751 | update_rq_clock(rq); |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5752 | next = pick_next_task(rq, rq->curr); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5753 | if (!next) |
| 5754 | break; |
| 5755 | migrate_dead(dead_cpu, next); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | } |
| 5758 | } |
| 5759 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 5760 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5761 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
| 5762 | |
| 5763 | static struct ctl_table sd_ctl_dir[] = { |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5764 | { |
| 5765 | .procname = "sched_domain", |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5766 | .mode = 0555, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5767 | }, |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 5768 | {0, }, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5769 | }; |
| 5770 | |
| 5771 | static struct ctl_table sd_ctl_root[] = { |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5772 | { |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5773 | .ctl_name = CTL_KERN, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5774 | .procname = "kernel", |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5775 | .mode = 0555, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5776 | .child = sd_ctl_dir, |
| 5777 | }, |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 5778 | {0, }, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5779 | }; |
| 5780 | |
| 5781 | static struct ctl_table *sd_alloc_ctl_entry(int n) |
| 5782 | { |
| 5783 | struct ctl_table *entry = |
Milton Miller | 5cf9f06 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5784 | kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5785 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5786 | return entry; |
| 5787 | } |
| 5788 | |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5789 | static void sd_free_ctl_entry(struct ctl_table **tablep) |
| 5790 | { |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5791 | struct ctl_table *entry; |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5792 | |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5793 | /* |
| 5794 | * In the intermediate directories, both the child directory and |
| 5795 | * procname are dynamically allocated and could fail but the mode |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5796 | * will always be set. In the lowest directory the names are |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5797 | * static strings and all have proc handlers. |
| 5798 | */ |
| 5799 | for (entry = *tablep; entry->mode; entry++) { |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5800 | if (entry->child) |
| 5801 | sd_free_ctl_entry(&entry->child); |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5802 | if (entry->proc_handler == NULL) |
| 5803 | kfree(entry->procname); |
| 5804 | } |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5805 | |
| 5806 | kfree(*tablep); |
| 5807 | *tablep = NULL; |
| 5808 | } |
| 5809 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5810 | static void |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5811 | set_table_entry(struct ctl_table *entry, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5812 | const char *procname, void *data, int maxlen, |
| 5813 | mode_t mode, proc_handler *proc_handler) |
| 5814 | { |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5815 | entry->procname = procname; |
| 5816 | entry->data = data; |
| 5817 | entry->maxlen = maxlen; |
| 5818 | entry->mode = mode; |
| 5819 | entry->proc_handler = proc_handler; |
| 5820 | } |
| 5821 | |
| 5822 | static struct ctl_table * |
| 5823 | sd_alloc_ctl_domain_table(struct sched_domain *sd) |
| 5824 | { |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5825 | struct ctl_table *table = sd_alloc_ctl_entry(12); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5826 | |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5827 | if (table == NULL) |
| 5828 | return NULL; |
| 5829 | |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5830 | set_table_entry(&table[0], "min_interval", &sd->min_interval, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5831 | sizeof(long), 0644, proc_doulongvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5832 | set_table_entry(&table[1], "max_interval", &sd->max_interval, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5833 | sizeof(long), 0644, proc_doulongvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5834 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5835 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5836 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5837 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5838 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5839 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5840 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5841 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5842 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5843 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5844 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5845 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5846 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5847 | sizeof(int), 0644, proc_dointvec_minmax); |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5848 | set_table_entry(&table[9], "cache_nice_tries", |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5849 | &sd->cache_nice_tries, |
| 5850 | sizeof(int), 0644, proc_dointvec_minmax); |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5851 | set_table_entry(&table[10], "flags", &sd->flags, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5852 | sizeof(int), 0644, proc_dointvec_minmax); |
Milton Miller | 6323469f | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5853 | /* &table[11] is terminator */ |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5854 | |
| 5855 | return table; |
| 5856 | } |
| 5857 | |
Ingo Molnar | 9a4e715 | 2007-11-28 15:52:56 +0100 | [diff] [blame] | 5858 | static ctl_table *sd_alloc_ctl_cpu_table(int cpu) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5859 | { |
| 5860 | struct ctl_table *entry, *table; |
| 5861 | struct sched_domain *sd; |
| 5862 | int domain_num = 0, i; |
| 5863 | char buf[32]; |
| 5864 | |
| 5865 | for_each_domain(cpu, sd) |
| 5866 | domain_num++; |
| 5867 | entry = table = sd_alloc_ctl_entry(domain_num + 1); |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5868 | if (table == NULL) |
| 5869 | return NULL; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5870 | |
| 5871 | i = 0; |
| 5872 | for_each_domain(cpu, sd) { |
| 5873 | snprintf(buf, 32, "domain%d", i); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5874 | entry->procname = kstrdup(buf, GFP_KERNEL); |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5875 | entry->mode = 0555; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5876 | entry->child = sd_alloc_ctl_domain_table(sd); |
| 5877 | entry++; |
| 5878 | i++; |
| 5879 | } |
| 5880 | return table; |
| 5881 | } |
| 5882 | |
| 5883 | static struct ctl_table_header *sd_sysctl_header; |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5884 | static void register_sched_domain_sysctl(void) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5885 | { |
| 5886 | int i, cpu_num = num_online_cpus(); |
| 5887 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); |
| 5888 | char buf[32]; |
| 5889 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5890 | WARN_ON(sd_ctl_dir[0].child); |
| 5891 | sd_ctl_dir[0].child = entry; |
| 5892 | |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5893 | if (entry == NULL) |
| 5894 | return; |
| 5895 | |
Milton Miller | 97b6ea7 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5896 | for_each_online_cpu(i) { |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5897 | snprintf(buf, 32, "cpu%d", i); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5898 | entry->procname = kstrdup(buf, GFP_KERNEL); |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5899 | entry->mode = 0555; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5900 | entry->child = sd_alloc_ctl_cpu_table(i); |
Milton Miller | 97b6ea7 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5901 | entry++; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5902 | } |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5903 | |
| 5904 | WARN_ON(sd_sysctl_header); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5905 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); |
| 5906 | } |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5907 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5908 | /* may be called multiple times per register */ |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5909 | static void unregister_sched_domain_sysctl(void) |
| 5910 | { |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5911 | if (sd_sysctl_header) |
| 5912 | unregister_sysctl_table(sd_sysctl_header); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5913 | sd_sysctl_header = NULL; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5914 | if (sd_ctl_dir[0].child) |
| 5915 | sd_free_ctl_entry(&sd_ctl_dir[0].child); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5916 | } |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5917 | #else |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5918 | static void register_sched_domain_sysctl(void) |
| 5919 | { |
| 5920 | } |
| 5921 | static void unregister_sched_domain_sysctl(void) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5922 | { |
| 5923 | } |
| 5924 | #endif |
| 5925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | /* |
| 5927 | * migration_call - callback that gets triggered when a CPU is added. |
| 5928 | * Here we can start up the necessary migration thread for the new CPU. |
| 5929 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5930 | static int __cpuinit |
| 5931 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | struct task_struct *p; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5934 | int cpu = (long)hcpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5935 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5936 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | |
| 5938 | switch (action) { |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 5939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5941 | case CPU_UP_PREPARE_FROZEN: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5942 | p = kthread_create(migration_thread, hcpu, "migration/%d", cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | if (IS_ERR(p)) |
| 5944 | return NOTIFY_BAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | kthread_bind(p, cpu); |
| 5946 | /* Must be high prio: stop_machine expects to yield to it. */ |
| 5947 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5948 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5949 | task_rq_unlock(rq, &flags); |
| 5950 | cpu_rq(cpu)->migration_thread = p; |
| 5951 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5954 | case CPU_ONLINE_FROZEN: |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 5955 | /* Strictly unnecessary, as first user will wake it. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | wake_up_process(cpu_rq(cpu)->migration_thread); |
Gregory Haskins | 1f94ef5 | 2008-03-10 16:52:41 -0400 | [diff] [blame] | 5957 | |
| 5958 | /* Update our root-domain */ |
| 5959 | rq = cpu_rq(cpu); |
| 5960 | spin_lock_irqsave(&rq->lock, flags); |
| 5961 | if (rq->rd) { |
| 5962 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); |
| 5963 | cpu_set(cpu, rq->rd->online); |
| 5964 | } |
| 5965 | spin_unlock_irqrestore(&rq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5966 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | #ifdef CONFIG_HOTPLUG_CPU |
| 5969 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5970 | case CPU_UP_CANCELED_FROZEN: |
Heiko Carstens | fc75cdf | 2006-06-25 05:49:10 -0700 | [diff] [blame] | 5971 | if (!cpu_rq(cpu)->migration_thread) |
| 5972 | break; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5973 | /* Unbind it from offline cpu so it can run. Fall thru. */ |
Heiko Carstens | a4c4af7 | 2005-11-07 00:58:38 -0800 | [diff] [blame] | 5974 | kthread_bind(cpu_rq(cpu)->migration_thread, |
| 5975 | any_online_cpu(cpu_online_map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | kthread_stop(cpu_rq(cpu)->migration_thread); |
| 5977 | cpu_rq(cpu)->migration_thread = NULL; |
| 5978 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5981 | case CPU_DEAD_FROZEN: |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5982 | cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5983 | migrate_live_tasks(cpu); |
| 5984 | rq = cpu_rq(cpu); |
| 5985 | kthread_stop(rq->migration_thread); |
| 5986 | rq->migration_thread = NULL; |
| 5987 | /* Idle task back to normal (off runqueue, low prio) */ |
Oleg Nesterov | d2da272 | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5988 | spin_lock_irq(&rq->lock); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 5989 | update_rq_clock(rq); |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5990 | deactivate_task(rq, rq->idle, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | rq->idle->static_prio = MAX_PRIO; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5992 | __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); |
| 5993 | rq->idle->sched_class = &idle_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5994 | migrate_dead_tasks(cpu); |
Oleg Nesterov | d2da272 | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5995 | spin_unlock_irq(&rq->lock); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5996 | cpuset_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | migrate_nr_uninterruptible(rq); |
| 5998 | BUG_ON(rq->nr_running != 0); |
| 5999 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6000 | /* |
| 6001 | * No need to migrate the tasks: it was best-effort if |
| 6002 | * they didn't take sched_hotcpu_mutex. Just wake up |
| 6003 | * the requestors. |
| 6004 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6005 | spin_lock_irq(&rq->lock); |
| 6006 | while (!list_empty(&rq->migration_queue)) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6007 | struct migration_req *req; |
| 6008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6009 | req = list_entry(rq->migration_queue.next, |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6010 | struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | list_del_init(&req->list); |
| 6012 | complete(&req->done); |
| 6013 | } |
| 6014 | spin_unlock_irq(&rq->lock); |
| 6015 | break; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6016 | |
Gregory Haskins | 08f503b | 2008-03-10 17:59:11 -0400 | [diff] [blame] | 6017 | case CPU_DYING: |
| 6018 | case CPU_DYING_FROZEN: |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6019 | /* Update our root-domain */ |
| 6020 | rq = cpu_rq(cpu); |
| 6021 | spin_lock_irqsave(&rq->lock, flags); |
| 6022 | if (rq->rd) { |
| 6023 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); |
| 6024 | cpu_clear(cpu, rq->rd->online); |
| 6025 | } |
| 6026 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6027 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | #endif |
| 6029 | } |
| 6030 | return NOTIFY_OK; |
| 6031 | } |
| 6032 | |
| 6033 | /* Register at highest priority so that task migration (migrate_all_tasks) |
| 6034 | * happens before everything else. |
| 6035 | */ |
Chandra Seetharaman | 26c2143 | 2006-06-27 02:54:10 -0700 | [diff] [blame] | 6036 | static struct notifier_block __cpuinitdata migration_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | .notifier_call = migration_call, |
| 6038 | .priority = 10 |
| 6039 | }; |
| 6040 | |
Adrian Bunk | e6fe664 | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 6041 | void __init migration_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6042 | { |
| 6043 | void *cpu = (void *)(long)smp_processor_id(); |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 6044 | int err; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6045 | |
| 6046 | /* Start one for the boot CPU: */ |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 6047 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
| 6048 | BUG_ON(err == NOTIFY_BAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
| 6050 | register_cpu_notifier(&migration_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | } |
| 6052 | #endif |
| 6053 | |
| 6054 | #ifdef CONFIG_SMP |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 6055 | |
| 6056 | /* Number of possible processor ids */ |
| 6057 | int nr_cpu_ids __read_mostly = NR_CPUS; |
| 6058 | EXPORT_SYMBOL(nr_cpu_ids); |
| 6059 | |
Ingo Molnar | 3e9830d | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 6060 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6061 | |
| 6062 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level) |
| 6063 | { |
| 6064 | struct sched_group *group = sd->groups; |
| 6065 | cpumask_t groupmask; |
| 6066 | char str[NR_CPUS]; |
| 6067 | |
| 6068 | cpumask_scnprintf(str, NR_CPUS, sd->span); |
| 6069 | cpus_clear(groupmask); |
| 6070 | |
| 6071 | printk(KERN_DEBUG "%*s domain %d: ", level, "", level); |
| 6072 | |
| 6073 | if (!(sd->flags & SD_LOAD_BALANCE)) { |
| 6074 | printk("does not load-balance\n"); |
| 6075 | if (sd->parent) |
| 6076 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" |
| 6077 | " has parent"); |
| 6078 | return -1; |
| 6079 | } |
| 6080 | |
| 6081 | printk(KERN_CONT "span %s\n", str); |
| 6082 | |
| 6083 | if (!cpu_isset(cpu, sd->span)) { |
| 6084 | printk(KERN_ERR "ERROR: domain->span does not contain " |
| 6085 | "CPU%d\n", cpu); |
| 6086 | } |
| 6087 | if (!cpu_isset(cpu, group->cpumask)) { |
| 6088 | printk(KERN_ERR "ERROR: domain->groups does not contain" |
| 6089 | " CPU%d\n", cpu); |
| 6090 | } |
| 6091 | |
| 6092 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); |
| 6093 | do { |
| 6094 | if (!group) { |
| 6095 | printk("\n"); |
| 6096 | printk(KERN_ERR "ERROR: group is NULL\n"); |
| 6097 | break; |
| 6098 | } |
| 6099 | |
| 6100 | if (!group->__cpu_power) { |
| 6101 | printk(KERN_CONT "\n"); |
| 6102 | printk(KERN_ERR "ERROR: domain->cpu_power not " |
| 6103 | "set\n"); |
| 6104 | break; |
| 6105 | } |
| 6106 | |
| 6107 | if (!cpus_weight(group->cpumask)) { |
| 6108 | printk(KERN_CONT "\n"); |
| 6109 | printk(KERN_ERR "ERROR: empty group\n"); |
| 6110 | break; |
| 6111 | } |
| 6112 | |
| 6113 | if (cpus_intersects(groupmask, group->cpumask)) { |
| 6114 | printk(KERN_CONT "\n"); |
| 6115 | printk(KERN_ERR "ERROR: repeated CPUs\n"); |
| 6116 | break; |
| 6117 | } |
| 6118 | |
| 6119 | cpus_or(groupmask, groupmask, group->cpumask); |
| 6120 | |
| 6121 | cpumask_scnprintf(str, NR_CPUS, group->cpumask); |
| 6122 | printk(KERN_CONT " %s", str); |
| 6123 | |
| 6124 | group = group->next; |
| 6125 | } while (group != sd->groups); |
| 6126 | printk(KERN_CONT "\n"); |
| 6127 | |
| 6128 | if (!cpus_equal(sd->span, groupmask)) |
| 6129 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
| 6130 | |
| 6131 | if (sd->parent && !cpus_subset(groupmask, sd->parent->span)) |
| 6132 | printk(KERN_ERR "ERROR: parent span is not a superset " |
| 6133 | "of domain->span\n"); |
| 6134 | return 0; |
| 6135 | } |
| 6136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
| 6138 | { |
| 6139 | int level = 0; |
| 6140 | |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 6141 | if (!sd) { |
| 6142 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); |
| 6143 | return; |
| 6144 | } |
| 6145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
| 6147 | |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6148 | for (;;) { |
| 6149 | if (sched_domain_debug_one(sd, cpu, level)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6150 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6151 | level++; |
| 6152 | sd = sd->parent; |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 6153 | if (!sd) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6154 | break; |
| 6155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | } |
| 6157 | #else |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6158 | # define sched_domain_debug(sd, cpu) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6159 | #endif |
| 6160 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6161 | static int sd_degenerate(struct sched_domain *sd) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6162 | { |
| 6163 | if (cpus_weight(sd->span) == 1) |
| 6164 | return 1; |
| 6165 | |
| 6166 | /* Following flags need at least 2 groups */ |
| 6167 | if (sd->flags & (SD_LOAD_BALANCE | |
| 6168 | SD_BALANCE_NEWIDLE | |
| 6169 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6170 | SD_BALANCE_EXEC | |
| 6171 | SD_SHARE_CPUPOWER | |
| 6172 | SD_SHARE_PKG_RESOURCES)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6173 | if (sd->groups != sd->groups->next) |
| 6174 | return 0; |
| 6175 | } |
| 6176 | |
| 6177 | /* Following flags don't use groups */ |
| 6178 | if (sd->flags & (SD_WAKE_IDLE | |
| 6179 | SD_WAKE_AFFINE | |
| 6180 | SD_WAKE_BALANCE)) |
| 6181 | return 0; |
| 6182 | |
| 6183 | return 1; |
| 6184 | } |
| 6185 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6186 | static int |
| 6187 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6188 | { |
| 6189 | unsigned long cflags = sd->flags, pflags = parent->flags; |
| 6190 | |
| 6191 | if (sd_degenerate(parent)) |
| 6192 | return 1; |
| 6193 | |
| 6194 | if (!cpus_equal(sd->span, parent->span)) |
| 6195 | return 0; |
| 6196 | |
| 6197 | /* Does parent contain flags not in child? */ |
| 6198 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ |
| 6199 | if (cflags & SD_WAKE_AFFINE) |
| 6200 | pflags &= ~SD_WAKE_BALANCE; |
| 6201 | /* Flags needing groups don't count if only 1 group in parent */ |
| 6202 | if (parent->groups == parent->groups->next) { |
| 6203 | pflags &= ~(SD_LOAD_BALANCE | |
| 6204 | SD_BALANCE_NEWIDLE | |
| 6205 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6206 | SD_BALANCE_EXEC | |
| 6207 | SD_SHARE_CPUPOWER | |
| 6208 | SD_SHARE_PKG_RESOURCES); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6209 | } |
| 6210 | if (~cflags & pflags) |
| 6211 | return 0; |
| 6212 | |
| 6213 | return 1; |
| 6214 | } |
| 6215 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6216 | static void rq_attach_root(struct rq *rq, struct root_domain *rd) |
| 6217 | { |
| 6218 | unsigned long flags; |
| 6219 | const struct sched_class *class; |
| 6220 | |
| 6221 | spin_lock_irqsave(&rq->lock, flags); |
| 6222 | |
| 6223 | if (rq->rd) { |
| 6224 | struct root_domain *old_rd = rq->rd; |
| 6225 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6226 | for (class = sched_class_highest; class; class = class->next) { |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6227 | if (class->leave_domain) |
| 6228 | class->leave_domain(rq); |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6229 | } |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6230 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6231 | cpu_clear(rq->cpu, old_rd->span); |
| 6232 | cpu_clear(rq->cpu, old_rd->online); |
| 6233 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6234 | if (atomic_dec_and_test(&old_rd->refcount)) |
| 6235 | kfree(old_rd); |
| 6236 | } |
| 6237 | |
| 6238 | atomic_inc(&rd->refcount); |
| 6239 | rq->rd = rd; |
| 6240 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6241 | cpu_set(rq->cpu, rd->span); |
Gregory Haskins | 1f94ef5 | 2008-03-10 16:52:41 -0400 | [diff] [blame] | 6242 | if (cpu_isset(rq->cpu, cpu_online_map)) |
| 6243 | cpu_set(rq->cpu, rd->online); |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6244 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6245 | for (class = sched_class_highest; class; class = class->next) { |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6246 | if (class->join_domain) |
| 6247 | class->join_domain(rq); |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6248 | } |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6249 | |
| 6250 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6251 | } |
| 6252 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6253 | static void init_rootdomain(struct root_domain *rd) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6254 | { |
| 6255 | memset(rd, 0, sizeof(*rd)); |
| 6256 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6257 | cpus_clear(rd->span); |
| 6258 | cpus_clear(rd->online); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6259 | } |
| 6260 | |
| 6261 | static void init_defrootdomain(void) |
| 6262 | { |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6263 | init_rootdomain(&def_root_domain); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6264 | atomic_set(&def_root_domain.refcount, 1); |
| 6265 | } |
| 6266 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6267 | static struct root_domain *alloc_rootdomain(void) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6268 | { |
| 6269 | struct root_domain *rd; |
| 6270 | |
| 6271 | rd = kmalloc(sizeof(*rd), GFP_KERNEL); |
| 6272 | if (!rd) |
| 6273 | return NULL; |
| 6274 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6275 | init_rootdomain(rd); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6276 | |
| 6277 | return rd; |
| 6278 | } |
| 6279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | /* |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6281 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | * hold the hotplug lock. |
| 6283 | */ |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6284 | static void |
| 6285 | 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] | 6286 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6287 | struct rq *rq = cpu_rq(cpu); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6288 | struct sched_domain *tmp; |
| 6289 | |
| 6290 | /* Remove the sched domains which do not contribute to scheduling. */ |
| 6291 | for (tmp = sd; tmp; tmp = tmp->parent) { |
| 6292 | struct sched_domain *parent = tmp->parent; |
| 6293 | if (!parent) |
| 6294 | break; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6295 | if (sd_parent_degenerate(tmp, parent)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6296 | tmp->parent = parent->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6297 | if (parent->parent) |
| 6298 | parent->parent->child = tmp; |
| 6299 | } |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6300 | } |
| 6301 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6302 | if (sd && sd_degenerate(sd)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6303 | sd = sd->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6304 | if (sd) |
| 6305 | sd->child = NULL; |
| 6306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6307 | |
| 6308 | sched_domain_debug(sd, cpu); |
| 6309 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6310 | rq_attach_root(rq, rd); |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 6311 | rcu_assign_pointer(rq->sd, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6312 | } |
| 6313 | |
| 6314 | /* cpus with isolated domains */ |
Tim Chen | 67af63a | 2006-12-22 01:07:50 -0800 | [diff] [blame] | 6315 | static cpumask_t cpu_isolated_map = CPU_MASK_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | |
| 6317 | /* Setup the mask of cpus configured for isolated domains */ |
| 6318 | static int __init isolated_cpu_setup(char *str) |
| 6319 | { |
| 6320 | int ints[NR_CPUS], i; |
| 6321 | |
| 6322 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 6323 | cpus_clear(cpu_isolated_map); |
| 6324 | for (i = 1; i <= ints[0]; i++) |
| 6325 | if (ints[i] < NR_CPUS) |
| 6326 | cpu_set(ints[i], cpu_isolated_map); |
| 6327 | return 1; |
| 6328 | } |
| 6329 | |
Ingo Molnar | 8927f49 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 6330 | __setup("isolcpus=", isolated_cpu_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6331 | |
| 6332 | /* |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6333 | * init_sched_build_groups takes the cpumask we wish to span, and a pointer |
| 6334 | * to a function which identifies what group(along with sched group) a CPU |
| 6335 | * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS |
| 6336 | * (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] | 6337 | * |
| 6338 | * init_sched_build_groups will build a circular linked list of the groups |
| 6339 | * covered by the given span, and will set each group's ->cpumask correctly, |
| 6340 | * and ->cpu_power to 0. |
| 6341 | */ |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6342 | static void |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6343 | init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map, |
| 6344 | int (*group_fn)(int cpu, const cpumask_t *cpu_map, |
| 6345 | struct sched_group **sg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6346 | { |
| 6347 | struct sched_group *first = NULL, *last = NULL; |
| 6348 | cpumask_t covered = CPU_MASK_NONE; |
| 6349 | int i; |
| 6350 | |
| 6351 | for_each_cpu_mask(i, span) { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6352 | struct sched_group *sg; |
| 6353 | int group = group_fn(i, cpu_map, &sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6354 | int j; |
| 6355 | |
| 6356 | if (cpu_isset(i, covered)) |
| 6357 | continue; |
| 6358 | |
| 6359 | sg->cpumask = CPU_MASK_NONE; |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6360 | sg->__cpu_power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | |
| 6362 | for_each_cpu_mask(j, span) { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6363 | if (group_fn(j, cpu_map, NULL) != group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6364 | continue; |
| 6365 | |
| 6366 | cpu_set(j, covered); |
| 6367 | cpu_set(j, sg->cpumask); |
| 6368 | } |
| 6369 | if (!first) |
| 6370 | first = sg; |
| 6371 | if (last) |
| 6372 | last->next = sg; |
| 6373 | last = sg; |
| 6374 | } |
| 6375 | last->next = first; |
| 6376 | } |
| 6377 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6378 | #define SD_NODES_PER_DOMAIN 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6379 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6380 | #ifdef CONFIG_NUMA |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 6381 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6382 | /** |
| 6383 | * find_next_best_node - find the next node to include in a sched_domain |
| 6384 | * @node: node whose sched_domain we're building |
| 6385 | * @used_nodes: nodes already in the sched_domain |
| 6386 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6387 | * Find the next node to include in a given scheduling domain. Simply |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6388 | * finds the closest node not already in the @used_nodes map. |
| 6389 | * |
| 6390 | * Should use nodemask_t. |
| 6391 | */ |
| 6392 | static int find_next_best_node(int node, unsigned long *used_nodes) |
| 6393 | { |
| 6394 | int i, n, val, min_val, best_node = 0; |
| 6395 | |
| 6396 | min_val = INT_MAX; |
| 6397 | |
| 6398 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6399 | /* Start at @node */ |
| 6400 | n = (node + i) % MAX_NUMNODES; |
| 6401 | |
| 6402 | if (!nr_cpus_node(n)) |
| 6403 | continue; |
| 6404 | |
| 6405 | /* Skip already used nodes */ |
| 6406 | if (test_bit(n, used_nodes)) |
| 6407 | continue; |
| 6408 | |
| 6409 | /* Simple min distance search */ |
| 6410 | val = node_distance(node, n); |
| 6411 | |
| 6412 | if (val < min_val) { |
| 6413 | min_val = val; |
| 6414 | best_node = n; |
| 6415 | } |
| 6416 | } |
| 6417 | |
| 6418 | set_bit(best_node, used_nodes); |
| 6419 | return best_node; |
| 6420 | } |
| 6421 | |
| 6422 | /** |
| 6423 | * sched_domain_node_span - get a cpumask for a node's sched_domain |
| 6424 | * @node: node whose cpumask we're constructing |
| 6425 | * @size: number of nodes to include in this span |
| 6426 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6427 | * 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] | 6428 | * should be one that prevents unnecessary balancing, but also spreads tasks |
| 6429 | * out optimally. |
| 6430 | */ |
| 6431 | static cpumask_t sched_domain_node_span(int node) |
| 6432 | { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6433 | DECLARE_BITMAP(used_nodes, MAX_NUMNODES); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6434 | cpumask_t span, nodemask; |
| 6435 | int i; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6436 | |
| 6437 | cpus_clear(span); |
| 6438 | bitmap_zero(used_nodes, MAX_NUMNODES); |
| 6439 | |
| 6440 | nodemask = node_to_cpumask(node); |
| 6441 | cpus_or(span, span, nodemask); |
| 6442 | set_bit(node, used_nodes); |
| 6443 | |
| 6444 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { |
| 6445 | int next_node = find_next_best_node(node, used_nodes); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6446 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6447 | nodemask = node_to_cpumask(next_node); |
| 6448 | cpus_or(span, span, nodemask); |
| 6449 | } |
| 6450 | |
| 6451 | return span; |
| 6452 | } |
| 6453 | #endif |
| 6454 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6455 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6456 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6457 | /* |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6458 | * SMT sched-domains: |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6459 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | #ifdef CONFIG_SCHED_SMT |
| 6461 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6462 | static DEFINE_PER_CPU(struct sched_group, sched_group_cpus); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6463 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6464 | static int |
| 6465 | cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6467 | if (sg) |
| 6468 | *sg = &per_cpu(sched_group_cpus, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | return cpu; |
| 6470 | } |
| 6471 | #endif |
| 6472 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6473 | /* |
| 6474 | * multi-core sched-domains: |
| 6475 | */ |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6476 | #ifdef CONFIG_SCHED_MC |
| 6477 | static DEFINE_PER_CPU(struct sched_domain, core_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6478 | static DEFINE_PER_CPU(struct sched_group, sched_group_core); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6479 | #endif |
| 6480 | |
| 6481 | #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6482 | static int |
| 6483 | cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6484 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6485 | int group; |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 6486 | cpumask_t mask = per_cpu(cpu_sibling_map, cpu); |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6487 | cpus_and(mask, mask, *cpu_map); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6488 | group = first_cpu(mask); |
| 6489 | if (sg) |
| 6490 | *sg = &per_cpu(sched_group_core, group); |
| 6491 | return group; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6492 | } |
| 6493 | #elif defined(CONFIG_SCHED_MC) |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6494 | static int |
| 6495 | cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6496 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6497 | if (sg) |
| 6498 | *sg = &per_cpu(sched_group_core, cpu); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6499 | return cpu; |
| 6500 | } |
| 6501 | #endif |
| 6502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6504 | static DEFINE_PER_CPU(struct sched_group, sched_group_phys); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6505 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6506 | static int |
| 6507 | cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6508 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6509 | int group; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6510 | #ifdef CONFIG_SCHED_MC |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6511 | cpumask_t mask = cpu_coregroup_map(cpu); |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6512 | cpus_and(mask, mask, *cpu_map); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6513 | group = first_cpu(mask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6514 | #elif defined(CONFIG_SCHED_SMT) |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 6515 | cpumask_t mask = per_cpu(cpu_sibling_map, cpu); |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6516 | cpus_and(mask, mask, *cpu_map); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6517 | group = first_cpu(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6518 | #else |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6519 | group = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | #endif |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6521 | if (sg) |
| 6522 | *sg = &per_cpu(sched_group_phys, group); |
| 6523 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6524 | } |
| 6525 | |
| 6526 | #ifdef CONFIG_NUMA |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6527 | /* |
| 6528 | * The init_sched_build_groups can't handle what we want to do with node |
| 6529 | * groups, so roll our own. Now each node has its own list of groups which |
| 6530 | * gets dynamically allocated. |
| 6531 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6532 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6533 | static struct sched_group **sched_group_nodes_bycpu[NR_CPUS]; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6534 | |
| 6535 | static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6536 | static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6537 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6538 | static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map, |
| 6539 | struct sched_group **sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6540 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6541 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu)); |
| 6542 | int group; |
| 6543 | |
| 6544 | cpus_and(nodemask, nodemask, *cpu_map); |
| 6545 | group = first_cpu(nodemask); |
| 6546 | |
| 6547 | if (sg) |
| 6548 | *sg = &per_cpu(sched_group_allnodes, group); |
| 6549 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | } |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6551 | |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6552 | static void init_numa_sched_groups_power(struct sched_group *group_head) |
| 6553 | { |
| 6554 | struct sched_group *sg = group_head; |
| 6555 | int j; |
| 6556 | |
| 6557 | if (!sg) |
| 6558 | return; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6559 | do { |
| 6560 | for_each_cpu_mask(j, sg->cpumask) { |
| 6561 | struct sched_domain *sd; |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6562 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6563 | sd = &per_cpu(phys_domains, j); |
| 6564 | if (j != first_cpu(sd->groups->cpumask)) { |
| 6565 | /* |
| 6566 | * Only add "power" once for each |
| 6567 | * physical package. |
| 6568 | */ |
| 6569 | continue; |
| 6570 | } |
| 6571 | |
| 6572 | sg_inc_cpu_power(sg, sd->groups->__cpu_power); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6573 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6574 | sg = sg->next; |
| 6575 | } while (sg != group_head); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6577 | #endif |
| 6578 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6579 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6580 | /* Free memory allocated for various sched_group structures */ |
| 6581 | static void free_sched_groups(const cpumask_t *cpu_map) |
| 6582 | { |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6583 | int cpu, i; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6584 | |
| 6585 | for_each_cpu_mask(cpu, *cpu_map) { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6586 | struct sched_group **sched_group_nodes |
| 6587 | = sched_group_nodes_bycpu[cpu]; |
| 6588 | |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6589 | if (!sched_group_nodes) |
| 6590 | continue; |
| 6591 | |
| 6592 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6593 | cpumask_t nodemask = node_to_cpumask(i); |
| 6594 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; |
| 6595 | |
| 6596 | cpus_and(nodemask, nodemask, *cpu_map); |
| 6597 | if (cpus_empty(nodemask)) |
| 6598 | continue; |
| 6599 | |
| 6600 | if (sg == NULL) |
| 6601 | continue; |
| 6602 | sg = sg->next; |
| 6603 | next_sg: |
| 6604 | oldsg = sg; |
| 6605 | sg = sg->next; |
| 6606 | kfree(oldsg); |
| 6607 | if (oldsg != sched_group_nodes[i]) |
| 6608 | goto next_sg; |
| 6609 | } |
| 6610 | kfree(sched_group_nodes); |
| 6611 | sched_group_nodes_bycpu[cpu] = NULL; |
| 6612 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6613 | } |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6614 | #else |
| 6615 | static void free_sched_groups(const cpumask_t *cpu_map) |
| 6616 | { |
| 6617 | } |
| 6618 | #endif |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6620 | /* |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6621 | * Initialize sched groups cpu_power. |
| 6622 | * |
| 6623 | * cpu_power indicates the capacity of sched group, which is used while |
| 6624 | * distributing the load between different sched groups in a sched domain. |
| 6625 | * Typically cpu_power for all the groups in a sched domain will be same unless |
| 6626 | * there are asymmetries in the topology. If there are asymmetries, group |
| 6627 | * having more cpu_power will pickup more load compared to the group having |
| 6628 | * less cpu_power. |
| 6629 | * |
| 6630 | * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents |
| 6631 | * the maximum number of tasks a group can handle in the presence of other idle |
| 6632 | * or lightly loaded groups in the same sched domain. |
| 6633 | */ |
| 6634 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) |
| 6635 | { |
| 6636 | struct sched_domain *child; |
| 6637 | struct sched_group *group; |
| 6638 | |
| 6639 | WARN_ON(!sd || !sd->groups); |
| 6640 | |
| 6641 | if (cpu != first_cpu(sd->groups->cpumask)) |
| 6642 | return; |
| 6643 | |
| 6644 | child = sd->child; |
| 6645 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6646 | sd->groups->__cpu_power = 0; |
| 6647 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6648 | /* |
| 6649 | * For perf policy, if the groups in child domain share resources |
| 6650 | * (for example cores sharing some portions of the cache hierarchy |
| 6651 | * or SMT), then set this domain groups cpu_power such that each group |
| 6652 | * can handle only one task, when there are other idle groups in the |
| 6653 | * same sched domain. |
| 6654 | */ |
| 6655 | if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) && |
| 6656 | (child->flags & |
| 6657 | (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6658 | sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6659 | return; |
| 6660 | } |
| 6661 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6662 | /* |
| 6663 | * add cpu_power of each child group to this groups cpu_power |
| 6664 | */ |
| 6665 | group = child->groups; |
| 6666 | do { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6667 | sg_inc_cpu_power(sd->groups, group->__cpu_power); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6668 | group = group->next; |
| 6669 | } while (group != child->groups); |
| 6670 | } |
| 6671 | |
| 6672 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6673 | * Build sched domains for a given set of cpus and attach the sched domains |
| 6674 | * to the individual cpus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6675 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6676 | static int build_sched_domains(const cpumask_t *cpu_map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6677 | { |
| 6678 | int i; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6679 | struct root_domain *rd; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6680 | #ifdef CONFIG_NUMA |
| 6681 | struct sched_group **sched_group_nodes = NULL; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6682 | int sd_allnodes = 0; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6683 | |
| 6684 | /* |
| 6685 | * Allocate the per-node list of sched groups |
| 6686 | */ |
Milton Miller | 5cf9f06 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6687 | sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *), |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6688 | GFP_KERNEL); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6689 | if (!sched_group_nodes) { |
| 6690 | printk(KERN_WARNING "Can not alloc sched group node list\n"); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6691 | return -ENOMEM; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6692 | } |
| 6693 | sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; |
| 6694 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6695 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6696 | rd = alloc_rootdomain(); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6697 | if (!rd) { |
| 6698 | printk(KERN_WARNING "Cannot alloc root domain\n"); |
| 6699 | return -ENOMEM; |
| 6700 | } |
| 6701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6702 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6703 | * Set up domains for cpus specified by the cpu_map. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6704 | */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6705 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | struct sched_domain *sd = NULL, *p; |
| 6707 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(i)); |
| 6708 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6709 | cpus_and(nodemask, nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6710 | |
| 6711 | #ifdef CONFIG_NUMA |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6712 | if (cpus_weight(*cpu_map) > |
| 6713 | SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6714 | sd = &per_cpu(allnodes_domains, i); |
| 6715 | *sd = SD_ALLNODES_INIT; |
| 6716 | sd->span = *cpu_map; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6717 | cpu_to_allnodes_group(i, cpu_map, &sd->groups); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6718 | p = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6719 | sd_allnodes = 1; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6720 | } else |
| 6721 | p = NULL; |
| 6722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6723 | sd = &per_cpu(node_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6724 | *sd = SD_NODE_INIT; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6725 | sd->span = sched_domain_node_span(cpu_to_node(i)); |
| 6726 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6727 | if (p) |
| 6728 | p->child = sd; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6729 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6730 | #endif |
| 6731 | |
| 6732 | p = sd; |
| 6733 | sd = &per_cpu(phys_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6734 | *sd = SD_CPU_INIT; |
| 6735 | sd->span = nodemask; |
| 6736 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6737 | if (p) |
| 6738 | p->child = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6739 | cpu_to_phys_group(i, cpu_map, &sd->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6740 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6741 | #ifdef CONFIG_SCHED_MC |
| 6742 | p = sd; |
| 6743 | sd = &per_cpu(core_domains, i); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6744 | *sd = SD_MC_INIT; |
| 6745 | sd->span = cpu_coregroup_map(i); |
| 6746 | cpus_and(sd->span, sd->span, *cpu_map); |
| 6747 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6748 | p->child = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6749 | cpu_to_core_group(i, cpu_map, &sd->groups); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6750 | #endif |
| 6751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | #ifdef CONFIG_SCHED_SMT |
| 6753 | p = sd; |
| 6754 | sd = &per_cpu(cpu_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6755 | *sd = SD_SIBLING_INIT; |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 6756 | sd->span = per_cpu(cpu_sibling_map, i); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6757 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6758 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6759 | p->child = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6760 | cpu_to_cpu_group(i, cpu_map, &sd->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6761 | #endif |
| 6762 | } |
| 6763 | |
| 6764 | #ifdef CONFIG_SCHED_SMT |
| 6765 | /* Set up CPU (sibling) groups */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6766 | for_each_cpu_mask(i, *cpu_map) { |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 6767 | cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6768 | cpus_and(this_sibling_map, this_sibling_map, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | if (i != first_cpu(this_sibling_map)) |
| 6770 | continue; |
| 6771 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6772 | init_sched_build_groups(this_sibling_map, cpu_map, |
| 6773 | &cpu_to_cpu_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6774 | } |
| 6775 | #endif |
| 6776 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6777 | #ifdef CONFIG_SCHED_MC |
| 6778 | /* Set up multi-core groups */ |
| 6779 | for_each_cpu_mask(i, *cpu_map) { |
| 6780 | cpumask_t this_core_map = cpu_coregroup_map(i); |
| 6781 | cpus_and(this_core_map, this_core_map, *cpu_map); |
| 6782 | if (i != first_cpu(this_core_map)) |
| 6783 | continue; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6784 | init_sched_build_groups(this_core_map, cpu_map, |
| 6785 | &cpu_to_core_group); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6786 | } |
| 6787 | #endif |
| 6788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | /* Set up physical groups */ |
| 6790 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6791 | cpumask_t nodemask = node_to_cpumask(i); |
| 6792 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6793 | cpus_and(nodemask, nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | if (cpus_empty(nodemask)) |
| 6795 | continue; |
| 6796 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6797 | init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | } |
| 6799 | |
| 6800 | #ifdef CONFIG_NUMA |
| 6801 | /* Set up node groups */ |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6802 | if (sd_allnodes) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6803 | init_sched_build_groups(*cpu_map, cpu_map, |
| 6804 | &cpu_to_allnodes_group); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6805 | |
| 6806 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6807 | /* Set up node groups */ |
| 6808 | struct sched_group *sg, *prev; |
| 6809 | cpumask_t nodemask = node_to_cpumask(i); |
| 6810 | cpumask_t domainspan; |
| 6811 | cpumask_t covered = CPU_MASK_NONE; |
| 6812 | int j; |
| 6813 | |
| 6814 | cpus_and(nodemask, nodemask, *cpu_map); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6815 | if (cpus_empty(nodemask)) { |
| 6816 | sched_group_nodes[i] = NULL; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6817 | continue; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6818 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6819 | |
| 6820 | domainspan = sched_domain_node_span(i); |
| 6821 | cpus_and(domainspan, domainspan, *cpu_map); |
| 6822 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 6823 | sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6824 | if (!sg) { |
| 6825 | printk(KERN_WARNING "Can not alloc domain group for " |
| 6826 | "node %d\n", i); |
| 6827 | goto error; |
| 6828 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6829 | sched_group_nodes[i] = sg; |
| 6830 | for_each_cpu_mask(j, nodemask) { |
| 6831 | struct sched_domain *sd; |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 6832 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6833 | sd = &per_cpu(node_domains, j); |
| 6834 | sd->groups = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6835 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6836 | sg->__cpu_power = 0; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6837 | sg->cpumask = nodemask; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6838 | sg->next = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6839 | cpus_or(covered, covered, nodemask); |
| 6840 | prev = sg; |
| 6841 | |
| 6842 | for (j = 0; j < MAX_NUMNODES; j++) { |
| 6843 | cpumask_t tmp, notcovered; |
| 6844 | int n = (i + j) % MAX_NUMNODES; |
| 6845 | |
| 6846 | cpus_complement(notcovered, covered); |
| 6847 | cpus_and(tmp, notcovered, *cpu_map); |
| 6848 | cpus_and(tmp, tmp, domainspan); |
| 6849 | if (cpus_empty(tmp)) |
| 6850 | break; |
| 6851 | |
| 6852 | nodemask = node_to_cpumask(n); |
| 6853 | cpus_and(tmp, tmp, nodemask); |
| 6854 | if (cpus_empty(tmp)) |
| 6855 | continue; |
| 6856 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 6857 | sg = kmalloc_node(sizeof(struct sched_group), |
| 6858 | GFP_KERNEL, i); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6859 | if (!sg) { |
| 6860 | printk(KERN_WARNING |
| 6861 | "Can not alloc domain group for node %d\n", j); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6862 | goto error; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6863 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6864 | sg->__cpu_power = 0; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6865 | sg->cpumask = tmp; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6866 | sg->next = prev->next; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6867 | cpus_or(covered, covered, tmp); |
| 6868 | prev->next = sg; |
| 6869 | prev = sg; |
| 6870 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6871 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6872 | #endif |
| 6873 | |
| 6874 | /* Calculate CPU power for physical packages and nodes */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6875 | #ifdef CONFIG_SCHED_SMT |
| 6876 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6877 | struct sched_domain *sd = &per_cpu(cpu_domains, i); |
| 6878 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6879 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6880 | } |
| 6881 | #endif |
| 6882 | #ifdef CONFIG_SCHED_MC |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6883 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6884 | struct sched_domain *sd = &per_cpu(core_domains, i); |
| 6885 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6886 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6887 | } |
| 6888 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6889 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6890 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6891 | struct sched_domain *sd = &per_cpu(phys_domains, i); |
| 6892 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6893 | init_sched_groups_power(i, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6894 | } |
| 6895 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6896 | #ifdef CONFIG_NUMA |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6897 | for (i = 0; i < MAX_NUMNODES; i++) |
| 6898 | init_numa_sched_groups_power(sched_group_nodes[i]); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6899 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6900 | if (sd_allnodes) { |
| 6901 | struct sched_group *sg; |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 6902 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6903 | cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg); |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 6904 | init_numa_sched_groups_power(sg); |
| 6905 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6906 | #endif |
| 6907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6908 | /* Attach the domains */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6909 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6910 | struct sched_domain *sd; |
| 6911 | #ifdef CONFIG_SCHED_SMT |
| 6912 | sd = &per_cpu(cpu_domains, i); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6913 | #elif defined(CONFIG_SCHED_MC) |
| 6914 | sd = &per_cpu(core_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6915 | #else |
| 6916 | sd = &per_cpu(phys_domains, i); |
| 6917 | #endif |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6918 | cpu_attach_domain(sd, rd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6919 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6920 | |
| 6921 | return 0; |
| 6922 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6923 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6924 | error: |
| 6925 | free_sched_groups(cpu_map); |
| 6926 | return -ENOMEM; |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6927 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | } |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6929 | |
| 6930 | static cpumask_t *doms_cur; /* current sched domains */ |
| 6931 | static int ndoms_cur; /* number of sched domains in 'doms_cur' */ |
| 6932 | |
| 6933 | /* |
| 6934 | * Special case: If a kmalloc of a doms_cur partition (array of |
| 6935 | * cpumask_t) fails, then fallback to a single sched domain, |
| 6936 | * as determined by the single cpumask_t fallback_doms. |
| 6937 | */ |
| 6938 | static cpumask_t fallback_doms; |
| 6939 | |
Heiko Carstens | 22e52b0 | 2008-03-12 18:31:59 +0100 | [diff] [blame] | 6940 | void __attribute__((weak)) arch_update_cpu_topology(void) |
| 6941 | { |
| 6942 | } |
| 6943 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6944 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6945 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6946 | * For now this just excludes isolated cpus, but could be used to |
| 6947 | * exclude other special cases in the future. |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6948 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6949 | static int arch_init_sched_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6950 | { |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6951 | int err; |
| 6952 | |
Heiko Carstens | 22e52b0 | 2008-03-12 18:31:59 +0100 | [diff] [blame] | 6953 | arch_update_cpu_topology(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6954 | ndoms_cur = 1; |
| 6955 | doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL); |
| 6956 | if (!doms_cur) |
| 6957 | doms_cur = &fallback_doms; |
| 6958 | cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map); |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6959 | err = build_sched_domains(doms_cur); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6960 | register_sched_domain_sysctl(); |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6961 | |
| 6962 | return err; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6963 | } |
| 6964 | |
| 6965 | static void arch_destroy_sched_domains(const cpumask_t *cpu_map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6966 | { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6967 | free_sched_groups(cpu_map); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6969 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6970 | /* |
| 6971 | * Detach sched domains from a group of cpus specified in cpu_map |
| 6972 | * These cpus will now be attached to the NULL domain |
| 6973 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 6974 | static void detach_destroy_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6975 | { |
| 6976 | int i; |
| 6977 | |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6978 | unregister_sched_domain_sysctl(); |
| 6979 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6980 | for_each_cpu_mask(i, *cpu_map) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6981 | cpu_attach_domain(NULL, &def_root_domain, i); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6982 | synchronize_sched(); |
| 6983 | arch_destroy_sched_domains(cpu_map); |
| 6984 | } |
| 6985 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6986 | /* |
| 6987 | * Partition sched domains as specified by the 'ndoms_new' |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6988 | * cpumasks in the array doms_new[] of cpumasks. This compares |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6989 | * doms_new[] to the current sched domain partitioning, doms_cur[]. |
| 6990 | * It destroys each deleted domain and builds each new domain. |
| 6991 | * |
| 6992 | * '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] | 6993 | * The masks don't intersect (don't overlap.) We should setup one |
| 6994 | * sched domain for each mask. CPUs not in any of the cpumasks will |
| 6995 | * not be load balanced. If the same cpumask appears both in the |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6996 | * current 'doms_cur' domains and in the new 'doms_new', we can leave |
| 6997 | * it as it is. |
| 6998 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6999 | * The passed in 'doms_new' should be kmalloc'd. This routine takes |
| 7000 | * 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] | 7001 | * failed the kmalloc call, then it can pass in doms_new == NULL, |
| 7002 | * and partition_sched_domains() will fallback to the single partition |
| 7003 | * 'fallback_doms'. |
| 7004 | * |
| 7005 | * Call with hotplug lock held |
| 7006 | */ |
| 7007 | void partition_sched_domains(int ndoms_new, cpumask_t *doms_new) |
| 7008 | { |
| 7009 | int i, j; |
| 7010 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7011 | lock_doms_cur(); |
| 7012 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7013 | /* always unregister in case we don't destroy any domains */ |
| 7014 | unregister_sched_domain_sysctl(); |
| 7015 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7016 | if (doms_new == NULL) { |
| 7017 | ndoms_new = 1; |
| 7018 | doms_new = &fallback_doms; |
| 7019 | cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map); |
| 7020 | } |
| 7021 | |
| 7022 | /* Destroy deleted domains */ |
| 7023 | for (i = 0; i < ndoms_cur; i++) { |
| 7024 | for (j = 0; j < ndoms_new; j++) { |
| 7025 | if (cpus_equal(doms_cur[i], doms_new[j])) |
| 7026 | goto match1; |
| 7027 | } |
| 7028 | /* no match - a current sched domain not in new doms_new[] */ |
| 7029 | detach_destroy_domains(doms_cur + i); |
| 7030 | match1: |
| 7031 | ; |
| 7032 | } |
| 7033 | |
| 7034 | /* Build new domains */ |
| 7035 | for (i = 0; i < ndoms_new; i++) { |
| 7036 | for (j = 0; j < ndoms_cur; j++) { |
| 7037 | if (cpus_equal(doms_new[i], doms_cur[j])) |
| 7038 | goto match2; |
| 7039 | } |
| 7040 | /* no match - add a new doms_new */ |
| 7041 | build_sched_domains(doms_new + i); |
| 7042 | match2: |
| 7043 | ; |
| 7044 | } |
| 7045 | |
| 7046 | /* Remember the new sched domains */ |
| 7047 | if (doms_cur != &fallback_doms) |
| 7048 | kfree(doms_cur); |
| 7049 | doms_cur = doms_new; |
| 7050 | ndoms_cur = ndoms_new; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 7051 | |
| 7052 | register_sched_domain_sysctl(); |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7053 | |
| 7054 | unlock_doms_cur(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7055 | } |
| 7056 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7057 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Heiko Carstens | 9aefd0a | 2008-03-12 18:31:58 +0100 | [diff] [blame] | 7058 | int arch_reinit_sched_domains(void) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7059 | { |
| 7060 | int err; |
| 7061 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7062 | get_online_cpus(); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7063 | detach_destroy_domains(&cpu_online_map); |
| 7064 | err = arch_init_sched_domains(&cpu_online_map); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7065 | put_online_cpus(); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7066 | |
| 7067 | return err; |
| 7068 | } |
| 7069 | |
| 7070 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) |
| 7071 | { |
| 7072 | int ret; |
| 7073 | |
| 7074 | if (buf[0] != '0' && buf[0] != '1') |
| 7075 | return -EINVAL; |
| 7076 | |
| 7077 | if (smt) |
| 7078 | sched_smt_power_savings = (buf[0] == '1'); |
| 7079 | else |
| 7080 | sched_mc_power_savings = (buf[0] == '1'); |
| 7081 | |
| 7082 | ret = arch_reinit_sched_domains(); |
| 7083 | |
| 7084 | return ret ? ret : count; |
| 7085 | } |
| 7086 | |
Adrian Bunk | 6707de00 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 7087 | #ifdef CONFIG_SCHED_MC |
| 7088 | static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page) |
| 7089 | { |
| 7090 | return sprintf(page, "%u\n", sched_mc_power_savings); |
| 7091 | } |
| 7092 | static ssize_t sched_mc_power_savings_store(struct sys_device *dev, |
| 7093 | const char *buf, size_t count) |
| 7094 | { |
| 7095 | return sched_power_savings_store(buf, count, 0); |
| 7096 | } |
| 7097 | static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show, |
| 7098 | sched_mc_power_savings_store); |
| 7099 | #endif |
| 7100 | |
| 7101 | #ifdef CONFIG_SCHED_SMT |
| 7102 | static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page) |
| 7103 | { |
| 7104 | return sprintf(page, "%u\n", sched_smt_power_savings); |
| 7105 | } |
| 7106 | static ssize_t sched_smt_power_savings_store(struct sys_device *dev, |
| 7107 | const char *buf, size_t count) |
| 7108 | { |
| 7109 | return sched_power_savings_store(buf, count, 1); |
| 7110 | } |
| 7111 | static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show, |
| 7112 | sched_smt_power_savings_store); |
| 7113 | #endif |
| 7114 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7115 | int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) |
| 7116 | { |
| 7117 | int err = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7118 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7119 | #ifdef CONFIG_SCHED_SMT |
| 7120 | if (smt_capable()) |
| 7121 | err = sysfs_create_file(&cls->kset.kobj, |
| 7122 | &attr_sched_smt_power_savings.attr); |
| 7123 | #endif |
| 7124 | #ifdef CONFIG_SCHED_MC |
| 7125 | if (!err && mc_capable()) |
| 7126 | err = sysfs_create_file(&cls->kset.kobj, |
| 7127 | &attr_sched_mc_power_savings.attr); |
| 7128 | #endif |
| 7129 | return err; |
| 7130 | } |
| 7131 | #endif |
| 7132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7134 | * Force a reinitialization of the sched domains hierarchy. The domains |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | * and groups cannot be updated in place without racing with the balancing |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 7136 | * code, so we temporarily attach all running cpus to the NULL domain |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7137 | * which will prevent rebalancing while the sched domains are recalculated. |
| 7138 | */ |
| 7139 | static int update_sched_domains(struct notifier_block *nfb, |
| 7140 | unsigned long action, void *hcpu) |
| 7141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7142 | switch (action) { |
| 7143 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7144 | case CPU_UP_PREPARE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | case CPU_DOWN_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7146 | case CPU_DOWN_PREPARE_FROZEN: |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7147 | detach_destroy_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7148 | return NOTIFY_OK; |
| 7149 | |
| 7150 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7151 | case CPU_UP_CANCELED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | case CPU_DOWN_FAILED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7153 | case CPU_DOWN_FAILED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7154 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7155 | case CPU_ONLINE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7156 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7157 | case CPU_DEAD_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7158 | /* |
| 7159 | * Fall through and re-initialise the domains. |
| 7160 | */ |
| 7161 | break; |
| 7162 | default: |
| 7163 | return NOTIFY_DONE; |
| 7164 | } |
| 7165 | |
| 7166 | /* The hotplug lock is already held by cpu_up/cpu_down */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7167 | arch_init_sched_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7168 | |
| 7169 | return NOTIFY_OK; |
| 7170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | |
| 7172 | void __init sched_init_smp(void) |
| 7173 | { |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7174 | cpumask_t non_isolated_cpus; |
| 7175 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7176 | get_online_cpus(); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7177 | arch_init_sched_domains(&cpu_online_map); |
Nathan Lynch | e5e5673 | 2007-01-10 23:15:28 -0800 | [diff] [blame] | 7178 | cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7179 | if (cpus_empty(non_isolated_cpus)) |
| 7180 | cpu_set(smp_processor_id(), non_isolated_cpus); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7181 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7182 | /* XXX: Theoretical race here - CPU may be hotplugged now */ |
| 7183 | hotcpu_notifier(update_sched_domains, 0); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7184 | |
| 7185 | /* Move init over to a non-isolated CPU */ |
| 7186 | if (set_cpus_allowed(current, non_isolated_cpus) < 0) |
| 7187 | BUG(); |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 7188 | sched_init_granularity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7189 | } |
| 7190 | #else |
| 7191 | void __init sched_init_smp(void) |
| 7192 | { |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 7193 | sched_init_granularity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | } |
| 7195 | #endif /* CONFIG_SMP */ |
| 7196 | |
| 7197 | int in_sched_functions(unsigned long addr) |
| 7198 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7199 | return in_lock_functions(addr) || |
| 7200 | (addr >= (unsigned long)__sched_text_start |
| 7201 | && addr < (unsigned long)__sched_text_end); |
| 7202 | } |
| 7203 | |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 7204 | 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] | 7205 | { |
| 7206 | cfs_rq->tasks_timeline = RB_ROOT; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7207 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7208 | cfs_rq->rq = rq; |
| 7209 | #endif |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 7210 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7211 | } |
| 7212 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7213 | static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) |
| 7214 | { |
| 7215 | struct rt_prio_array *array; |
| 7216 | int i; |
| 7217 | |
| 7218 | array = &rt_rq->active; |
| 7219 | for (i = 0; i < MAX_RT_PRIO; i++) { |
| 7220 | INIT_LIST_HEAD(array->queue + i); |
| 7221 | __clear_bit(i, array->bitmap); |
| 7222 | } |
| 7223 | /* delimiter for bitsearch: */ |
| 7224 | __set_bit(MAX_RT_PRIO, array->bitmap); |
| 7225 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7226 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 7227 | rt_rq->highest_prio = MAX_RT_PRIO; |
| 7228 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7229 | #ifdef CONFIG_SMP |
| 7230 | rt_rq->rt_nr_migratory = 0; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7231 | rt_rq->overloaded = 0; |
| 7232 | #endif |
| 7233 | |
| 7234 | rt_rq->rt_time = 0; |
| 7235 | rt_rq->rt_throttled = 0; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7236 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7237 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7238 | rt_rq->rt_nr_boosted = 0; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7239 | rt_rq->rq = rq; |
| 7240 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7241 | } |
| 7242 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7243 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7244 | static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg, |
| 7245 | struct cfs_rq *cfs_rq, struct sched_entity *se, |
| 7246 | int cpu, int add) |
| 7247 | { |
| 7248 | tg->cfs_rq[cpu] = cfs_rq; |
| 7249 | init_cfs_rq(cfs_rq, rq); |
| 7250 | cfs_rq->tg = tg; |
| 7251 | if (add) |
| 7252 | list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list); |
| 7253 | |
| 7254 | tg->se[cpu] = se; |
| 7255 | se->cfs_rq = &rq->cfs; |
| 7256 | se->my_q = cfs_rq; |
| 7257 | se->load.weight = tg->shares; |
| 7258 | se->load.inv_weight = div64_64(1ULL<<32, se->load.weight); |
| 7259 | se->parent = NULL; |
| 7260 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7261 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7262 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7263 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7264 | static void init_tg_rt_entry(struct rq *rq, struct task_group *tg, |
| 7265 | struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, |
| 7266 | int cpu, int add) |
| 7267 | { |
| 7268 | tg->rt_rq[cpu] = rt_rq; |
| 7269 | init_rt_rq(rt_rq, rq); |
| 7270 | rt_rq->tg = tg; |
| 7271 | rt_rq->rt_se = rt_se; |
| 7272 | if (add) |
| 7273 | list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list); |
| 7274 | |
| 7275 | tg->rt_se[cpu] = rt_se; |
| 7276 | rt_se->rt_rq = &rq->rt; |
| 7277 | rt_se->my_q = rt_rq; |
| 7278 | rt_se->parent = NULL; |
| 7279 | INIT_LIST_HEAD(&rt_se->run_list); |
| 7280 | } |
| 7281 | #endif |
| 7282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7283 | void __init sched_init(void) |
| 7284 | { |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 7285 | int highest_cpu = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7286 | int i, j; |
| 7287 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7288 | #ifdef CONFIG_SMP |
| 7289 | init_defrootdomain(); |
| 7290 | #endif |
| 7291 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7292 | #ifdef CONFIG_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7293 | list_add(&init_task_group.list, &task_groups); |
| 7294 | #endif |
| 7295 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 7296 | for_each_possible_cpu(i) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 7297 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7298 | |
| 7299 | rq = cpu_rq(i); |
| 7300 | spin_lock_init(&rq->lock); |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 7301 | lockdep_set_class(&rq->lock, &rq->rq_lock_key); |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 7302 | rq->nr_running = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7303 | rq->clock = 1; |
Guillaume Chazarain | 15934a3 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 7304 | update_last_tick_seen(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7305 | init_cfs_rq(&rq->cfs, rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7306 | init_rt_rq(&rq->rt, rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7307 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7308 | init_task_group.shares = init_task_group_load; |
| 7309 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
| 7310 | init_tg_cfs_entry(rq, &init_task_group, |
| 7311 | &per_cpu(init_cfs_rq, i), |
| 7312 | &per_cpu(init_sched_entity, i), i, 1); |
| 7313 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7314 | #endif |
| 7315 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7316 | init_task_group.rt_runtime = |
| 7317 | sysctl_sched_rt_runtime * NSEC_PER_USEC; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7318 | INIT_LIST_HEAD(&rq->leaf_rt_rq_list); |
| 7319 | init_tg_rt_entry(rq, &init_task_group, |
| 7320 | &per_cpu(init_rt_rq, i), |
| 7321 | &per_cpu(init_sched_rt_entity, i), i, 1); |
| 7322 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7323 | rq->rt_period_expire = 0; |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 7324 | rq->rt_throttled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7325 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7326 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) |
| 7327 | rq->cpu_load[j] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7328 | #ifdef CONFIG_SMP |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 7329 | rq->sd = NULL; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7330 | rq->rd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7331 | rq->active_balance = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7332 | rq->next_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7333 | rq->push_cpu = 0; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 7334 | rq->cpu = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7335 | rq->migration_thread = NULL; |
| 7336 | INIT_LIST_HEAD(&rq->migration_queue); |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 7337 | rq_attach_root(rq, &def_root_domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7338 | #endif |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7339 | init_rq_hrtick(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7340 | atomic_set(&rq->nr_iowait, 0); |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 7341 | highest_cpu = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7342 | } |
| 7343 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 7344 | set_load_weight(&init_task); |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 7345 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 7346 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 7347 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); |
| 7348 | #endif |
| 7349 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 7350 | #ifdef CONFIG_SMP |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 7351 | nr_cpu_ids = highest_cpu + 1; |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 7352 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL); |
| 7353 | #endif |
| 7354 | |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 7355 | #ifdef CONFIG_RT_MUTEXES |
| 7356 | plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); |
| 7357 | #endif |
| 7358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | /* |
| 7360 | * The boot idle thread does lazy MMU switching as well: |
| 7361 | */ |
| 7362 | atomic_inc(&init_mm.mm_count); |
| 7363 | enter_lazy_tlb(&init_mm, current); |
| 7364 | |
| 7365 | /* |
| 7366 | * Make us the idle thread. Technically, schedule() should not be |
| 7367 | * called from this thread, however somewhere below it might be, |
| 7368 | * but because we are the idle thread, we just pick up running again |
| 7369 | * when this runqueue becomes "idle". |
| 7370 | */ |
| 7371 | init_idle(current, smp_processor_id()); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7372 | /* |
| 7373 | * During early bootup we pretend to be a normal task: |
| 7374 | */ |
| 7375 | current->sched_class = &fair_sched_class; |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 7376 | |
| 7377 | scheduler_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7378 | } |
| 7379 | |
| 7380 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 7381 | void __might_sleep(char *file, int line) |
| 7382 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7383 | #ifdef in_atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7384 | static unsigned long prev_jiffy; /* ratelimiting */ |
| 7385 | |
| 7386 | if ((in_atomic() || irqs_disabled()) && |
| 7387 | system_state == SYSTEM_RUNNING && !oops_in_progress) { |
| 7388 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 7389 | return; |
| 7390 | prev_jiffy = jiffies; |
Ingo Molnar | 91368d7 | 2006-03-23 03:00:54 -0800 | [diff] [blame] | 7391 | printk(KERN_ERR "BUG: sleeping function called from invalid" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7392 | " context at %s:%d\n", file, line); |
| 7393 | printk("in_atomic():%d, irqs_disabled():%d\n", |
| 7394 | in_atomic(), irqs_disabled()); |
Peter Zijlstra | a4c410f | 2006-12-06 20:37:21 -0800 | [diff] [blame] | 7395 | debug_show_held_locks(current); |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 7396 | if (irqs_disabled()) |
| 7397 | print_irqtrace_events(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7398 | dump_stack(); |
| 7399 | } |
| 7400 | #endif |
| 7401 | } |
| 7402 | EXPORT_SYMBOL(__might_sleep); |
| 7403 | #endif |
| 7404 | |
| 7405 | #ifdef CONFIG_MAGIC_SYSRQ |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 7406 | static void normalize_task(struct rq *rq, struct task_struct *p) |
| 7407 | { |
| 7408 | int on_rq; |
| 7409 | update_rq_clock(rq); |
| 7410 | on_rq = p->se.on_rq; |
| 7411 | if (on_rq) |
| 7412 | deactivate_task(rq, p, 0); |
| 7413 | __setscheduler(rq, p, SCHED_NORMAL, 0); |
| 7414 | if (on_rq) { |
| 7415 | activate_task(rq, p, 0); |
| 7416 | resched_task(rq->curr); |
| 7417 | } |
| 7418 | } |
| 7419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7420 | void normalize_rt_tasks(void) |
| 7421 | { |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 7422 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7423 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 7424 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7425 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7426 | read_lock_irqsave(&tasklist_lock, flags); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 7427 | do_each_thread(g, p) { |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 7428 | /* |
| 7429 | * Only normalize user tasks: |
| 7430 | */ |
| 7431 | if (!p->mm) |
| 7432 | continue; |
| 7433 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7434 | p->se.exec_start = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 7435 | #ifdef CONFIG_SCHEDSTATS |
| 7436 | p->se.wait_start = 0; |
| 7437 | p->se.sleep_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7438 | p->se.block_start = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 7439 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7440 | task_rq(p)->clock = 0; |
| 7441 | |
| 7442 | if (!rt_task(p)) { |
| 7443 | /* |
| 7444 | * Renice negative nice level userspace |
| 7445 | * tasks back to 0: |
| 7446 | */ |
| 7447 | if (TASK_NICE(p) < 0 && p->mm) |
| 7448 | set_user_nice(p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7449 | continue; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7452 | spin_lock(&p->pi_lock); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 7453 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7454 | |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 7455 | normalize_task(rq, p); |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 7456 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 7457 | __task_rq_unlock(rq); |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7458 | spin_unlock(&p->pi_lock); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 7459 | } while_each_thread(g, p); |
| 7460 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7461 | read_unlock_irqrestore(&tasklist_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7462 | } |
| 7463 | |
| 7464 | #endif /* CONFIG_MAGIC_SYSRQ */ |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 7465 | |
| 7466 | #ifdef CONFIG_IA64 |
| 7467 | /* |
| 7468 | * These functions are only useful for the IA64 MCA handling. |
| 7469 | * |
| 7470 | * They can only be called when the whole system has been |
| 7471 | * stopped - every CPU needs to be quiescent, and no scheduling |
| 7472 | * activity can take place. Using them for anything else would |
| 7473 | * be a serious bug, and as a result, they aren't even visible |
| 7474 | * under any other configuration. |
| 7475 | */ |
| 7476 | |
| 7477 | /** |
| 7478 | * curr_task - return the current task for a given cpu. |
| 7479 | * @cpu: the processor in question. |
| 7480 | * |
| 7481 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 7482 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 7483 | struct task_struct *curr_task(int cpu) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 7484 | { |
| 7485 | return cpu_curr(cpu); |
| 7486 | } |
| 7487 | |
| 7488 | /** |
| 7489 | * set_curr_task - set the current task for a given cpu. |
| 7490 | * @cpu: the processor in question. |
| 7491 | * @p: the task pointer to set. |
| 7492 | * |
| 7493 | * Description: This function must only be used when non-maskable interrupts |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7494 | * are serviced on a separate stack. It allows the architecture to switch the |
| 7495 | * 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] | 7496 | * must be called with all CPU's synchronized, and interrupts disabled, the |
| 7497 | * and caller must save the original value of the current task (see |
| 7498 | * curr_task() above) and restore that value before reenabling interrupts and |
| 7499 | * re-starting the system. |
| 7500 | * |
| 7501 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 7502 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 7503 | void set_curr_task(int cpu, struct task_struct *p) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 7504 | { |
| 7505 | cpu_curr(cpu) = p; |
| 7506 | } |
| 7507 | |
| 7508 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7509 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7510 | #ifdef CONFIG_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7511 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7512 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7513 | static void free_fair_sched_group(struct task_group *tg) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7514 | { |
| 7515 | int i; |
| 7516 | |
| 7517 | for_each_possible_cpu(i) { |
| 7518 | if (tg->cfs_rq) |
| 7519 | kfree(tg->cfs_rq[i]); |
| 7520 | if (tg->se) |
| 7521 | kfree(tg->se[i]); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7522 | } |
| 7523 | |
| 7524 | kfree(tg->cfs_rq); |
| 7525 | kfree(tg->se); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7526 | } |
| 7527 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7528 | static int alloc_fair_sched_group(struct task_group *tg) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7529 | { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7530 | struct cfs_rq *cfs_rq; |
| 7531 | struct sched_entity *se; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7532 | struct rq *rq; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7533 | int i; |
| 7534 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7535 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7536 | if (!tg->cfs_rq) |
| 7537 | goto err; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7538 | tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7539 | if (!tg->se) |
| 7540 | goto err; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7541 | |
| 7542 | tg->shares = NICE_0_LOAD; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7543 | |
| 7544 | for_each_possible_cpu(i) { |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7545 | rq = cpu_rq(i); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7546 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7547 | cfs_rq = kmalloc_node(sizeof(struct cfs_rq), |
| 7548 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7549 | if (!cfs_rq) |
| 7550 | goto err; |
| 7551 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7552 | se = kmalloc_node(sizeof(struct sched_entity), |
| 7553 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7554 | if (!se) |
| 7555 | goto err; |
| 7556 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7557 | init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7558 | } |
| 7559 | |
| 7560 | return 1; |
| 7561 | |
| 7562 | err: |
| 7563 | return 0; |
| 7564 | } |
| 7565 | |
| 7566 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) |
| 7567 | { |
| 7568 | list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list, |
| 7569 | &cpu_rq(cpu)->leaf_cfs_rq_list); |
| 7570 | } |
| 7571 | |
| 7572 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 7573 | { |
| 7574 | list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list); |
| 7575 | } |
| 7576 | #else |
| 7577 | static inline void free_fair_sched_group(struct task_group *tg) |
| 7578 | { |
| 7579 | } |
| 7580 | |
| 7581 | static inline int alloc_fair_sched_group(struct task_group *tg) |
| 7582 | { |
| 7583 | return 1; |
| 7584 | } |
| 7585 | |
| 7586 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) |
| 7587 | { |
| 7588 | } |
| 7589 | |
| 7590 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 7591 | { |
| 7592 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7593 | #endif |
| 7594 | |
| 7595 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7596 | static void free_rt_sched_group(struct task_group *tg) |
| 7597 | { |
| 7598 | int i; |
| 7599 | |
| 7600 | for_each_possible_cpu(i) { |
| 7601 | if (tg->rt_rq) |
| 7602 | kfree(tg->rt_rq[i]); |
| 7603 | if (tg->rt_se) |
| 7604 | kfree(tg->rt_se[i]); |
| 7605 | } |
| 7606 | |
| 7607 | kfree(tg->rt_rq); |
| 7608 | kfree(tg->rt_se); |
| 7609 | } |
| 7610 | |
| 7611 | static int alloc_rt_sched_group(struct task_group *tg) |
| 7612 | { |
| 7613 | struct rt_rq *rt_rq; |
| 7614 | struct sched_rt_entity *rt_se; |
| 7615 | struct rq *rq; |
| 7616 | int i; |
| 7617 | |
| 7618 | tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL); |
| 7619 | if (!tg->rt_rq) |
| 7620 | goto err; |
| 7621 | tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL); |
| 7622 | if (!tg->rt_se) |
| 7623 | goto err; |
| 7624 | |
| 7625 | tg->rt_runtime = 0; |
| 7626 | |
| 7627 | for_each_possible_cpu(i) { |
| 7628 | rq = cpu_rq(i); |
| 7629 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7630 | rt_rq = kmalloc_node(sizeof(struct rt_rq), |
| 7631 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
| 7632 | if (!rt_rq) |
| 7633 | goto err; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7634 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7635 | rt_se = kmalloc_node(sizeof(struct sched_rt_entity), |
| 7636 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
| 7637 | if (!rt_se) |
| 7638 | goto err; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7639 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7640 | init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7641 | } |
| 7642 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7643 | return 1; |
| 7644 | |
| 7645 | err: |
| 7646 | return 0; |
| 7647 | } |
| 7648 | |
| 7649 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) |
| 7650 | { |
| 7651 | list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list, |
| 7652 | &cpu_rq(cpu)->leaf_rt_rq_list); |
| 7653 | } |
| 7654 | |
| 7655 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) |
| 7656 | { |
| 7657 | list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list); |
| 7658 | } |
| 7659 | #else |
| 7660 | static inline void free_rt_sched_group(struct task_group *tg) |
| 7661 | { |
| 7662 | } |
| 7663 | |
| 7664 | static inline int alloc_rt_sched_group(struct task_group *tg) |
| 7665 | { |
| 7666 | return 1; |
| 7667 | } |
| 7668 | |
| 7669 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) |
| 7670 | { |
| 7671 | } |
| 7672 | |
| 7673 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) |
| 7674 | { |
| 7675 | } |
| 7676 | #endif |
| 7677 | |
| 7678 | static void free_sched_group(struct task_group *tg) |
| 7679 | { |
| 7680 | free_fair_sched_group(tg); |
| 7681 | free_rt_sched_group(tg); |
| 7682 | kfree(tg); |
| 7683 | } |
| 7684 | |
| 7685 | /* allocate runqueue etc for a new task group */ |
| 7686 | struct task_group *sched_create_group(void) |
| 7687 | { |
| 7688 | struct task_group *tg; |
| 7689 | unsigned long flags; |
| 7690 | int i; |
| 7691 | |
| 7692 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); |
| 7693 | if (!tg) |
| 7694 | return ERR_PTR(-ENOMEM); |
| 7695 | |
| 7696 | if (!alloc_fair_sched_group(tg)) |
| 7697 | goto err; |
| 7698 | |
| 7699 | if (!alloc_rt_sched_group(tg)) |
| 7700 | goto err; |
| 7701 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7702 | spin_lock_irqsave(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7703 | for_each_possible_cpu(i) { |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7704 | register_fair_sched_group(tg, i); |
| 7705 | register_rt_sched_group(tg, i); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7706 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7707 | list_add_rcu(&tg->list, &task_groups); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7708 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7709 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7710 | return tg; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7711 | |
| 7712 | err: |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7713 | free_sched_group(tg); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7714 | return ERR_PTR(-ENOMEM); |
| 7715 | } |
| 7716 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7717 | /* rcu callback to free various structures associated with a task group */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7718 | static void free_sched_group_rcu(struct rcu_head *rhp) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7719 | { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7720 | /* now it should be safe to free those cfs_rqs */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7721 | free_sched_group(container_of(rhp, struct task_group, rcu)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7722 | } |
| 7723 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7724 | /* Destroy runqueue etc associated with a task group */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7725 | void sched_destroy_group(struct task_group *tg) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7726 | { |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7727 | unsigned long flags; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7728 | int i; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7729 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7730 | spin_lock_irqsave(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7731 | for_each_possible_cpu(i) { |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7732 | unregister_fair_sched_group(tg, i); |
| 7733 | unregister_rt_sched_group(tg, i); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7734 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7735 | list_del_rcu(&tg->list); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7736 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7737 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7738 | /* wait for possible concurrent references to cfs_rqs complete */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7739 | call_rcu(&tg->rcu, free_sched_group_rcu); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7740 | } |
| 7741 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7742 | /* change task's runqueue when it moves between groups. |
Ingo Molnar | 3a25201 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 7743 | * The caller of this function should have put the task in its new group |
| 7744 | * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to |
| 7745 | * reflect its new group. |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7746 | */ |
| 7747 | void sched_move_task(struct task_struct *tsk) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7748 | { |
| 7749 | int on_rq, running; |
| 7750 | unsigned long flags; |
| 7751 | struct rq *rq; |
| 7752 | |
| 7753 | rq = task_rq_lock(tsk, &flags); |
| 7754 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7755 | update_rq_clock(rq); |
| 7756 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 7757 | running = task_current(rq, tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7758 | on_rq = tsk->se.on_rq; |
| 7759 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 7760 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7761 | dequeue_task(rq, tsk, 0); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 7762 | if (unlikely(running)) |
| 7763 | tsk->sched_class->put_prev_task(rq, tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7764 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7765 | set_task_rq(tsk, task_cpu(tsk)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7766 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 7767 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7768 | if (tsk->sched_class->moved_group) |
| 7769 | tsk->sched_class->moved_group(tsk); |
| 7770 | #endif |
| 7771 | |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 7772 | if (unlikely(running)) |
| 7773 | tsk->sched_class->set_curr_task(rq); |
| 7774 | if (on_rq) |
Dmitry Adamushko | 7074bad | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7775 | enqueue_task(rq, tsk, 0); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7776 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7777 | task_rq_unlock(rq, &flags); |
| 7778 | } |
| 7779 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7780 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7781 | static void set_se_shares(struct sched_entity *se, unsigned long shares) |
| 7782 | { |
| 7783 | struct cfs_rq *cfs_rq = se->cfs_rq; |
| 7784 | struct rq *rq = cfs_rq->rq; |
| 7785 | int on_rq; |
| 7786 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 7787 | spin_lock_irq(&rq->lock); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7788 | |
| 7789 | on_rq = se->on_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 7790 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7791 | dequeue_entity(cfs_rq, se, 0); |
| 7792 | |
| 7793 | se->load.weight = shares; |
| 7794 | se->load.inv_weight = div64_64((1ULL<<32), shares); |
| 7795 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 7796 | if (on_rq) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7797 | enqueue_entity(cfs_rq, se, 0); |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 7798 | |
| 7799 | spin_unlock_irq(&rq->lock); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7800 | } |
| 7801 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7802 | static DEFINE_MUTEX(shares_mutex); |
| 7803 | |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7804 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7805 | { |
| 7806 | int i; |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7807 | unsigned long flags; |
Ingo Molnar | c61935f | 2008-01-22 11:24:58 +0100 | [diff] [blame] | 7808 | |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 7809 | /* |
| 7810 | * A weight of 0 or 1 can cause arithmetics problems. |
| 7811 | * (The default weight is 1024 - so there's no practical |
| 7812 | * limitation from this.) |
| 7813 | */ |
| 7814 | if (shares < 2) |
| 7815 | shares = 2; |
| 7816 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7817 | mutex_lock(&shares_mutex); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7818 | if (tg->shares == shares) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7819 | goto done; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7820 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7821 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7822 | for_each_possible_cpu(i) |
| 7823 | unregister_fair_sched_group(tg, i); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7824 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7825 | |
| 7826 | /* wait for any ongoing reference to this group to finish */ |
| 7827 | synchronize_sched(); |
| 7828 | |
| 7829 | /* |
| 7830 | * Now we are free to modify the group's share on each cpu |
| 7831 | * w/o tripping rebalance_share or load_balance_fair. |
| 7832 | */ |
| 7833 | tg->shares = shares; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 7834 | for_each_possible_cpu(i) |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7835 | set_se_shares(tg->se[i], shares); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7836 | |
| 7837 | /* |
| 7838 | * Enable load balance activity on this group, by inserting it back on |
| 7839 | * each cpu's rq->leaf_cfs_rq_list. |
| 7840 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7841 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7842 | for_each_possible_cpu(i) |
| 7843 | register_fair_sched_group(tg, i); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7844 | spin_unlock_irqrestore(&task_group_lock, flags); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7845 | done: |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7846 | mutex_unlock(&shares_mutex); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7847 | return 0; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7848 | } |
| 7849 | |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7850 | unsigned long sched_group_shares(struct task_group *tg) |
| 7851 | { |
| 7852 | return tg->shares; |
| 7853 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7854 | #endif |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7855 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7856 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7857 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7858 | * Ensure that the real time constraints are schedulable. |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7859 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7860 | static DEFINE_MUTEX(rt_constraints_mutex); |
| 7861 | |
| 7862 | static unsigned long to_ratio(u64 period, u64 runtime) |
| 7863 | { |
| 7864 | if (runtime == RUNTIME_INF) |
| 7865 | return 1ULL << 16; |
| 7866 | |
Peter Zijlstra | 2692a24 | 2008-02-27 12:00:46 +0100 | [diff] [blame] | 7867 | return div64_64(runtime << 16, period); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7868 | } |
| 7869 | |
| 7870 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7871 | { |
| 7872 | struct task_group *tgi; |
| 7873 | unsigned long total = 0; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7874 | unsigned long global_ratio = |
| 7875 | to_ratio(sysctl_sched_rt_period, |
| 7876 | sysctl_sched_rt_runtime < 0 ? |
| 7877 | RUNTIME_INF : sysctl_sched_rt_runtime); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7878 | |
| 7879 | rcu_read_lock(); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7880 | list_for_each_entry_rcu(tgi, &task_groups, list) { |
| 7881 | if (tgi == tg) |
| 7882 | continue; |
| 7883 | |
| 7884 | total += to_ratio(period, tgi->rt_runtime); |
| 7885 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7886 | rcu_read_unlock(); |
| 7887 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7888 | return total + to_ratio(period, runtime) < global_ratio; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7889 | } |
| 7890 | |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 7891 | /* Must be called with tasklist_lock held */ |
| 7892 | static inline int tg_has_rt_tasks(struct task_group *tg) |
| 7893 | { |
| 7894 | struct task_struct *g, *p; |
| 7895 | do_each_thread(g, p) { |
| 7896 | if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg) |
| 7897 | return 1; |
| 7898 | } while_each_thread(g, p); |
| 7899 | return 0; |
| 7900 | } |
| 7901 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7902 | int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7903 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7904 | u64 rt_runtime, rt_period; |
| 7905 | int err = 0; |
| 7906 | |
Peter Zijlstra | 2692a24 | 2008-02-27 12:00:46 +0100 | [diff] [blame] | 7907 | rt_period = (u64)sysctl_sched_rt_period * NSEC_PER_USEC; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7908 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; |
| 7909 | if (rt_runtime_us == -1) |
Peter Zijlstra | 2692a24 | 2008-02-27 12:00:46 +0100 | [diff] [blame] | 7910 | rt_runtime = RUNTIME_INF; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7911 | |
| 7912 | mutex_lock(&rt_constraints_mutex); |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 7913 | read_lock(&tasklist_lock); |
| 7914 | if (rt_runtime_us == 0 && tg_has_rt_tasks(tg)) { |
| 7915 | err = -EBUSY; |
| 7916 | goto unlock; |
| 7917 | } |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7918 | if (!__rt_schedulable(tg, rt_period, rt_runtime)) { |
| 7919 | err = -EINVAL; |
| 7920 | goto unlock; |
| 7921 | } |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7922 | tg->rt_runtime = rt_runtime; |
| 7923 | unlock: |
Dhaval Giani | 521f1a24 | 2008-02-28 15:21:56 +0530 | [diff] [blame] | 7924 | read_unlock(&tasklist_lock); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7925 | mutex_unlock(&rt_constraints_mutex); |
| 7926 | |
| 7927 | return err; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7928 | } |
| 7929 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7930 | long sched_group_rt_runtime(struct task_group *tg) |
| 7931 | { |
| 7932 | u64 rt_runtime_us; |
| 7933 | |
| 7934 | if (tg->rt_runtime == RUNTIME_INF) |
| 7935 | return -1; |
| 7936 | |
| 7937 | rt_runtime_us = tg->rt_runtime; |
| 7938 | do_div(rt_runtime_us, NSEC_PER_USEC); |
| 7939 | return rt_runtime_us; |
| 7940 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7941 | #endif |
| 7942 | #endif /* CONFIG_GROUP_SCHED */ |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7943 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7944 | #ifdef CONFIG_CGROUP_SCHED |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7945 | |
| 7946 | /* return corresponding task_group object of a cgroup */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 7947 | static inline struct task_group *cgroup_tg(struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7948 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 7949 | return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id), |
| 7950 | struct task_group, css); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7951 | } |
| 7952 | |
| 7953 | static struct cgroup_subsys_state * |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 7954 | cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7955 | { |
| 7956 | struct task_group *tg; |
| 7957 | |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 7958 | if (!cgrp->parent) { |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7959 | /* This is early initialization for the top cgroup */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 7960 | init_task_group.css.cgroup = cgrp; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7961 | return &init_task_group.css; |
| 7962 | } |
| 7963 | |
| 7964 | /* we support only 1-level deep hierarchical scheduler atm */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 7965 | if (cgrp->parent->parent) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7966 | return ERR_PTR(-EINVAL); |
| 7967 | |
| 7968 | tg = sched_create_group(); |
| 7969 | if (IS_ERR(tg)) |
| 7970 | return ERR_PTR(-ENOMEM); |
| 7971 | |
| 7972 | /* Bind the cgroup to task_group object we just created */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 7973 | tg->css.cgroup = cgrp; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7974 | |
| 7975 | return &tg->css; |
| 7976 | } |
| 7977 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7978 | static void |
| 7979 | cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7980 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 7981 | struct task_group *tg = cgroup_tg(cgrp); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7982 | |
| 7983 | sched_destroy_group(tg); |
| 7984 | } |
| 7985 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7986 | static int |
| 7987 | cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
| 7988 | struct task_struct *tsk) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7989 | { |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7990 | #ifdef CONFIG_RT_GROUP_SCHED |
| 7991 | /* Don't accept realtime tasks when there is no way for them to run */ |
| 7992 | if (rt_task(tsk) && cgroup_tg(cgrp)->rt_runtime == 0) |
| 7993 | return -EINVAL; |
| 7994 | #else |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7995 | /* We don't support RT-tasks being in separate groups */ |
| 7996 | if (tsk->sched_class != &fair_sched_class) |
| 7997 | return -EINVAL; |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7998 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7999 | |
| 8000 | return 0; |
| 8001 | } |
| 8002 | |
| 8003 | static void |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8004 | cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8005 | struct cgroup *old_cont, struct task_struct *tsk) |
| 8006 | { |
| 8007 | sched_move_task(tsk); |
| 8008 | } |
| 8009 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8010 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8011 | static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype, |
| 8012 | u64 shareval) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8013 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8014 | return sched_group_set_shares(cgroup_tg(cgrp), shareval); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8015 | } |
| 8016 | |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8017 | static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8018 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8019 | struct task_group *tg = cgroup_tg(cgrp); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8020 | |
| 8021 | return (u64) tg->shares; |
| 8022 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8023 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8024 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8025 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8026 | static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft, |
| 8027 | struct file *file, |
| 8028 | const char __user *userbuf, |
| 8029 | size_t nbytes, loff_t *unused_ppos) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8030 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8031 | char buffer[64]; |
| 8032 | int retval = 0; |
| 8033 | s64 val; |
| 8034 | char *end; |
| 8035 | |
| 8036 | if (!nbytes) |
| 8037 | return -EINVAL; |
| 8038 | if (nbytes >= sizeof(buffer)) |
| 8039 | return -E2BIG; |
| 8040 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 8041 | return -EFAULT; |
| 8042 | |
| 8043 | buffer[nbytes] = 0; /* nul-terminate */ |
| 8044 | |
| 8045 | /* strip newline if necessary */ |
| 8046 | if (nbytes && (buffer[nbytes-1] == '\n')) |
| 8047 | buffer[nbytes-1] = 0; |
| 8048 | val = simple_strtoll(buffer, &end, 0); |
| 8049 | if (*end) |
| 8050 | return -EINVAL; |
| 8051 | |
| 8052 | /* Pass to subsystem */ |
| 8053 | retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val); |
| 8054 | if (!retval) |
| 8055 | retval = nbytes; |
| 8056 | return retval; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8057 | } |
| 8058 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8059 | static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft, |
| 8060 | struct file *file, |
| 8061 | char __user *buf, size_t nbytes, |
| 8062 | loff_t *ppos) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8063 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8064 | char tmp[64]; |
| 8065 | long val = sched_group_rt_runtime(cgroup_tg(cgrp)); |
| 8066 | int len = sprintf(tmp, "%ld\n", val); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8067 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8068 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8069 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8070 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8071 | |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8072 | static struct cftype cpu_files[] = { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8073 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8074 | { |
| 8075 | .name = "shares", |
| 8076 | .read_uint = cpu_shares_read_uint, |
| 8077 | .write_uint = cpu_shares_write_uint, |
| 8078 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8079 | #endif |
| 8080 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8081 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8082 | .name = "rt_runtime_us", |
| 8083 | .read = cpu_rt_runtime_read, |
| 8084 | .write = cpu_rt_runtime_write, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8085 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8086 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8087 | }; |
| 8088 | |
| 8089 | static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont) |
| 8090 | { |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8091 | return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files)); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8092 | } |
| 8093 | |
| 8094 | struct cgroup_subsys cpu_cgroup_subsys = { |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8095 | .name = "cpu", |
| 8096 | .create = cpu_cgroup_create, |
| 8097 | .destroy = cpu_cgroup_destroy, |
| 8098 | .can_attach = cpu_cgroup_can_attach, |
| 8099 | .attach = cpu_cgroup_attach, |
| 8100 | .populate = cpu_cgroup_populate, |
| 8101 | .subsys_id = cpu_cgroup_subsys_id, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8102 | .early_init = 1, |
| 8103 | }; |
| 8104 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8105 | #endif /* CONFIG_CGROUP_SCHED */ |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8106 | |
| 8107 | #ifdef CONFIG_CGROUP_CPUACCT |
| 8108 | |
| 8109 | /* |
| 8110 | * CPU accounting code for task groups. |
| 8111 | * |
| 8112 | * Based on the work by Paul Menage (menage@google.com) and Balbir Singh |
| 8113 | * (balbir@in.ibm.com). |
| 8114 | */ |
| 8115 | |
| 8116 | /* track cpu usage of a group of tasks */ |
| 8117 | struct cpuacct { |
| 8118 | struct cgroup_subsys_state css; |
| 8119 | /* cpuusage holds pointer to a u64-type object on every cpu */ |
| 8120 | u64 *cpuusage; |
| 8121 | }; |
| 8122 | |
| 8123 | struct cgroup_subsys cpuacct_subsys; |
| 8124 | |
| 8125 | /* return cpu accounting group corresponding to this container */ |
| 8126 | static inline struct cpuacct *cgroup_ca(struct cgroup *cont) |
| 8127 | { |
| 8128 | return container_of(cgroup_subsys_state(cont, cpuacct_subsys_id), |
| 8129 | struct cpuacct, css); |
| 8130 | } |
| 8131 | |
| 8132 | /* return cpu accounting group to which this task belongs */ |
| 8133 | static inline struct cpuacct *task_ca(struct task_struct *tsk) |
| 8134 | { |
| 8135 | return container_of(task_subsys_state(tsk, cpuacct_subsys_id), |
| 8136 | struct cpuacct, css); |
| 8137 | } |
| 8138 | |
| 8139 | /* create a new cpu accounting group */ |
| 8140 | static struct cgroup_subsys_state *cpuacct_create( |
| 8141 | struct cgroup_subsys *ss, struct cgroup *cont) |
| 8142 | { |
| 8143 | struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL); |
| 8144 | |
| 8145 | if (!ca) |
| 8146 | return ERR_PTR(-ENOMEM); |
| 8147 | |
| 8148 | ca->cpuusage = alloc_percpu(u64); |
| 8149 | if (!ca->cpuusage) { |
| 8150 | kfree(ca); |
| 8151 | return ERR_PTR(-ENOMEM); |
| 8152 | } |
| 8153 | |
| 8154 | return &ca->css; |
| 8155 | } |
| 8156 | |
| 8157 | /* destroy an existing cpu accounting group */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8158 | static void |
| 8159 | cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cont) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8160 | { |
| 8161 | struct cpuacct *ca = cgroup_ca(cont); |
| 8162 | |
| 8163 | free_percpu(ca->cpuusage); |
| 8164 | kfree(ca); |
| 8165 | } |
| 8166 | |
| 8167 | /* return total cpu usage (in nanoseconds) of a group */ |
| 8168 | static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft) |
| 8169 | { |
| 8170 | struct cpuacct *ca = cgroup_ca(cont); |
| 8171 | u64 totalcpuusage = 0; |
| 8172 | int i; |
| 8173 | |
| 8174 | for_each_possible_cpu(i) { |
| 8175 | u64 *cpuusage = percpu_ptr(ca->cpuusage, i); |
| 8176 | |
| 8177 | /* |
| 8178 | * Take rq->lock to make 64-bit addition safe on 32-bit |
| 8179 | * platforms. |
| 8180 | */ |
| 8181 | spin_lock_irq(&cpu_rq(i)->lock); |
| 8182 | totalcpuusage += *cpuusage; |
| 8183 | spin_unlock_irq(&cpu_rq(i)->lock); |
| 8184 | } |
| 8185 | |
| 8186 | return totalcpuusage; |
| 8187 | } |
| 8188 | |
| 8189 | static struct cftype files[] = { |
| 8190 | { |
| 8191 | .name = "usage", |
| 8192 | .read_uint = cpuusage_read, |
| 8193 | }, |
| 8194 | }; |
| 8195 | |
| 8196 | static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cont) |
| 8197 | { |
| 8198 | return cgroup_add_files(cont, ss, files, ARRAY_SIZE(files)); |
| 8199 | } |
| 8200 | |
| 8201 | /* |
| 8202 | * charge this task's execution time to its accounting group. |
| 8203 | * |
| 8204 | * called with rq->lock held. |
| 8205 | */ |
| 8206 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime) |
| 8207 | { |
| 8208 | struct cpuacct *ca; |
| 8209 | |
| 8210 | if (!cpuacct_subsys.active) |
| 8211 | return; |
| 8212 | |
| 8213 | ca = task_ca(tsk); |
| 8214 | if (ca) { |
| 8215 | u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk)); |
| 8216 | |
| 8217 | *cpuusage += cputime; |
| 8218 | } |
| 8219 | } |
| 8220 | |
| 8221 | struct cgroup_subsys cpuacct_subsys = { |
| 8222 | .name = "cpuacct", |
| 8223 | .create = cpuacct_create, |
| 8224 | .destroy = cpuacct_destroy, |
| 8225 | .populate = cpuacct_populate, |
| 8226 | .subsys_id = cpuacct_subsys_id, |
| 8227 | }; |
| 8228 | #endif /* CONFIG_CGROUP_CPUACCT */ |