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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 70 | #include <asm/tlb.h> |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 71 | #include <asm/irq_regs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
Alexey Dobriyan | b035b6d | 2007-02-10 01:45:10 -0800 | [diff] [blame] | 74 | * Scheduler clock - returns current time in nanosec units. |
| 75 | * This is default implementation. |
| 76 | * Architectures and sub-architectures can override this. |
| 77 | */ |
| 78 | unsigned long long __attribute__((weak)) sched_clock(void) |
| 79 | { |
Eric Dumazet | d6322fa | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 80 | return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ); |
Alexey Dobriyan | b035b6d | 2007-02-10 01:45:10 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * Convert user-nice values [ -20 ... 0 ... 19 ] |
| 85 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], |
| 86 | * and back. |
| 87 | */ |
| 88 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) |
| 89 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) |
| 90 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) |
| 91 | |
| 92 | /* |
| 93 | * 'User priority' is the nice value converted to something we |
| 94 | * can work with better when scaling various scheduler parameters, |
| 95 | * it's a [ 0 ... 39 ] range. |
| 96 | */ |
| 97 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) |
| 98 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) |
| 99 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) |
| 100 | |
| 101 | /* |
Ingo Molnar | d7876a0 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 102 | * Helpers for converting nanosecond timing to jiffy resolution |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | */ |
Eric Dumazet | d6322fa | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 104 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 106 | #define NICE_0_LOAD SCHED_LOAD_SCALE |
| 107 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | /* |
| 110 | * These are the 'tuning knobs' of the scheduler: |
| 111 | * |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 112 | * default timeslice is 100 msecs (used only for SCHED_RR tasks). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * Timeslices get refilled after they expire. |
| 114 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | #define DEF_TIMESLICE (100 * HZ / 1000) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 116 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 117 | #ifdef CONFIG_SMP |
| 118 | /* |
| 119 | * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) |
| 120 | * Since cpu_power is a 'constant', we can use a reciprocal divide. |
| 121 | */ |
| 122 | static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load) |
| 123 | { |
| 124 | return reciprocal_divide(load, sg->reciprocal_cpu_power); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Each time a sched group cpu_power is changed, |
| 129 | * we must compute its reciprocal value |
| 130 | */ |
| 131 | static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val) |
| 132 | { |
| 133 | sg->__cpu_power += val; |
| 134 | sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power); |
| 135 | } |
| 136 | #endif |
| 137 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 138 | static inline int rt_policy(int policy) |
| 139 | { |
| 140 | if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR)) |
| 141 | return 1; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static inline int task_has_rt_policy(struct task_struct *p) |
| 146 | { |
| 147 | return rt_policy(p->policy); |
| 148 | } |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | /* |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 151 | * This is the priority-queue data structure of the RT scheduling class: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | */ |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 153 | struct rt_prio_array { |
| 154 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ |
| 155 | struct list_head queue[MAX_RT_PRIO]; |
| 156 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 158 | #ifdef CONFIG_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 159 | |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 160 | #include <linux/cgroup.h> |
| 161 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 162 | struct cfs_rq; |
| 163 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 164 | static LIST_HEAD(task_groups); |
| 165 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 166 | /* task group related information */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 167 | struct task_group { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 168 | #ifdef CONFIG_CGROUP_SCHED |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 169 | struct cgroup_subsys_state css; |
| 170 | #endif |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 171 | |
| 172 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 173 | /* schedulable entities of this group on each cpu */ |
| 174 | struct sched_entity **se; |
| 175 | /* runqueue "owned" by this group on each cpu */ |
| 176 | struct cfs_rq **cfs_rq; |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * shares assigned to a task group governs how much of cpu bandwidth |
| 180 | * is allocated to the group. The more shares a group has, the more is |
| 181 | * the cpu bandwidth allocated to it. |
| 182 | * |
| 183 | * For ex, lets say that there are three task groups, A, B and C which |
| 184 | * have been assigned shares 1000, 2000 and 3000 respectively. Then, |
| 185 | * cpu bandwidth allocated by the scheduler to task groups A, B and C |
| 186 | * should be: |
| 187 | * |
| 188 | * Bw(A) = 1000/(1000+2000+3000) * 100 = 16.66% |
| 189 | * Bw(B) = 2000/(1000+2000+3000) * 100 = 33.33% |
Ingo Molnar | 03319ec | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 190 | * Bw(C) = 3000/(1000+2000+3000) * 100 = 50% |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 191 | * |
| 192 | * The weight assigned to a task group's schedulable entities on every |
| 193 | * cpu (task_group.se[a_cpu]->load.weight) is derived from the task |
| 194 | * group's shares. For ex: lets say that task group A has been |
| 195 | * assigned shares of 1000 and there are two CPUs in a system. Then, |
| 196 | * |
| 197 | * tg_A->se[0]->load.weight = tg_A->se[1]->load.weight = 1000; |
| 198 | * |
| 199 | * Note: It's not necessary that each of a task's group schedulable |
Ingo Molnar | 03319ec | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 200 | * entity have the same weight on all CPUs. If the group |
| 201 | * has 2 of its tasks on CPU0 and 1 task on CPU1, then a |
| 202 | * better distribution of weight could be: |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 203 | * |
| 204 | * tg_A->se[0]->load.weight = 2/3 * 2000 = 1333 |
| 205 | * tg_A->se[1]->load.weight = 1/2 * 2000 = 667 |
| 206 | * |
| 207 | * rebalance_shares() is responsible for distributing the shares of a |
| 208 | * task groups like this among the group's schedulable entities across |
| 209 | * cpus. |
| 210 | * |
| 211 | */ |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 212 | unsigned long shares; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 213 | #endif |
| 214 | |
| 215 | #ifdef CONFIG_RT_GROUP_SCHED |
| 216 | struct sched_rt_entity **rt_se; |
| 217 | struct rt_rq **rt_rq; |
| 218 | |
| 219 | u64 rt_runtime; |
| 220 | #endif |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 221 | |
Srivatsa Vaddagiri | ae8393e | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 222 | struct rcu_head rcu; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 223 | struct list_head list; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 224 | }; |
| 225 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 226 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 227 | /* Default task group's sched entity on each cpu */ |
| 228 | static DEFINE_PER_CPU(struct sched_entity, init_sched_entity); |
| 229 | /* Default task group's cfs_rq on each cpu */ |
| 230 | static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp; |
| 231 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 232 | static struct sched_entity *init_sched_entity_p[NR_CPUS]; |
| 233 | static struct cfs_rq *init_cfs_rq_p[NR_CPUS]; |
| 234 | #endif |
| 235 | |
| 236 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 237 | static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity); |
| 238 | static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp; |
| 239 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 240 | static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS]; |
| 241 | static struct rt_rq *init_rt_rq_p[NR_CPUS]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 242 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 243 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 244 | /* 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] | 245 | * a task group's cpu shares. |
| 246 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 247 | static DEFINE_SPINLOCK(task_group_lock); |
Srivatsa Vaddagiri | ec2c507 | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 248 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 249 | /* doms_cur_mutex serializes access to doms_cur[] array */ |
| 250 | static DEFINE_MUTEX(doms_cur_mutex); |
| 251 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 252 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 253 | #ifdef CONFIG_SMP |
| 254 | /* kernel thread that runs rebalance_shares() periodically */ |
| 255 | static struct task_struct *lb_monitor_task; |
| 256 | static int load_balance_monitor(void *unused); |
| 257 | #endif |
| 258 | |
| 259 | static void set_se_shares(struct sched_entity *se, unsigned long shares); |
| 260 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 261 | #ifdef CONFIG_USER_SCHED |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 262 | # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD) |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 263 | #else |
Srivatsa Vaddagiri | 93f992c | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 264 | # define INIT_TASK_GROUP_LOAD NICE_0_LOAD |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 265 | #endif |
| 266 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 267 | #define MIN_GROUP_SHARES 2 |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 268 | |
Srivatsa Vaddagiri | 93f992c | 2008-01-25 21:07:59 +0100 | [diff] [blame] | 269 | static int init_task_group_load = INIT_TASK_GROUP_LOAD; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 270 | #endif |
| 271 | |
| 272 | /* Default task group. |
| 273 | * Every task in system belong to this group at bootup. |
| 274 | */ |
| 275 | struct task_group init_task_group = { |
| 276 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 277 | .se = init_sched_entity_p, |
| 278 | .cfs_rq = init_cfs_rq_p, |
| 279 | #endif |
| 280 | |
| 281 | #ifdef CONFIG_RT_GROUP_SCHED |
| 282 | .rt_se = init_sched_rt_entity_p, |
| 283 | .rt_rq = init_rt_rq_p, |
| 284 | #endif |
| 285 | }; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 286 | |
| 287 | /* return group to which a task belongs */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 288 | static inline struct task_group *task_group(struct task_struct *p) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 289 | { |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 290 | struct task_group *tg; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 291 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 292 | #ifdef CONFIG_USER_SCHED |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 293 | tg = p->user->tg; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 294 | #elif defined(CONFIG_CGROUP_SCHED) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 295 | tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id), |
| 296 | struct task_group, css); |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 297 | #else |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 298 | tg = &init_task_group; |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 299 | #endif |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 300 | return tg; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* 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] | 304 | 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] | 305 | { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 306 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Dmitry Adamushko | ce96b5a | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 307 | p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; |
| 308 | p->se.parent = task_group(p)->se[cpu]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 309 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 310 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 311 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 312 | p->rt.rt_rq = task_group(p)->rt_rq[cpu]; |
| 313 | p->rt.parent = task_group(p)->rt_se[cpu]; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 314 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 315 | } |
| 316 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 317 | static inline void lock_doms_cur(void) |
| 318 | { |
| 319 | mutex_lock(&doms_cur_mutex); |
| 320 | } |
| 321 | |
| 322 | static inline void unlock_doms_cur(void) |
| 323 | { |
| 324 | mutex_unlock(&doms_cur_mutex); |
| 325 | } |
| 326 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 327 | #else |
| 328 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 329 | 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] | 330 | static inline void lock_doms_cur(void) { } |
| 331 | static inline void unlock_doms_cur(void) { } |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 332 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 333 | #endif /* CONFIG_GROUP_SCHED */ |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 334 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 335 | /* CFS-related fields in a runqueue */ |
| 336 | struct cfs_rq { |
| 337 | struct load_weight load; |
| 338 | unsigned long nr_running; |
| 339 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 340 | u64 exec_clock; |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 341 | u64 min_vruntime; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 342 | |
| 343 | struct rb_root tasks_timeline; |
| 344 | struct rb_node *rb_leftmost; |
| 345 | struct rb_node *rb_load_balance_curr; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 346 | /* 'curr' points to currently running entity on this cfs_rq. |
| 347 | * It is set to NULL otherwise (i.e when none are currently running). |
| 348 | */ |
| 349 | struct sched_entity *curr; |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 350 | |
| 351 | unsigned long nr_spread_over; |
| 352 | |
Ingo Molnar | 62160e3 | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 353 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 354 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ |
| 355 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 356 | /* |
| 357 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 358 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities |
| 359 | * (like users, containers etc.) |
| 360 | * |
| 361 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This |
| 362 | * list is used during load balance. |
| 363 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 364 | struct list_head leaf_cfs_rq_list; |
| 365 | struct task_group *tg; /* group that "owns" this runqueue */ |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 366 | #endif |
| 367 | }; |
| 368 | |
| 369 | /* Real-Time classes' related field in a runqueue: */ |
| 370 | struct rt_rq { |
| 371 | struct rt_prio_array active; |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 372 | unsigned long rt_nr_running; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 373 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 374 | int highest_prio; /* highest queued rt task prio */ |
| 375 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 376 | #ifdef CONFIG_SMP |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 377 | unsigned long rt_nr_migratory; |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 378 | int overloaded; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 379 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 380 | int rt_throttled; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 381 | u64 rt_time; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 382 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 383 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 384 | unsigned long rt_nr_boosted; |
| 385 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 386 | struct rq *rq; |
| 387 | struct list_head leaf_rt_rq_list; |
| 388 | struct task_group *tg; |
| 389 | struct sched_rt_entity *rt_se; |
| 390 | #endif |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 391 | }; |
| 392 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 393 | #ifdef CONFIG_SMP |
| 394 | |
| 395 | /* |
| 396 | * 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] | 397 | * variables. Each exclusive cpuset essentially defines an island domain by |
| 398 | * fully partitioning the member cpus from any other cpuset. Whenever a new |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 399 | * exclusive cpuset is created, we also create and attach a new root-domain |
| 400 | * object. |
| 401 | * |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 402 | */ |
| 403 | struct root_domain { |
| 404 | atomic_t refcount; |
| 405 | cpumask_t span; |
| 406 | cpumask_t online; |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 407 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 408 | /* |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 409 | * The "RT overload" flag: it gets set if a CPU has more than |
| 410 | * one runnable RT task. |
| 411 | */ |
| 412 | cpumask_t rto_mask; |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 413 | atomic_t rto_count; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 414 | }; |
| 415 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 416 | /* |
| 417 | * By default the system creates a single root-domain with all cpus as |
| 418 | * members (mimicking the global state we have today). |
| 419 | */ |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 420 | static struct root_domain def_root_domain; |
| 421 | |
| 422 | #endif |
| 423 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 424 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | * This is the main, per-CPU runqueue data structure. |
| 426 | * |
| 427 | * Locking rule: those places that want to lock multiple runqueues |
| 428 | * (such as the load balancing or the thread migration code), lock |
| 429 | * acquire operations must be ordered by ascending &runqueue. |
| 430 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 431 | struct rq { |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 432 | /* runqueue lock: */ |
| 433 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
| 435 | /* |
| 436 | * nr_running and cpu_load should be in the same cacheline because |
| 437 | * remote CPUs use both these fields when doing load calculation. |
| 438 | */ |
| 439 | unsigned long nr_running; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 440 | #define CPU_LOAD_IDX_MAX 5 |
| 441 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; |
Siddha, Suresh B | bdecea3 | 2007-05-08 00:32:48 -0700 | [diff] [blame] | 442 | unsigned char idle_at_tick; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 443 | #ifdef CONFIG_NO_HZ |
| 444 | unsigned char in_nohz_recently; |
| 445 | #endif |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 446 | /* capture load from *all* tasks on this cpu: */ |
| 447 | struct load_weight load; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 448 | unsigned long nr_load_updates; |
| 449 | u64 nr_switches; |
| 450 | |
| 451 | struct cfs_rq cfs; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 452 | struct rt_rq rt; |
| 453 | u64 rt_period_expire; |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 454 | int rt_throttled; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 455 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 456 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 457 | /* list of leaf cfs_rq on this cpu: */ |
| 458 | struct list_head leaf_cfs_rq_list; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 459 | #endif |
| 460 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 461 | struct list_head leaf_rt_rq_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | * This is part of a global counter where only the total sum |
| 466 | * over all CPUs matters. A task can increase this counter on |
| 467 | * one CPU and if it got migrated afterwards it may decrease |
| 468 | * it on another CPU. Always updated under the runqueue lock: |
| 469 | */ |
| 470 | unsigned long nr_uninterruptible; |
| 471 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 472 | struct task_struct *curr, *idle; |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 473 | unsigned long next_balance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | struct mm_struct *prev_mm; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 475 | |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 476 | u64 clock, prev_clock_raw; |
| 477 | s64 clock_max_delta; |
| 478 | |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 479 | unsigned int clock_warps, clock_overflows, clock_underflows; |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 480 | u64 idle_clock; |
| 481 | unsigned int clock_deep_idle_events; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 482 | u64 tick_timestamp; |
Ingo Molnar | 6aa645e | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | atomic_t nr_iowait; |
| 485 | |
| 486 | #ifdef CONFIG_SMP |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 487 | struct root_domain *rd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | struct sched_domain *sd; |
| 489 | |
| 490 | /* For active balancing */ |
| 491 | int active_balance; |
| 492 | int push_cpu; |
Ingo Molnar | d801649 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 493 | /* cpu of this runqueue: */ |
| 494 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 496 | struct task_struct *migration_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | struct list_head migration_queue; |
| 498 | #endif |
| 499 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 500 | #ifdef CONFIG_SCHED_HRTICK |
| 501 | unsigned long hrtick_flags; |
| 502 | ktime_t hrtick_expire; |
| 503 | struct hrtimer hrtick_timer; |
| 504 | #endif |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | #ifdef CONFIG_SCHEDSTATS |
| 507 | /* latency stats */ |
| 508 | struct sched_info rq_sched_info; |
| 509 | |
| 510 | /* sys_sched_yield() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 511 | unsigned int yld_exp_empty; |
| 512 | unsigned int yld_act_empty; |
| 513 | unsigned int yld_both_empty; |
| 514 | unsigned int yld_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
| 516 | /* schedule() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 517 | unsigned int sched_switch; |
| 518 | unsigned int sched_count; |
| 519 | unsigned int sched_goidle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | /* try_to_wake_up() stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 522 | unsigned int ttwu_count; |
| 523 | unsigned int ttwu_local; |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 524 | |
| 525 | /* BKL stats */ |
Ken Chen | 480b943 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 526 | unsigned int bkl_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | #endif |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 528 | struct lock_class_key rq_lock_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | }; |
| 530 | |
Fenghua Yu | f34e3b6 | 2007-07-19 01:48:13 -0700 | [diff] [blame] | 531 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 533 | static inline void check_preempt_curr(struct rq *rq, struct task_struct *p) |
| 534 | { |
| 535 | rq->curr->sched_class->check_preempt_curr(rq, p); |
| 536 | } |
| 537 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 538 | static inline int cpu_of(struct rq *rq) |
| 539 | { |
| 540 | #ifdef CONFIG_SMP |
| 541 | return rq->cpu; |
| 542 | #else |
| 543 | return 0; |
| 544 | #endif |
| 545 | } |
| 546 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 547 | /* |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 548 | * Update the per-runqueue clock, as finegrained as the platform can give |
| 549 | * us, but without assuming monotonicity, etc.: |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 550 | */ |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 551 | static void __update_rq_clock(struct rq *rq) |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 552 | { |
| 553 | u64 prev_raw = rq->prev_clock_raw; |
| 554 | u64 now = sched_clock(); |
| 555 | s64 delta = now - prev_raw; |
| 556 | u64 clock = rq->clock; |
| 557 | |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 558 | #ifdef CONFIG_SCHED_DEBUG |
| 559 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 560 | #endif |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 561 | /* |
| 562 | * Protect against sched_clock() occasionally going backwards: |
| 563 | */ |
| 564 | if (unlikely(delta < 0)) { |
| 565 | clock++; |
| 566 | rq->clock_warps++; |
| 567 | } else { |
| 568 | /* |
| 569 | * Catch too large forward jumps too: |
| 570 | */ |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 571 | if (unlikely(clock + delta > rq->tick_timestamp + TICK_NSEC)) { |
| 572 | if (clock < rq->tick_timestamp + TICK_NSEC) |
| 573 | clock = rq->tick_timestamp + TICK_NSEC; |
| 574 | else |
| 575 | clock++; |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 576 | rq->clock_overflows++; |
| 577 | } else { |
| 578 | if (unlikely(delta > rq->clock_max_delta)) |
| 579 | rq->clock_max_delta = delta; |
| 580 | clock += delta; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | rq->prev_clock_raw = now; |
| 585 | rq->clock = clock; |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 586 | } |
| 587 | |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 588 | static void update_rq_clock(struct rq *rq) |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 589 | { |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 590 | if (likely(smp_processor_id() == cpu_of(rq))) |
| 591 | __update_rq_clock(rq); |
| 592 | } |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 593 | |
Ingo Molnar | 20d315d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 594 | /* |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 595 | * 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] | 596 | * See detach_destroy_domains: synchronize_sched for details. |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 597 | * |
| 598 | * The domain tree of any CPU may only be accessed from within |
| 599 | * preempt-disabled sections. |
| 600 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 601 | #define for_each_domain(cpu, __sd) \ |
| 602 | for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
| 604 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
| 605 | #define this_rq() (&__get_cpu_var(runqueues)) |
| 606 | #define task_rq(p) cpu_rq(task_cpu(p)) |
| 607 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) |
| 608 | |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 609 | unsigned long rt_needs_cpu(int cpu) |
| 610 | { |
| 611 | struct rq *rq = cpu_rq(cpu); |
| 612 | u64 delta; |
| 613 | |
| 614 | if (!rq->rt_throttled) |
| 615 | return 0; |
| 616 | |
| 617 | if (rq->clock > rq->rt_period_expire) |
| 618 | return 1; |
| 619 | |
| 620 | delta = rq->rt_period_expire - rq->clock; |
| 621 | do_div(delta, NSEC_PER_SEC / HZ); |
| 622 | |
| 623 | return (unsigned long)delta; |
| 624 | } |
| 625 | |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 626 | /* |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 627 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: |
| 628 | */ |
| 629 | #ifdef CONFIG_SCHED_DEBUG |
| 630 | # define const_debug __read_mostly |
| 631 | #else |
| 632 | # define const_debug static const |
| 633 | #endif |
| 634 | |
| 635 | /* |
| 636 | * Debugging: various feature bits |
| 637 | */ |
| 638 | enum { |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 639 | SCHED_FEAT_NEW_FAIR_SLEEPERS = 1, |
Ingo Molnar | 9612633 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 640 | SCHED_FEAT_WAKEUP_PREEMPT = 2, |
| 641 | SCHED_FEAT_START_DEBIT = 4, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 642 | SCHED_FEAT_TREE_AVG = 8, |
| 643 | SCHED_FEAT_APPROX_AVG = 16, |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 644 | SCHED_FEAT_HRTICK = 32, |
| 645 | SCHED_FEAT_DOUBLE_TICK = 64, |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 646 | }; |
| 647 | |
| 648 | const_debug unsigned int sysctl_sched_features = |
Ingo Molnar | 8401f77 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 649 | SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 | |
Ingo Molnar | 9612633 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 650 | SCHED_FEAT_WAKEUP_PREEMPT * 1 | |
Ingo Molnar | 8401f77 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 651 | SCHED_FEAT_START_DEBIT * 1 | |
| 652 | SCHED_FEAT_TREE_AVG * 0 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 653 | SCHED_FEAT_APPROX_AVG * 0 | |
| 654 | SCHED_FEAT_HRTICK * 1 | |
| 655 | SCHED_FEAT_DOUBLE_TICK * 0; |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 656 | |
| 657 | #define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x) |
| 658 | |
| 659 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 660 | * Number of tasks to iterate in a single balance run. |
| 661 | * Limited because this is done with IRQs disabled. |
| 662 | */ |
| 663 | const_debug unsigned int sysctl_sched_nr_migrate = 32; |
| 664 | |
| 665 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 666 | * period over which we measure -rt task cpu usage in us. |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 667 | * default: 1s |
| 668 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 669 | unsigned int sysctl_sched_rt_period = 1000000; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 670 | |
| 671 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 672 | * part of the period that we allow rt tasks to run in us. |
| 673 | * default: 0.95s |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 674 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 675 | int sysctl_sched_rt_runtime = 950000; |
| 676 | |
| 677 | /* |
| 678 | * single value that denotes runtime == period, ie unlimited time. |
| 679 | */ |
| 680 | #define RUNTIME_INF ((u64)~0ULL) |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 681 | |
| 682 | /* |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 683 | * For kernel-internal use: high-speed (but slightly incorrect) per-cpu |
| 684 | * clock constructed from sched_clock(): |
| 685 | */ |
| 686 | unsigned long long cpu_clock(int cpu) |
| 687 | { |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 688 | unsigned long long now; |
| 689 | unsigned long flags; |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 690 | struct rq *rq; |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 691 | |
Ingo Molnar | 2cd4d0e | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 692 | local_irq_save(flags); |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 693 | rq = cpu_rq(cpu); |
Ingo Molnar | 8ced5f6 | 2007-12-07 19:02:47 +0100 | [diff] [blame] | 694 | /* |
| 695 | * Only call sched_clock() if the scheduler has already been |
| 696 | * initialized (some code might call cpu_clock() very early): |
| 697 | */ |
| 698 | if (rq->idle) |
| 699 | update_rq_clock(rq); |
Ingo Molnar | b04a0f4 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 700 | now = rq->clock; |
Ingo Molnar | 2cd4d0e | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 701 | local_irq_restore(flags); |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 702 | |
| 703 | return now; |
| 704 | } |
Paul E. McKenney | a58f6f2 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 705 | EXPORT_SYMBOL_GPL(cpu_clock); |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | #ifndef prepare_arch_switch |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 708 | # define prepare_arch_switch(next) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | #endif |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 710 | #ifndef finish_arch_switch |
| 711 | # define finish_arch_switch(prev) do { } while (0) |
| 712 | #endif |
| 713 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 714 | static inline int task_current(struct rq *rq, struct task_struct *p) |
| 715 | { |
| 716 | return rq->curr == p; |
| 717 | } |
| 718 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 719 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 720 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 721 | { |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 722 | return task_current(rq, p); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 725 | 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] | 726 | { |
| 727 | } |
| 728 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 729 | 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] | 730 | { |
Ingo Molnar | da04c03 | 2005-09-13 11:17:59 +0200 | [diff] [blame] | 731 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 732 | /* this is a valid case when another task releases the spinlock */ |
| 733 | rq->lock.owner = current; |
| 734 | #endif |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 735 | /* |
| 736 | * If we are tracking spinlock dependencies then we have to |
| 737 | * fix up the runqueue lock - which gets 'carried over' from |
| 738 | * prev into current: |
| 739 | */ |
| 740 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); |
| 741 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 742 | spin_unlock_irq(&rq->lock); |
| 743 | } |
| 744 | |
| 745 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 746 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 747 | { |
| 748 | #ifdef CONFIG_SMP |
| 749 | return p->oncpu; |
| 750 | #else |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 751 | return task_current(rq, p); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 752 | #endif |
| 753 | } |
| 754 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 755 | 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] | 756 | { |
| 757 | #ifdef CONFIG_SMP |
| 758 | /* |
| 759 | * We can optimise this out completely for !SMP, because the |
| 760 | * SMP rebalancing from interrupt is the only thing that cares |
| 761 | * here. |
| 762 | */ |
| 763 | next->oncpu = 1; |
| 764 | #endif |
| 765 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 766 | spin_unlock_irq(&rq->lock); |
| 767 | #else |
| 768 | spin_unlock(&rq->lock); |
| 769 | #endif |
| 770 | } |
| 771 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 772 | 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] | 773 | { |
| 774 | #ifdef CONFIG_SMP |
| 775 | /* |
| 776 | * After ->oncpu is cleared, the task can be moved to a different CPU. |
| 777 | * We must ensure this doesn't happen until the switch is completely |
| 778 | * finished. |
| 779 | */ |
| 780 | smp_wmb(); |
| 781 | prev->oncpu = 0; |
| 782 | #endif |
| 783 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 784 | local_irq_enable(); |
| 785 | #endif |
| 786 | } |
| 787 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
| 789 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 790 | * __task_rq_lock - lock the runqueue a given task resides on. |
| 791 | * Must be called interrupts disabled. |
| 792 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 793 | static inline struct rq *__task_rq_lock(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 794 | __acquires(rq->lock) |
| 795 | { |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 796 | for (;;) { |
| 797 | struct rq *rq = task_rq(p); |
| 798 | spin_lock(&rq->lock); |
| 799 | if (likely(rq == task_rq(p))) |
| 800 | return rq; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 801 | spin_unlock(&rq->lock); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 802 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | * 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] | 807 | * interrupts. Note the ordering: we can safely lookup the task_rq without |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | * explicitly disabling preemption. |
| 809 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 810 | 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] | 811 | __acquires(rq->lock) |
| 812 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 813 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 815 | for (;;) { |
| 816 | local_irq_save(*flags); |
| 817 | rq = task_rq(p); |
| 818 | spin_lock(&rq->lock); |
| 819 | if (likely(rq == task_rq(p))) |
| 820 | return rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | spin_unlock_irqrestore(&rq->lock, *flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 825 | static void __task_rq_unlock(struct rq *rq) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 826 | __releases(rq->lock) |
| 827 | { |
| 828 | spin_unlock(&rq->lock); |
| 829 | } |
| 830 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 831 | static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | __releases(rq->lock) |
| 833 | { |
| 834 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 835 | } |
| 836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | /* |
Robert P. J. Day | cc2a73b | 2006-12-10 02:20:00 -0800 | [diff] [blame] | 838 | * this_rq_lock - lock this runqueue and disable interrupts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 840 | static struct rq *this_rq_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | __acquires(rq->lock) |
| 842 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 843 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
| 845 | local_irq_disable(); |
| 846 | rq = this_rq(); |
| 847 | spin_lock(&rq->lock); |
| 848 | |
| 849 | return rq; |
| 850 | } |
| 851 | |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 852 | /* |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 853 | * We are going deep-idle (irqs are disabled): |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 854 | */ |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 855 | void sched_clock_idle_sleep_event(void) |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 856 | { |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 857 | struct rq *rq = cpu_rq(smp_processor_id()); |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 858 | |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 859 | spin_lock(&rq->lock); |
| 860 | __update_rq_clock(rq); |
| 861 | spin_unlock(&rq->lock); |
| 862 | rq->clock_deep_idle_events++; |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 863 | } |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 864 | EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event); |
| 865 | |
| 866 | /* |
| 867 | * We just idled delta nanoseconds (called with irqs disabled): |
| 868 | */ |
| 869 | void sched_clock_idle_wakeup_event(u64 delta_ns) |
| 870 | { |
| 871 | struct rq *rq = cpu_rq(smp_processor_id()); |
| 872 | u64 now = sched_clock(); |
| 873 | |
| 874 | rq->idle_clock += delta_ns; |
| 875 | /* |
| 876 | * Override the previous timestamp and ignore all |
| 877 | * sched_clock() deltas that occured while we idled, |
| 878 | * and use the PM-provided delta_ns to advance the |
| 879 | * rq clock: |
| 880 | */ |
| 881 | spin_lock(&rq->lock); |
| 882 | rq->prev_clock_raw = now; |
| 883 | rq->clock += delta_ns; |
| 884 | spin_unlock(&rq->lock); |
Guillaume Chazarain | 782daee | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 885 | touch_softlockup_watchdog(); |
Ingo Molnar | 2aa44d0 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 886 | } |
| 887 | EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event); |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 888 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 889 | static void __resched_task(struct task_struct *p, int tif_bit); |
| 890 | |
| 891 | static inline void resched_task(struct task_struct *p) |
| 892 | { |
| 893 | __resched_task(p, TIF_NEED_RESCHED); |
| 894 | } |
| 895 | |
| 896 | #ifdef CONFIG_SCHED_HRTICK |
| 897 | /* |
| 898 | * Use HR-timers to deliver accurate preemption points. |
| 899 | * |
| 900 | * Its all a bit involved since we cannot program an hrt while holding the |
| 901 | * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a |
| 902 | * reschedule event. |
| 903 | * |
| 904 | * When we get rescheduled we reprogram the hrtick_timer outside of the |
| 905 | * rq->lock. |
| 906 | */ |
| 907 | static inline void resched_hrt(struct task_struct *p) |
| 908 | { |
| 909 | __resched_task(p, TIF_HRTICK_RESCHED); |
| 910 | } |
| 911 | |
| 912 | static inline void resched_rq(struct rq *rq) |
| 913 | { |
| 914 | unsigned long flags; |
| 915 | |
| 916 | spin_lock_irqsave(&rq->lock, flags); |
| 917 | resched_task(rq->curr); |
| 918 | spin_unlock_irqrestore(&rq->lock, flags); |
| 919 | } |
| 920 | |
| 921 | enum { |
| 922 | HRTICK_SET, /* re-programm hrtick_timer */ |
| 923 | HRTICK_RESET, /* not a new slice */ |
| 924 | }; |
| 925 | |
| 926 | /* |
| 927 | * Use hrtick when: |
| 928 | * - enabled by features |
| 929 | * - hrtimer is actually high res |
| 930 | */ |
| 931 | static inline int hrtick_enabled(struct rq *rq) |
| 932 | { |
| 933 | if (!sched_feat(HRTICK)) |
| 934 | return 0; |
| 935 | return hrtimer_is_hres_active(&rq->hrtick_timer); |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | * Called to set the hrtick timer state. |
| 940 | * |
| 941 | * called with rq->lock held and irqs disabled |
| 942 | */ |
| 943 | static void hrtick_start(struct rq *rq, u64 delay, int reset) |
| 944 | { |
| 945 | assert_spin_locked(&rq->lock); |
| 946 | |
| 947 | /* |
| 948 | * preempt at: now + delay |
| 949 | */ |
| 950 | rq->hrtick_expire = |
| 951 | ktime_add_ns(rq->hrtick_timer.base->get_time(), delay); |
| 952 | /* |
| 953 | * indicate we need to program the timer |
| 954 | */ |
| 955 | __set_bit(HRTICK_SET, &rq->hrtick_flags); |
| 956 | if (reset) |
| 957 | __set_bit(HRTICK_RESET, &rq->hrtick_flags); |
| 958 | |
| 959 | /* |
| 960 | * New slices are called from the schedule path and don't need a |
| 961 | * forced reschedule. |
| 962 | */ |
| 963 | if (reset) |
| 964 | resched_hrt(rq->curr); |
| 965 | } |
| 966 | |
| 967 | static void hrtick_clear(struct rq *rq) |
| 968 | { |
| 969 | if (hrtimer_active(&rq->hrtick_timer)) |
| 970 | hrtimer_cancel(&rq->hrtick_timer); |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * Update the timer from the possible pending state. |
| 975 | */ |
| 976 | static void hrtick_set(struct rq *rq) |
| 977 | { |
| 978 | ktime_t time; |
| 979 | int set, reset; |
| 980 | unsigned long flags; |
| 981 | |
| 982 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 983 | |
| 984 | spin_lock_irqsave(&rq->lock, flags); |
| 985 | set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags); |
| 986 | reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags); |
| 987 | time = rq->hrtick_expire; |
| 988 | clear_thread_flag(TIF_HRTICK_RESCHED); |
| 989 | spin_unlock_irqrestore(&rq->lock, flags); |
| 990 | |
| 991 | if (set) { |
| 992 | hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS); |
| 993 | if (reset && !hrtimer_active(&rq->hrtick_timer)) |
| 994 | resched_rq(rq); |
| 995 | } else |
| 996 | hrtick_clear(rq); |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * High-resolution timer tick. |
| 1001 | * Runs from hardirq context with interrupts disabled. |
| 1002 | */ |
| 1003 | static enum hrtimer_restart hrtick(struct hrtimer *timer) |
| 1004 | { |
| 1005 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); |
| 1006 | |
| 1007 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 1008 | |
| 1009 | spin_lock(&rq->lock); |
| 1010 | __update_rq_clock(rq); |
| 1011 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
| 1012 | spin_unlock(&rq->lock); |
| 1013 | |
| 1014 | return HRTIMER_NORESTART; |
| 1015 | } |
| 1016 | |
| 1017 | static inline void init_rq_hrtick(struct rq *rq) |
| 1018 | { |
| 1019 | rq->hrtick_flags = 0; |
| 1020 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1021 | rq->hrtick_timer.function = hrtick; |
| 1022 | rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; |
| 1023 | } |
| 1024 | |
| 1025 | void hrtick_resched(void) |
| 1026 | { |
| 1027 | struct rq *rq; |
| 1028 | unsigned long flags; |
| 1029 | |
| 1030 | if (!test_thread_flag(TIF_HRTICK_RESCHED)) |
| 1031 | return; |
| 1032 | |
| 1033 | local_irq_save(flags); |
| 1034 | rq = cpu_rq(smp_processor_id()); |
| 1035 | hrtick_set(rq); |
| 1036 | local_irq_restore(flags); |
| 1037 | } |
| 1038 | #else |
| 1039 | static inline void hrtick_clear(struct rq *rq) |
| 1040 | { |
| 1041 | } |
| 1042 | |
| 1043 | static inline void hrtick_set(struct rq *rq) |
| 1044 | { |
| 1045 | } |
| 1046 | |
| 1047 | static inline void init_rq_hrtick(struct rq *rq) |
| 1048 | { |
| 1049 | } |
| 1050 | |
| 1051 | void hrtick_resched(void) |
| 1052 | { |
| 1053 | } |
| 1054 | #endif |
| 1055 | |
Ingo Molnar | 1b9f19c | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1056 | /* |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1057 | * resched_task - mark a task 'to be rescheduled now'. |
| 1058 | * |
| 1059 | * On UP this means the setting of the need_resched flag, on SMP it |
| 1060 | * might also involve a cross-CPU call to trigger the scheduler on |
| 1061 | * the target CPU. |
| 1062 | */ |
| 1063 | #ifdef CONFIG_SMP |
| 1064 | |
| 1065 | #ifndef tsk_is_polling |
| 1066 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) |
| 1067 | #endif |
| 1068 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1069 | static void __resched_task(struct task_struct *p, int tif_bit) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1070 | { |
| 1071 | int cpu; |
| 1072 | |
| 1073 | assert_spin_locked(&task_rq(p)->lock); |
| 1074 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1075 | if (unlikely(test_tsk_thread_flag(p, tif_bit))) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1076 | return; |
| 1077 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1078 | set_tsk_thread_flag(p, tif_bit); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1079 | |
| 1080 | cpu = task_cpu(p); |
| 1081 | if (cpu == smp_processor_id()) |
| 1082 | return; |
| 1083 | |
| 1084 | /* NEED_RESCHED must be visible before we test polling */ |
| 1085 | smp_mb(); |
| 1086 | if (!tsk_is_polling(p)) |
| 1087 | smp_send_reschedule(cpu); |
| 1088 | } |
| 1089 | |
| 1090 | static void resched_cpu(int cpu) |
| 1091 | { |
| 1092 | struct rq *rq = cpu_rq(cpu); |
| 1093 | unsigned long flags; |
| 1094 | |
| 1095 | if (!spin_trylock_irqsave(&rq->lock, flags)) |
| 1096 | return; |
| 1097 | resched_task(cpu_curr(cpu)); |
| 1098 | spin_unlock_irqrestore(&rq->lock, flags); |
| 1099 | } |
| 1100 | #else |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1101 | static void __resched_task(struct task_struct *p, int tif_bit) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1102 | { |
| 1103 | assert_spin_locked(&task_rq(p)->lock); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1104 | set_tsk_thread_flag(p, tif_bit); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1105 | } |
| 1106 | #endif |
| 1107 | |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1108 | #if BITS_PER_LONG == 32 |
| 1109 | # define WMULT_CONST (~0UL) |
| 1110 | #else |
| 1111 | # define WMULT_CONST (1UL << 32) |
| 1112 | #endif |
| 1113 | |
| 1114 | #define WMULT_SHIFT 32 |
| 1115 | |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1116 | /* |
| 1117 | * Shift right and round: |
| 1118 | */ |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1119 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1120 | |
Ingo Molnar | cb1c4fc | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1121 | static unsigned long |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1122 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, |
| 1123 | struct load_weight *lw) |
| 1124 | { |
| 1125 | u64 tmp; |
| 1126 | |
| 1127 | if (unlikely(!lw->inv_weight)) |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1128 | lw->inv_weight = (WMULT_CONST - lw->weight/2) / lw->weight + 1; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1129 | |
| 1130 | tmp = (u64)delta_exec * weight; |
| 1131 | /* |
| 1132 | * Check whether we'd overflow the 64-bit multiplication: |
| 1133 | */ |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1134 | if (unlikely(tmp > WMULT_CONST)) |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1135 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, |
Ingo Molnar | 194081e | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1136 | WMULT_SHIFT/2); |
| 1137 | else |
Ingo Molnar | cf2ab46 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1138 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1139 | |
Ingo Molnar | ecf691d | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1140 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | static inline unsigned long |
| 1144 | calc_delta_fair(unsigned long delta_exec, struct load_weight *lw) |
| 1145 | { |
| 1146 | return calc_delta_mine(delta_exec, NICE_0_LOAD, lw); |
| 1147 | } |
| 1148 | |
Ingo Molnar | 1091985 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1149 | 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] | 1150 | { |
| 1151 | lw->weight += inc; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
Ingo Molnar | 1091985 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1154 | 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] | 1155 | { |
| 1156 | lw->weight -= dec; |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1160 | * To aid in avoiding the subversion of "niceness" due to uneven distribution |
| 1161 | * of tasks with abnormal "nice" values across CPUs the contribution that |
| 1162 | * 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] | 1163 | * 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] | 1164 | * scaled version of the new time slice allocation that they receive on time |
| 1165 | * slice expiry etc. |
| 1166 | */ |
| 1167 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1168 | #define WEIGHT_IDLEPRIO 2 |
| 1169 | #define WMULT_IDLEPRIO (1 << 31) |
| 1170 | |
| 1171 | /* |
| 1172 | * Nice levels are multiplicative, with a gentle 10% change for every |
| 1173 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to |
| 1174 | * nice 1, it will get ~10% less CPU time than another CPU-bound task |
| 1175 | * that remained on nice 0. |
| 1176 | * |
| 1177 | * The "10% effect" is relative and cumulative: from _any_ nice level, |
| 1178 | * 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] | 1179 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. |
| 1180 | * If a task goes up by ~10% and another task goes down by ~10% then |
| 1181 | * the relative distance between them is ~25%.) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1182 | */ |
| 1183 | static const int prio_to_weight[40] = { |
Ingo Molnar | 254753d | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1184 | /* -20 */ 88761, 71755, 56483, 46273, 36291, |
| 1185 | /* -15 */ 29154, 23254, 18705, 14949, 11916, |
| 1186 | /* -10 */ 9548, 7620, 6100, 4904, 3906, |
| 1187 | /* -5 */ 3121, 2501, 1991, 1586, 1277, |
| 1188 | /* 0 */ 1024, 820, 655, 526, 423, |
| 1189 | /* 5 */ 335, 272, 215, 172, 137, |
| 1190 | /* 10 */ 110, 87, 70, 56, 45, |
| 1191 | /* 15 */ 36, 29, 23, 18, 15, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1192 | }; |
| 1193 | |
Ingo Molnar | 5714d2d | 2007-07-16 09:46:31 +0200 | [diff] [blame] | 1194 | /* |
| 1195 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. |
| 1196 | * |
| 1197 | * In cases where the weight does not change often, we can use the |
| 1198 | * precalculated inverse to speed up arithmetics by turning divisions |
| 1199 | * into multiplications: |
| 1200 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1201 | static const u32 prio_to_wmult[40] = { |
Ingo Molnar | 254753d | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1202 | /* -20 */ 48388, 59856, 76040, 92818, 118348, |
| 1203 | /* -15 */ 147320, 184698, 229616, 287308, 360437, |
| 1204 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, |
| 1205 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, |
| 1206 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, |
| 1207 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, |
| 1208 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, |
| 1209 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1210 | }; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1211 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1212 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup); |
| 1213 | |
| 1214 | /* |
| 1215 | * runqueue iterator, to support SMP load-balancing between different |
| 1216 | * scheduling classes, without having to expose their internal data |
| 1217 | * structures to the load-balancing proper: |
| 1218 | */ |
| 1219 | struct rq_iterator { |
| 1220 | void *arg; |
| 1221 | struct task_struct *(*start)(void *); |
| 1222 | struct task_struct *(*next)(void *); |
| 1223 | }; |
| 1224 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1225 | #ifdef CONFIG_SMP |
| 1226 | static unsigned long |
| 1227 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1228 | unsigned long max_load_move, struct sched_domain *sd, |
| 1229 | enum cpu_idle_type idle, int *all_pinned, |
| 1230 | int *this_best_prio, struct rq_iterator *iterator); |
| 1231 | |
| 1232 | static int |
| 1233 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1234 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 1235 | struct rq_iterator *iterator); |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1236 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1237 | |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 1238 | #ifdef CONFIG_CGROUP_CPUACCT |
| 1239 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime); |
| 1240 | #else |
| 1241 | static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} |
| 1242 | #endif |
| 1243 | |
Srivatsa Vaddagiri | 58e2d4c | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 1244 | static inline void inc_cpu_load(struct rq *rq, unsigned long load) |
| 1245 | { |
| 1246 | update_load_add(&rq->load, load); |
| 1247 | } |
| 1248 | |
| 1249 | static inline void dec_cpu_load(struct rq *rq, unsigned long load) |
| 1250 | { |
| 1251 | update_load_sub(&rq->load, load); |
| 1252 | } |
| 1253 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1254 | #ifdef CONFIG_SMP |
| 1255 | static unsigned long source_load(int cpu, int type); |
| 1256 | static unsigned long target_load(int cpu, int type); |
| 1257 | static unsigned long cpu_avg_load_per_task(int cpu); |
| 1258 | static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd); |
| 1259 | #endif /* CONFIG_SMP */ |
| 1260 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1261 | #include "sched_stats.h" |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1262 | #include "sched_idletask.c" |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1263 | #include "sched_fair.c" |
| 1264 | #include "sched_rt.c" |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1265 | #ifdef CONFIG_SCHED_DEBUG |
| 1266 | # include "sched_debug.c" |
| 1267 | #endif |
| 1268 | |
| 1269 | #define sched_class_highest (&rt_sched_class) |
| 1270 | |
Gerald Stralko | 5aff053 | 2008-01-31 22:45:23 +0100 | [diff] [blame] | 1271 | static void inc_nr_running(struct rq *rq) |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1272 | { |
| 1273 | rq->nr_running++; |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Gerald Stralko | 5aff053 | 2008-01-31 22:45:23 +0100 | [diff] [blame] | 1276 | static void dec_nr_running(struct rq *rq) |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1277 | { |
| 1278 | rq->nr_running--; |
Ingo Molnar | 9c21724 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1279 | } |
| 1280 | |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1281 | static void set_load_weight(struct task_struct *p) |
| 1282 | { |
| 1283 | if (task_has_rt_policy(p)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1284 | p->se.load.weight = prio_to_weight[0] * 2; |
| 1285 | p->se.load.inv_weight = prio_to_wmult[0] >> 1; |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * SCHED_IDLE tasks get minimal weight: |
| 1291 | */ |
| 1292 | if (p->policy == SCHED_IDLE) { |
| 1293 | p->se.load.weight = WEIGHT_IDLEPRIO; |
| 1294 | p->se.load.inv_weight = WMULT_IDLEPRIO; |
| 1295 | return; |
| 1296 | } |
| 1297 | |
| 1298 | p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO]; |
| 1299 | 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] | 1300 | } |
| 1301 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1302 | 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] | 1303 | { |
| 1304 | sched_info_queued(p); |
Ingo Molnar | fd390f6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1305 | p->sched_class->enqueue_task(rq, p, wakeup); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1306 | p->se.on_rq = 1; |
| 1307 | } |
| 1308 | |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1309 | 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] | 1310 | { |
Ingo Molnar | f02231e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1311 | p->sched_class->dequeue_task(rq, p, sleep); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1312 | p->se.on_rq = 0; |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1316 | * __normal_prio - return the priority that is based on the static prio |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1317 | */ |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1318 | static inline int __normal_prio(struct task_struct *p) |
| 1319 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1320 | return p->static_prio; |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1324 | * Calculate the expected normal priority: i.e. priority |
| 1325 | * without taking RT-inheritance into account. Might be |
| 1326 | * boosted by interactivity modifiers. Changes upon fork, |
| 1327 | * setprio syscalls, and whenever the interactivity |
| 1328 | * estimator recalculates. |
| 1329 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1330 | static inline int normal_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1331 | { |
| 1332 | int prio; |
| 1333 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1334 | if (task_has_rt_policy(p)) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1335 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
| 1336 | else |
| 1337 | prio = __normal_prio(p); |
| 1338 | return prio; |
| 1339 | } |
| 1340 | |
| 1341 | /* |
| 1342 | * Calculate the current priority, i.e. the priority |
| 1343 | * taken into account by the scheduler. This value might |
| 1344 | * be boosted by RT tasks, or might be boosted by |
| 1345 | * interactivity modifiers. Will be RT if the task got |
| 1346 | * RT-boosted. If not then it returns p->normal_prio. |
| 1347 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1348 | static int effective_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1349 | { |
| 1350 | p->normal_prio = normal_prio(p); |
| 1351 | /* |
| 1352 | * If we are RT tasks or we were boosted to RT priority, |
| 1353 | * keep the priority unchanged. Otherwise, update priority |
| 1354 | * to the normal priority: |
| 1355 | */ |
| 1356 | if (!rt_prio(p->prio)) |
| 1357 | return p->normal_prio; |
| 1358 | return p->prio; |
| 1359 | } |
| 1360 | |
| 1361 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1362 | * activate_task - move a task to the runqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1364 | 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] | 1365 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 1366 | if (task_contributes_to_load(p)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1367 | rq->nr_uninterruptible--; |
| 1368 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1369 | enqueue_task(rq, p, wakeup); |
Gerald Stralko | 5aff053 | 2008-01-31 22:45:23 +0100 | [diff] [blame] | 1370 | inc_nr_running(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | * deactivate_task - remove a task from the runqueue. |
| 1375 | */ |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1376 | 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] | 1377 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 1378 | if (task_contributes_to_load(p)) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1379 | rq->nr_uninterruptible++; |
| 1380 | |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1381 | dequeue_task(rq, p, sleep); |
Gerald Stralko | 5aff053 | 2008-01-31 22:45:23 +0100 | [diff] [blame] | 1382 | dec_nr_running(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | /** |
| 1386 | * task_curr - is this task currently executing on a CPU? |
| 1387 | * @p: the task in question. |
| 1388 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1389 | inline int task_curr(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | { |
| 1391 | return cpu_curr(task_cpu(p)) == p; |
| 1392 | } |
| 1393 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1394 | /* Used instead of source_load when we know the type == 0 */ |
| 1395 | unsigned long weighted_cpuload(const int cpu) |
| 1396 | { |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1397 | return cpu_rq(cpu)->load.weight; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) |
| 1401 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1402 | set_task_rq(p, cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1403 | #ifdef CONFIG_SMP |
Dmitry Adamushko | ce96b5a | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 1404 | /* |
| 1405 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be |
| 1406 | * successfuly executed on another CPU. We must ensure that updates of |
| 1407 | * per-task data have been completed by this moment. |
| 1408 | */ |
| 1409 | smp_wmb(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1410 | task_thread_info(p)->cpu = cpu; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1411 | #endif |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1412 | } |
| 1413 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1414 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
| 1415 | const struct sched_class *prev_class, |
| 1416 | int oldprio, int running) |
| 1417 | { |
| 1418 | if (prev_class != p->sched_class) { |
| 1419 | if (prev_class->switched_from) |
| 1420 | prev_class->switched_from(rq, p, running); |
| 1421 | p->sched_class->switched_to(rq, p, running); |
| 1422 | } else |
| 1423 | p->sched_class->prio_changed(rq, p, oldprio, running); |
| 1424 | } |
| 1425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | #ifdef CONFIG_SMP |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1427 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1428 | /* |
| 1429 | * Is this task likely cache-hot: |
| 1430 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1431 | static int |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1432 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) |
| 1433 | { |
| 1434 | s64 delta; |
| 1435 | |
| 1436 | if (p->sched_class != &fair_sched_class) |
| 1437 | return 0; |
| 1438 | |
Ingo Molnar | 6bc1665 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1439 | if (sysctl_sched_migration_cost == -1) |
| 1440 | return 1; |
| 1441 | if (sysctl_sched_migration_cost == 0) |
| 1442 | return 0; |
| 1443 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1444 | delta = now - p->se.exec_start; |
| 1445 | |
| 1446 | return delta < (s64)sysctl_sched_migration_cost; |
| 1447 | } |
| 1448 | |
| 1449 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1450 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1451 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1452 | int old_cpu = task_cpu(p); |
| 1453 | 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] | 1454 | struct cfs_rq *old_cfsrq = task_cfs_rq(p), |
| 1455 | *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu); |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1456 | u64 clock_offset; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1457 | |
| 1458 | clock_offset = old_rq->clock - new_rq->clock; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1459 | |
| 1460 | #ifdef CONFIG_SCHEDSTATS |
| 1461 | if (p->se.wait_start) |
| 1462 | p->se.wait_start -= clock_offset; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1463 | if (p->se.sleep_start) |
| 1464 | p->se.sleep_start -= clock_offset; |
| 1465 | if (p->se.block_start) |
| 1466 | p->se.block_start -= clock_offset; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1467 | if (old_cpu != new_cpu) { |
| 1468 | schedstat_inc(p, se.nr_migrations); |
| 1469 | if (task_hot(p, old_rq->clock, NULL)) |
| 1470 | schedstat_inc(p, se.nr_forced2_migrations); |
| 1471 | } |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1472 | #endif |
Srivatsa Vaddagiri | 2830cf8 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1473 | p->se.vruntime -= old_cfsrq->min_vruntime - |
| 1474 | new_cfsrq->min_vruntime; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1475 | |
| 1476 | __set_task_cpu(p, new_cpu); |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1477 | } |
| 1478 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1479 | struct migration_req { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1482 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | int dest_cpu; |
| 1484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | struct completion done; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1486 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
| 1488 | /* |
| 1489 | * The task's runqueue lock must be held. |
| 1490 | * Returns true if you have to wait for migration thread. |
| 1491 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1492 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1493 | 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] | 1494 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1495 | struct rq *rq = task_rq(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
| 1497 | /* |
| 1498 | * If the task is not on a runqueue (and not running), then |
| 1499 | * it is sufficient to simply update the task's cpu field. |
| 1500 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1501 | if (!p->se.on_rq && !task_running(rq, p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | set_task_cpu(p, dest_cpu); |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | init_completion(&req->done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | req->task = p; |
| 1508 | req->dest_cpu = dest_cpu; |
| 1509 | list_add(&req->list, &rq->migration_queue); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 1510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | return 1; |
| 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * wait_task_inactive - wait for a thread to unschedule. |
| 1516 | * |
| 1517 | * The caller must ensure that the task *will* unschedule sometime soon, |
| 1518 | * else this function might spin for a *long* time. This function can't |
| 1519 | * be called with interrupts off, or it may introduce deadlock with |
| 1520 | * smp_call_function() if an IPI is sent by the same process we are |
| 1521 | * waiting to become inactive. |
| 1522 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1523 | void wait_task_inactive(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | { |
| 1525 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1526 | int running, on_rq; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1527 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1529 | for (;;) { |
| 1530 | /* |
| 1531 | * We do the initial early heuristics without holding |
| 1532 | * any task-queue locks at all. We'll only try to get |
| 1533 | * the runqueue lock when things look like they will |
| 1534 | * work out! |
| 1535 | */ |
| 1536 | rq = task_rq(p); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 1537 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1538 | /* |
| 1539 | * If the task is actively running on another CPU |
| 1540 | * still, just relax and busy-wait without holding |
| 1541 | * any locks. |
| 1542 | * |
| 1543 | * NOTE! Since we don't hold any locks, it's not |
| 1544 | * even sure that "rq" stays as the right runqueue! |
| 1545 | * But we don't care, since "task_running()" will |
| 1546 | * return false if the runqueue has changed and p |
| 1547 | * is actually now running somewhere else! |
| 1548 | */ |
| 1549 | while (task_running(rq, p)) |
| 1550 | cpu_relax(); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 1551 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1552 | /* |
| 1553 | * Ok, time to look more closely! We need the rq |
| 1554 | * lock now, to be *sure*. If we're wrong, we'll |
| 1555 | * just go back and repeat. |
| 1556 | */ |
| 1557 | rq = task_rq_lock(p, &flags); |
| 1558 | running = task_running(rq, p); |
| 1559 | on_rq = p->se.on_rq; |
| 1560 | task_rq_unlock(rq, &flags); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 1561 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1562 | /* |
| 1563 | * Was it really running after all now that we |
| 1564 | * checked with the proper locks actually held? |
| 1565 | * |
| 1566 | * Oops. Go back and try again.. |
| 1567 | */ |
| 1568 | if (unlikely(running)) { |
| 1569 | cpu_relax(); |
| 1570 | continue; |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * It's not enough that it's not actively running, |
| 1575 | * it must be off the runqueue _entirely_, and not |
| 1576 | * preempted! |
| 1577 | * |
| 1578 | * So if it wa still runnable (but just not actively |
| 1579 | * running right now), it's preempted, and we should |
| 1580 | * yield - it could be a while. |
| 1581 | */ |
| 1582 | if (unlikely(on_rq)) { |
| 1583 | schedule_timeout_uninterruptible(1); |
| 1584 | continue; |
| 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * Ahh, all good. It wasn't running, and it wasn't |
| 1589 | * runnable, which means that it will never become |
| 1590 | * running in the future either. We're all done! |
| 1591 | */ |
| 1592 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | /*** |
| 1597 | * kick_process - kick a running thread to enter/exit the kernel |
| 1598 | * @p: the to-be-kicked thread |
| 1599 | * |
| 1600 | * Cause a process which is running on another CPU to enter |
| 1601 | * kernel-mode, without any delay. (to get signals handled.) |
| 1602 | * |
| 1603 | * NOTE: this function doesnt have to take the runqueue lock, |
| 1604 | * because all it wants to ensure is that the remote task enters |
| 1605 | * the kernel. If the IPI races and the task has been migrated |
| 1606 | * to another CPU then no harm is done and the purpose has been |
| 1607 | * achieved as well. |
| 1608 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1609 | void kick_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | { |
| 1611 | int cpu; |
| 1612 | |
| 1613 | preempt_disable(); |
| 1614 | cpu = task_cpu(p); |
| 1615 | if ((cpu != smp_processor_id()) && task_curr(p)) |
| 1616 | smp_send_reschedule(cpu); |
| 1617 | preempt_enable(); |
| 1618 | } |
| 1619 | |
| 1620 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1621 | * Return a low guess at the load of a migration-source cpu weighted |
| 1622 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | * |
| 1624 | * We want to under-estimate the load of migration sources, to |
| 1625 | * balance conservatively. |
| 1626 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1627 | static unsigned long source_load(int cpu, int type) |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 1628 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1629 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1630 | unsigned long total = weighted_cpuload(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 1631 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1632 | if (type == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1633 | return total; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1634 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1635 | return min(rq->cpu_load[type-1], total); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1639 | * Return a high guess at the load of a migration-target cpu weighted |
| 1640 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1642 | static unsigned long target_load(int cpu, int type) |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 1643 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1644 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1645 | unsigned long total = weighted_cpuload(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 1646 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1647 | if (type == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1648 | return total; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1649 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1650 | return max(rq->cpu_load[type-1], total); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* |
| 1654 | * Return the average load per task on the cpu's run queue |
| 1655 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1656 | static unsigned long cpu_avg_load_per_task(int cpu) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1657 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1658 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1659 | unsigned long total = weighted_cpuload(cpu); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1660 | unsigned long n = rq->nr_running; |
| 1661 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1662 | return n ? total / n : SCHED_LOAD_SCALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1665 | /* |
| 1666 | * find_idlest_group finds and returns the least busy CPU group within the |
| 1667 | * domain. |
| 1668 | */ |
| 1669 | static struct sched_group * |
| 1670 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) |
| 1671 | { |
| 1672 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; |
| 1673 | unsigned long min_load = ULONG_MAX, this_load = 0; |
| 1674 | int load_idx = sd->forkexec_idx; |
| 1675 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
| 1676 | |
| 1677 | do { |
| 1678 | unsigned long load, avg_load; |
| 1679 | int local_group; |
| 1680 | int i; |
| 1681 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1682 | /* Skip over this group if it has no CPUs allowed */ |
| 1683 | if (!cpus_intersects(group->cpumask, p->cpus_allowed)) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1684 | continue; |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1685 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1686 | local_group = cpu_isset(this_cpu, group->cpumask); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1687 | |
| 1688 | /* Tally up the load of all CPUs in the group */ |
| 1689 | avg_load = 0; |
| 1690 | |
| 1691 | for_each_cpu_mask(i, group->cpumask) { |
| 1692 | /* Bias balancing toward cpus of our domain */ |
| 1693 | if (local_group) |
| 1694 | load = source_load(i, load_idx); |
| 1695 | else |
| 1696 | load = target_load(i, load_idx); |
| 1697 | |
| 1698 | avg_load += load; |
| 1699 | } |
| 1700 | |
| 1701 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 1702 | avg_load = sg_div_cpu_power(group, |
| 1703 | avg_load * SCHED_LOAD_SCALE); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1704 | |
| 1705 | if (local_group) { |
| 1706 | this_load = avg_load; |
| 1707 | this = group; |
| 1708 | } else if (avg_load < min_load) { |
| 1709 | min_load = avg_load; |
| 1710 | idlest = group; |
| 1711 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1712 | } while (group = group->next, group != sd->groups); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1713 | |
| 1714 | if (!idlest || 100*this_load < imbalance*min_load) |
| 1715 | return NULL; |
| 1716 | return idlest; |
| 1717 | } |
| 1718 | |
| 1719 | /* |
Satoru Takeuchi | 0feaece | 2006-10-03 01:14:10 -0700 | [diff] [blame] | 1720 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1721 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 1722 | static int |
| 1723 | 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] | 1724 | { |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1725 | cpumask_t tmp; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1726 | unsigned long load, min_load = ULONG_MAX; |
| 1727 | int idlest = -1; |
| 1728 | int i; |
| 1729 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1730 | /* Traverse only the allowed CPUs */ |
| 1731 | cpus_and(tmp, group->cpumask, p->cpus_allowed); |
| 1732 | |
| 1733 | for_each_cpu_mask(i, tmp) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1734 | load = weighted_cpuload(i); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1735 | |
| 1736 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 1737 | min_load = load; |
| 1738 | idlest = i; |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | return idlest; |
| 1743 | } |
| 1744 | |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1745 | /* |
| 1746 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 1747 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 1748 | * SD_BALANCE_EXEC. |
| 1749 | * |
| 1750 | * Balance, ie. select the least loaded group. |
| 1751 | * |
| 1752 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 1753 | * |
| 1754 | * preempt must be disabled. |
| 1755 | */ |
| 1756 | static int sched_balance_self(int cpu, int flag) |
| 1757 | { |
| 1758 | struct task_struct *t = current; |
| 1759 | struct sched_domain *tmp, *sd = NULL; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1760 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1761 | for_each_domain(cpu, tmp) { |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1762 | /* |
| 1763 | * If power savings logic is enabled for a domain, stop there. |
| 1764 | */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 1765 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) |
| 1766 | break; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1767 | if (tmp->flags & flag) |
| 1768 | sd = tmp; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1769 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1770 | |
| 1771 | while (sd) { |
| 1772 | cpumask_t span; |
| 1773 | struct sched_group *group; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1774 | int new_cpu, weight; |
| 1775 | |
| 1776 | if (!(sd->flags & flag)) { |
| 1777 | sd = sd->child; |
| 1778 | continue; |
| 1779 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1780 | |
| 1781 | span = sd->span; |
| 1782 | group = find_idlest_group(sd, t, cpu); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1783 | if (!group) { |
| 1784 | sd = sd->child; |
| 1785 | continue; |
| 1786 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1787 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1788 | new_cpu = find_idlest_cpu(group, t, cpu); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1789 | if (new_cpu == -1 || new_cpu == cpu) { |
| 1790 | /* Now try balancing at a lower domain level of cpu */ |
| 1791 | sd = sd->child; |
| 1792 | continue; |
| 1793 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1794 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1795 | /* Now try balancing at a lower domain level of new_cpu */ |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1796 | cpu = new_cpu; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1797 | sd = NULL; |
| 1798 | weight = cpus_weight(span); |
| 1799 | for_each_domain(cpu, tmp) { |
| 1800 | if (weight <= cpus_weight(tmp->span)) |
| 1801 | break; |
| 1802 | if (tmp->flags & flag) |
| 1803 | sd = tmp; |
| 1804 | } |
| 1805 | /* while loop will break here if sd == NULL */ |
| 1806 | } |
| 1807 | |
| 1808 | return cpu; |
| 1809 | } |
| 1810 | |
| 1811 | #endif /* CONFIG_SMP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | /*** |
| 1814 | * try_to_wake_up - wake up a thread |
| 1815 | * @p: the to-be-woken-up thread |
| 1816 | * @state: the mask of task states that can be woken |
| 1817 | * @sync: do a synchronous wakeup? |
| 1818 | * |
| 1819 | * Put it on the run-queue if it's not already there. The "current" |
| 1820 | * thread is always on the run-queue (except when the actual |
| 1821 | * re-schedule is in progress), and as such you're allowed to do |
| 1822 | * the simpler "current->state = TASK_RUNNING" to mark yourself |
| 1823 | * runnable without the overhead of this. |
| 1824 | * |
| 1825 | * returns failure only if the task is already active. |
| 1826 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1827 | 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] | 1828 | { |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1829 | int cpu, orig_cpu, this_cpu, success = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | unsigned long flags; |
| 1831 | long old_state; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1832 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | |
| 1834 | rq = task_rq_lock(p, &flags); |
| 1835 | old_state = p->state; |
| 1836 | if (!(old_state & state)) |
| 1837 | goto out; |
| 1838 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1839 | if (p->se.on_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | goto out_running; |
| 1841 | |
| 1842 | cpu = task_cpu(p); |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1843 | orig_cpu = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | this_cpu = smp_processor_id(); |
| 1845 | |
| 1846 | #ifdef CONFIG_SMP |
| 1847 | if (unlikely(task_running(rq, p))) |
| 1848 | goto out_activate; |
| 1849 | |
Dmitry Adamushko | 5d2f5a6 | 2008-01-25 21:08:21 +0100 | [diff] [blame] | 1850 | cpu = p->sched_class->select_task_rq(p, sync); |
| 1851 | if (cpu != orig_cpu) { |
| 1852 | set_task_cpu(p, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | task_rq_unlock(rq, &flags); |
| 1854 | /* might preempt at this point */ |
| 1855 | rq = task_rq_lock(p, &flags); |
| 1856 | old_state = p->state; |
| 1857 | if (!(old_state & state)) |
| 1858 | goto out; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1859 | if (p->se.on_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | goto out_running; |
| 1861 | |
| 1862 | this_cpu = smp_processor_id(); |
| 1863 | cpu = task_cpu(p); |
| 1864 | } |
| 1865 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1866 | #ifdef CONFIG_SCHEDSTATS |
| 1867 | schedstat_inc(rq, ttwu_count); |
| 1868 | if (cpu == this_cpu) |
| 1869 | schedstat_inc(rq, ttwu_local); |
| 1870 | else { |
| 1871 | struct sched_domain *sd; |
| 1872 | for_each_domain(this_cpu, sd) { |
| 1873 | if (cpu_isset(cpu, sd->span)) { |
| 1874 | schedstat_inc(sd, ttwu_wake_remote); |
| 1875 | break; |
| 1876 | } |
| 1877 | } |
| 1878 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1879 | #endif |
| 1880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | out_activate: |
| 1882 | #endif /* CONFIG_SMP */ |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1883 | schedstat_inc(p, se.nr_wakeups); |
| 1884 | if (sync) |
| 1885 | schedstat_inc(p, se.nr_wakeups_sync); |
| 1886 | if (orig_cpu != cpu) |
| 1887 | schedstat_inc(p, se.nr_wakeups_migrate); |
| 1888 | if (cpu == this_cpu) |
| 1889 | schedstat_inc(p, se.nr_wakeups_local); |
| 1890 | else |
| 1891 | schedstat_inc(p, se.nr_wakeups_remote); |
Ingo Molnar | 2daa357 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1892 | update_rq_clock(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1893 | activate_task(rq, p, 1); |
Ingo Molnar | 9c63d9c | 2007-10-15 17:00:20 +0200 | [diff] [blame] | 1894 | check_preempt_curr(rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | success = 1; |
| 1896 | |
| 1897 | out_running: |
| 1898 | p->state = TASK_RUNNING; |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1899 | #ifdef CONFIG_SMP |
| 1900 | if (p->sched_class->task_wake_up) |
| 1901 | p->sched_class->task_wake_up(rq, p); |
| 1902 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | out: |
| 1904 | task_rq_unlock(rq, &flags); |
| 1905 | |
| 1906 | return success; |
| 1907 | } |
| 1908 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 1909 | int wake_up_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | { |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 1911 | return try_to_wake_up(p, TASK_ALL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | EXPORT_SYMBOL(wake_up_process); |
| 1914 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 1915 | int wake_up_state(struct task_struct *p, unsigned int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | { |
| 1917 | return try_to_wake_up(p, state, 0); |
| 1918 | } |
| 1919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | /* |
| 1921 | * Perform scheduler related setup for a newly forked process p. |
| 1922 | * p is forked by current. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1923 | * |
| 1924 | * __sched_fork() is basic setup used by init_idle() too: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1926 | static void __sched_fork(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1928 | p->se.exec_start = 0; |
| 1929 | p->se.sum_exec_runtime = 0; |
Ingo Molnar | f6cf891 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 1930 | p->se.prev_sum_exec_runtime = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1931 | |
| 1932 | #ifdef CONFIG_SCHEDSTATS |
| 1933 | p->se.wait_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1934 | p->se.sum_sleep_runtime = 0; |
| 1935 | p->se.sleep_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1936 | p->se.block_start = 0; |
| 1937 | p->se.sleep_max = 0; |
| 1938 | p->se.block_max = 0; |
| 1939 | p->se.exec_max = 0; |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 1940 | p->se.slice_max = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1941 | p->se.wait_max = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 1942 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1943 | |
Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1944 | INIT_LIST_HEAD(&p->rt.run_list); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1945 | p->se.on_rq = 0; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1946 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 1947 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 1948 | INIT_HLIST_HEAD(&p->preempt_notifiers); |
| 1949 | #endif |
| 1950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | /* |
| 1952 | * We mark the process as running here, but have not actually |
| 1953 | * inserted it onto the runqueue yet. This guarantees that |
| 1954 | * nobody will actually run it, and a signal or other external |
| 1955 | * event cannot wake it up and insert it on the runqueue either. |
| 1956 | */ |
| 1957 | p->state = TASK_RUNNING; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | /* |
| 1961 | * fork()/clone()-time setup: |
| 1962 | */ |
| 1963 | void sched_fork(struct task_struct *p, int clone_flags) |
| 1964 | { |
| 1965 | int cpu = get_cpu(); |
| 1966 | |
| 1967 | __sched_fork(p); |
| 1968 | |
| 1969 | #ifdef CONFIG_SMP |
| 1970 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); |
| 1971 | #endif |
Ingo Molnar | 02e4bac2 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 1972 | set_task_cpu(p, cpu); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1973 | |
| 1974 | /* |
| 1975 | * Make sure we do not leak PI boosting priority to the child: |
| 1976 | */ |
| 1977 | p->prio = current->normal_prio; |
Hiroshi Shimamoto | 2ddbf95 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 1978 | if (!rt_prio(p->prio)) |
| 1979 | p->sched_class = &fair_sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1980 | |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 1981 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1982 | if (likely(sched_info_on())) |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 1983 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | #endif |
Chen, Kenneth W | d6077cb | 2006-02-14 13:53:10 -0800 | [diff] [blame] | 1985 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1986 | p->oncpu = 0; |
| 1987 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | #ifdef CONFIG_PREEMPT |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1989 | /* Want to start with kernel preemption disabled. */ |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 1990 | task_thread_info(p)->preempt_count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1992 | put_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | /* |
| 1996 | * wake_up_new_task - wake up a newly created task for the first time. |
| 1997 | * |
| 1998 | * This function will do some initial scheduler statistics housekeeping |
| 1999 | * that must be done for every newly created context, then puts the task |
| 2000 | * on the runqueue and wakes it. |
| 2001 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2002 | 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] | 2003 | { |
| 2004 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2005 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | |
| 2007 | rq = task_rq_lock(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | BUG_ON(p->state != TASK_RUNNING); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 2009 | update_rq_clock(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | |
| 2011 | p->prio = effective_prio(p); |
| 2012 | |
Srivatsa Vaddagiri | b9dca1e | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2013 | if (!p->sched_class->task_new || !current->se.on_rq) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2014 | activate_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2017 | * Let the scheduling class do new task startup |
| 2018 | * management (if any): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | */ |
Ingo Molnar | ee0827d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2020 | p->sched_class->task_new(rq, p); |
Gerald Stralko | 5aff053 | 2008-01-31 22:45:23 +0100 | [diff] [blame] | 2021 | inc_nr_running(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2023 | check_preempt_curr(rq, p); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2024 | #ifdef CONFIG_SMP |
| 2025 | if (p->sched_class->task_wake_up) |
| 2026 | p->sched_class->task_wake_up(rq, p); |
| 2027 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2028 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2031 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 2032 | |
| 2033 | /** |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2034 | * preempt_notifier_register - tell me when current is being being preempted & rescheduled |
| 2035 | * @notifier: notifier struct to register |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2036 | */ |
| 2037 | void preempt_notifier_register(struct preempt_notifier *notifier) |
| 2038 | { |
| 2039 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); |
| 2040 | } |
| 2041 | EXPORT_SYMBOL_GPL(preempt_notifier_register); |
| 2042 | |
| 2043 | /** |
| 2044 | * preempt_notifier_unregister - no longer interested in preemption notifications |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2045 | * @notifier: notifier struct to unregister |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2046 | * |
| 2047 | * This is safe to call from within a preemption notifier. |
| 2048 | */ |
| 2049 | void preempt_notifier_unregister(struct preempt_notifier *notifier) |
| 2050 | { |
| 2051 | hlist_del(¬ifier->link); |
| 2052 | } |
| 2053 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); |
| 2054 | |
| 2055 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 2056 | { |
| 2057 | struct preempt_notifier *notifier; |
| 2058 | struct hlist_node *node; |
| 2059 | |
| 2060 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) |
| 2061 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
| 2062 | } |
| 2063 | |
| 2064 | static void |
| 2065 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 2066 | struct task_struct *next) |
| 2067 | { |
| 2068 | struct preempt_notifier *notifier; |
| 2069 | struct hlist_node *node; |
| 2070 | |
| 2071 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) |
| 2072 | notifier->ops->sched_out(notifier, next); |
| 2073 | } |
| 2074 | |
| 2075 | #else |
| 2076 | |
| 2077 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 2078 | { |
| 2079 | } |
| 2080 | |
| 2081 | static void |
| 2082 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 2083 | struct task_struct *next) |
| 2084 | { |
| 2085 | } |
| 2086 | |
| 2087 | #endif |
| 2088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | /** |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2090 | * prepare_task_switch - prepare to switch tasks |
| 2091 | * @rq: the runqueue preparing to switch |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 2092 | * @prev: the current task that is being switched out |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2093 | * @next: the task we are going to switch to. |
| 2094 | * |
| 2095 | * This is called with the rq lock held and interrupts off. It must |
| 2096 | * be paired with a subsequent finish_task_switch after the context |
| 2097 | * switch. |
| 2098 | * |
| 2099 | * prepare_task_switch sets up locking and calls architecture specific |
| 2100 | * hooks. |
| 2101 | */ |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2102 | static inline void |
| 2103 | prepare_task_switch(struct rq *rq, struct task_struct *prev, |
| 2104 | struct task_struct *next) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2105 | { |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2106 | fire_sched_out_preempt_notifiers(prev, next); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2107 | prepare_lock_switch(rq, next); |
| 2108 | prepare_arch_switch(next); |
| 2109 | } |
| 2110 | |
| 2111 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | * finish_task_switch - clean up after a task-switch |
Jeff Garzik | 344baba | 2005-09-07 01:15:17 -0400 | [diff] [blame] | 2113 | * @rq: runqueue associated with task-switch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | * @prev: the thread we just switched away from. |
| 2115 | * |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2116 | * finish_task_switch must be called after the context switch, paired |
| 2117 | * with a prepare_task_switch call before the context switch. |
| 2118 | * finish_task_switch will reconcile locking set up by prepare_task_switch, |
| 2119 | * and do any other architecture-specific cleanup actions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | * |
| 2121 | * 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] | 2122 | * so, we finish that here outside of the runqueue lock. (Doing it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | * with the lock held can cause deadlocks; see schedule() for |
| 2124 | * details.) |
| 2125 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2126 | static void finish_task_switch(struct rq *rq, struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | __releases(rq->lock) |
| 2128 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | struct mm_struct *mm = rq->prev_mm; |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 2130 | long prev_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | |
| 2132 | rq->prev_mm = NULL; |
| 2133 | |
| 2134 | /* |
| 2135 | * A task struct has one reference for the use as "current". |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2136 | * 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] | 2137 | * schedule one last time. The schedule call will never return, and |
| 2138 | * the scheduled task must drop that reference. |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2139 | * The test for TASK_DEAD must occur while the runqueue locks are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | * still held, otherwise prev could be scheduled on another cpu, die |
| 2141 | * there before we look at prev->state, and then the reference would |
| 2142 | * be dropped twice. |
| 2143 | * Manfred Spraul <manfred@colorfullife.com> |
| 2144 | */ |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 2145 | prev_state = prev->state; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2146 | finish_arch_switch(prev); |
| 2147 | finish_lock_switch(rq, prev); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2148 | #ifdef CONFIG_SMP |
| 2149 | if (current->sched_class->post_schedule) |
| 2150 | current->sched_class->post_schedule(rq); |
| 2151 | #endif |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2152 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2153 | fire_sched_in_preempt_notifiers(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | if (mm) |
| 2155 | mmdrop(mm); |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 2156 | if (unlikely(prev_state == TASK_DEAD)) { |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2157 | /* |
| 2158 | * Remove function-return probe instances associated with this |
| 2159 | * task and put them back on the free list. |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2160 | */ |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2161 | kprobe_flush_task(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | put_task_struct(prev); |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 2163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | /** |
| 2167 | * schedule_tail - first thing a freshly forked thread must call. |
| 2168 | * @prev: the thread we just switched away from. |
| 2169 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2170 | asmlinkage void schedule_tail(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | __releases(rq->lock) |
| 2172 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2173 | struct rq *rq = this_rq(); |
| 2174 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 2175 | finish_task_switch(rq, prev); |
| 2176 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW |
| 2177 | /* In this case, finish_task_switch does not reenable preemption */ |
| 2178 | preempt_enable(); |
| 2179 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | if (current->set_child_tid) |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2181 | put_user(task_pid_vnr(current), current->set_child_tid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | /* |
| 2185 | * context_switch - switch to the new MM and the new |
| 2186 | * thread's register state. |
| 2187 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2188 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2189 | context_switch(struct rq *rq, struct task_struct *prev, |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2190 | struct task_struct *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2192 | struct mm_struct *mm, *oldmm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 2194 | prepare_task_switch(rq, prev, next); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2195 | mm = next->mm; |
| 2196 | oldmm = prev->active_mm; |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 2197 | /* |
| 2198 | * For paravirt, this is coupled with an exit in switch_to to |
| 2199 | * combine the page table reload and the switch backend into |
| 2200 | * one hypercall. |
| 2201 | */ |
| 2202 | arch_enter_lazy_cpu_mode(); |
| 2203 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2204 | if (unlikely(!mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | next->active_mm = oldmm; |
| 2206 | atomic_inc(&oldmm->mm_count); |
| 2207 | enter_lazy_tlb(oldmm, next); |
| 2208 | } else |
| 2209 | switch_mm(oldmm, mm, next); |
| 2210 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2211 | if (unlikely(!prev->mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | prev->active_mm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | rq->prev_mm = oldmm; |
| 2214 | } |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 2215 | /* |
| 2216 | * Since the runqueue lock will be released by the next |
| 2217 | * task (which is an invalid locking op but in the case |
| 2218 | * of the scheduler it's an obvious special-case), so we |
| 2219 | * do an early lockdep release here: |
| 2220 | */ |
| 2221 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 2222 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 2223 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | |
| 2225 | /* Here we just switch the register state and the stack. */ |
| 2226 | switch_to(prev, next, prev); |
| 2227 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2228 | barrier(); |
| 2229 | /* |
| 2230 | * this_rq must be evaluated again because prev may have moved |
| 2231 | * CPUs since it called schedule(), thus the 'rq' on its stack |
| 2232 | * frame will be invalid. |
| 2233 | */ |
| 2234 | finish_task_switch(this_rq(), prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | /* |
| 2238 | * nr_running, nr_uninterruptible and nr_context_switches: |
| 2239 | * |
| 2240 | * externally visible scheduler statistics: current number of runnable |
| 2241 | * threads, current number of uninterruptible-sleeping threads, total |
| 2242 | * number of context switches performed since bootup. |
| 2243 | */ |
| 2244 | unsigned long nr_running(void) |
| 2245 | { |
| 2246 | unsigned long i, sum = 0; |
| 2247 | |
| 2248 | for_each_online_cpu(i) |
| 2249 | sum += cpu_rq(i)->nr_running; |
| 2250 | |
| 2251 | return sum; |
| 2252 | } |
| 2253 | |
| 2254 | unsigned long nr_uninterruptible(void) |
| 2255 | { |
| 2256 | unsigned long i, sum = 0; |
| 2257 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2258 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | sum += cpu_rq(i)->nr_uninterruptible; |
| 2260 | |
| 2261 | /* |
| 2262 | * Since we read the counters lockless, it might be slightly |
| 2263 | * inaccurate. Do not allow it to go below zero though: |
| 2264 | */ |
| 2265 | if (unlikely((long)sum < 0)) |
| 2266 | sum = 0; |
| 2267 | |
| 2268 | return sum; |
| 2269 | } |
| 2270 | |
| 2271 | unsigned long long nr_context_switches(void) |
| 2272 | { |
Steven Rostedt | cc94abf | 2006-06-27 02:54:31 -0700 | [diff] [blame] | 2273 | int i; |
| 2274 | unsigned long long sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2276 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | sum += cpu_rq(i)->nr_switches; |
| 2278 | |
| 2279 | return sum; |
| 2280 | } |
| 2281 | |
| 2282 | unsigned long nr_iowait(void) |
| 2283 | { |
| 2284 | unsigned long i, sum = 0; |
| 2285 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 2286 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
| 2288 | |
| 2289 | return sum; |
| 2290 | } |
| 2291 | |
Jack Steiner | db1b1fe | 2006-03-31 02:31:21 -0800 | [diff] [blame] | 2292 | unsigned long nr_active(void) |
| 2293 | { |
| 2294 | unsigned long i, running = 0, uninterruptible = 0; |
| 2295 | |
| 2296 | for_each_online_cpu(i) { |
| 2297 | running += cpu_rq(i)->nr_running; |
| 2298 | uninterruptible += cpu_rq(i)->nr_uninterruptible; |
| 2299 | } |
| 2300 | |
| 2301 | if (unlikely((long)uninterruptible < 0)) |
| 2302 | uninterruptible = 0; |
| 2303 | |
| 2304 | return running + uninterruptible; |
| 2305 | } |
| 2306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2308 | * Update rq->cpu_load[] statistics. This function is usually called every |
| 2309 | * scheduler tick (TICK_NSEC). |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2310 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2311 | static void update_cpu_load(struct rq *this_rq) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2312 | { |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 2313 | unsigned long this_load = this_rq->load.weight; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2314 | int i, scale; |
| 2315 | |
| 2316 | this_rq->nr_load_updates++; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2317 | |
| 2318 | /* Update our load: */ |
| 2319 | for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { |
| 2320 | unsigned long old_load, new_load; |
| 2321 | |
| 2322 | /* scale is effectively 1 << i now, and >> i divides by scale */ |
| 2323 | |
| 2324 | old_load = this_rq->cpu_load[i]; |
| 2325 | new_load = this_load; |
Ingo Molnar | a25707f | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 2326 | /* |
| 2327 | * Round up the averaging division if load is increasing. This |
| 2328 | * prevents us from getting stuck on 9 if the load is 10, for |
| 2329 | * example. |
| 2330 | */ |
| 2331 | if (new_load > old_load) |
| 2332 | new_load += scale-1; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2333 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; |
| 2334 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2335 | } |
| 2336 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2337 | #ifdef CONFIG_SMP |
| 2338 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2339 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | * double_rq_lock - safely lock two runqueues |
| 2341 | * |
| 2342 | * Note this does not disable interrupts like task_rq_lock, |
| 2343 | * you need to do so manually before calling. |
| 2344 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2345 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | __acquires(rq1->lock) |
| 2347 | __acquires(rq2->lock) |
| 2348 | { |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 2349 | BUG_ON(!irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | if (rq1 == rq2) { |
| 2351 | spin_lock(&rq1->lock); |
| 2352 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 2353 | } else { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2354 | if (rq1 < rq2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | spin_lock(&rq1->lock); |
| 2356 | spin_lock(&rq2->lock); |
| 2357 | } else { |
| 2358 | spin_lock(&rq2->lock); |
| 2359 | spin_lock(&rq1->lock); |
| 2360 | } |
| 2361 | } |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 2362 | update_rq_clock(rq1); |
| 2363 | update_rq_clock(rq2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | /* |
| 2367 | * double_rq_unlock - safely unlock two runqueues |
| 2368 | * |
| 2369 | * Note this does not restore interrupts like task_rq_unlock, |
| 2370 | * you need to do so manually after calling. |
| 2371 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2372 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | __releases(rq1->lock) |
| 2374 | __releases(rq2->lock) |
| 2375 | { |
| 2376 | spin_unlock(&rq1->lock); |
| 2377 | if (rq1 != rq2) |
| 2378 | spin_unlock(&rq2->lock); |
| 2379 | else |
| 2380 | __release(rq2->lock); |
| 2381 | } |
| 2382 | |
| 2383 | /* |
| 2384 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. |
| 2385 | */ |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2386 | static int double_lock_balance(struct rq *this_rq, struct rq *busiest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | __releases(this_rq->lock) |
| 2388 | __acquires(busiest->lock) |
| 2389 | __acquires(this_rq->lock) |
| 2390 | { |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2391 | int ret = 0; |
| 2392 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 2393 | if (unlikely(!irqs_disabled())) { |
| 2394 | /* printk() doesn't work good under rq->lock */ |
| 2395 | spin_unlock(&this_rq->lock); |
| 2396 | BUG_ON(1); |
| 2397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | if (unlikely(!spin_trylock(&busiest->lock))) { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2399 | if (busiest < this_rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | spin_unlock(&this_rq->lock); |
| 2401 | spin_lock(&busiest->lock); |
| 2402 | spin_lock(&this_rq->lock); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2403 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | } else |
| 2405 | spin_lock(&busiest->lock); |
| 2406 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2407 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | * If dest_cpu is allowed for this process, migrate the task to it. |
| 2412 | * This is accomplished by forcing the cpu_allowed mask to only |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 2413 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | * the cpu_allowed mask is restored. |
| 2415 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2416 | static void sched_migrate_task(struct task_struct *p, int dest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2418 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2420 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | |
| 2422 | rq = task_rq_lock(p, &flags); |
| 2423 | if (!cpu_isset(dest_cpu, p->cpus_allowed) |
| 2424 | || unlikely(cpu_is_offline(dest_cpu))) |
| 2425 | goto out; |
| 2426 | |
| 2427 | /* force the process onto the specified CPU */ |
| 2428 | if (migrate_task(p, dest_cpu, &req)) { |
| 2429 | /* Need to wait for migration thread (might exit: take ref). */ |
| 2430 | struct task_struct *mt = rq->migration_thread; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | get_task_struct(mt); |
| 2433 | task_rq_unlock(rq, &flags); |
| 2434 | wake_up_process(mt); |
| 2435 | put_task_struct(mt); |
| 2436 | wait_for_completion(&req.done); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | return; |
| 2439 | } |
| 2440 | out: |
| 2441 | task_rq_unlock(rq, &flags); |
| 2442 | } |
| 2443 | |
| 2444 | /* |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2445 | * sched_exec - execve() is a valuable balancing opportunity, because at |
| 2446 | * this point the task has the smallest effective memory and cache footprint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | */ |
| 2448 | void sched_exec(void) |
| 2449 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | int new_cpu, this_cpu = get_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2451 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | put_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2453 | if (new_cpu != this_cpu) |
| 2454 | sched_migrate_task(current, new_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | /* |
| 2458 | * pull_task - move a task from a remote runqueue to the local runqueue. |
| 2459 | * Both runqueues must be locked. |
| 2460 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2461 | static void pull_task(struct rq *src_rq, struct task_struct *p, |
| 2462 | struct rq *this_rq, int this_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 2464 | deactivate_task(src_rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | set_task_cpu(p, this_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2466 | activate_task(this_rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | /* |
| 2468 | * Note that idle threads have a prio of MAX_PRIO, for this test |
| 2469 | * to be always true for them. |
| 2470 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2471 | check_preempt_curr(this_rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | /* |
| 2475 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 2476 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 2477 | static |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2478 | 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] | 2479 | struct sched_domain *sd, enum cpu_idle_type idle, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 2480 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | { |
| 2482 | /* |
| 2483 | * We do not migrate tasks that are: |
| 2484 | * 1) running (obviously), or |
| 2485 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 2486 | * 3) are cache-hot on their current CPU. |
| 2487 | */ |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2488 | if (!cpu_isset(this_cpu, p->cpus_allowed)) { |
| 2489 | schedstat_inc(p, se.nr_failed_migrations_affine); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2491 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2492 | *all_pinned = 0; |
| 2493 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2494 | if (task_running(rq, p)) { |
| 2495 | schedstat_inc(p, se.nr_failed_migrations_running); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2496 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2499 | /* |
| 2500 | * Aggressive migration if: |
| 2501 | * 1) task is cache cold, or |
| 2502 | * 2) too many balance attempts have failed. |
| 2503 | */ |
| 2504 | |
Ingo Molnar | 6bc1665 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2505 | if (!task_hot(p, rq->clock, sd) || |
| 2506 | sd->nr_balance_failed > sd->cache_nice_tries) { |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2507 | #ifdef CONFIG_SCHEDSTATS |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2508 | if (task_hot(p, rq->clock, sd)) { |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2509 | schedstat_inc(sd, lb_hot_gained[idle]); |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2510 | schedstat_inc(p, se.nr_forced_migrations); |
| 2511 | } |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2512 | #endif |
| 2513 | return 1; |
| 2514 | } |
| 2515 | |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2516 | if (task_hot(p, rq->clock, sd)) { |
| 2517 | schedstat_inc(p, se.nr_failed_migrations_hot); |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2518 | return 0; |
Ingo Molnar | cc36773 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 2519 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | return 1; |
| 2521 | } |
| 2522 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2523 | static unsigned long |
| 2524 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2525 | unsigned long max_load_move, struct sched_domain *sd, |
| 2526 | enum cpu_idle_type idle, int *all_pinned, |
| 2527 | int *this_best_prio, struct rq_iterator *iterator) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2528 | { |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2529 | int loops = 0, pulled = 0, pinned = 0, skip_for_load; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2530 | struct task_struct *p; |
| 2531 | long rem_load_move = max_load_move; |
| 2532 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2533 | if (max_load_move == 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2534 | goto out; |
| 2535 | |
| 2536 | pinned = 1; |
| 2537 | |
| 2538 | /* |
| 2539 | * Start the load-balancing iterator: |
| 2540 | */ |
| 2541 | p = iterator->start(iterator->arg); |
| 2542 | next: |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2543 | if (!p || loops++ > sysctl_sched_nr_migrate) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2544 | goto out; |
| 2545 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2546 | * To help distribute high priority tasks across CPUs we don't |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2547 | * skip a task if it will be the highest priority task (i.e. smallest |
| 2548 | * prio value) on its new queue regardless of its load weight |
| 2549 | */ |
| 2550 | skip_for_load = (p->se.load.weight >> 1) > rem_load_move + |
| 2551 | SCHED_LOAD_SCALE_FUZZ; |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2552 | if ((skip_for_load && p->prio >= *this_best_prio) || |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2553 | !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2554 | p = iterator->next(iterator->arg); |
| 2555 | goto next; |
| 2556 | } |
| 2557 | |
| 2558 | pull_task(busiest, p, this_rq, this_cpu); |
| 2559 | pulled++; |
| 2560 | rem_load_move -= p->se.load.weight; |
| 2561 | |
| 2562 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 2563 | * 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] | 2564 | */ |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2565 | if (rem_load_move > 0) { |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2566 | if (p->prio < *this_best_prio) |
| 2567 | *this_best_prio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2568 | p = iterator->next(iterator->arg); |
| 2569 | goto next; |
| 2570 | } |
| 2571 | out: |
| 2572 | /* |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2573 | * 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] | 2574 | * so we can safely collect pull_task() stats here rather than |
| 2575 | * inside pull_task(). |
| 2576 | */ |
| 2577 | schedstat_add(sd, lb_gained[idle], pulled); |
| 2578 | |
| 2579 | if (all_pinned) |
| 2580 | *all_pinned = pinned; |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2581 | |
| 2582 | return max_load_move - rem_load_move; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2583 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | /* |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2586 | * move_tasks tries to move up to max_load_move weighted load from busiest to |
| 2587 | * this_rq, as part of a balancing operation within domain "sd". |
| 2588 | * Returns 1 if successful and 0 otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | * |
| 2590 | * Called with both runqueues locked. |
| 2591 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2592 | 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] | 2593 | unsigned long max_load_move, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2594 | struct sched_domain *sd, enum cpu_idle_type idle, |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2595 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2597 | const struct sched_class *class = sched_class_highest; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2598 | unsigned long total_load_moved = 0; |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2599 | int this_best_prio = this_rq->curr->prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2601 | do { |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2602 | total_load_moved += |
| 2603 | class->load_balance(this_rq, this_cpu, busiest, |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2604 | max_load_move - total_load_moved, |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2605 | sd, idle, all_pinned, &this_best_prio); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2606 | class = class->next; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2607 | } while (class && max_load_move > total_load_moved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2609 | return total_load_moved > 0; |
| 2610 | } |
| 2611 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2612 | static int |
| 2613 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2614 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 2615 | struct rq_iterator *iterator) |
| 2616 | { |
| 2617 | struct task_struct *p = iterator->start(iterator->arg); |
| 2618 | int pinned = 0; |
| 2619 | |
| 2620 | while (p) { |
| 2621 | if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
| 2622 | pull_task(busiest, p, this_rq, this_cpu); |
| 2623 | /* |
| 2624 | * Right now, this is only the second place pull_task() |
| 2625 | * is called, so we can safely collect pull_task() |
| 2626 | * stats here rather than inside pull_task(). |
| 2627 | */ |
| 2628 | schedstat_inc(sd, lb_gained[idle]); |
| 2629 | |
| 2630 | return 1; |
| 2631 | } |
| 2632 | p = iterator->next(iterator->arg); |
| 2633 | } |
| 2634 | |
| 2635 | return 0; |
| 2636 | } |
| 2637 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2638 | /* |
| 2639 | * move_one_task tries to move exactly one task from busiest to this_rq, as |
| 2640 | * part of active balancing operations within "domain". |
| 2641 | * Returns 1 if successful and 0 otherwise. |
| 2642 | * |
| 2643 | * Called with both runqueues locked. |
| 2644 | */ |
| 2645 | static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2646 | struct sched_domain *sd, enum cpu_idle_type idle) |
| 2647 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2648 | const struct sched_class *class; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2649 | |
| 2650 | for (class = sched_class_highest; class; class = class->next) |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2651 | if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle)) |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 2652 | return 1; |
| 2653 | |
| 2654 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | /* |
| 2658 | * find_busiest_group finds and returns the busiest CPU group within the |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2659 | * domain. It calculates and returns the amount of weighted load which |
| 2660 | * should be moved to restore balance via the imbalance parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | */ |
| 2662 | static struct sched_group * |
| 2663 | find_busiest_group(struct sched_domain *sd, int this_cpu, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2664 | unsigned long *imbalance, enum cpu_idle_type idle, |
| 2665 | int *sd_idle, cpumask_t *cpus, int *balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | { |
| 2667 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; |
| 2668 | 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] | 2669 | unsigned long max_pull; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2670 | unsigned long busiest_load_per_task, busiest_nr_running; |
| 2671 | unsigned long this_load_per_task, this_nr_running; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2672 | int load_idx, group_imb = 0; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2673 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2674 | int power_savings_balance = 1; |
| 2675 | unsigned long leader_nr_running = 0, min_load_per_task = 0; |
| 2676 | unsigned long min_nr_running = ULONG_MAX; |
| 2677 | struct sched_group *group_min = NULL, *group_leader = NULL; |
| 2678 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | |
| 2680 | max_load = this_load = total_load = total_pwr = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2681 | busiest_load_per_task = busiest_nr_running = 0; |
| 2682 | this_load_per_task = this_nr_running = 0; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2683 | if (idle == CPU_NOT_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2684 | load_idx = sd->busy_idx; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2685 | else if (idle == CPU_NEWLY_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2686 | load_idx = sd->newidle_idx; |
| 2687 | else |
| 2688 | load_idx = sd->idle_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | |
| 2690 | do { |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2691 | unsigned long load, group_capacity, max_cpu_load, min_cpu_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | int local_group; |
| 2693 | int i; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2694 | int __group_imb = 0; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2695 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2696 | unsigned long sum_nr_running, sum_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | |
| 2698 | local_group = cpu_isset(this_cpu, group->cpumask); |
| 2699 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2700 | if (local_group) |
| 2701 | balance_cpu = first_cpu(group->cpumask); |
| 2702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | /* Tally up the load of all CPUs in the group */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2704 | sum_weighted_load = sum_nr_running = avg_load = 0; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2705 | max_cpu_load = 0; |
| 2706 | min_cpu_load = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | |
| 2708 | for_each_cpu_mask(i, group->cpumask) { |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2709 | struct rq *rq; |
| 2710 | |
| 2711 | if (!cpu_isset(i, *cpus)) |
| 2712 | continue; |
| 2713 | |
| 2714 | rq = cpu_rq(i); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2715 | |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 2716 | if (*sd_idle && rq->nr_running) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2717 | *sd_idle = 0; |
| 2718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | /* Bias balancing toward cpus of our domain */ |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2720 | if (local_group) { |
| 2721 | if (idle_cpu(i) && !first_idle_cpu) { |
| 2722 | first_idle_cpu = 1; |
| 2723 | balance_cpu = i; |
| 2724 | } |
| 2725 | |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2726 | load = target_load(i, load_idx); |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2727 | } else { |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2728 | load = source_load(i, load_idx); |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2729 | if (load > max_cpu_load) |
| 2730 | max_cpu_load = load; |
| 2731 | if (min_cpu_load > load) |
| 2732 | min_cpu_load = load; |
| 2733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | |
| 2735 | avg_load += load; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2736 | sum_nr_running += rq->nr_running; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2737 | sum_weighted_load += weighted_cpuload(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2740 | /* |
| 2741 | * First idle cpu or the first cpu(busiest) in this sched group |
| 2742 | * is eligible for doing load balancing at this and above |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 2743 | * domains. In the newly idle case, we will allow all the cpu's |
| 2744 | * to do the newly idle load balance. |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2745 | */ |
Suresh Siddha | 9439aab | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 2746 | if (idle != CPU_NEWLY_IDLE && local_group && |
| 2747 | balance_cpu != this_cpu && balance) { |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2748 | *balance = 0; |
| 2749 | goto ret; |
| 2750 | } |
| 2751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | total_load += avg_load; |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2753 | total_pwr += group->__cpu_power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | |
| 2755 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2756 | avg_load = sg_div_cpu_power(group, |
| 2757 | avg_load * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2759 | if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE) |
| 2760 | __group_imb = 1; |
| 2761 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2762 | group_capacity = group->__cpu_power / SCHED_LOAD_SCALE; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | if (local_group) { |
| 2765 | this_load = avg_load; |
| 2766 | this = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2767 | this_nr_running = sum_nr_running; |
| 2768 | this_load_per_task = sum_weighted_load; |
| 2769 | } else if (avg_load > max_load && |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2770 | (sum_nr_running > group_capacity || __group_imb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | max_load = avg_load; |
| 2772 | busiest = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2773 | busiest_nr_running = sum_nr_running; |
| 2774 | busiest_load_per_task = sum_weighted_load; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2775 | group_imb = __group_imb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2777 | |
| 2778 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2779 | /* |
| 2780 | * Busy processors will not participate in power savings |
| 2781 | * balance. |
| 2782 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2783 | if (idle == CPU_NOT_IDLE || |
| 2784 | !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
| 2785 | goto group_next; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2786 | |
| 2787 | /* |
| 2788 | * If the local group is idle or completely loaded |
| 2789 | * no need to do power savings balance at this domain |
| 2790 | */ |
| 2791 | if (local_group && (this_nr_running >= group_capacity || |
| 2792 | !this_nr_running)) |
| 2793 | power_savings_balance = 0; |
| 2794 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2795 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2796 | * If a group is already running at full capacity or idle, |
| 2797 | * don't include that group in power savings calculations |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2798 | */ |
| 2799 | if (!power_savings_balance || sum_nr_running >= group_capacity |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2800 | || !sum_nr_running) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2801 | goto group_next; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2802 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2803 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2804 | * Calculate the group which has the least non-idle load. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2805 | * This is the group from where we need to pick up the load |
| 2806 | * for saving power |
| 2807 | */ |
| 2808 | if ((sum_nr_running < min_nr_running) || |
| 2809 | (sum_nr_running == min_nr_running && |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2810 | first_cpu(group->cpumask) < |
| 2811 | first_cpu(group_min->cpumask))) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2812 | group_min = group; |
| 2813 | min_nr_running = sum_nr_running; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2814 | min_load_per_task = sum_weighted_load / |
| 2815 | sum_nr_running; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2816 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2817 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2818 | /* |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2819 | * Calculate the group which is almost near its |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2820 | * capacity but still has some space to pick up some load |
| 2821 | * from other group and save more power |
| 2822 | */ |
| 2823 | if (sum_nr_running <= group_capacity - 1) { |
| 2824 | if (sum_nr_running > leader_nr_running || |
| 2825 | (sum_nr_running == leader_nr_running && |
| 2826 | first_cpu(group->cpumask) > |
| 2827 | first_cpu(group_leader->cpumask))) { |
| 2828 | group_leader = group; |
| 2829 | leader_nr_running = sum_nr_running; |
| 2830 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2831 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2832 | group_next: |
| 2833 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | group = group->next; |
| 2835 | } while (group != sd->groups); |
| 2836 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2837 | if (!busiest || this_load >= max_load || busiest_nr_running == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | goto out_balanced; |
| 2839 | |
| 2840 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; |
| 2841 | |
| 2842 | if (this_load >= avg_load || |
| 2843 | 100*max_load <= sd->imbalance_pct*this_load) |
| 2844 | goto out_balanced; |
| 2845 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2846 | busiest_load_per_task /= busiest_nr_running; |
Ken Chen | 908a7c1 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 2847 | if (group_imb) |
| 2848 | busiest_load_per_task = min(busiest_load_per_task, avg_load); |
| 2849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | /* |
| 2851 | * We're trying to get all the cpus to the average_load, so we don't |
| 2852 | * want to push ourselves above the average load, nor do we wish to |
| 2853 | * reduce the max loaded cpu below the average load, as either of these |
| 2854 | * actions would just result in more rebalancing later, and ping-pong |
| 2855 | * tasks around. Thus we look for the minimum possible imbalance. |
| 2856 | * Negative imbalances (*we* are more loaded than anyone else) will |
| 2857 | * 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] | 2858 | * 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] | 2859 | * appear as very large values with unsigned longs. |
| 2860 | */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2861 | if (max_load <= busiest_load_per_task) |
| 2862 | goto out_balanced; |
| 2863 | |
| 2864 | /* |
| 2865 | * In the presence of smp nice balancing, certain scenarios can have |
| 2866 | * max load less than avg load(as we skip the groups at or below |
| 2867 | * its cpu_power, while calculating max_load..) |
| 2868 | */ |
| 2869 | if (max_load < avg_load) { |
| 2870 | *imbalance = 0; |
| 2871 | goto small_imbalance; |
| 2872 | } |
Siddha, Suresh B | 0c117f1 | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2873 | |
| 2874 | /* 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] | 2875 | 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] | 2876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | /* How much load to actually move to equalise the imbalance */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2878 | *imbalance = min(max_pull * busiest->__cpu_power, |
| 2879 | (avg_load - this_load) * this->__cpu_power) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | / SCHED_LOAD_SCALE; |
| 2881 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2882 | /* |
| 2883 | * if *imbalance is less than the average load per runnable task |
| 2884 | * there is no gaurantee that any tasks will be moved so we'll have |
| 2885 | * a think about bumping its value to force at least one task to be |
| 2886 | * moved |
| 2887 | */ |
Suresh Siddha | 7fd0d2d | 2007-09-05 14:32:48 +0200 | [diff] [blame] | 2888 | if (*imbalance < busiest_load_per_task) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2889 | unsigned long tmp, pwr_now, pwr_move; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2890 | unsigned int imbn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2892 | small_imbalance: |
| 2893 | pwr_move = pwr_now = 0; |
| 2894 | imbn = 2; |
| 2895 | if (this_nr_running) { |
| 2896 | this_load_per_task /= this_nr_running; |
| 2897 | if (busiest_load_per_task > this_load_per_task) |
| 2898 | imbn = 1; |
| 2899 | } else |
| 2900 | this_load_per_task = SCHED_LOAD_SCALE; |
| 2901 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2902 | if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >= |
| 2903 | busiest_load_per_task * imbn) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2904 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | return busiest; |
| 2906 | } |
| 2907 | |
| 2908 | /* |
| 2909 | * OK, we don't have enough imbalance to justify moving tasks, |
| 2910 | * however we may be able to increase total CPU power used by |
| 2911 | * moving them. |
| 2912 | */ |
| 2913 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2914 | pwr_now += busiest->__cpu_power * |
| 2915 | min(busiest_load_per_task, max_load); |
| 2916 | pwr_now += this->__cpu_power * |
| 2917 | min(this_load_per_task, this_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | pwr_now /= SCHED_LOAD_SCALE; |
| 2919 | |
| 2920 | /* Amount of load we'd subtract */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2921 | tmp = sg_div_cpu_power(busiest, |
| 2922 | busiest_load_per_task * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | if (max_load > tmp) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2924 | pwr_move += busiest->__cpu_power * |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2925 | min(busiest_load_per_task, max_load - tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | |
| 2927 | /* Amount of load we'd add */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2928 | if (max_load * busiest->__cpu_power < |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 2929 | busiest_load_per_task * SCHED_LOAD_SCALE) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2930 | tmp = sg_div_cpu_power(this, |
| 2931 | max_load * busiest->__cpu_power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | else |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2933 | tmp = sg_div_cpu_power(this, |
| 2934 | busiest_load_per_task * SCHED_LOAD_SCALE); |
| 2935 | pwr_move += this->__cpu_power * |
| 2936 | min(this_load_per_task, this_load + tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | pwr_move /= SCHED_LOAD_SCALE; |
| 2938 | |
| 2939 | /* Move if we gain throughput */ |
Suresh Siddha | 7fd0d2d | 2007-09-05 14:32:48 +0200 | [diff] [blame] | 2940 | if (pwr_move > pwr_now) |
| 2941 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | } |
| 2943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | return busiest; |
| 2945 | |
| 2946 | out_balanced: |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2947 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2948 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2949 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2951 | if (this == group_leader && group_leader != group_min) { |
| 2952 | *imbalance = min_load_per_task; |
| 2953 | return group_min; |
| 2954 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2955 | #endif |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2956 | ret: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | *imbalance = 0; |
| 2958 | return NULL; |
| 2959 | } |
| 2960 | |
| 2961 | /* |
| 2962 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 2963 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2964 | static struct rq * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2965 | find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2966 | unsigned long imbalance, cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2968 | struct rq *busiest = NULL, *rq; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2969 | unsigned long max_load = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | int i; |
| 2971 | |
| 2972 | for_each_cpu_mask(i, group->cpumask) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2973 | unsigned long wl; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2974 | |
| 2975 | if (!cpu_isset(i, *cpus)) |
| 2976 | continue; |
| 2977 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2978 | rq = cpu_rq(i); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2979 | wl = weighted_cpuload(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2981 | if (rq->nr_running == 1 && wl > imbalance) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2982 | continue; |
| 2983 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2984 | if (wl > max_load) { |
| 2985 | max_load = wl; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2986 | busiest = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | return busiest; |
| 2991 | } |
| 2992 | |
| 2993 | /* |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 2994 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 2995 | * so long as it is large enough. |
| 2996 | */ |
| 2997 | #define MAX_PINNED_INTERVAL 512 |
| 2998 | |
| 2999 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3001 | * tasks if there is an imbalance. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3003 | static int load_balance(int this_cpu, struct rq *this_rq, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3004 | struct sched_domain *sd, enum cpu_idle_type idle, |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3005 | int *balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | { |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3007 | int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | struct sched_group *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | unsigned long imbalance; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3010 | struct rq *busiest; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3011 | cpumask_t cpus = CPU_MASK_ALL; |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3012 | unsigned long flags; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3013 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3014 | /* |
| 3015 | * When power savings policy is enabled for the parent domain, idle |
| 3016 | * sibling can pick up load irrespective of busy siblings. In this case, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3017 | * 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] | 3018 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3019 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3020 | if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3021 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3022 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3024 | schedstat_inc(sd, lb_count[idle]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3026 | redo: |
| 3027 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3028 | &cpus, balance); |
| 3029 | |
Chen, Kenneth W | 0606671 | 2006-12-10 02:20:35 -0800 | [diff] [blame] | 3030 | if (*balance == 0) |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3031 | goto out_balanced; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | if (!group) { |
| 3034 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 3035 | goto out_balanced; |
| 3036 | } |
| 3037 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3038 | busiest = find_busiest_queue(group, idle, imbalance, &cpus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | if (!busiest) { |
| 3040 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 3041 | goto out_balanced; |
| 3042 | } |
| 3043 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3044 | BUG_ON(busiest == this_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | |
| 3046 | schedstat_add(sd, lb_imbalance[idle], imbalance); |
| 3047 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3048 | ld_moved = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | if (busiest->nr_running > 1) { |
| 3050 | /* |
| 3051 | * Attempt to move tasks. If find_busiest_group has found |
| 3052 | * an imbalance but busiest->nr_running <= 1, the group is |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3053 | * still unbalanced. ld_moved simply stays zero, so it is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | * correctly treated as an imbalance. |
| 3055 | */ |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3056 | local_irq_save(flags); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 3057 | double_rq_lock(this_rq, busiest); |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3058 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3059 | imbalance, sd, idle, &all_pinned); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 3060 | double_rq_unlock(this_rq, busiest); |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3061 | local_irq_restore(flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3062 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3063 | /* |
| 3064 | * some other cpu did the load balance for us. |
| 3065 | */ |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3066 | if (ld_moved && this_cpu != smp_processor_id()) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3067 | resched_cpu(this_cpu); |
| 3068 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3069 | /* All tasks on this runqueue were pinned by CPU affinity */ |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3070 | if (unlikely(all_pinned)) { |
| 3071 | cpu_clear(cpu_of(busiest), cpus); |
| 3072 | if (!cpus_empty(cpus)) |
| 3073 | goto redo; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3074 | goto out_balanced; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3075 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3077 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3078 | if (!ld_moved) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | schedstat_inc(sd, lb_failed[idle]); |
| 3080 | sd->nr_balance_failed++; |
| 3081 | |
| 3082 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3084 | spin_lock_irqsave(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3085 | |
| 3086 | /* don't kick the migration_thread, if the curr |
| 3087 | * task on busiest cpu can't be moved to this_cpu |
| 3088 | */ |
| 3089 | if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3090 | spin_unlock_irqrestore(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3091 | all_pinned = 1; |
| 3092 | goto out_one_pinned; |
| 3093 | } |
| 3094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | if (!busiest->active_balance) { |
| 3096 | busiest->active_balance = 1; |
| 3097 | busiest->push_cpu = this_cpu; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3098 | active_balance = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | } |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 3100 | spin_unlock_irqrestore(&busiest->lock, flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3101 | if (active_balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | wake_up_process(busiest->migration_thread); |
| 3103 | |
| 3104 | /* |
| 3105 | * We've kicked active balancing, reset the failure |
| 3106 | * counter. |
| 3107 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3108 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3110 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | sd->nr_balance_failed = 0; |
| 3112 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3113 | if (likely(!active_balance)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | /* We were unbalanced, so reset the balancing interval */ |
| 3115 | sd->balance_interval = sd->min_interval; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 3116 | } else { |
| 3117 | /* |
| 3118 | * If we've begun active balancing, start to back off. This |
| 3119 | * case may not be covered by the all_pinned logic if there |
| 3120 | * is only 1 task on the busy runqueue (because we don't call |
| 3121 | * move_tasks). |
| 3122 | */ |
| 3123 | if (sd->balance_interval < sd->max_interval) |
| 3124 | sd->balance_interval *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | } |
| 3126 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3127 | if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3128 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3129 | return -1; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3130 | return ld_moved; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | |
| 3132 | out_balanced: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | schedstat_inc(sd, lb_balanced[idle]); |
| 3134 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3135 | sd->nr_balance_failed = 0; |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3136 | |
| 3137 | out_one_pinned: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | /* tune up the balancing interval */ |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 3139 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
| 3140 | (sd->balance_interval < sd->max_interval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | sd->balance_interval *= 2; |
| 3142 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3143 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3144 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3145 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | return 0; |
| 3147 | } |
| 3148 | |
| 3149 | /* |
| 3150 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3151 | * tasks if there is an imbalance. |
| 3152 | * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3153 | * 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] | 3154 | * this_rq is locked. |
| 3155 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3156 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3157 | 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] | 3158 | { |
| 3159 | struct sched_group *group; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3160 | struct rq *busiest = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | unsigned long imbalance; |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3162 | int ld_moved = 0; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3163 | int sd_idle = 0; |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3164 | int all_pinned = 0; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3165 | cpumask_t cpus = CPU_MASK_ALL; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3166 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3167 | /* |
| 3168 | * When power savings policy is enabled for the parent domain, idle |
| 3169 | * sibling can pick up load irrespective of busy siblings. In this case, |
| 3170 | * let the state of idle sibling percolate up as IDLE, instead of |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3171 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3172 | */ |
| 3173 | if (sd->flags & SD_SHARE_CPUPOWER && |
| 3174 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3175 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3177 | schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3178 | redo: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3179 | group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE, |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3180 | &sd_idle, &cpus, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | if (!group) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3182 | schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3183 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | } |
| 3185 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3186 | busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3187 | &cpus); |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3188 | if (!busiest) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3189 | schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3190 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | } |
| 3192 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 3193 | BUG_ON(busiest == this_rq); |
| 3194 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3195 | schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3196 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3197 | ld_moved = 0; |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3198 | if (busiest->nr_running > 1) { |
| 3199 | /* Attempt to move tasks */ |
| 3200 | double_lock_balance(this_rq, busiest); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3201 | /* this_rq->clock is already updated */ |
| 3202 | update_rq_clock(busiest); |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3203 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3204 | imbalance, sd, CPU_NEWLY_IDLE, |
| 3205 | &all_pinned); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3206 | spin_unlock(&busiest->lock); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3207 | |
Suresh Siddha | 969bb4e | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 3208 | if (unlikely(all_pinned)) { |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 3209 | cpu_clear(cpu_of(busiest), cpus); |
| 3210 | if (!cpus_empty(cpus)) |
| 3211 | goto redo; |
| 3212 | } |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3213 | } |
| 3214 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3215 | if (!ld_moved) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3216 | schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3217 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 3218 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3219 | return -1; |
| 3220 | } else |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3221 | sd->nr_balance_failed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3222 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3223 | return ld_moved; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3224 | |
| 3225 | out_balanced: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3226 | schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3227 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 3228 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3229 | return -1; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3230 | sd->nr_balance_failed = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3231 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 3232 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | /* |
| 3236 | * idle_balance is called by schedule() if this_cpu is about to become |
| 3237 | * idle. Attempts to pull tasks from other CPUs. |
| 3238 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3239 | static void idle_balance(int this_cpu, struct rq *this_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | { |
| 3241 | struct sched_domain *sd; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3242 | int pulled_task = -1; |
| 3243 | unsigned long next_balance = jiffies + HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | |
| 3245 | for_each_domain(this_cpu, sd) { |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 3246 | unsigned long interval; |
| 3247 | |
| 3248 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 3249 | continue; |
| 3250 | |
| 3251 | if (sd->flags & SD_BALANCE_NEWIDLE) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3252 | /* If we've pulled tasks over stop searching: */ |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 3253 | pulled_task = load_balance_newidle(this_cpu, |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 3254 | this_rq, sd); |
| 3255 | |
| 3256 | interval = msecs_to_jiffies(sd->balance_interval); |
| 3257 | if (time_after(next_balance, sd->last_balance + interval)) |
| 3258 | next_balance = sd->last_balance + interval; |
| 3259 | if (pulled_task) |
| 3260 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3262 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 3263 | /* |
| 3264 | * We are going idle. next_balance may be set based on |
| 3265 | * a busy processor. So reset next_balance. |
| 3266 | */ |
| 3267 | this_rq->next_balance = next_balance; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | /* |
| 3272 | * active_load_balance is run by migration threads. It pushes running tasks |
| 3273 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be |
| 3274 | * running on each physical CPU where possible, and avoids physical / |
| 3275 | * logical imbalances. |
| 3276 | * |
| 3277 | * Called with busiest_rq locked. |
| 3278 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3279 | static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3281 | int target_cpu = busiest_rq->push_cpu; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3282 | struct sched_domain *sd; |
| 3283 | struct rq *target_rq; |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3284 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3285 | /* Is there any task to move? */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3286 | if (busiest_rq->nr_running <= 1) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3287 | return; |
| 3288 | |
| 3289 | target_rq = cpu_rq(target_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | |
| 3291 | /* |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3292 | * This condition is "impossible", if it occurs |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3293 | * we need to fix it. Originally reported by |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3294 | * Bjorn Helgaas on a 128-cpu setup. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3296 | BUG_ON(busiest_rq == target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3298 | /* move a task from busiest_rq to target_rq */ |
| 3299 | double_lock_balance(busiest_rq, target_rq); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3300 | update_rq_clock(busiest_rq); |
| 3301 | update_rq_clock(target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3303 | /* Search for an sd spanning us and the target CPU. */ |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3304 | for_each_domain(target_cpu, sd) { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3305 | if ((sd->flags & SD_LOAD_BALANCE) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3306 | cpu_isset(busiest_cpu, sd->span)) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3307 | break; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3310 | if (likely(sd)) { |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3311 | schedstat_inc(sd, alb_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 3313 | if (move_one_task(target_rq, target_cpu, busiest_rq, |
| 3314 | sd, CPU_IDLE)) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3315 | schedstat_inc(sd, alb_pushed); |
| 3316 | else |
| 3317 | schedstat_inc(sd, alb_failed); |
| 3318 | } |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 3319 | spin_unlock(&target_rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3322 | #ifdef CONFIG_NO_HZ |
| 3323 | static struct { |
| 3324 | atomic_t load_balancer; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3325 | cpumask_t cpu_mask; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3326 | } nohz ____cacheline_aligned = { |
| 3327 | .load_balancer = ATOMIC_INIT(-1), |
| 3328 | .cpu_mask = CPU_MASK_NONE, |
| 3329 | }; |
| 3330 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3331 | /* |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3332 | * This routine will try to nominate the ilb (idle load balancing) |
| 3333 | * owner among the cpus whose ticks are stopped. ilb owner will do the idle |
| 3334 | * load balancing on behalf of all those cpus. If all the cpus in the system |
| 3335 | * go into this tickless mode, then there will be no ilb owner (as there is |
| 3336 | * no need for one) and all the cpus will sleep till the next wakeup event |
| 3337 | * arrives... |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3338 | * |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3339 | * For the ilb owner, tick is not stopped. And this tick will be used |
| 3340 | * for idle load balancing. ilb owner will still be part of |
| 3341 | * nohz.cpu_mask.. |
| 3342 | * |
| 3343 | * While stopping the tick, this cpu will become the ilb owner if there |
| 3344 | * is no other owner. And will be the owner till that cpu becomes busy |
| 3345 | * or if all cpus in the system stop their ticks at which point |
| 3346 | * there is no need for ilb owner. |
| 3347 | * |
| 3348 | * When the ilb owner becomes busy, it nominates another owner, during the |
| 3349 | * next busy scheduler_tick() |
| 3350 | */ |
| 3351 | int select_nohz_load_balancer(int stop_tick) |
| 3352 | { |
| 3353 | int cpu = smp_processor_id(); |
| 3354 | |
| 3355 | if (stop_tick) { |
| 3356 | cpu_set(cpu, nohz.cpu_mask); |
| 3357 | cpu_rq(cpu)->in_nohz_recently = 1; |
| 3358 | |
| 3359 | /* |
| 3360 | * If we are going offline and still the leader, give up! |
| 3361 | */ |
| 3362 | if (cpu_is_offline(cpu) && |
| 3363 | atomic_read(&nohz.load_balancer) == cpu) { |
| 3364 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 3365 | BUG(); |
| 3366 | return 0; |
| 3367 | } |
| 3368 | |
| 3369 | /* time for ilb owner also to sleep */ |
| 3370 | if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 3371 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 3372 | atomic_set(&nohz.load_balancer, -1); |
| 3373 | return 0; |
| 3374 | } |
| 3375 | |
| 3376 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 3377 | /* make me the ilb owner */ |
| 3378 | if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) |
| 3379 | return 1; |
| 3380 | } else if (atomic_read(&nohz.load_balancer) == cpu) |
| 3381 | return 1; |
| 3382 | } else { |
| 3383 | if (!cpu_isset(cpu, nohz.cpu_mask)) |
| 3384 | return 0; |
| 3385 | |
| 3386 | cpu_clear(cpu, nohz.cpu_mask); |
| 3387 | |
| 3388 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 3389 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 3390 | BUG(); |
| 3391 | } |
| 3392 | return 0; |
| 3393 | } |
| 3394 | #endif |
| 3395 | |
| 3396 | static DEFINE_SPINLOCK(balancing); |
| 3397 | |
| 3398 | /* |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3399 | * It checks each scheduling domain to see if it is due to be balanced, |
| 3400 | * and initiates a balancing operation if so. |
| 3401 | * |
| 3402 | * Balancing parameters are set up in arch_init_sched_domains. |
| 3403 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 3404 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3405 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3406 | int balance = 1; |
| 3407 | struct rq *rq = cpu_rq(cpu); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3408 | unsigned long interval; |
| 3409 | struct sched_domain *sd; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3410 | /* Earliest time when we have to do rebalance again */ |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 3411 | unsigned long next_balance = jiffies + 60*HZ; |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3412 | int update_next_balance = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3414 | for_each_domain(cpu, sd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 3416 | continue; |
| 3417 | |
| 3418 | interval = sd->balance_interval; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3419 | if (idle != CPU_IDLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | interval *= sd->busy_factor; |
| 3421 | |
| 3422 | /* scale ms to jiffies */ |
| 3423 | interval = msecs_to_jiffies(interval); |
| 3424 | if (unlikely(!interval)) |
| 3425 | interval = 1; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3426 | if (interval > HZ*NR_CPUS/10) |
| 3427 | interval = HZ*NR_CPUS/10; |
| 3428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 3430 | if (sd->flags & SD_SERIALIZE) { |
| 3431 | if (!spin_trylock(&balancing)) |
| 3432 | goto out; |
| 3433 | } |
| 3434 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 3435 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3436 | if (load_balance(cpu, rq, sd, idle, &balance)) { |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 3437 | /* |
| 3438 | * We've pulled tasks over so either we're no |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 3439 | * longer idle, or one of our SMT siblings is |
| 3440 | * not idle. |
| 3441 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 3442 | idle = CPU_NOT_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | } |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 3444 | sd->last_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | } |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 3446 | if (sd->flags & SD_SERIALIZE) |
| 3447 | spin_unlock(&balancing); |
| 3448 | out: |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3449 | if (time_after(next_balance, sd->last_balance + interval)) { |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 3450 | next_balance = sd->last_balance + interval; |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3451 | update_next_balance = 1; |
| 3452 | } |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 3453 | |
| 3454 | /* |
| 3455 | * Stop the load balance at this level. There is another |
| 3456 | * CPU in our sched group which is doing load balancing more |
| 3457 | * actively. |
| 3458 | */ |
| 3459 | if (!balance) |
| 3460 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | } |
Suresh Siddha | f549da8 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 3462 | |
| 3463 | /* |
| 3464 | * next_balance will be updated only when there is a need. |
| 3465 | * When the cpu is attached to null domain for ex, it will not be |
| 3466 | * updated. |
| 3467 | */ |
| 3468 | if (likely(update_next_balance)) |
| 3469 | rq->next_balance = next_balance; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | /* |
| 3473 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
| 3474 | * In CONFIG_NO_HZ case, the idle load balance owner will do the |
| 3475 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 3476 | */ |
| 3477 | static void run_rebalance_domains(struct softirq_action *h) |
| 3478 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3479 | int this_cpu = smp_processor_id(); |
| 3480 | struct rq *this_rq = cpu_rq(this_cpu); |
| 3481 | enum cpu_idle_type idle = this_rq->idle_at_tick ? |
| 3482 | CPU_IDLE : CPU_NOT_IDLE; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3483 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3484 | rebalance_domains(this_cpu, idle); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3485 | |
| 3486 | #ifdef CONFIG_NO_HZ |
| 3487 | /* |
| 3488 | * If this cpu is the owner for idle load balancing, then do the |
| 3489 | * balancing on behalf of the other idle cpus whose ticks are |
| 3490 | * stopped. |
| 3491 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3492 | if (this_rq->idle_at_tick && |
| 3493 | atomic_read(&nohz.load_balancer) == this_cpu) { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3494 | cpumask_t cpus = nohz.cpu_mask; |
| 3495 | struct rq *rq; |
| 3496 | int balance_cpu; |
| 3497 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3498 | cpu_clear(this_cpu, cpus); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3499 | for_each_cpu_mask(balance_cpu, cpus) { |
| 3500 | /* |
| 3501 | * If this cpu gets work to do, stop the load balancing |
| 3502 | * work being done for other cpus. Next load |
| 3503 | * balancing owner will pick it up. |
| 3504 | */ |
| 3505 | if (need_resched()) |
| 3506 | break; |
| 3507 | |
Oleg Nesterov | de0cf89 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 3508 | rebalance_domains(balance_cpu, CPU_IDLE); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3509 | |
| 3510 | rq = cpu_rq(balance_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3511 | if (time_after(this_rq->next_balance, rq->next_balance)) |
| 3512 | this_rq->next_balance = rq->next_balance; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3513 | } |
| 3514 | } |
| 3515 | #endif |
| 3516 | } |
| 3517 | |
| 3518 | /* |
| 3519 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. |
| 3520 | * |
| 3521 | * In case of CONFIG_NO_HZ, this is the place where we nominate a new |
| 3522 | * idle load balancing owner or decide to stop the periodic load balancing, |
| 3523 | * if the whole system is idle. |
| 3524 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3525 | static inline void trigger_load_balance(struct rq *rq, int cpu) |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3526 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3527 | #ifdef CONFIG_NO_HZ |
| 3528 | /* |
| 3529 | * If we were in the nohz mode recently and busy at the current |
| 3530 | * scheduler tick, then check if we need to nominate new idle |
| 3531 | * load balancer. |
| 3532 | */ |
| 3533 | if (rq->in_nohz_recently && !rq->idle_at_tick) { |
| 3534 | rq->in_nohz_recently = 0; |
| 3535 | |
| 3536 | if (atomic_read(&nohz.load_balancer) == cpu) { |
| 3537 | cpu_clear(cpu, nohz.cpu_mask); |
| 3538 | atomic_set(&nohz.load_balancer, -1); |
| 3539 | } |
| 3540 | |
| 3541 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 3542 | /* |
| 3543 | * simple selection for now: Nominate the |
| 3544 | * first cpu in the nohz list to be the next |
| 3545 | * ilb owner. |
| 3546 | * |
| 3547 | * TBD: Traverse the sched domains and nominate |
| 3548 | * the nearest cpu in the nohz.cpu_mask. |
| 3549 | */ |
| 3550 | int ilb = first_cpu(nohz.cpu_mask); |
| 3551 | |
| 3552 | if (ilb != NR_CPUS) |
| 3553 | resched_cpu(ilb); |
| 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | /* |
| 3558 | * If this cpu is idle and doing idle load balancing for all the |
| 3559 | * cpus with ticks stopped, is it time for that to stop? |
| 3560 | */ |
| 3561 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && |
| 3562 | cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 3563 | resched_cpu(cpu); |
| 3564 | return; |
| 3565 | } |
| 3566 | |
| 3567 | /* |
| 3568 | * If this cpu is idle and the idle load balancing is done by |
| 3569 | * someone else, then no need raise the SCHED_SOFTIRQ |
| 3570 | */ |
| 3571 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && |
| 3572 | cpu_isset(cpu, nohz.cpu_mask)) |
| 3573 | return; |
| 3574 | #endif |
| 3575 | if (time_after_eq(jiffies, rq->next_balance)) |
| 3576 | raise_softirq(SCHED_SOFTIRQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3578 | |
| 3579 | #else /* CONFIG_SMP */ |
| 3580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | /* |
| 3582 | * on UP we do not need to balance between CPUs: |
| 3583 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3584 | static inline void idle_balance(int cpu, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3585 | { |
| 3586 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | #endif |
| 3589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
| 3591 | |
| 3592 | EXPORT_PER_CPU_SYMBOL(kstat); |
| 3593 | |
| 3594 | /* |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3595 | * Return p->sum_exec_runtime plus any more ns on the sched_clock |
| 3596 | * 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] | 3597 | */ |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3598 | unsigned long long task_sched_runtime(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | unsigned long flags; |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3601 | u64 ns, delta_exec; |
| 3602 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3603 | |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3604 | rq = task_rq_lock(p, &flags); |
| 3605 | ns = p->se.sum_exec_runtime; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 3606 | if (task_current(rq, p)) { |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 3607 | update_rq_clock(rq); |
| 3608 | delta_exec = rq->clock - p->se.exec_start; |
Ingo Molnar | 41b86e9 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3609 | if ((s64)delta_exec > 0) |
| 3610 | ns += delta_exec; |
| 3611 | } |
| 3612 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | return ns; |
| 3615 | } |
| 3616 | |
| 3617 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | * Account user cpu time to a process. |
| 3619 | * @p: the process that the cpu time gets accounted to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | * @cputime: the cpu time spent in user space since the last update |
| 3621 | */ |
| 3622 | void account_user_time(struct task_struct *p, cputime_t cputime) |
| 3623 | { |
| 3624 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3625 | cputime64_t tmp; |
| 3626 | |
| 3627 | p->utime = cputime_add(p->utime, cputime); |
| 3628 | |
| 3629 | /* Add user time to cpustat. */ |
| 3630 | tmp = cputime_to_cputime64(cputime); |
| 3631 | if (TASK_NICE(p) > 0) |
| 3632 | cpustat->nice = cputime64_add(cpustat->nice, tmp); |
| 3633 | else |
| 3634 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 3635 | } |
| 3636 | |
| 3637 | /* |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 3638 | * Account guest cpu time to a process. |
| 3639 | * @p: the process that the cpu time gets accounted to |
| 3640 | * @cputime: the cpu time spent in virtual machine since the last update |
| 3641 | */ |
Adrian Bunk | f7402e0 | 2007-10-29 21:18:10 +0100 | [diff] [blame] | 3642 | static void account_guest_time(struct task_struct *p, cputime_t cputime) |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 3643 | { |
| 3644 | cputime64_t tmp; |
| 3645 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3646 | |
| 3647 | tmp = cputime_to_cputime64(cputime); |
| 3648 | |
| 3649 | p->utime = cputime_add(p->utime, cputime); |
| 3650 | p->gtime = cputime_add(p->gtime, cputime); |
| 3651 | |
| 3652 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 3653 | cpustat->guest = cputime64_add(cpustat->guest, tmp); |
| 3654 | } |
| 3655 | |
| 3656 | /* |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 3657 | * Account scaled user cpu time to a process. |
| 3658 | * @p: the process that the cpu time gets accounted to |
| 3659 | * @cputime: the cpu time spent in user space since the last update |
| 3660 | */ |
| 3661 | void account_user_time_scaled(struct task_struct *p, cputime_t cputime) |
| 3662 | { |
| 3663 | p->utimescaled = cputime_add(p->utimescaled, cputime); |
| 3664 | } |
| 3665 | |
| 3666 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | * Account system cpu time to a process. |
| 3668 | * @p: the process that the cpu time gets accounted to |
| 3669 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 3670 | * @cputime: the cpu time spent in kernel space since the last update |
| 3671 | */ |
| 3672 | void account_system_time(struct task_struct *p, int hardirq_offset, |
| 3673 | cputime_t cputime) |
| 3674 | { |
| 3675 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3676 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | cputime64_t tmp; |
| 3678 | |
Christian Borntraeger | 9778385 | 2007-11-15 20:57:39 +0100 | [diff] [blame] | 3679 | if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) |
| 3680 | return account_guest_time(p, cputime); |
Laurent Vivier | 94886b8 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 3681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | p->stime = cputime_add(p->stime, cputime); |
| 3683 | |
| 3684 | /* Add system time to cpustat. */ |
| 3685 | tmp = cputime_to_cputime64(cputime); |
| 3686 | if (hardirq_count() - hardirq_offset) |
| 3687 | cpustat->irq = cputime64_add(cpustat->irq, tmp); |
| 3688 | else if (softirq_count()) |
| 3689 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 3690 | else if (p != rq->idle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | cpustat->system = cputime64_add(cpustat->system, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 3692 | else if (atomic_read(&rq->nr_iowait) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 3694 | else |
| 3695 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 3696 | /* Account for system time used */ |
| 3697 | acct_update_integrals(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | /* |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 3701 | * Account scaled system cpu time to a process. |
| 3702 | * @p: the process that the cpu time gets accounted to |
| 3703 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 3704 | * @cputime: the cpu time spent in kernel space since the last update |
| 3705 | */ |
| 3706 | void account_system_time_scaled(struct task_struct *p, cputime_t cputime) |
| 3707 | { |
| 3708 | p->stimescaled = cputime_add(p->stimescaled, cputime); |
| 3709 | } |
| 3710 | |
| 3711 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | * Account for involuntary wait time. |
| 3713 | * @p: the process from which the cpu time has been stolen |
| 3714 | * @steal: the cpu time spent in involuntary wait |
| 3715 | */ |
| 3716 | void account_steal_time(struct task_struct *p, cputime_t steal) |
| 3717 | { |
| 3718 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3719 | cputime64_t tmp = cputime_to_cputime64(steal); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3720 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | |
| 3722 | if (p == rq->idle) { |
| 3723 | p->stime = cputime_add(p->stime, steal); |
| 3724 | if (atomic_read(&rq->nr_iowait) > 0) |
| 3725 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 3726 | else |
| 3727 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
Andrew Morton | cfb5285 | 2007-11-14 16:59:45 -0800 | [diff] [blame] | 3728 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | cpustat->steal = cputime64_add(cpustat->steal, tmp); |
| 3730 | } |
| 3731 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3732 | /* |
| 3733 | * This function gets called by the timer code, with HZ frequency. |
| 3734 | * We call it with interrupts disabled. |
| 3735 | * |
| 3736 | * It also gets called by the fork code, when changing the parent's |
| 3737 | * timeslices. |
| 3738 | */ |
| 3739 | void scheduler_tick(void) |
| 3740 | { |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3741 | int cpu = smp_processor_id(); |
| 3742 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3743 | struct task_struct *curr = rq->curr; |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 3744 | u64 next_tick = rq->tick_timestamp + TICK_NSEC; |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3745 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3746 | spin_lock(&rq->lock); |
Ingo Molnar | 546fe3c | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 3747 | __update_rq_clock(rq); |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 3748 | /* |
| 3749 | * Let rq->clock advance by at least TICK_NSEC: |
| 3750 | */ |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 3751 | if (unlikely(rq->clock < next_tick)) { |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 3752 | rq->clock = next_tick; |
Guillaume Chazarain | cc203d2 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 3753 | rq->clock_underflows++; |
| 3754 | } |
Ingo Molnar | 529c772 | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 3755 | rq->tick_timestamp = rq->clock; |
Ingo Molnar | f1a438d | 2007-08-09 11:16:45 +0200 | [diff] [blame] | 3756 | update_cpu_load(rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3757 | curr->sched_class->task_tick(rq, curr, 0); |
| 3758 | update_sched_rt_period(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3759 | spin_unlock(&rq->lock); |
| 3760 | |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 3761 | #ifdef CONFIG_SMP |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3762 | rq->idle_at_tick = idle_cpu(cpu); |
| 3763 | trigger_load_balance(rq, cpu); |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 3764 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | } |
| 3766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) |
| 3768 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 3769 | void add_preempt_count(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | { |
| 3771 | /* |
| 3772 | * Underflow? |
| 3773 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3774 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
| 3775 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | preempt_count() += val; |
| 3777 | /* |
| 3778 | * Spinlock count overflowing soon? |
| 3779 | */ |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 3780 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
| 3781 | PREEMPT_MASK - 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3782 | } |
| 3783 | EXPORT_SYMBOL(add_preempt_count); |
| 3784 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 3785 | void sub_preempt_count(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | { |
| 3787 | /* |
| 3788 | * Underflow? |
| 3789 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3790 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
| 3791 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3792 | /* |
| 3793 | * Is the spinlock portion underflowing? |
| 3794 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3795 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
| 3796 | !(preempt_count() & PREEMPT_MASK))) |
| 3797 | return; |
| 3798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | preempt_count() -= val; |
| 3800 | } |
| 3801 | EXPORT_SYMBOL(sub_preempt_count); |
| 3802 | |
| 3803 | #endif |
| 3804 | |
| 3805 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3806 | * Print scheduling while atomic bug: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3807 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3808 | static noinline void __schedule_bug(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | { |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 3810 | struct pt_regs *regs = get_irq_regs(); |
| 3811 | |
| 3812 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
| 3813 | prev->comm, prev->pid, preempt_count()); |
| 3814 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3815 | debug_show_held_locks(prev); |
| 3816 | if (irqs_disabled()) |
| 3817 | print_irqtrace_events(prev); |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 3818 | |
| 3819 | if (regs) |
| 3820 | show_regs(regs); |
| 3821 | else |
| 3822 | dump_stack(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3825 | /* |
| 3826 | * Various schedule()-time debugging checks and statistics: |
| 3827 | */ |
| 3828 | static inline void schedule_debug(struct task_struct *prev) |
| 3829 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3831 | * Test if we are atomic. Since do_exit() needs to call into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | * schedule() atomically, we ignore that path for now. |
| 3833 | * Otherwise, whine if we are scheduling when we should not be. |
| 3834 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3835 | if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state)) |
| 3836 | __schedule_bug(prev); |
| 3837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
| 3839 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3840 | schedstat_inc(this_rq(), sched_count); |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 3841 | #ifdef CONFIG_SCHEDSTATS |
| 3842 | if (unlikely(prev->lock_depth >= 0)) { |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3843 | schedstat_inc(this_rq(), bkl_count); |
| 3844 | schedstat_inc(prev, sched_info.bkl_count); |
Ingo Molnar | b8efb56 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 3845 | } |
| 3846 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | /* |
| 3850 | * Pick up the highest-prio task: |
| 3851 | */ |
| 3852 | static inline struct task_struct * |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 3853 | pick_next_task(struct rq *rq, struct task_struct *prev) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3854 | { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3855 | const struct sched_class *class; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3856 | struct task_struct *p; |
| 3857 | |
| 3858 | /* |
| 3859 | * Optimization: we know that if all tasks are in |
| 3860 | * the fair class we can call that function directly: |
| 3861 | */ |
| 3862 | if (likely(rq->nr_running == rq->cfs.nr_running)) { |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3863 | p = fair_sched_class.pick_next_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3864 | if (likely(p)) |
| 3865 | return p; |
| 3866 | } |
| 3867 | |
| 3868 | class = sched_class_highest; |
| 3869 | for ( ; ; ) { |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3870 | p = class->pick_next_task(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3871 | if (p) |
| 3872 | return p; |
| 3873 | /* |
| 3874 | * Will never be NULL as the idle class always |
| 3875 | * returns a non-NULL p: |
| 3876 | */ |
| 3877 | class = class->next; |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | /* |
| 3882 | * schedule() is the main scheduler function. |
| 3883 | */ |
| 3884 | asmlinkage void __sched schedule(void) |
| 3885 | { |
| 3886 | struct task_struct *prev, *next; |
| 3887 | long *switch_count; |
| 3888 | struct rq *rq; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3889 | int cpu; |
| 3890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | need_resched: |
| 3892 | preempt_disable(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3893 | cpu = smp_processor_id(); |
| 3894 | rq = cpu_rq(cpu); |
| 3895 | rcu_qsctr_inc(cpu); |
| 3896 | prev = rq->curr; |
| 3897 | switch_count = &prev->nivcsw; |
| 3898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3899 | release_kernel_lock(prev); |
| 3900 | need_resched_nonpreemptible: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3902 | schedule_debug(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3903 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3904 | hrtick_clear(rq); |
| 3905 | |
Ingo Molnar | 1e81995 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 3906 | /* |
| 3907 | * Do the rq-clock update outside the rq lock: |
| 3908 | */ |
| 3909 | local_irq_disable(); |
Ingo Molnar | c1b3da3 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 3910 | __update_rq_clock(rq); |
Ingo Molnar | 1e81995 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 3911 | spin_lock(&rq->lock); |
| 3912 | clear_tsk_need_resched(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3914 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
| 3915 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && |
| 3916 | unlikely(signal_pending(prev)))) { |
| 3917 | prev->state = TASK_RUNNING; |
| 3918 | } else { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 3919 | deactivate_task(rq, prev, 1); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3920 | } |
| 3921 | switch_count = &prev->nvcsw; |
| 3922 | } |
| 3923 | |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 3924 | #ifdef CONFIG_SMP |
| 3925 | if (prev->sched_class->pre_schedule) |
| 3926 | prev->sched_class->pre_schedule(rq, prev); |
| 3927 | #endif |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 3928 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3929 | if (unlikely(!rq->nr_running)) |
| 3930 | idle_balance(cpu, rq); |
| 3931 | |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 3932 | prev->sched_class->put_prev_task(rq, prev); |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 3933 | next = pick_next_task(rq, prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | |
| 3935 | sched_info_switch(prev, next); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | if (likely(prev != next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3938 | rq->nr_switches++; |
| 3939 | rq->curr = next; |
| 3940 | ++*switch_count; |
| 3941 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3942 | context_switch(rq, prev, next); /* unlocks the rq */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3943 | /* |
| 3944 | * the context switch might have flipped the stack from under |
| 3945 | * us, hence refresh the local variables. |
| 3946 | */ |
| 3947 | cpu = smp_processor_id(); |
| 3948 | rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | } else |
| 3950 | spin_unlock_irq(&rq->lock); |
| 3951 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3952 | hrtick_set(rq); |
| 3953 | |
| 3954 | if (unlikely(reacquire_kernel_lock(current) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | goto need_resched_nonpreemptible; |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | preempt_enable_no_resched(); |
| 3958 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 3959 | goto need_resched; |
| 3960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 | EXPORT_SYMBOL(schedule); |
| 3962 | |
| 3963 | #ifdef CONFIG_PREEMPT |
| 3964 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 3965 | * this is the entry point to schedule() from in-kernel preemption |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3966 | * off of preempt_enable. Kernel preemptions off return from interrupt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | * occur there and call schedule directly. |
| 3968 | */ |
| 3969 | asmlinkage void __sched preempt_schedule(void) |
| 3970 | { |
| 3971 | struct thread_info *ti = current_thread_info(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | struct task_struct *task = current; |
| 3973 | int saved_lock_depth; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 3974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | /* |
| 3976 | * If there is a non-zero preempt_count or interrupts are disabled, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3977 | * we do not want to preempt the current task. Just return.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3978 | */ |
Nick Piggin | beed33a | 2006-10-11 01:21:52 -0700 | [diff] [blame] | 3979 | if (likely(ti->preempt_count || irqs_disabled())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3980 | return; |
| 3981 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3982 | do { |
| 3983 | add_preempt_count(PREEMPT_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3985 | /* |
| 3986 | * We keep the big kernel semaphore locked, but we |
| 3987 | * clear ->lock_depth so that schedule() doesnt |
| 3988 | * auto-release the semaphore: |
| 3989 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3990 | saved_lock_depth = task->lock_depth; |
| 3991 | task->lock_depth = -1; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3992 | schedule(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3993 | task->lock_depth = saved_lock_depth; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3994 | sub_preempt_count(PREEMPT_ACTIVE); |
| 3995 | |
| 3996 | /* |
| 3997 | * Check again in case we missed a preemption opportunity |
| 3998 | * between schedule and now. |
| 3999 | */ |
| 4000 | barrier(); |
| 4001 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | EXPORT_SYMBOL(preempt_schedule); |
| 4004 | |
| 4005 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4006 | * this is the entry point to schedule() from kernel preemption |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | * off of irq context. |
| 4008 | * Note, that this is called and return with irqs disabled. This will |
| 4009 | * protect us against recursive calling from irq. |
| 4010 | */ |
| 4011 | asmlinkage void __sched preempt_schedule_irq(void) |
| 4012 | { |
| 4013 | struct thread_info *ti = current_thread_info(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | struct task_struct *task = current; |
| 4015 | int saved_lock_depth; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 4016 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4017 | /* Catch callers which need to be fixed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
| 4019 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4020 | do { |
| 4021 | add_preempt_count(PREEMPT_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4022 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4023 | /* |
| 4024 | * We keep the big kernel semaphore locked, but we |
| 4025 | * clear ->lock_depth so that schedule() doesnt |
| 4026 | * auto-release the semaphore: |
| 4027 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4028 | saved_lock_depth = task->lock_depth; |
| 4029 | task->lock_depth = -1; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4030 | local_irq_enable(); |
| 4031 | schedule(); |
| 4032 | local_irq_disable(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4033 | task->lock_depth = saved_lock_depth; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4034 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4035 | |
| 4036 | /* |
| 4037 | * Check again in case we missed a preemption opportunity |
| 4038 | * between schedule and now. |
| 4039 | */ |
| 4040 | barrier(); |
| 4041 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | #endif /* CONFIG_PREEMPT */ |
| 4045 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4046 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, |
| 4047 | void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4048 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4049 | return try_to_wake_up(curr->private, mode, sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | EXPORT_SYMBOL(default_wake_function); |
| 4052 | |
| 4053 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4054 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just |
| 4055 | * 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] | 4056 | * number) then we wake all the non-exclusive tasks and one exclusive task. |
| 4057 | * |
| 4058 | * 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] | 4059 | * 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] | 4060 | * zero in this (rare) case, and we handle it by continuing to scan the queue. |
| 4061 | */ |
| 4062 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, |
| 4063 | int nr_exclusive, int sync, void *key) |
| 4064 | { |
Matthias Kaehlcke | 2e45874 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 4065 | wait_queue_t *curr, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | |
Matthias Kaehlcke | 2e45874 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 4067 | list_for_each_entry_safe(curr, next, &q->task_list, task_list) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4068 | unsigned flags = curr->flags; |
| 4069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | if (curr->func(curr, mode, sync, key) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4071 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | break; |
| 4073 | } |
| 4074 | } |
| 4075 | |
| 4076 | /** |
| 4077 | * __wake_up - wake up threads blocked on a waitqueue. |
| 4078 | * @q: the waitqueue |
| 4079 | * @mode: which threads |
| 4080 | * @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] | 4081 | * @key: is directly passed to the wakeup function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4083 | void __wake_up(wait_queue_head_t *q, unsigned int mode, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4084 | int nr_exclusive, void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | { |
| 4086 | unsigned long flags; |
| 4087 | |
| 4088 | spin_lock_irqsave(&q->lock, flags); |
| 4089 | __wake_up_common(q, mode, nr_exclusive, 0, key); |
| 4090 | spin_unlock_irqrestore(&q->lock, flags); |
| 4091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | EXPORT_SYMBOL(__wake_up); |
| 4093 | |
| 4094 | /* |
| 4095 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. |
| 4096 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4097 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | { |
| 4099 | __wake_up_common(q, mode, 1, 0, NULL); |
| 4100 | } |
| 4101 | |
| 4102 | /** |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 4103 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 | * @q: the waitqueue |
| 4105 | * @mode: which threads |
| 4106 | * @nr_exclusive: how many wake-one or wake-many threads to wake up |
| 4107 | * |
| 4108 | * The sync wakeup differs that the waker knows that it will schedule |
| 4109 | * away soon, so while the target thread will be woken up, it will not |
| 4110 | * be migrated to another CPU - ie. the two threads are 'synchronized' |
| 4111 | * with each other. This can prevent needless bouncing between CPUs. |
| 4112 | * |
| 4113 | * On UP it can prevent extra preemption. |
| 4114 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 4115 | void |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4116 | __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] | 4117 | { |
| 4118 | unsigned long flags; |
| 4119 | int sync = 1; |
| 4120 | |
| 4121 | if (unlikely(!q)) |
| 4122 | return; |
| 4123 | |
| 4124 | if (unlikely(!nr_exclusive)) |
| 4125 | sync = 0; |
| 4126 | |
| 4127 | spin_lock_irqsave(&q->lock, flags); |
| 4128 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); |
| 4129 | spin_unlock_irqrestore(&q->lock, flags); |
| 4130 | } |
| 4131 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ |
| 4132 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4133 | void complete(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | { |
| 4135 | unsigned long flags; |
| 4136 | |
| 4137 | spin_lock_irqsave(&x->wait.lock, flags); |
| 4138 | x->done++; |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 4139 | __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 4141 | } |
| 4142 | EXPORT_SYMBOL(complete); |
| 4143 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4144 | void complete_all(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | { |
| 4146 | unsigned long flags; |
| 4147 | |
| 4148 | spin_lock_irqsave(&x->wait.lock, flags); |
| 4149 | x->done += UINT_MAX/2; |
Matthew Wilcox | d9514f6 | 2007-12-06 11:07:07 -0500 | [diff] [blame] | 4150 | __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 4152 | } |
| 4153 | EXPORT_SYMBOL(complete_all); |
| 4154 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4155 | static inline long __sched |
| 4156 | do_wait_for_common(struct completion *x, long timeout, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | if (!x->done) { |
| 4159 | DECLARE_WAITQUEUE(wait, current); |
| 4160 | |
| 4161 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 4162 | __add_wait_queue_tail(&x->wait, &wait); |
| 4163 | do { |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 4164 | if ((state == TASK_INTERRUPTIBLE && |
| 4165 | signal_pending(current)) || |
| 4166 | (state == TASK_KILLABLE && |
| 4167 | fatal_signal_pending(current))) { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4168 | __remove_wait_queue(&x->wait, &wait); |
| 4169 | return -ERESTARTSYS; |
| 4170 | } |
| 4171 | __set_current_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | spin_unlock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4173 | timeout = schedule_timeout(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | spin_lock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4175 | if (!timeout) { |
| 4176 | __remove_wait_queue(&x->wait, &wait); |
| 4177 | return timeout; |
| 4178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | } while (!x->done); |
| 4180 | __remove_wait_queue(&x->wait, &wait); |
| 4181 | } |
| 4182 | x->done--; |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4183 | return timeout; |
| 4184 | } |
| 4185 | |
| 4186 | static long __sched |
| 4187 | wait_for_common(struct completion *x, long timeout, int state) |
| 4188 | { |
| 4189 | might_sleep(); |
| 4190 | |
| 4191 | spin_lock_irq(&x->wait.lock); |
| 4192 | timeout = do_wait_for_common(x, timeout, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | spin_unlock_irq(&x->wait.lock); |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4194 | return timeout; |
| 4195 | } |
| 4196 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4197 | void __sched wait_for_completion(struct completion *x) |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4198 | { |
| 4199 | wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4200 | } |
| 4201 | EXPORT_SYMBOL(wait_for_completion); |
| 4202 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4203 | unsigned long __sched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) |
| 4205 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4206 | return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | } |
| 4208 | EXPORT_SYMBOL(wait_for_completion_timeout); |
| 4209 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4210 | int __sched wait_for_completion_interruptible(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | { |
Andi Kleen | 51e9799 | 2007-10-18 21:32:55 +0200 | [diff] [blame] | 4212 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE); |
| 4213 | if (t == -ERESTARTSYS) |
| 4214 | return t; |
| 4215 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | } |
| 4217 | EXPORT_SYMBOL(wait_for_completion_interruptible); |
| 4218 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 4219 | unsigned long __sched |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | wait_for_completion_interruptible_timeout(struct completion *x, |
| 4221 | unsigned long timeout) |
| 4222 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4223 | return wait_for_common(x, timeout, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | } |
| 4225 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); |
| 4226 | |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 4227 | int __sched wait_for_completion_killable(struct completion *x) |
| 4228 | { |
| 4229 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE); |
| 4230 | if (t == -ERESTARTSYS) |
| 4231 | return t; |
| 4232 | return 0; |
| 4233 | } |
| 4234 | EXPORT_SYMBOL(wait_for_completion_killable); |
| 4235 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4236 | static long __sched |
| 4237 | sleep_on_common(wait_queue_head_t *q, int state, long timeout) |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4238 | { |
| 4239 | unsigned long flags; |
| 4240 | wait_queue_t wait; |
| 4241 | |
| 4242 | init_waitqueue_entry(&wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4244 | __set_current_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4246 | spin_lock_irqsave(&q->lock, flags); |
| 4247 | __add_wait_queue(q, &wait); |
| 4248 | spin_unlock(&q->lock); |
| 4249 | timeout = schedule_timeout(timeout); |
| 4250 | spin_lock_irq(&q->lock); |
| 4251 | __remove_wait_queue(q, &wait); |
| 4252 | spin_unlock_irqrestore(&q->lock, flags); |
| 4253 | |
| 4254 | return timeout; |
| 4255 | } |
| 4256 | |
| 4257 | void __sched interruptible_sleep_on(wait_queue_head_t *q) |
| 4258 | { |
| 4259 | sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | EXPORT_SYMBOL(interruptible_sleep_on); |
| 4262 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4263 | long __sched |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4264 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4266 | return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
| 4269 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4270 | void __sched sleep_on(wait_queue_head_t *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4272 | sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | EXPORT_SYMBOL(sleep_on); |
| 4275 | |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 4276 | long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | { |
Andi Kleen | 8cbbe86 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4278 | return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | EXPORT_SYMBOL(sleep_on_timeout); |
| 4281 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4282 | #ifdef CONFIG_RT_MUTEXES |
| 4283 | |
| 4284 | /* |
| 4285 | * rt_mutex_setprio - set the current priority of a task |
| 4286 | * @p: task |
| 4287 | * @prio: prio value (kernel-internal form) |
| 4288 | * |
| 4289 | * This function changes the 'effective' priority of a task. It does |
| 4290 | * not touch ->normal_prio like __setscheduler(). |
| 4291 | * |
| 4292 | * Used by the rt_mutex code to implement priority inheritance logic. |
| 4293 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4294 | void rt_mutex_setprio(struct task_struct *p, int prio) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4295 | { |
| 4296 | unsigned long flags; |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4297 | int oldprio, on_rq, running; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4298 | struct rq *rq; |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4299 | const struct sched_class *prev_class = p->sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4300 | |
| 4301 | BUG_ON(prio < 0 || prio > MAX_PRIO); |
| 4302 | |
| 4303 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4304 | update_rq_clock(rq); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4305 | |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4306 | oldprio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4307 | on_rq = p->se.on_rq; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 4308 | running = task_current(rq, p); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4309 | if (on_rq) { |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4310 | dequeue_task(rq, p, 0); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4311 | if (running) |
| 4312 | p->sched_class->put_prev_task(rq, p); |
| 4313 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4314 | |
| 4315 | if (rt_prio(prio)) |
| 4316 | p->sched_class = &rt_sched_class; |
| 4317 | else |
| 4318 | p->sched_class = &fair_sched_class; |
| 4319 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4320 | p->prio = prio; |
| 4321 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4322 | if (on_rq) { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4323 | if (running) |
| 4324 | p->sched_class->set_curr_task(rq); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4325 | |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4326 | enqueue_task(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4327 | |
| 4328 | check_class_changed(rq, p, prev_class, oldprio, running); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4329 | } |
| 4330 | task_rq_unlock(rq, &flags); |
| 4331 | } |
| 4332 | |
| 4333 | #endif |
| 4334 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4335 | void set_user_nice(struct task_struct *p, long nice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4336 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4337 | int old_prio, delta, on_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4339 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | |
| 4341 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) |
| 4342 | return; |
| 4343 | /* |
| 4344 | * We have to be careful, if called from sys_setpriority(), |
| 4345 | * the task might be in the middle of scheduling on another CPU. |
| 4346 | */ |
| 4347 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4348 | update_rq_clock(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | /* |
| 4350 | * The RT priorities are set via sched_setscheduler(), but we still |
| 4351 | * allow the 'normal' nice value to be set - but as expected |
| 4352 | * it wont have any effect on scheduling until the task is |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4353 | * SCHED_FIFO/SCHED_RR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | */ |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4355 | if (task_has_rt_policy(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | p->static_prio = NICE_TO_PRIO(nice); |
| 4357 | goto out_unlock; |
| 4358 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4359 | on_rq = p->se.on_rq; |
Srivatsa Vaddagiri | 58e2d4c | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 4360 | if (on_rq) |
Ingo Molnar | 69be72c | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4361 | dequeue_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | p->static_prio = NICE_TO_PRIO(nice); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 4364 | set_load_weight(p); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4365 | old_prio = p->prio; |
| 4366 | p->prio = effective_prio(p); |
| 4367 | delta = p->prio - old_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4369 | if (on_rq) { |
Ingo Molnar | 8159f87 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4370 | enqueue_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | /* |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4372 | * If the task increased its priority or is running and |
| 4373 | * lowered its priority, then reschedule its CPU: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | */ |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4375 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | resched_task(rq->curr); |
| 4377 | } |
| 4378 | out_unlock: |
| 4379 | task_rq_unlock(rq, &flags); |
| 4380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | EXPORT_SYMBOL(set_user_nice); |
| 4382 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4383 | /* |
| 4384 | * can_nice - check if a task can reduce its nice value |
| 4385 | * @p: task |
| 4386 | * @nice: nice value |
| 4387 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4388 | int can_nice(const struct task_struct *p, const int nice) |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4389 | { |
Matt Mackall | 024f474 | 2005-08-18 11:24:19 -0700 | [diff] [blame] | 4390 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
| 4391 | int nice_rlim = 20 - nice; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4392 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4393 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
| 4394 | capable(CAP_SYS_NICE)); |
| 4395 | } |
| 4396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | #ifdef __ARCH_WANT_SYS_NICE |
| 4398 | |
| 4399 | /* |
| 4400 | * sys_nice - change the priority of the current process. |
| 4401 | * @increment: priority increment |
| 4402 | * |
| 4403 | * sys_setpriority is a more generic, but much slower function that |
| 4404 | * does similar things. |
| 4405 | */ |
| 4406 | asmlinkage long sys_nice(int increment) |
| 4407 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4408 | long nice, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | |
| 4410 | /* |
| 4411 | * Setpriority might change our priority at the same moment. |
| 4412 | * We don't have to worry. Conceptually one call occurs first |
| 4413 | * and we have a single winner. |
| 4414 | */ |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4415 | if (increment < -40) |
| 4416 | increment = -40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | if (increment > 40) |
| 4418 | increment = 40; |
| 4419 | |
| 4420 | nice = PRIO_TO_NICE(current->static_prio) + increment; |
| 4421 | if (nice < -20) |
| 4422 | nice = -20; |
| 4423 | if (nice > 19) |
| 4424 | nice = 19; |
| 4425 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4426 | if (increment < 0 && !can_nice(current, nice)) |
| 4427 | return -EPERM; |
| 4428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | retval = security_task_setnice(current, nice); |
| 4430 | if (retval) |
| 4431 | return retval; |
| 4432 | |
| 4433 | set_user_nice(current, nice); |
| 4434 | return 0; |
| 4435 | } |
| 4436 | |
| 4437 | #endif |
| 4438 | |
| 4439 | /** |
| 4440 | * task_prio - return the priority value of a given task. |
| 4441 | * @p: the task in question. |
| 4442 | * |
| 4443 | * This is the priority value as seen by users in /proc. |
| 4444 | * RT tasks are offset by -200. Normal tasks are centered |
| 4445 | * around 0, value goes from -16 to +15. |
| 4446 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4447 | int task_prio(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | { |
| 4449 | return p->prio - MAX_RT_PRIO; |
| 4450 | } |
| 4451 | |
| 4452 | /** |
| 4453 | * task_nice - return the nice value of a given task. |
| 4454 | * @p: the task in question. |
| 4455 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4456 | int task_nice(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | { |
| 4458 | return TASK_NICE(p); |
| 4459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | EXPORT_SYMBOL_GPL(task_nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | |
| 4462 | /** |
| 4463 | * idle_cpu - is a given cpu idle currently? |
| 4464 | * @cpu: the processor in question. |
| 4465 | */ |
| 4466 | int idle_cpu(int cpu) |
| 4467 | { |
| 4468 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; |
| 4469 | } |
| 4470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | /** |
| 4472 | * idle_task - return the idle task for a given cpu. |
| 4473 | * @cpu: the processor in question. |
| 4474 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4475 | struct task_struct *idle_task(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | { |
| 4477 | return cpu_rq(cpu)->idle; |
| 4478 | } |
| 4479 | |
| 4480 | /** |
| 4481 | * find_process_by_pid - find a process with a matching PID value. |
| 4482 | * @pid: the pid in question. |
| 4483 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 4484 | static struct task_struct *find_process_by_pid(pid_t pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | { |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 4486 | return pid ? find_task_by_vpid(pid) : current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | /* Actually do priority change: must hold rq lock. */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4490 | static void |
| 4491 | __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4493 | BUG_ON(p->se.on_rq); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | p->policy = policy; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4496 | switch (p->policy) { |
| 4497 | case SCHED_NORMAL: |
| 4498 | case SCHED_BATCH: |
| 4499 | case SCHED_IDLE: |
| 4500 | p->sched_class = &fair_sched_class; |
| 4501 | break; |
| 4502 | case SCHED_FIFO: |
| 4503 | case SCHED_RR: |
| 4504 | p->sched_class = &rt_sched_class; |
| 4505 | break; |
| 4506 | } |
| 4507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | p->rt_priority = prio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4509 | p->normal_prio = normal_prio(p); |
| 4510 | /* we are holding p->pi_lock already */ |
| 4511 | p->prio = rt_mutex_getprio(p); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 4512 | set_load_weight(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 4516 | * 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] | 4517 | * @p: the task in question. |
| 4518 | * @policy: new policy. |
| 4519 | * @param: structure containing the new RT priority. |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4520 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 4521 | * NOTE that the task may be already dead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4523 | int sched_setscheduler(struct task_struct *p, int policy, |
| 4524 | struct sched_param *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4526 | int retval, oldprio, oldpolicy = -1, on_rq, running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | unsigned long flags; |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4528 | const struct sched_class *prev_class = p->sched_class; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4529 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | |
Steven Rostedt | 66e5393 | 2006-06-27 02:54:44 -0700 | [diff] [blame] | 4531 | /* may grab non-irq protected spin_locks */ |
| 4532 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4533 | recheck: |
| 4534 | /* double check policy once rq lock held */ |
| 4535 | if (policy < 0) |
| 4536 | policy = oldpolicy = p->policy; |
| 4537 | else if (policy != SCHED_FIFO && policy != SCHED_RR && |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4538 | policy != SCHED_NORMAL && policy != SCHED_BATCH && |
| 4539 | policy != SCHED_IDLE) |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4540 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | /* |
| 4542 | * Valid priorities for SCHED_FIFO and SCHED_RR are |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4543 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
| 4544 | * SCHED_BATCH and SCHED_IDLE is 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | */ |
| 4546 | if (param->sched_priority < 0 || |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4547 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
Steven Rostedt | d46523e | 2005-07-25 16:28:39 -0400 | [diff] [blame] | 4548 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | return -EINVAL; |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4550 | if (rt_policy(policy) != (param->sched_priority != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | return -EINVAL; |
| 4552 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4553 | /* |
| 4554 | * Allow unprivileged RT tasks to decrease priority: |
| 4555 | */ |
| 4556 | if (!capable(CAP_SYS_NICE)) { |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4557 | if (rt_policy(policy)) { |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4558 | unsigned long rlim_rtprio; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4559 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4560 | if (!lock_task_sighand(p, &flags)) |
| 4561 | return -ESRCH; |
| 4562 | rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur; |
| 4563 | unlock_task_sighand(p, &flags); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4564 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4565 | /* can't set/change the rt policy */ |
| 4566 | if (policy != p->policy && !rlim_rtprio) |
| 4567 | return -EPERM; |
| 4568 | |
| 4569 | /* can't increase priority */ |
| 4570 | if (param->sched_priority > p->rt_priority && |
| 4571 | param->sched_priority > rlim_rtprio) |
| 4572 | return -EPERM; |
| 4573 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4574 | /* |
| 4575 | * Like positive nice levels, dont allow tasks to |
| 4576 | * move out of SCHED_IDLE either: |
| 4577 | */ |
| 4578 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) |
| 4579 | return -EPERM; |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4580 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4581 | /* can't change other user's priorities */ |
| 4582 | if ((current->euid != p->euid) && |
| 4583 | (current->euid != p->uid)) |
| 4584 | return -EPERM; |
| 4585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame^] | 4587 | #ifdef CONFIG_RT_GROUP_SCHED |
| 4588 | /* |
| 4589 | * Do not allow realtime tasks into groups that have no runtime |
| 4590 | * assigned. |
| 4591 | */ |
| 4592 | if (rt_policy(policy) && task_group(p)->rt_runtime == 0) |
| 4593 | return -EPERM; |
| 4594 | #endif |
| 4595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | retval = security_task_setscheduler(p, policy, param); |
| 4597 | if (retval) |
| 4598 | return retval; |
| 4599 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4600 | * make sure no PI-waiters arrive (or leave) while we are |
| 4601 | * changing the priority of the task: |
| 4602 | */ |
| 4603 | spin_lock_irqsave(&p->pi_lock, flags); |
| 4604 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | * To be able to change p->policy safely, the apropriate |
| 4606 | * runqueue lock must be held. |
| 4607 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4608 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | /* recheck policy now with rq lock held */ |
| 4610 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
| 4611 | policy = oldpolicy = -1; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4612 | __task_rq_unlock(rq); |
| 4613 | spin_unlock_irqrestore(&p->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | goto recheck; |
| 4615 | } |
Ingo Molnar | 2daa357 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 4616 | update_rq_clock(rq); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4617 | on_rq = p->se.on_rq; |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 4618 | running = task_current(rq, p); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4619 | if (on_rq) { |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4620 | deactivate_task(rq, p, 0); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4621 | if (running) |
| 4622 | p->sched_class->put_prev_task(rq, p); |
| 4623 | } |
Dmitry Adamushko | f6b53205 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | oldprio = p->prio; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4626 | __setscheduler(rq, p, policy, param->sched_priority); |
Dmitry Adamushko | f6b53205 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4627 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4628 | if (on_rq) { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4629 | if (running) |
| 4630 | p->sched_class->set_curr_task(rq); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4631 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4632 | activate_task(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4633 | |
| 4634 | check_class_changed(rq, p, prev_class, oldprio, running); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4636 | __task_rq_unlock(rq); |
| 4637 | spin_unlock_irqrestore(&p->pi_lock, flags); |
| 4638 | |
Thomas Gleixner | 95e02ca | 2006-06-27 02:55:02 -0700 | [diff] [blame] | 4639 | rt_mutex_adjust_pi(p); |
| 4640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | return 0; |
| 4642 | } |
| 4643 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
| 4644 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4645 | static int |
| 4646 | 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] | 4647 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | struct sched_param lparam; |
| 4649 | struct task_struct *p; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4650 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4651 | |
| 4652 | if (!param || pid < 0) |
| 4653 | return -EINVAL; |
| 4654 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) |
| 4655 | return -EFAULT; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4656 | |
| 4657 | rcu_read_lock(); |
| 4658 | retval = -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | p = find_process_by_pid(pid); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4660 | if (p != NULL) |
| 4661 | retval = sched_setscheduler(p, policy, &lparam); |
| 4662 | rcu_read_unlock(); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | return retval; |
| 4665 | } |
| 4666 | |
| 4667 | /** |
| 4668 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority |
| 4669 | * @pid: the pid in question. |
| 4670 | * @policy: new policy. |
| 4671 | * @param: structure containing the new RT priority. |
| 4672 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4673 | asmlinkage long |
| 4674 | 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] | 4675 | { |
Jason Baron | c21761f | 2006-01-18 17:43:03 -0800 | [diff] [blame] | 4676 | /* negative values for policy are not valid */ |
| 4677 | if (policy < 0) |
| 4678 | return -EINVAL; |
| 4679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | return do_sched_setscheduler(pid, policy, param); |
| 4681 | } |
| 4682 | |
| 4683 | /** |
| 4684 | * sys_sched_setparam - set/change the RT priority of a thread |
| 4685 | * @pid: the pid in question. |
| 4686 | * @param: structure containing the new RT priority. |
| 4687 | */ |
| 4688 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) |
| 4689 | { |
| 4690 | return do_sched_setscheduler(pid, -1, param); |
| 4691 | } |
| 4692 | |
| 4693 | /** |
| 4694 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread |
| 4695 | * @pid: the pid in question. |
| 4696 | */ |
| 4697 | asmlinkage long sys_sched_getscheduler(pid_t pid) |
| 4698 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4699 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4700 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | |
| 4702 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4703 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | |
| 4705 | retval = -ESRCH; |
| 4706 | read_lock(&tasklist_lock); |
| 4707 | p = find_process_by_pid(pid); |
| 4708 | if (p) { |
| 4709 | retval = security_task_getscheduler(p); |
| 4710 | if (!retval) |
| 4711 | retval = p->policy; |
| 4712 | } |
| 4713 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | return retval; |
| 4715 | } |
| 4716 | |
| 4717 | /** |
| 4718 | * sys_sched_getscheduler - get the RT priority of a thread |
| 4719 | * @pid: the pid in question. |
| 4720 | * @param: structure containing the RT priority. |
| 4721 | */ |
| 4722 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) |
| 4723 | { |
| 4724 | struct sched_param lp; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4725 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4726 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | |
| 4728 | if (!param || pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 4729 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | |
| 4731 | read_lock(&tasklist_lock); |
| 4732 | p = find_process_by_pid(pid); |
| 4733 | retval = -ESRCH; |
| 4734 | if (!p) |
| 4735 | goto out_unlock; |
| 4736 | |
| 4737 | retval = security_task_getscheduler(p); |
| 4738 | if (retval) |
| 4739 | goto out_unlock; |
| 4740 | |
| 4741 | lp.sched_priority = p->rt_priority; |
| 4742 | read_unlock(&tasklist_lock); |
| 4743 | |
| 4744 | /* |
| 4745 | * This one might sleep, we cannot do it with a spinlock held ... |
| 4746 | */ |
| 4747 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; |
| 4748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | return retval; |
| 4750 | |
| 4751 | out_unlock: |
| 4752 | read_unlock(&tasklist_lock); |
| 4753 | return retval; |
| 4754 | } |
| 4755 | |
| 4756 | long sched_setaffinity(pid_t pid, cpumask_t new_mask) |
| 4757 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | cpumask_t cpus_allowed; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4759 | struct task_struct *p; |
| 4760 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4762 | get_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | read_lock(&tasklist_lock); |
| 4764 | |
| 4765 | p = find_process_by_pid(pid); |
| 4766 | if (!p) { |
| 4767 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4768 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | return -ESRCH; |
| 4770 | } |
| 4771 | |
| 4772 | /* |
| 4773 | * It is not safe to call set_cpus_allowed with the |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4774 | * tasklist_lock held. We will bump the task_struct's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | * usage count and then drop tasklist_lock. |
| 4776 | */ |
| 4777 | get_task_struct(p); |
| 4778 | read_unlock(&tasklist_lock); |
| 4779 | |
| 4780 | retval = -EPERM; |
| 4781 | if ((current->euid != p->euid) && (current->euid != p->uid) && |
| 4782 | !capable(CAP_SYS_NICE)) |
| 4783 | goto out_unlock; |
| 4784 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 4785 | retval = security_task_setscheduler(p, 0, NULL); |
| 4786 | if (retval) |
| 4787 | goto out_unlock; |
| 4788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | cpus_allowed = cpuset_cpus_allowed(p); |
| 4790 | cpus_and(new_mask, new_mask, cpus_allowed); |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 4791 | again: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | retval = set_cpus_allowed(p, new_mask); |
| 4793 | |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 4794 | if (!retval) { |
| 4795 | cpus_allowed = cpuset_cpus_allowed(p); |
| 4796 | if (!cpus_subset(new_mask, cpus_allowed)) { |
| 4797 | /* |
| 4798 | * We must have raced with a concurrent cpuset |
| 4799 | * update. Just reset the cpus_allowed to the |
| 4800 | * cpuset's cpus_allowed |
| 4801 | */ |
| 4802 | new_mask = cpus_allowed; |
| 4803 | goto again; |
| 4804 | } |
| 4805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | out_unlock: |
| 4807 | put_task_struct(p); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4808 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | return retval; |
| 4810 | } |
| 4811 | |
| 4812 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, |
| 4813 | cpumask_t *new_mask) |
| 4814 | { |
| 4815 | if (len < sizeof(cpumask_t)) { |
| 4816 | memset(new_mask, 0, sizeof(cpumask_t)); |
| 4817 | } else if (len > sizeof(cpumask_t)) { |
| 4818 | len = sizeof(cpumask_t); |
| 4819 | } |
| 4820 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
| 4821 | } |
| 4822 | |
| 4823 | /** |
| 4824 | * sys_sched_setaffinity - set the cpu affinity of a process |
| 4825 | * @pid: pid of the process |
| 4826 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 4827 | * @user_mask_ptr: user-space pointer to the new cpu mask |
| 4828 | */ |
| 4829 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, |
| 4830 | unsigned long __user *user_mask_ptr) |
| 4831 | { |
| 4832 | cpumask_t new_mask; |
| 4833 | int retval; |
| 4834 | |
| 4835 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); |
| 4836 | if (retval) |
| 4837 | return retval; |
| 4838 | |
| 4839 | return sched_setaffinity(pid, new_mask); |
| 4840 | } |
| 4841 | |
| 4842 | /* |
| 4843 | * Represents all cpu's present in the system |
| 4844 | * In systems capable of hotplug, this map could dynamically grow |
| 4845 | * as new cpu's are detected in the system via any platform specific |
| 4846 | * method, such as ACPI for e.g. |
| 4847 | */ |
| 4848 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4849 | cpumask_t cpu_present_map __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | EXPORT_SYMBOL(cpu_present_map); |
| 4851 | |
| 4852 | #ifndef CONFIG_SMP |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4853 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 4854 | EXPORT_SYMBOL(cpu_online_map); |
| 4855 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4856 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 4857 | EXPORT_SYMBOL(cpu_possible_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | #endif |
| 4859 | |
| 4860 | long sched_getaffinity(pid_t pid, cpumask_t *mask) |
| 4861 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4862 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4865 | get_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | read_lock(&tasklist_lock); |
| 4867 | |
| 4868 | retval = -ESRCH; |
| 4869 | p = find_process_by_pid(pid); |
| 4870 | if (!p) |
| 4871 | goto out_unlock; |
| 4872 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 4873 | retval = security_task_getscheduler(p); |
| 4874 | if (retval) |
| 4875 | goto out_unlock; |
| 4876 | |
Jack Steiner | 2f7016d | 2006-02-01 03:05:18 -0800 | [diff] [blame] | 4877 | cpus_and(*mask, p->cpus_allowed, cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | |
| 4879 | out_unlock: |
| 4880 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4881 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | |
Ulrich Drepper | 9531b62 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 4883 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | /** |
| 4887 | * sys_sched_getaffinity - get the cpu affinity of a process |
| 4888 | * @pid: pid of the process |
| 4889 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 4890 | * @user_mask_ptr: user-space pointer to hold the current cpu mask |
| 4891 | */ |
| 4892 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, |
| 4893 | unsigned long __user *user_mask_ptr) |
| 4894 | { |
| 4895 | int ret; |
| 4896 | cpumask_t mask; |
| 4897 | |
| 4898 | if (len < sizeof(cpumask_t)) |
| 4899 | return -EINVAL; |
| 4900 | |
| 4901 | ret = sched_getaffinity(pid, &mask); |
| 4902 | if (ret < 0) |
| 4903 | return ret; |
| 4904 | |
| 4905 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) |
| 4906 | return -EFAULT; |
| 4907 | |
| 4908 | return sizeof(cpumask_t); |
| 4909 | } |
| 4910 | |
| 4911 | /** |
| 4912 | * sys_sched_yield - yield the current processor to other threads. |
| 4913 | * |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4914 | * This function yields the current CPU to other tasks. If there are no |
| 4915 | * other threads running on this CPU then this function will return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | */ |
| 4917 | asmlinkage long sys_sched_yield(void) |
| 4918 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4919 | struct rq *rq = this_rq_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4921 | schedstat_inc(rq, yld_count); |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4922 | current->sched_class->yield_task(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | |
| 4924 | /* |
| 4925 | * Since we are going to call schedule() anyway, there's |
| 4926 | * no need to preempt or enable interrupts: |
| 4927 | */ |
| 4928 | __release(rq->lock); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 4929 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | _raw_spin_unlock(&rq->lock); |
| 4931 | preempt_enable_no_resched(); |
| 4932 | |
| 4933 | schedule(); |
| 4934 | |
| 4935 | return 0; |
| 4936 | } |
| 4937 | |
Andrew Morton | e7b3840 | 2006-06-30 01:56:00 -0700 | [diff] [blame] | 4938 | static void __cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | { |
Ingo Molnar | 8e0a43d | 2006-06-23 02:05:23 -0700 | [diff] [blame] | 4940 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 4941 | __might_sleep(__FILE__, __LINE__); |
| 4942 | #endif |
Ingo Molnar | 5bbcfd9 | 2005-07-07 17:57:04 -0700 | [diff] [blame] | 4943 | /* |
| 4944 | * The BKS might be reacquired before we have dropped |
| 4945 | * PREEMPT_ACTIVE, which could trigger a second |
| 4946 | * cond_resched() call. |
| 4947 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | do { |
| 4949 | add_preempt_count(PREEMPT_ACTIVE); |
| 4950 | schedule(); |
| 4951 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4952 | } while (need_resched()); |
| 4953 | } |
| 4954 | |
Herbert Xu | 02b67cc3 | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 4955 | #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY) |
| 4956 | int __sched _cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | { |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 4958 | if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) && |
| 4959 | system_state == SYSTEM_RUNNING) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | __cond_resched(); |
| 4961 | return 1; |
| 4962 | } |
| 4963 | return 0; |
| 4964 | } |
Herbert Xu | 02b67cc3 | 2008-01-25 21:08:28 +0100 | [diff] [blame] | 4965 | EXPORT_SYMBOL(_cond_resched); |
| 4966 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | |
| 4968 | /* |
| 4969 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, |
| 4970 | * call schedule, and on return reacquire the lock. |
| 4971 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4972 | * 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] | 4973 | * operations here to prevent schedule() from being called twice (once via |
| 4974 | * spin_unlock(), once by hand). |
| 4975 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4976 | int cond_resched_lock(spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | { |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 4978 | int resched = need_resched() && system_state == SYSTEM_RUNNING; |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4979 | int ret = 0; |
| 4980 | |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 4981 | if (spin_needbreak(lock) || resched) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | spin_unlock(lock); |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 4983 | if (resched && need_resched()) |
| 4984 | __cond_resched(); |
| 4985 | else |
| 4986 | cpu_relax(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4987 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | spin_lock(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | } |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4990 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4991 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | EXPORT_SYMBOL(cond_resched_lock); |
| 4993 | |
| 4994 | int __sched cond_resched_softirq(void) |
| 4995 | { |
| 4996 | BUG_ON(!in_softirq()); |
| 4997 | |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 4998 | if (need_resched() && system_state == SYSTEM_RUNNING) { |
Thomas Gleixner | 98d82567 | 2007-05-23 13:58:18 -0700 | [diff] [blame] | 4999 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | __cond_resched(); |
| 5001 | local_bh_disable(); |
| 5002 | return 1; |
| 5003 | } |
| 5004 | return 0; |
| 5005 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | EXPORT_SYMBOL(cond_resched_softirq); |
| 5007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | /** |
| 5009 | * yield - yield the current processor to other threads. |
| 5010 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 5011 | * This is a shortcut for kernel-space yielding - it marks the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | * thread runnable and calls sys_sched_yield(). |
| 5013 | */ |
| 5014 | void __sched yield(void) |
| 5015 | { |
| 5016 | set_current_state(TASK_RUNNING); |
| 5017 | sys_sched_yield(); |
| 5018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | EXPORT_SYMBOL(yield); |
| 5020 | |
| 5021 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5022 | * 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] | 5023 | * that process accounting knows that this is a task in IO wait state. |
| 5024 | * |
| 5025 | * But don't do that if it is a deliberate, throttling IO wait (this task |
| 5026 | * has set its backing_dev_info: the queue against which it should throttle) |
| 5027 | */ |
| 5028 | void __sched io_schedule(void) |
| 5029 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5030 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5032 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | atomic_inc(&rq->nr_iowait); |
| 5034 | schedule(); |
| 5035 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5036 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | EXPORT_SYMBOL(io_schedule); |
| 5039 | |
| 5040 | long __sched io_schedule_timeout(long timeout) |
| 5041 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5042 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | long ret; |
| 5044 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5045 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | atomic_inc(&rq->nr_iowait); |
| 5047 | ret = schedule_timeout(timeout); |
| 5048 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 5049 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | return ret; |
| 5051 | } |
| 5052 | |
| 5053 | /** |
| 5054 | * sys_sched_get_priority_max - return maximum RT priority. |
| 5055 | * @policy: scheduling class. |
| 5056 | * |
| 5057 | * this syscall returns the maximum rt_priority that can be used |
| 5058 | * by a given scheduling class. |
| 5059 | */ |
| 5060 | asmlinkage long sys_sched_get_priority_max(int policy) |
| 5061 | { |
| 5062 | int ret = -EINVAL; |
| 5063 | |
| 5064 | switch (policy) { |
| 5065 | case SCHED_FIFO: |
| 5066 | case SCHED_RR: |
| 5067 | ret = MAX_USER_RT_PRIO-1; |
| 5068 | break; |
| 5069 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5070 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5071 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | ret = 0; |
| 5073 | break; |
| 5074 | } |
| 5075 | return ret; |
| 5076 | } |
| 5077 | |
| 5078 | /** |
| 5079 | * sys_sched_get_priority_min - return minimum RT priority. |
| 5080 | * @policy: scheduling class. |
| 5081 | * |
| 5082 | * this syscall returns the minimum rt_priority that can be used |
| 5083 | * by a given scheduling class. |
| 5084 | */ |
| 5085 | asmlinkage long sys_sched_get_priority_min(int policy) |
| 5086 | { |
| 5087 | int ret = -EINVAL; |
| 5088 | |
| 5089 | switch (policy) { |
| 5090 | case SCHED_FIFO: |
| 5091 | case SCHED_RR: |
| 5092 | ret = 1; |
| 5093 | break; |
| 5094 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 5095 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5096 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | ret = 0; |
| 5098 | } |
| 5099 | return ret; |
| 5100 | } |
| 5101 | |
| 5102 | /** |
| 5103 | * sys_sched_rr_get_interval - return the default timeslice of a process. |
| 5104 | * @pid: pid of the process. |
| 5105 | * @interval: userspace pointer to the timeslice value. |
| 5106 | * |
| 5107 | * this syscall writes the default timeslice value of a given process |
| 5108 | * into the user-space timespec buffer. A value of '0' means infinity. |
| 5109 | */ |
| 5110 | asmlinkage |
| 5111 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) |
| 5112 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5113 | struct task_struct *p; |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5114 | unsigned int time_slice; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5115 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5116 | struct timespec t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | |
| 5118 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5119 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | |
| 5121 | retval = -ESRCH; |
| 5122 | read_lock(&tasklist_lock); |
| 5123 | p = find_process_by_pid(pid); |
| 5124 | if (!p) |
| 5125 | goto out_unlock; |
| 5126 | |
| 5127 | retval = security_task_getscheduler(p); |
| 5128 | if (retval) |
| 5129 | goto out_unlock; |
| 5130 | |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5131 | /* |
| 5132 | * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER |
| 5133 | * tasks that are on an otherwise idle runqueue: |
| 5134 | */ |
| 5135 | time_slice = 0; |
| 5136 | if (p->policy == SCHED_RR) { |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5137 | time_slice = DEF_TIMESLICE; |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5138 | } else { |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5139 | struct sched_entity *se = &p->se; |
| 5140 | unsigned long flags; |
| 5141 | struct rq *rq; |
| 5142 | |
| 5143 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | 7703493 | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 5144 | if (rq->cfs.load.weight) |
| 5145 | time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5146 | task_rq_unlock(rq, &flags); |
| 5147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | read_unlock(&tasklist_lock); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5149 | jiffies_to_timespec(time_slice, &t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | return retval; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | out_unlock: |
| 5154 | read_unlock(&tasklist_lock); |
| 5155 | return retval; |
| 5156 | } |
| 5157 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5158 | static const char stat_nam[] = "RSDTtZX"; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5159 | |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5160 | void sched_show_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | unsigned long free = 0; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5163 | unsigned state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5165 | state = p->state ? __ffs(p->state) + 1 : 0; |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5166 | printk(KERN_INFO "%-13.13s %c", p->comm, |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5167 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5168 | #if BITS_PER_LONG == 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | if (state == TASK_RUNNING) |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5170 | printk(KERN_CONT " running "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | else |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5172 | printk(KERN_CONT " %08lx ", thread_saved_pc(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | #else |
| 5174 | if (state == TASK_RUNNING) |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5175 | printk(KERN_CONT " running task "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | else |
Ingo Molnar | cc4ea79 | 2007-10-18 21:32:56 +0200 | [diff] [blame] | 5177 | printk(KERN_CONT " %016lx ", thread_saved_pc(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | #endif |
| 5179 | #ifdef CONFIG_DEBUG_STACK_USAGE |
| 5180 | { |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 5181 | unsigned long *n = end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | while (!*n) |
| 5183 | n++; |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 5184 | free = (unsigned long)n - (unsigned long)end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | } |
| 5186 | #endif |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5187 | printk(KERN_CONT "%5lu %5d %6d\n", free, |
Roland McGrath | fcfd50a | 2008-01-09 00:03:23 -0800 | [diff] [blame] | 5188 | task_pid_nr(p), task_pid_nr(p->real_parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | |
Nick Piggin | 5fb5e6d | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 5190 | show_stack(p, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | } |
| 5192 | |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 5193 | void show_state_filter(unsigned long state_filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5195 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5197 | #if BITS_PER_LONG == 32 |
| 5198 | printk(KERN_INFO |
| 5199 | " task PC stack pid father\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | #else |
Ingo Molnar | 4bd7732 | 2007-07-11 21:21:47 +0200 | [diff] [blame] | 5201 | printk(KERN_INFO |
| 5202 | " task PC stack pid father\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | #endif |
| 5204 | read_lock(&tasklist_lock); |
| 5205 | do_each_thread(g, p) { |
| 5206 | /* |
| 5207 | * reset the NMI-timeout, listing all files on a slow |
| 5208 | * console might take alot of time: |
| 5209 | */ |
| 5210 | touch_nmi_watchdog(); |
Ingo Molnar | 39bc89f | 2007-04-25 20:50:03 -0700 | [diff] [blame] | 5211 | if (!state_filter || (p->state & state_filter)) |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 5212 | sched_show_task(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | } while_each_thread(g, p); |
| 5214 | |
Jeremy Fitzhardinge | 04c9167 | 2007-05-08 00:28:05 -0700 | [diff] [blame] | 5215 | touch_all_softlockup_watchdogs(); |
| 5216 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5217 | #ifdef CONFIG_SCHED_DEBUG |
| 5218 | sysrq_sched_debug_show(); |
| 5219 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | read_unlock(&tasklist_lock); |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 5221 | /* |
| 5222 | * Only show locks if all tasks are dumped: |
| 5223 | */ |
| 5224 | if (state_filter == -1) |
| 5225 | debug_show_all_locks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | } |
| 5227 | |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5228 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) |
| 5229 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5230 | idle->sched_class = &idle_sched_class; |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5231 | } |
| 5232 | |
Ingo Molnar | f340c0d | 2005-06-28 16:40:42 +0200 | [diff] [blame] | 5233 | /** |
| 5234 | * init_idle - set up an idle thread for a given CPU |
| 5235 | * @idle: task in question |
| 5236 | * @cpu: cpu the idle task belongs to |
| 5237 | * |
| 5238 | * NOTE: this function does not set the idle thread's NEED_RESCHED |
| 5239 | * flag, to make booting more robust. |
| 5240 | */ |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 5241 | void __cpuinit init_idle(struct task_struct *idle, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5243 | struct rq *rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | unsigned long flags; |
| 5245 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5246 | __sched_fork(idle); |
| 5247 | idle->se.exec_start = sched_clock(); |
| 5248 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 5249 | idle->prio = idle->normal_prio = MAX_PRIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | idle->cpus_allowed = cpumask_of_cpu(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5251 | __set_task_cpu(idle, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | |
| 5253 | spin_lock_irqsave(&rq->lock, flags); |
| 5254 | rq->curr = rq->idle = idle; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 5255 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
| 5256 | idle->oncpu = 1; |
| 5257 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5259 | |
| 5260 | /* Set the preempt count _outside_ the spinlocks! */ |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 5261 | task_thread_info(idle)->preempt_count = 0; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 5262 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5263 | /* |
| 5264 | * The idle tasks have their own, simple scheduling class: |
| 5265 | */ |
| 5266 | idle->sched_class = &idle_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | } |
| 5268 | |
| 5269 | /* |
| 5270 | * In a system that switches off the HZ timer nohz_cpu_mask |
| 5271 | * indicates which cpus entered this state. This is used |
| 5272 | * in the rcu update to wait only for active cpus. For system |
| 5273 | * which do not switch off the HZ timer nohz_cpu_mask should |
| 5274 | * always be CPU_MASK_NONE. |
| 5275 | */ |
| 5276 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; |
| 5277 | |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 5278 | /* |
| 5279 | * Increase the granularity value when there are more CPUs, |
| 5280 | * because with more CPUs the 'effective latency' as visible |
| 5281 | * to users decreases. But the relationship is not linear, |
| 5282 | * so pick a second-best guess by going with the log2 of the |
| 5283 | * number of CPUs. |
| 5284 | * |
| 5285 | * This idea comes from the SD scheduler of Con Kolivas: |
| 5286 | */ |
| 5287 | static inline void sched_init_granularity(void) |
| 5288 | { |
| 5289 | unsigned int factor = 1 + ilog2(num_online_cpus()); |
| 5290 | const unsigned long limit = 200000000; |
| 5291 | |
| 5292 | sysctl_sched_min_granularity *= factor; |
| 5293 | if (sysctl_sched_min_granularity > limit) |
| 5294 | sysctl_sched_min_granularity = limit; |
| 5295 | |
| 5296 | sysctl_sched_latency *= factor; |
| 5297 | if (sysctl_sched_latency > limit) |
| 5298 | sysctl_sched_latency = limit; |
| 5299 | |
| 5300 | sysctl_sched_wakeup_granularity *= factor; |
| 5301 | sysctl_sched_batch_wakeup_granularity *= factor; |
| 5302 | } |
| 5303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | #ifdef CONFIG_SMP |
| 5305 | /* |
| 5306 | * This is how migration works: |
| 5307 | * |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5308 | * 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] | 5309 | * runqueue and wake up that CPU's migration thread. |
| 5310 | * 2) we down() the locked semaphore => thread blocks. |
| 5311 | * 3) migration thread wakes up (implicitly it forces the migrated |
| 5312 | * thread off the CPU) |
| 5313 | * 4) it gets the migration request and checks whether the migrated |
| 5314 | * task is still in the wrong runqueue. |
| 5315 | * 5) if it's in the wrong runqueue then the migration thread removes |
| 5316 | * it and puts it into the right queue. |
| 5317 | * 6) migration thread up()s the semaphore. |
| 5318 | * 7) we wake up and the migration is done. |
| 5319 | */ |
| 5320 | |
| 5321 | /* |
| 5322 | * Change a given task's CPU affinity. Migrate the thread to a |
| 5323 | * proper CPU and schedule it away if the CPU it's executing on |
| 5324 | * is removed from the allowed bitmask. |
| 5325 | * |
| 5326 | * NOTE: the caller must have a valid reference to the task, the |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5327 | * task must not exit() & deallocate itself prematurely. The |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | * call is not atomic; no spinlocks may be held. |
| 5329 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5330 | int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5332 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5334 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5335 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | |
| 5337 | rq = task_rq_lock(p, &flags); |
| 5338 | if (!cpus_intersects(new_mask, cpu_online_map)) { |
| 5339 | ret = -EINVAL; |
| 5340 | goto out; |
| 5341 | } |
| 5342 | |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 5343 | if (p->sched_class->set_cpus_allowed) |
| 5344 | p->sched_class->set_cpus_allowed(p, &new_mask); |
| 5345 | else { |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 5346 | p->cpus_allowed = new_mask; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 5347 | p->rt.nr_cpus_allowed = cpus_weight(new_mask); |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 5348 | } |
| 5349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | /* Can the task run on the task's current CPU? If so, we're done */ |
| 5351 | if (cpu_isset(task_cpu(p), new_mask)) |
| 5352 | goto out; |
| 5353 | |
| 5354 | if (migrate_task(p, any_online_cpu(new_mask), &req)) { |
| 5355 | /* Need help from migration thread: drop lock and wait. */ |
| 5356 | task_rq_unlock(rq, &flags); |
| 5357 | wake_up_process(rq->migration_thread); |
| 5358 | wait_for_completion(&req.done); |
| 5359 | tlb_migrate_finish(p->mm); |
| 5360 | return 0; |
| 5361 | } |
| 5362 | out: |
| 5363 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | return ret; |
| 5366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5367 | EXPORT_SYMBOL_GPL(set_cpus_allowed); |
| 5368 | |
| 5369 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5370 | * 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] | 5371 | * this because either it can't run here any more (set_cpus_allowed() |
| 5372 | * away from this CPU, or CPU going down), or because we're |
| 5373 | * attempting to rebalance this task on exec (sched_exec). |
| 5374 | * |
| 5375 | * So we race with normal scheduler movements, but that's OK, as long |
| 5376 | * as the task is no longer on this CPU. |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5377 | * |
| 5378 | * Returns non-zero if task was successfully migrated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | */ |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5380 | 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] | 5381 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5382 | struct rq *rq_dest, *rq_src; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5383 | int ret = 0, on_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | |
| 5385 | if (unlikely(cpu_is_offline(dest_cpu))) |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5386 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | |
| 5388 | rq_src = cpu_rq(src_cpu); |
| 5389 | rq_dest = cpu_rq(dest_cpu); |
| 5390 | |
| 5391 | double_rq_lock(rq_src, rq_dest); |
| 5392 | /* Already moved. */ |
| 5393 | if (task_cpu(p) != src_cpu) |
| 5394 | goto out; |
| 5395 | /* Affinity changed (again). */ |
| 5396 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) |
| 5397 | goto out; |
| 5398 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5399 | on_rq = p->se.on_rq; |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5400 | if (on_rq) |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5401 | deactivate_task(rq_src, p, 0); |
Ingo Molnar | 6e82a3b | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5403 | set_task_cpu(p, dest_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5404 | if (on_rq) { |
| 5405 | activate_task(rq_dest, p, 0); |
| 5406 | check_preempt_curr(rq_dest, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | } |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5408 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | out: |
| 5410 | double_rq_unlock(rq_src, rq_dest); |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5411 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | /* |
| 5415 | * migration_thread - this is a highprio system thread that performs |
| 5416 | * thread migration by bumping thread off CPU then 'pushing' onto |
| 5417 | * another runqueue. |
| 5418 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 5419 | static int migration_thread(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | int cpu = (long)data; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5422 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | |
| 5424 | rq = cpu_rq(cpu); |
| 5425 | BUG_ON(rq->migration_thread != current); |
| 5426 | |
| 5427 | set_current_state(TASK_INTERRUPTIBLE); |
| 5428 | while (!kthread_should_stop()) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5429 | struct migration_req *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5430 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | spin_lock_irq(&rq->lock); |
| 5433 | |
| 5434 | if (cpu_is_offline(cpu)) { |
| 5435 | spin_unlock_irq(&rq->lock); |
| 5436 | goto wait_to_die; |
| 5437 | } |
| 5438 | |
| 5439 | if (rq->active_balance) { |
| 5440 | active_load_balance(rq, cpu); |
| 5441 | rq->active_balance = 0; |
| 5442 | } |
| 5443 | |
| 5444 | head = &rq->migration_queue; |
| 5445 | |
| 5446 | if (list_empty(head)) { |
| 5447 | spin_unlock_irq(&rq->lock); |
| 5448 | schedule(); |
| 5449 | set_current_state(TASK_INTERRUPTIBLE); |
| 5450 | continue; |
| 5451 | } |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5452 | req = list_entry(head->next, struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | list_del_init(head->next); |
| 5454 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 5455 | spin_unlock(&rq->lock); |
| 5456 | __migrate_task(req->task, cpu, req->dest_cpu); |
| 5457 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | |
| 5459 | complete(&req->done); |
| 5460 | } |
| 5461 | __set_current_state(TASK_RUNNING); |
| 5462 | return 0; |
| 5463 | |
| 5464 | wait_to_die: |
| 5465 | /* Wait for kthread_stop */ |
| 5466 | set_current_state(TASK_INTERRUPTIBLE); |
| 5467 | while (!kthread_should_stop()) { |
| 5468 | schedule(); |
| 5469 | set_current_state(TASK_INTERRUPTIBLE); |
| 5470 | } |
| 5471 | __set_current_state(TASK_RUNNING); |
| 5472 | return 0; |
| 5473 | } |
| 5474 | |
| 5475 | #ifdef CONFIG_HOTPLUG_CPU |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5476 | |
| 5477 | static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu) |
| 5478 | { |
| 5479 | int ret; |
| 5480 | |
| 5481 | local_irq_disable(); |
| 5482 | ret = __migrate_task(p, src_cpu, dest_cpu); |
| 5483 | local_irq_enable(); |
| 5484 | return ret; |
| 5485 | } |
| 5486 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5487 | /* |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 5488 | * 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] | 5489 | * NOTE: interrupts should be disabled by the caller |
| 5490 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5491 | 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] | 5492 | { |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5493 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | cpumask_t mask; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5495 | struct rq *rq; |
| 5496 | int dest_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5498 | do { |
| 5499 | /* On same node? */ |
| 5500 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); |
| 5501 | cpus_and(mask, mask, p->cpus_allowed); |
| 5502 | dest_cpu = any_online_cpu(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5504 | /* On any allowed CPU? */ |
| 5505 | if (dest_cpu == NR_CPUS) |
| 5506 | dest_cpu = any_online_cpu(p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5508 | /* No more Mr. Nice Guy. */ |
| 5509 | if (dest_cpu == NR_CPUS) { |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5510 | cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p); |
| 5511 | /* |
| 5512 | * Try to stay on the same cpuset, where the |
| 5513 | * current cpuset may be a subset of all cpus. |
| 5514 | * The cpuset_cpus_allowed_locked() variant of |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5515 | * cpuset_cpus_allowed() will not block. It must be |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5516 | * called within calls to cpuset_lock/cpuset_unlock. |
| 5517 | */ |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5518 | rq = task_rq_lock(p, &flags); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5519 | p->cpus_allowed = cpus_allowed; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5520 | dest_cpu = any_online_cpu(p->cpus_allowed); |
| 5521 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5523 | /* |
| 5524 | * Don't tell them about moving exiting tasks or |
| 5525 | * kernel threads (both mm NULL), since they never |
| 5526 | * leave kernel. |
| 5527 | */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5528 | if (p->mm && printk_ratelimit()) { |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5529 | printk(KERN_INFO "process %d (%s) no " |
| 5530 | "longer affine to cpu%d\n", |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5531 | task_pid_nr(p), p->comm, dead_cpu); |
| 5532 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5533 | } |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5534 | } while (!__migrate_task_irq(p, dead_cpu, dest_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | /* |
| 5538 | * While a dead CPU has no uninterruptible tasks queued at this point, |
| 5539 | * it might still have a nonzero ->nr_uninterruptible counter, because |
| 5540 | * for performance reasons the counter is not stricly tracking tasks to |
| 5541 | * their home CPUs. So we just add the counter to another CPU's counter, |
| 5542 | * to keep the global sum constant after CPU-down: |
| 5543 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5544 | static void migrate_nr_uninterruptible(struct rq *rq_src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5545 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5546 | struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | unsigned long flags; |
| 5548 | |
| 5549 | local_irq_save(flags); |
| 5550 | double_rq_lock(rq_src, rq_dest); |
| 5551 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; |
| 5552 | rq_src->nr_uninterruptible = 0; |
| 5553 | double_rq_unlock(rq_src, rq_dest); |
| 5554 | local_irq_restore(flags); |
| 5555 | } |
| 5556 | |
| 5557 | /* Run through task list and migrate tasks from the dead cpu. */ |
| 5558 | static void migrate_live_tasks(int src_cpu) |
| 5559 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5560 | struct task_struct *p, *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5562 | read_lock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5564 | do_each_thread(t, p) { |
| 5565 | if (p == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | continue; |
| 5567 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5568 | if (task_cpu(p) == src_cpu) |
| 5569 | move_task_off_dead_cpu(src_cpu, p); |
| 5570 | } while_each_thread(t, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5572 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | } |
| 5574 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5575 | /* |
| 5576 | * Schedules idle task to be the next runnable task on current CPU. |
Dmitry Adamushko | 94bc9a7 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 5577 | * It does so by boosting its priority to highest possible. |
| 5578 | * Used by CPU offline code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | */ |
| 5580 | void sched_idle_next(void) |
| 5581 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5582 | int this_cpu = smp_processor_id(); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5583 | struct rq *rq = cpu_rq(this_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | struct task_struct *p = rq->idle; |
| 5585 | unsigned long flags; |
| 5586 | |
| 5587 | /* cpu has to be offline */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5588 | BUG_ON(cpu_online(this_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5590 | /* |
| 5591 | * Strictly not necessary since rest of the CPUs are stopped by now |
| 5592 | * and interrupts disabled on the current cpu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5593 | */ |
| 5594 | spin_lock_irqsave(&rq->lock, flags); |
| 5595 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5596 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5597 | |
Dmitry Adamushko | 94bc9a7 | 2007-11-15 20:57:40 +0100 | [diff] [blame] | 5598 | update_rq_clock(rq); |
| 5599 | activate_task(rq, p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | |
| 5601 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5602 | } |
| 5603 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5604 | /* |
| 5605 | * 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] | 5606 | * offline. |
| 5607 | */ |
| 5608 | void idle_task_exit(void) |
| 5609 | { |
| 5610 | struct mm_struct *mm = current->active_mm; |
| 5611 | |
| 5612 | BUG_ON(cpu_online(smp_processor_id())); |
| 5613 | |
| 5614 | if (mm != &init_mm) |
| 5615 | switch_mm(mm, &init_mm, current); |
| 5616 | mmdrop(mm); |
| 5617 | } |
| 5618 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5619 | /* called under rq->lock with disabled interrupts */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5620 | static void migrate_dead(unsigned int dead_cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5622 | struct rq *rq = cpu_rq(dead_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | |
| 5624 | /* Must be exiting, otherwise would be on tasklist. */ |
Eugene Teo | 270f722 | 2007-10-18 23:40:38 -0700 | [diff] [blame] | 5625 | BUG_ON(!p->exit_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | |
| 5627 | /* Cannot have done final schedule yet: would have vanished. */ |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 5628 | BUG_ON(p->state == TASK_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5629 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5630 | get_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | |
| 5632 | /* |
| 5633 | * Drop lock around migration; if someone else moves it, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5634 | * 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] | 5635 | * fine. |
| 5636 | */ |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5637 | spin_unlock_irq(&rq->lock); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5638 | move_task_off_dead_cpu(dead_cpu, p); |
Oleg Nesterov | f7b4cdd | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5639 | spin_lock_irq(&rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5641 | put_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | } |
| 5643 | |
| 5644 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ |
| 5645 | static void migrate_dead_tasks(unsigned int dead_cpu) |
| 5646 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5647 | struct rq *rq = cpu_rq(dead_cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5648 | struct task_struct *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5650 | for ( ; ; ) { |
| 5651 | if (!rq->nr_running) |
| 5652 | break; |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 5653 | update_rq_clock(rq); |
Ingo Molnar | ff95f3d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5654 | next = pick_next_task(rq, rq->curr); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5655 | if (!next) |
| 5656 | break; |
| 5657 | migrate_dead(dead_cpu, next); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | } |
| 5660 | } |
| 5661 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 5662 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5663 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
| 5664 | |
| 5665 | static struct ctl_table sd_ctl_dir[] = { |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5666 | { |
| 5667 | .procname = "sched_domain", |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5668 | .mode = 0555, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5669 | }, |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 5670 | {0, }, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5671 | }; |
| 5672 | |
| 5673 | static struct ctl_table sd_ctl_root[] = { |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5674 | { |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5675 | .ctl_name = CTL_KERN, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5676 | .procname = "kernel", |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5677 | .mode = 0555, |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5678 | .child = sd_ctl_dir, |
| 5679 | }, |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 5680 | {0, }, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5681 | }; |
| 5682 | |
| 5683 | static struct ctl_table *sd_alloc_ctl_entry(int n) |
| 5684 | { |
| 5685 | struct ctl_table *entry = |
Milton Miller | 5cf9f06 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5686 | kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5687 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5688 | return entry; |
| 5689 | } |
| 5690 | |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5691 | static void sd_free_ctl_entry(struct ctl_table **tablep) |
| 5692 | { |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5693 | struct ctl_table *entry; |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5694 | |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5695 | /* |
| 5696 | * In the intermediate directories, both the child directory and |
| 5697 | * procname are dynamically allocated and could fail but the mode |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5698 | * will always be set. In the lowest directory the names are |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5699 | * static strings and all have proc handlers. |
| 5700 | */ |
| 5701 | for (entry = *tablep; entry->mode; entry++) { |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5702 | if (entry->child) |
| 5703 | sd_free_ctl_entry(&entry->child); |
Milton Miller | cd790076 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 5704 | if (entry->proc_handler == NULL) |
| 5705 | kfree(entry->procname); |
| 5706 | } |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5707 | |
| 5708 | kfree(*tablep); |
| 5709 | *tablep = NULL; |
| 5710 | } |
| 5711 | |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5712 | static void |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5713 | set_table_entry(struct ctl_table *entry, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5714 | const char *procname, void *data, int maxlen, |
| 5715 | mode_t mode, proc_handler *proc_handler) |
| 5716 | { |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5717 | entry->procname = procname; |
| 5718 | entry->data = data; |
| 5719 | entry->maxlen = maxlen; |
| 5720 | entry->mode = mode; |
| 5721 | entry->proc_handler = proc_handler; |
| 5722 | } |
| 5723 | |
| 5724 | static struct ctl_table * |
| 5725 | sd_alloc_ctl_domain_table(struct sched_domain *sd) |
| 5726 | { |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5727 | struct ctl_table *table = sd_alloc_ctl_entry(12); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5728 | |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5729 | if (table == NULL) |
| 5730 | return NULL; |
| 5731 | |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5732 | set_table_entry(&table[0], "min_interval", &sd->min_interval, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5733 | sizeof(long), 0644, proc_doulongvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5734 | set_table_entry(&table[1], "max_interval", &sd->max_interval, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5735 | sizeof(long), 0644, proc_doulongvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5736 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5737 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5738 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5739 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5740 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5741 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5742 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5743 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5744 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5745 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5746 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5747 | sizeof(int), 0644, proc_dointvec_minmax); |
Alexey Dobriyan | e036185 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 5748 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5749 | sizeof(int), 0644, proc_dointvec_minmax); |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5750 | set_table_entry(&table[9], "cache_nice_tries", |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5751 | &sd->cache_nice_tries, |
| 5752 | sizeof(int), 0644, proc_dointvec_minmax); |
Zou Nan hai | ace8b3d | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5753 | set_table_entry(&table[10], "flags", &sd->flags, |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5754 | sizeof(int), 0644, proc_dointvec_minmax); |
Milton Miller | 6323469f | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5755 | /* &table[11] is terminator */ |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5756 | |
| 5757 | return table; |
| 5758 | } |
| 5759 | |
Ingo Molnar | 9a4e715 | 2007-11-28 15:52:56 +0100 | [diff] [blame] | 5760 | static ctl_table *sd_alloc_ctl_cpu_table(int cpu) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5761 | { |
| 5762 | struct ctl_table *entry, *table; |
| 5763 | struct sched_domain *sd; |
| 5764 | int domain_num = 0, i; |
| 5765 | char buf[32]; |
| 5766 | |
| 5767 | for_each_domain(cpu, sd) |
| 5768 | domain_num++; |
| 5769 | entry = table = sd_alloc_ctl_entry(domain_num + 1); |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5770 | if (table == NULL) |
| 5771 | return NULL; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5772 | |
| 5773 | i = 0; |
| 5774 | for_each_domain(cpu, sd) { |
| 5775 | snprintf(buf, 32, "domain%d", i); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5776 | entry->procname = kstrdup(buf, GFP_KERNEL); |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5777 | entry->mode = 0555; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5778 | entry->child = sd_alloc_ctl_domain_table(sd); |
| 5779 | entry++; |
| 5780 | i++; |
| 5781 | } |
| 5782 | return table; |
| 5783 | } |
| 5784 | |
| 5785 | static struct ctl_table_header *sd_sysctl_header; |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5786 | static void register_sched_domain_sysctl(void) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5787 | { |
| 5788 | int i, cpu_num = num_online_cpus(); |
| 5789 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); |
| 5790 | char buf[32]; |
| 5791 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5792 | WARN_ON(sd_ctl_dir[0].child); |
| 5793 | sd_ctl_dir[0].child = entry; |
| 5794 | |
Milton Miller | ad1cdc1 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5795 | if (entry == NULL) |
| 5796 | return; |
| 5797 | |
Milton Miller | 97b6ea7 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5798 | for_each_online_cpu(i) { |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5799 | snprintf(buf, 32, "cpu%d", i); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5800 | entry->procname = kstrdup(buf, GFP_KERNEL); |
Eric W. Biederman | c57baf1 | 2007-08-23 15:18:02 +0200 | [diff] [blame] | 5801 | entry->mode = 0555; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5802 | entry->child = sd_alloc_ctl_cpu_table(i); |
Milton Miller | 97b6ea7 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5803 | entry++; |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5804 | } |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5805 | |
| 5806 | WARN_ON(sd_sysctl_header); |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5807 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); |
| 5808 | } |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5809 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5810 | /* may be called multiple times per register */ |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5811 | static void unregister_sched_domain_sysctl(void) |
| 5812 | { |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5813 | if (sd_sysctl_header) |
| 5814 | unregister_sysctl_table(sd_sysctl_header); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5815 | sd_sysctl_header = NULL; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5816 | if (sd_ctl_dir[0].child) |
| 5817 | sd_free_ctl_entry(&sd_ctl_dir[0].child); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5818 | } |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5819 | #else |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 5820 | static void register_sched_domain_sysctl(void) |
| 5821 | { |
| 5822 | } |
| 5823 | static void unregister_sched_domain_sysctl(void) |
Nick Piggin | e692ab5 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 5824 | { |
| 5825 | } |
| 5826 | #endif |
| 5827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | /* |
| 5829 | * migration_call - callback that gets triggered when a CPU is added. |
| 5830 | * Here we can start up the necessary migration thread for the new CPU. |
| 5831 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5832 | static int __cpuinit |
| 5833 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5834 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | struct task_struct *p; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5836 | int cpu = (long)hcpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5838 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | |
| 5840 | switch (action) { |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 5841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5842 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5843 | case CPU_UP_PREPARE_FROZEN: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5844 | p = kthread_create(migration_thread, hcpu, "migration/%d", cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | if (IS_ERR(p)) |
| 5846 | return NOTIFY_BAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | kthread_bind(p, cpu); |
| 5848 | /* Must be high prio: stop_machine expects to yield to it. */ |
| 5849 | rq = task_rq_lock(p, &flags); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5850 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | task_rq_unlock(rq, &flags); |
| 5852 | cpu_rq(cpu)->migration_thread = p; |
| 5853 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5856 | case CPU_ONLINE_FROZEN: |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 5857 | /* Strictly unnecessary, as first user will wake it. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | wake_up_process(cpu_rq(cpu)->migration_thread); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 5859 | |
| 5860 | /* Update our root-domain */ |
| 5861 | rq = cpu_rq(cpu); |
| 5862 | spin_lock_irqsave(&rq->lock, flags); |
| 5863 | if (rq->rd) { |
| 5864 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); |
| 5865 | cpu_set(cpu, rq->rd->online); |
| 5866 | } |
| 5867 | spin_unlock_irqrestore(&rq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | #ifdef CONFIG_HOTPLUG_CPU |
| 5871 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5872 | case CPU_UP_CANCELED_FROZEN: |
Heiko Carstens | fc75cdf | 2006-06-25 05:49:10 -0700 | [diff] [blame] | 5873 | if (!cpu_rq(cpu)->migration_thread) |
| 5874 | break; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5875 | /* Unbind it from offline cpu so it can run. Fall thru. */ |
Heiko Carstens | a4c4af7 | 2005-11-07 00:58:38 -0800 | [diff] [blame] | 5876 | kthread_bind(cpu_rq(cpu)->migration_thread, |
| 5877 | any_online_cpu(cpu_online_map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | kthread_stop(cpu_rq(cpu)->migration_thread); |
| 5879 | cpu_rq(cpu)->migration_thread = NULL; |
| 5880 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5882 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5883 | case CPU_DEAD_FROZEN: |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5884 | cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | migrate_live_tasks(cpu); |
| 5886 | rq = cpu_rq(cpu); |
| 5887 | kthread_stop(rq->migration_thread); |
| 5888 | rq->migration_thread = NULL; |
| 5889 | /* Idle task back to normal (off runqueue, low prio) */ |
Oleg Nesterov | d2da272 | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5890 | spin_lock_irq(&rq->lock); |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 5891 | update_rq_clock(rq); |
Ingo Molnar | 2e1cb74 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 5892 | deactivate_task(rq, rq->idle, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5893 | rq->idle->static_prio = MAX_PRIO; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5894 | __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); |
| 5895 | rq->idle->sched_class = &idle_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | migrate_dead_tasks(cpu); |
Oleg Nesterov | d2da272 | 2007-10-16 23:30:56 -0700 | [diff] [blame] | 5897 | spin_unlock_irq(&rq->lock); |
Cliff Wickman | 470fd64 | 2007-10-18 23:40:46 -0700 | [diff] [blame] | 5898 | cpuset_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | migrate_nr_uninterruptible(rq); |
| 5900 | BUG_ON(rq->nr_running != 0); |
| 5901 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 5902 | /* |
| 5903 | * No need to migrate the tasks: it was best-effort if |
| 5904 | * they didn't take sched_hotcpu_mutex. Just wake up |
| 5905 | * the requestors. |
| 5906 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | spin_lock_irq(&rq->lock); |
| 5908 | while (!list_empty(&rq->migration_queue)) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5909 | struct migration_req *req; |
| 5910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | req = list_entry(rq->migration_queue.next, |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5912 | struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | list_del_init(&req->list); |
| 5914 | complete(&req->done); |
| 5915 | } |
| 5916 | spin_unlock_irq(&rq->lock); |
| 5917 | break; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 5918 | |
| 5919 | case CPU_DOWN_PREPARE: |
| 5920 | /* Update our root-domain */ |
| 5921 | rq = cpu_rq(cpu); |
| 5922 | spin_lock_irqsave(&rq->lock, flags); |
| 5923 | if (rq->rd) { |
| 5924 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); |
| 5925 | cpu_clear(cpu, rq->rd->online); |
| 5926 | } |
| 5927 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5928 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | #endif |
| 5930 | } |
| 5931 | return NOTIFY_OK; |
| 5932 | } |
| 5933 | |
| 5934 | /* Register at highest priority so that task migration (migrate_all_tasks) |
| 5935 | * happens before everything else. |
| 5936 | */ |
Chandra Seetharaman | 26c2143 | 2006-06-27 02:54:10 -0700 | [diff] [blame] | 5937 | static struct notifier_block __cpuinitdata migration_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | .notifier_call = migration_call, |
| 5939 | .priority = 10 |
| 5940 | }; |
| 5941 | |
Adrian Bunk | e6fe664 | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 5942 | void __init migration_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | { |
| 5944 | void *cpu = (void *)(long)smp_processor_id(); |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 5945 | int err; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5946 | |
| 5947 | /* Start one for the boot CPU: */ |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 5948 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
| 5949 | BUG_ON(err == NOTIFY_BAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
| 5951 | register_cpu_notifier(&migration_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | } |
| 5953 | #endif |
| 5954 | |
| 5955 | #ifdef CONFIG_SMP |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 5956 | |
| 5957 | /* Number of possible processor ids */ |
| 5958 | int nr_cpu_ids __read_mostly = NR_CPUS; |
| 5959 | EXPORT_SYMBOL(nr_cpu_ids); |
| 5960 | |
Ingo Molnar | 3e9830d | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 5961 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 5962 | |
| 5963 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level) |
| 5964 | { |
| 5965 | struct sched_group *group = sd->groups; |
| 5966 | cpumask_t groupmask; |
| 5967 | char str[NR_CPUS]; |
| 5968 | |
| 5969 | cpumask_scnprintf(str, NR_CPUS, sd->span); |
| 5970 | cpus_clear(groupmask); |
| 5971 | |
| 5972 | printk(KERN_DEBUG "%*s domain %d: ", level, "", level); |
| 5973 | |
| 5974 | if (!(sd->flags & SD_LOAD_BALANCE)) { |
| 5975 | printk("does not load-balance\n"); |
| 5976 | if (sd->parent) |
| 5977 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" |
| 5978 | " has parent"); |
| 5979 | return -1; |
| 5980 | } |
| 5981 | |
| 5982 | printk(KERN_CONT "span %s\n", str); |
| 5983 | |
| 5984 | if (!cpu_isset(cpu, sd->span)) { |
| 5985 | printk(KERN_ERR "ERROR: domain->span does not contain " |
| 5986 | "CPU%d\n", cpu); |
| 5987 | } |
| 5988 | if (!cpu_isset(cpu, group->cpumask)) { |
| 5989 | printk(KERN_ERR "ERROR: domain->groups does not contain" |
| 5990 | " CPU%d\n", cpu); |
| 5991 | } |
| 5992 | |
| 5993 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); |
| 5994 | do { |
| 5995 | if (!group) { |
| 5996 | printk("\n"); |
| 5997 | printk(KERN_ERR "ERROR: group is NULL\n"); |
| 5998 | break; |
| 5999 | } |
| 6000 | |
| 6001 | if (!group->__cpu_power) { |
| 6002 | printk(KERN_CONT "\n"); |
| 6003 | printk(KERN_ERR "ERROR: domain->cpu_power not " |
| 6004 | "set\n"); |
| 6005 | break; |
| 6006 | } |
| 6007 | |
| 6008 | if (!cpus_weight(group->cpumask)) { |
| 6009 | printk(KERN_CONT "\n"); |
| 6010 | printk(KERN_ERR "ERROR: empty group\n"); |
| 6011 | break; |
| 6012 | } |
| 6013 | |
| 6014 | if (cpus_intersects(groupmask, group->cpumask)) { |
| 6015 | printk(KERN_CONT "\n"); |
| 6016 | printk(KERN_ERR "ERROR: repeated CPUs\n"); |
| 6017 | break; |
| 6018 | } |
| 6019 | |
| 6020 | cpus_or(groupmask, groupmask, group->cpumask); |
| 6021 | |
| 6022 | cpumask_scnprintf(str, NR_CPUS, group->cpumask); |
| 6023 | printk(KERN_CONT " %s", str); |
| 6024 | |
| 6025 | group = group->next; |
| 6026 | } while (group != sd->groups); |
| 6027 | printk(KERN_CONT "\n"); |
| 6028 | |
| 6029 | if (!cpus_equal(sd->span, groupmask)) |
| 6030 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
| 6031 | |
| 6032 | if (sd->parent && !cpus_subset(groupmask, sd->parent->span)) |
| 6033 | printk(KERN_ERR "ERROR: parent span is not a superset " |
| 6034 | "of domain->span\n"); |
| 6035 | return 0; |
| 6036 | } |
| 6037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
| 6039 | { |
| 6040 | int level = 0; |
| 6041 | |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 6042 | if (!sd) { |
| 6043 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); |
| 6044 | return; |
| 6045 | } |
| 6046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
| 6048 | |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6049 | for (;;) { |
| 6050 | if (sched_domain_debug_one(sd, cpu, level)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | level++; |
| 6053 | sd = sd->parent; |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 6054 | if (!sd) |
Ingo Molnar | 4dcf6af | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6055 | break; |
| 6056 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | } |
| 6058 | #else |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6059 | # define sched_domain_debug(sd, cpu) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | #endif |
| 6061 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6062 | static int sd_degenerate(struct sched_domain *sd) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6063 | { |
| 6064 | if (cpus_weight(sd->span) == 1) |
| 6065 | return 1; |
| 6066 | |
| 6067 | /* Following flags need at least 2 groups */ |
| 6068 | if (sd->flags & (SD_LOAD_BALANCE | |
| 6069 | SD_BALANCE_NEWIDLE | |
| 6070 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6071 | SD_BALANCE_EXEC | |
| 6072 | SD_SHARE_CPUPOWER | |
| 6073 | SD_SHARE_PKG_RESOURCES)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6074 | if (sd->groups != sd->groups->next) |
| 6075 | return 0; |
| 6076 | } |
| 6077 | |
| 6078 | /* Following flags don't use groups */ |
| 6079 | if (sd->flags & (SD_WAKE_IDLE | |
| 6080 | SD_WAKE_AFFINE | |
| 6081 | SD_WAKE_BALANCE)) |
| 6082 | return 0; |
| 6083 | |
| 6084 | return 1; |
| 6085 | } |
| 6086 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6087 | static int |
| 6088 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6089 | { |
| 6090 | unsigned long cflags = sd->flags, pflags = parent->flags; |
| 6091 | |
| 6092 | if (sd_degenerate(parent)) |
| 6093 | return 1; |
| 6094 | |
| 6095 | if (!cpus_equal(sd->span, parent->span)) |
| 6096 | return 0; |
| 6097 | |
| 6098 | /* Does parent contain flags not in child? */ |
| 6099 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ |
| 6100 | if (cflags & SD_WAKE_AFFINE) |
| 6101 | pflags &= ~SD_WAKE_BALANCE; |
| 6102 | /* Flags needing groups don't count if only 1 group in parent */ |
| 6103 | if (parent->groups == parent->groups->next) { |
| 6104 | pflags &= ~(SD_LOAD_BALANCE | |
| 6105 | SD_BALANCE_NEWIDLE | |
| 6106 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6107 | SD_BALANCE_EXEC | |
| 6108 | SD_SHARE_CPUPOWER | |
| 6109 | SD_SHARE_PKG_RESOURCES); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6110 | } |
| 6111 | if (~cflags & pflags) |
| 6112 | return 0; |
| 6113 | |
| 6114 | return 1; |
| 6115 | } |
| 6116 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6117 | static void rq_attach_root(struct rq *rq, struct root_domain *rd) |
| 6118 | { |
| 6119 | unsigned long flags; |
| 6120 | const struct sched_class *class; |
| 6121 | |
| 6122 | spin_lock_irqsave(&rq->lock, flags); |
| 6123 | |
| 6124 | if (rq->rd) { |
| 6125 | struct root_domain *old_rd = rq->rd; |
| 6126 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6127 | for (class = sched_class_highest; class; class = class->next) { |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6128 | if (class->leave_domain) |
| 6129 | class->leave_domain(rq); |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6130 | } |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6131 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6132 | cpu_clear(rq->cpu, old_rd->span); |
| 6133 | cpu_clear(rq->cpu, old_rd->online); |
| 6134 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6135 | if (atomic_dec_and_test(&old_rd->refcount)) |
| 6136 | kfree(old_rd); |
| 6137 | } |
| 6138 | |
| 6139 | atomic_inc(&rd->refcount); |
| 6140 | rq->rd = rd; |
| 6141 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6142 | cpu_set(rq->cpu, rd->span); |
| 6143 | if (cpu_isset(rq->cpu, cpu_online_map)) |
| 6144 | cpu_set(rq->cpu, rd->online); |
| 6145 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6146 | for (class = sched_class_highest; class; class = class->next) { |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6147 | if (class->join_domain) |
| 6148 | class->join_domain(rq); |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6149 | } |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6150 | |
| 6151 | spin_unlock_irqrestore(&rq->lock, flags); |
| 6152 | } |
| 6153 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6154 | static void init_rootdomain(struct root_domain *rd) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6155 | { |
| 6156 | memset(rd, 0, sizeof(*rd)); |
| 6157 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6158 | cpus_clear(rd->span); |
| 6159 | cpus_clear(rd->online); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | static void init_defrootdomain(void) |
| 6163 | { |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6164 | init_rootdomain(&def_root_domain); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6165 | atomic_set(&def_root_domain.refcount, 1); |
| 6166 | } |
| 6167 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6168 | static struct root_domain *alloc_rootdomain(void) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6169 | { |
| 6170 | struct root_domain *rd; |
| 6171 | |
| 6172 | rd = kmalloc(sizeof(*rd), GFP_KERNEL); |
| 6173 | if (!rd) |
| 6174 | return NULL; |
| 6175 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6176 | init_rootdomain(rd); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6177 | |
| 6178 | return rd; |
| 6179 | } |
| 6180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | /* |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6182 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6183 | * hold the hotplug lock. |
| 6184 | */ |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 6185 | static void |
| 6186 | 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] | 6187 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6188 | struct rq *rq = cpu_rq(cpu); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6189 | struct sched_domain *tmp; |
| 6190 | |
| 6191 | /* Remove the sched domains which do not contribute to scheduling. */ |
| 6192 | for (tmp = sd; tmp; tmp = tmp->parent) { |
| 6193 | struct sched_domain *parent = tmp->parent; |
| 6194 | if (!parent) |
| 6195 | break; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6196 | if (sd_parent_degenerate(tmp, parent)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6197 | tmp->parent = parent->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6198 | if (parent->parent) |
| 6199 | parent->parent->child = tmp; |
| 6200 | } |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6201 | } |
| 6202 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6203 | if (sd && sd_degenerate(sd)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 6204 | sd = sd->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6205 | if (sd) |
| 6206 | sd->child = NULL; |
| 6207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6208 | |
| 6209 | sched_domain_debug(sd, cpu); |
| 6210 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6211 | rq_attach_root(rq, rd); |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 6212 | rcu_assign_pointer(rq->sd, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | } |
| 6214 | |
| 6215 | /* cpus with isolated domains */ |
Tim Chen | 67af63a | 2006-12-22 01:07:50 -0800 | [diff] [blame] | 6216 | static cpumask_t cpu_isolated_map = CPU_MASK_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | |
| 6218 | /* Setup the mask of cpus configured for isolated domains */ |
| 6219 | static int __init isolated_cpu_setup(char *str) |
| 6220 | { |
| 6221 | int ints[NR_CPUS], i; |
| 6222 | |
| 6223 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 6224 | cpus_clear(cpu_isolated_map); |
| 6225 | for (i = 1; i <= ints[0]; i++) |
| 6226 | if (ints[i] < NR_CPUS) |
| 6227 | cpu_set(ints[i], cpu_isolated_map); |
| 6228 | return 1; |
| 6229 | } |
| 6230 | |
Ingo Molnar | 8927f49 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 6231 | __setup("isolcpus=", isolated_cpu_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6232 | |
| 6233 | /* |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6234 | * init_sched_build_groups takes the cpumask we wish to span, and a pointer |
| 6235 | * to a function which identifies what group(along with sched group) a CPU |
| 6236 | * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS |
| 6237 | * (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] | 6238 | * |
| 6239 | * init_sched_build_groups will build a circular linked list of the groups |
| 6240 | * covered by the given span, and will set each group's ->cpumask correctly, |
| 6241 | * and ->cpu_power to 0. |
| 6242 | */ |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6243 | static void |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6244 | init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map, |
| 6245 | int (*group_fn)(int cpu, const cpumask_t *cpu_map, |
| 6246 | struct sched_group **sg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6247 | { |
| 6248 | struct sched_group *first = NULL, *last = NULL; |
| 6249 | cpumask_t covered = CPU_MASK_NONE; |
| 6250 | int i; |
| 6251 | |
| 6252 | for_each_cpu_mask(i, span) { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6253 | struct sched_group *sg; |
| 6254 | int group = group_fn(i, cpu_map, &sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6255 | int j; |
| 6256 | |
| 6257 | if (cpu_isset(i, covered)) |
| 6258 | continue; |
| 6259 | |
| 6260 | sg->cpumask = CPU_MASK_NONE; |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6261 | sg->__cpu_power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6262 | |
| 6263 | for_each_cpu_mask(j, span) { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6264 | if (group_fn(j, cpu_map, NULL) != group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6265 | continue; |
| 6266 | |
| 6267 | cpu_set(j, covered); |
| 6268 | cpu_set(j, sg->cpumask); |
| 6269 | } |
| 6270 | if (!first) |
| 6271 | first = sg; |
| 6272 | if (last) |
| 6273 | last->next = sg; |
| 6274 | last = sg; |
| 6275 | } |
| 6276 | last->next = first; |
| 6277 | } |
| 6278 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6279 | #define SD_NODES_PER_DOMAIN 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6281 | #ifdef CONFIG_NUMA |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 6282 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6283 | /** |
| 6284 | * find_next_best_node - find the next node to include in a sched_domain |
| 6285 | * @node: node whose sched_domain we're building |
| 6286 | * @used_nodes: nodes already in the sched_domain |
| 6287 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6288 | * Find the next node to include in a given scheduling domain. Simply |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6289 | * finds the closest node not already in the @used_nodes map. |
| 6290 | * |
| 6291 | * Should use nodemask_t. |
| 6292 | */ |
| 6293 | static int find_next_best_node(int node, unsigned long *used_nodes) |
| 6294 | { |
| 6295 | int i, n, val, min_val, best_node = 0; |
| 6296 | |
| 6297 | min_val = INT_MAX; |
| 6298 | |
| 6299 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6300 | /* Start at @node */ |
| 6301 | n = (node + i) % MAX_NUMNODES; |
| 6302 | |
| 6303 | if (!nr_cpus_node(n)) |
| 6304 | continue; |
| 6305 | |
| 6306 | /* Skip already used nodes */ |
| 6307 | if (test_bit(n, used_nodes)) |
| 6308 | continue; |
| 6309 | |
| 6310 | /* Simple min distance search */ |
| 6311 | val = node_distance(node, n); |
| 6312 | |
| 6313 | if (val < min_val) { |
| 6314 | min_val = val; |
| 6315 | best_node = n; |
| 6316 | } |
| 6317 | } |
| 6318 | |
| 6319 | set_bit(best_node, used_nodes); |
| 6320 | return best_node; |
| 6321 | } |
| 6322 | |
| 6323 | /** |
| 6324 | * sched_domain_node_span - get a cpumask for a node's sched_domain |
| 6325 | * @node: node whose cpumask we're constructing |
| 6326 | * @size: number of nodes to include in this span |
| 6327 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6328 | * 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] | 6329 | * should be one that prevents unnecessary balancing, but also spreads tasks |
| 6330 | * out optimally. |
| 6331 | */ |
| 6332 | static cpumask_t sched_domain_node_span(int node) |
| 6333 | { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6334 | DECLARE_BITMAP(used_nodes, MAX_NUMNODES); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6335 | cpumask_t span, nodemask; |
| 6336 | int i; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6337 | |
| 6338 | cpus_clear(span); |
| 6339 | bitmap_zero(used_nodes, MAX_NUMNODES); |
| 6340 | |
| 6341 | nodemask = node_to_cpumask(node); |
| 6342 | cpus_or(span, span, nodemask); |
| 6343 | set_bit(node, used_nodes); |
| 6344 | |
| 6345 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { |
| 6346 | int next_node = find_next_best_node(node, used_nodes); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6347 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6348 | nodemask = node_to_cpumask(next_node); |
| 6349 | cpus_or(span, span, nodemask); |
| 6350 | } |
| 6351 | |
| 6352 | return span; |
| 6353 | } |
| 6354 | #endif |
| 6355 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6356 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6357 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6358 | /* |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6359 | * SMT sched-domains: |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6360 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | #ifdef CONFIG_SCHED_SMT |
| 6362 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6363 | static DEFINE_PER_CPU(struct sched_group, sched_group_cpus); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6364 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6365 | static int |
| 6366 | 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] | 6367 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6368 | if (sg) |
| 6369 | *sg = &per_cpu(sched_group_cpus, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6370 | return cpu; |
| 6371 | } |
| 6372 | #endif |
| 6373 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6374 | /* |
| 6375 | * multi-core sched-domains: |
| 6376 | */ |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6377 | #ifdef CONFIG_SCHED_MC |
| 6378 | static DEFINE_PER_CPU(struct sched_domain, core_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6379 | static DEFINE_PER_CPU(struct sched_group, sched_group_core); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6380 | #endif |
| 6381 | |
| 6382 | #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6383 | static int |
| 6384 | 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] | 6385 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6386 | int group; |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 6387 | cpumask_t mask = per_cpu(cpu_sibling_map, cpu); |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6388 | cpus_and(mask, mask, *cpu_map); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6389 | group = first_cpu(mask); |
| 6390 | if (sg) |
| 6391 | *sg = &per_cpu(sched_group_core, group); |
| 6392 | return group; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6393 | } |
| 6394 | #elif defined(CONFIG_SCHED_MC) |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6395 | static int |
| 6396 | 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] | 6397 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6398 | if (sg) |
| 6399 | *sg = &per_cpu(sched_group_core, cpu); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6400 | return cpu; |
| 6401 | } |
| 6402 | #endif |
| 6403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6404 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6405 | static DEFINE_PER_CPU(struct sched_group, sched_group_phys); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6406 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6407 | static int |
| 6408 | 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] | 6409 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6410 | int group; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6411 | #ifdef CONFIG_SCHED_MC |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6412 | cpumask_t mask = cpu_coregroup_map(cpu); |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6413 | cpus_and(mask, mask, *cpu_map); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6414 | group = first_cpu(mask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6415 | #elif defined(CONFIG_SCHED_SMT) |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 6416 | cpumask_t mask = per_cpu(cpu_sibling_map, cpu); |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6417 | cpus_and(mask, mask, *cpu_map); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6418 | group = first_cpu(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6419 | #else |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6420 | group = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | #endif |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6422 | if (sg) |
| 6423 | *sg = &per_cpu(sched_group_phys, group); |
| 6424 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | #ifdef CONFIG_NUMA |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6428 | /* |
| 6429 | * The init_sched_build_groups can't handle what we want to do with node |
| 6430 | * groups, so roll our own. Now each node has its own list of groups which |
| 6431 | * gets dynamically allocated. |
| 6432 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6433 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6434 | static struct sched_group **sched_group_nodes_bycpu[NR_CPUS]; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6435 | |
| 6436 | static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6437 | static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6438 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6439 | static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map, |
| 6440 | struct sched_group **sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6441 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6442 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu)); |
| 6443 | int group; |
| 6444 | |
| 6445 | cpus_and(nodemask, nodemask, *cpu_map); |
| 6446 | group = first_cpu(nodemask); |
| 6447 | |
| 6448 | if (sg) |
| 6449 | *sg = &per_cpu(sched_group_allnodes, group); |
| 6450 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6451 | } |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6452 | |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6453 | static void init_numa_sched_groups_power(struct sched_group *group_head) |
| 6454 | { |
| 6455 | struct sched_group *sg = group_head; |
| 6456 | int j; |
| 6457 | |
| 6458 | if (!sg) |
| 6459 | return; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6460 | do { |
| 6461 | for_each_cpu_mask(j, sg->cpumask) { |
| 6462 | struct sched_domain *sd; |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6463 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6464 | sd = &per_cpu(phys_domains, j); |
| 6465 | if (j != first_cpu(sd->groups->cpumask)) { |
| 6466 | /* |
| 6467 | * Only add "power" once for each |
| 6468 | * physical package. |
| 6469 | */ |
| 6470 | continue; |
| 6471 | } |
| 6472 | |
| 6473 | sg_inc_cpu_power(sg, sd->groups->__cpu_power); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6474 | } |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6475 | sg = sg->next; |
| 6476 | } while (sg != group_head); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6478 | #endif |
| 6479 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6480 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6481 | /* Free memory allocated for various sched_group structures */ |
| 6482 | static void free_sched_groups(const cpumask_t *cpu_map) |
| 6483 | { |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6484 | int cpu, i; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6485 | |
| 6486 | for_each_cpu_mask(cpu, *cpu_map) { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6487 | struct sched_group **sched_group_nodes |
| 6488 | = sched_group_nodes_bycpu[cpu]; |
| 6489 | |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6490 | if (!sched_group_nodes) |
| 6491 | continue; |
| 6492 | |
| 6493 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6494 | cpumask_t nodemask = node_to_cpumask(i); |
| 6495 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; |
| 6496 | |
| 6497 | cpus_and(nodemask, nodemask, *cpu_map); |
| 6498 | if (cpus_empty(nodemask)) |
| 6499 | continue; |
| 6500 | |
| 6501 | if (sg == NULL) |
| 6502 | continue; |
| 6503 | sg = sg->next; |
| 6504 | next_sg: |
| 6505 | oldsg = sg; |
| 6506 | sg = sg->next; |
| 6507 | kfree(oldsg); |
| 6508 | if (oldsg != sched_group_nodes[i]) |
| 6509 | goto next_sg; |
| 6510 | } |
| 6511 | kfree(sched_group_nodes); |
| 6512 | sched_group_nodes_bycpu[cpu] = NULL; |
| 6513 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6514 | } |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6515 | #else |
| 6516 | static void free_sched_groups(const cpumask_t *cpu_map) |
| 6517 | { |
| 6518 | } |
| 6519 | #endif |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6521 | /* |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6522 | * Initialize sched groups cpu_power. |
| 6523 | * |
| 6524 | * cpu_power indicates the capacity of sched group, which is used while |
| 6525 | * distributing the load between different sched groups in a sched domain. |
| 6526 | * Typically cpu_power for all the groups in a sched domain will be same unless |
| 6527 | * there are asymmetries in the topology. If there are asymmetries, group |
| 6528 | * having more cpu_power will pickup more load compared to the group having |
| 6529 | * less cpu_power. |
| 6530 | * |
| 6531 | * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents |
| 6532 | * the maximum number of tasks a group can handle in the presence of other idle |
| 6533 | * or lightly loaded groups in the same sched domain. |
| 6534 | */ |
| 6535 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) |
| 6536 | { |
| 6537 | struct sched_domain *child; |
| 6538 | struct sched_group *group; |
| 6539 | |
| 6540 | WARN_ON(!sd || !sd->groups); |
| 6541 | |
| 6542 | if (cpu != first_cpu(sd->groups->cpumask)) |
| 6543 | return; |
| 6544 | |
| 6545 | child = sd->child; |
| 6546 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6547 | sd->groups->__cpu_power = 0; |
| 6548 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6549 | /* |
| 6550 | * For perf policy, if the groups in child domain share resources |
| 6551 | * (for example cores sharing some portions of the cache hierarchy |
| 6552 | * or SMT), then set this domain groups cpu_power such that each group |
| 6553 | * can handle only one task, when there are other idle groups in the |
| 6554 | * same sched domain. |
| 6555 | */ |
| 6556 | if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) && |
| 6557 | (child->flags & |
| 6558 | (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6559 | sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6560 | return; |
| 6561 | } |
| 6562 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6563 | /* |
| 6564 | * add cpu_power of each child group to this groups cpu_power |
| 6565 | */ |
| 6566 | group = child->groups; |
| 6567 | do { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6568 | sg_inc_cpu_power(sd->groups, group->__cpu_power); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6569 | group = group->next; |
| 6570 | } while (group != child->groups); |
| 6571 | } |
| 6572 | |
| 6573 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6574 | * Build sched domains for a given set of cpus and attach the sched domains |
| 6575 | * to the individual cpus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6576 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6577 | static int build_sched_domains(const cpumask_t *cpu_map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | { |
| 6579 | int i; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6580 | struct root_domain *rd; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6581 | #ifdef CONFIG_NUMA |
| 6582 | struct sched_group **sched_group_nodes = NULL; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6583 | int sd_allnodes = 0; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6584 | |
| 6585 | /* |
| 6586 | * Allocate the per-node list of sched groups |
| 6587 | */ |
Milton Miller | 5cf9f06 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6588 | sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *), |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6589 | GFP_KERNEL); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6590 | if (!sched_group_nodes) { |
| 6591 | printk(KERN_WARNING "Can not alloc sched group node list\n"); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6592 | return -ENOMEM; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6593 | } |
| 6594 | sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; |
| 6595 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6596 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 6597 | rd = alloc_rootdomain(); |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6598 | if (!rd) { |
| 6599 | printk(KERN_WARNING "Cannot alloc root domain\n"); |
| 6600 | return -ENOMEM; |
| 6601 | } |
| 6602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6603 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6604 | * Set up domains for cpus specified by the cpu_map. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6606 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 | struct sched_domain *sd = NULL, *p; |
| 6608 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(i)); |
| 6609 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6610 | cpus_and(nodemask, nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | |
| 6612 | #ifdef CONFIG_NUMA |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6613 | if (cpus_weight(*cpu_map) > |
| 6614 | SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6615 | sd = &per_cpu(allnodes_domains, i); |
| 6616 | *sd = SD_ALLNODES_INIT; |
| 6617 | sd->span = *cpu_map; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6618 | cpu_to_allnodes_group(i, cpu_map, &sd->groups); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6619 | p = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6620 | sd_allnodes = 1; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6621 | } else |
| 6622 | p = NULL; |
| 6623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6624 | sd = &per_cpu(node_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6625 | *sd = SD_NODE_INIT; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6626 | sd->span = sched_domain_node_span(cpu_to_node(i)); |
| 6627 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6628 | if (p) |
| 6629 | p->child = sd; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6630 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6631 | #endif |
| 6632 | |
| 6633 | p = sd; |
| 6634 | sd = &per_cpu(phys_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6635 | *sd = SD_CPU_INIT; |
| 6636 | sd->span = nodemask; |
| 6637 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6638 | if (p) |
| 6639 | p->child = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6640 | cpu_to_phys_group(i, cpu_map, &sd->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6641 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6642 | #ifdef CONFIG_SCHED_MC |
| 6643 | p = sd; |
| 6644 | sd = &per_cpu(core_domains, i); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6645 | *sd = SD_MC_INIT; |
| 6646 | sd->span = cpu_coregroup_map(i); |
| 6647 | cpus_and(sd->span, sd->span, *cpu_map); |
| 6648 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6649 | p->child = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6650 | cpu_to_core_group(i, cpu_map, &sd->groups); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6651 | #endif |
| 6652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6653 | #ifdef CONFIG_SCHED_SMT |
| 6654 | p = sd; |
| 6655 | sd = &per_cpu(cpu_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | *sd = SD_SIBLING_INIT; |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 6657 | sd->span = per_cpu(cpu_sibling_map, i); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6658 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6659 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 6660 | p->child = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6661 | cpu_to_cpu_group(i, cpu_map, &sd->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | #endif |
| 6663 | } |
| 6664 | |
| 6665 | #ifdef CONFIG_SCHED_SMT |
| 6666 | /* Set up CPU (sibling) groups */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6667 | for_each_cpu_mask(i, *cpu_map) { |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 6668 | cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6669 | cpus_and(this_sibling_map, this_sibling_map, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6670 | if (i != first_cpu(this_sibling_map)) |
| 6671 | continue; |
| 6672 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6673 | init_sched_build_groups(this_sibling_map, cpu_map, |
| 6674 | &cpu_to_cpu_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6675 | } |
| 6676 | #endif |
| 6677 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6678 | #ifdef CONFIG_SCHED_MC |
| 6679 | /* Set up multi-core groups */ |
| 6680 | for_each_cpu_mask(i, *cpu_map) { |
| 6681 | cpumask_t this_core_map = cpu_coregroup_map(i); |
| 6682 | cpus_and(this_core_map, this_core_map, *cpu_map); |
| 6683 | if (i != first_cpu(this_core_map)) |
| 6684 | continue; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6685 | init_sched_build_groups(this_core_map, cpu_map, |
| 6686 | &cpu_to_core_group); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6687 | } |
| 6688 | #endif |
| 6689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6690 | /* Set up physical groups */ |
| 6691 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6692 | cpumask_t nodemask = node_to_cpumask(i); |
| 6693 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6694 | cpus_and(nodemask, nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6695 | if (cpus_empty(nodemask)) |
| 6696 | continue; |
| 6697 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6698 | init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | #ifdef CONFIG_NUMA |
| 6702 | /* Set up node groups */ |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6703 | if (sd_allnodes) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6704 | init_sched_build_groups(*cpu_map, cpu_map, |
| 6705 | &cpu_to_allnodes_group); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6706 | |
| 6707 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6708 | /* Set up node groups */ |
| 6709 | struct sched_group *sg, *prev; |
| 6710 | cpumask_t nodemask = node_to_cpumask(i); |
| 6711 | cpumask_t domainspan; |
| 6712 | cpumask_t covered = CPU_MASK_NONE; |
| 6713 | int j; |
| 6714 | |
| 6715 | cpus_and(nodemask, nodemask, *cpu_map); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6716 | if (cpus_empty(nodemask)) { |
| 6717 | sched_group_nodes[i] = NULL; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6718 | continue; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6719 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6720 | |
| 6721 | domainspan = sched_domain_node_span(i); |
| 6722 | cpus_and(domainspan, domainspan, *cpu_map); |
| 6723 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 6724 | sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6725 | if (!sg) { |
| 6726 | printk(KERN_WARNING "Can not alloc domain group for " |
| 6727 | "node %d\n", i); |
| 6728 | goto error; |
| 6729 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6730 | sched_group_nodes[i] = sg; |
| 6731 | for_each_cpu_mask(j, nodemask) { |
| 6732 | struct sched_domain *sd; |
Ingo Molnar | 9761eea | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 6733 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6734 | sd = &per_cpu(node_domains, j); |
| 6735 | sd->groups = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6736 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6737 | sg->__cpu_power = 0; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6738 | sg->cpumask = nodemask; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6739 | sg->next = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6740 | cpus_or(covered, covered, nodemask); |
| 6741 | prev = sg; |
| 6742 | |
| 6743 | for (j = 0; j < MAX_NUMNODES; j++) { |
| 6744 | cpumask_t tmp, notcovered; |
| 6745 | int n = (i + j) % MAX_NUMNODES; |
| 6746 | |
| 6747 | cpus_complement(notcovered, covered); |
| 6748 | cpus_and(tmp, notcovered, *cpu_map); |
| 6749 | cpus_and(tmp, tmp, domainspan); |
| 6750 | if (cpus_empty(tmp)) |
| 6751 | break; |
| 6752 | |
| 6753 | nodemask = node_to_cpumask(n); |
| 6754 | cpus_and(tmp, tmp, nodemask); |
| 6755 | if (cpus_empty(tmp)) |
| 6756 | continue; |
| 6757 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 6758 | sg = kmalloc_node(sizeof(struct sched_group), |
| 6759 | GFP_KERNEL, i); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6760 | if (!sg) { |
| 6761 | printk(KERN_WARNING |
| 6762 | "Can not alloc domain group for node %d\n", j); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6763 | goto error; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6764 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6765 | sg->__cpu_power = 0; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6766 | sg->cpumask = tmp; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6767 | sg->next = prev->next; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6768 | cpus_or(covered, covered, tmp); |
| 6769 | prev->next = sg; |
| 6770 | prev = sg; |
| 6771 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | #endif |
| 6774 | |
| 6775 | /* Calculate CPU power for physical packages and nodes */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6776 | #ifdef CONFIG_SCHED_SMT |
| 6777 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6778 | struct sched_domain *sd = &per_cpu(cpu_domains, i); |
| 6779 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6780 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6781 | } |
| 6782 | #endif |
| 6783 | #ifdef CONFIG_SCHED_MC |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6784 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6785 | struct sched_domain *sd = &per_cpu(core_domains, i); |
| 6786 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6787 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6788 | } |
| 6789 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6790 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6791 | for_each_cpu_mask(i, *cpu_map) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6792 | struct sched_domain *sd = &per_cpu(phys_domains, i); |
| 6793 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6794 | init_sched_groups_power(i, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6795 | } |
| 6796 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6797 | #ifdef CONFIG_NUMA |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6798 | for (i = 0; i < MAX_NUMNODES; i++) |
| 6799 | init_numa_sched_groups_power(sched_group_nodes[i]); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6800 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6801 | if (sd_allnodes) { |
| 6802 | struct sched_group *sg; |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 6803 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6804 | cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg); |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 6805 | init_numa_sched_groups_power(sg); |
| 6806 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6807 | #endif |
| 6808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | /* Attach the domains */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6810 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6811 | struct sched_domain *sd; |
| 6812 | #ifdef CONFIG_SCHED_SMT |
| 6813 | sd = &per_cpu(cpu_domains, i); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6814 | #elif defined(CONFIG_SCHED_MC) |
| 6815 | sd = &per_cpu(core_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | #else |
| 6817 | sd = &per_cpu(phys_domains, i); |
| 6818 | #endif |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6819 | cpu_attach_domain(sd, rd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6820 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6821 | |
| 6822 | return 0; |
| 6823 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6824 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6825 | error: |
| 6826 | free_sched_groups(cpu_map); |
| 6827 | return -ENOMEM; |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6828 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6829 | } |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6830 | |
| 6831 | static cpumask_t *doms_cur; /* current sched domains */ |
| 6832 | static int ndoms_cur; /* number of sched domains in 'doms_cur' */ |
| 6833 | |
| 6834 | /* |
| 6835 | * Special case: If a kmalloc of a doms_cur partition (array of |
| 6836 | * cpumask_t) fails, then fallback to a single sched domain, |
| 6837 | * as determined by the single cpumask_t fallback_doms. |
| 6838 | */ |
| 6839 | static cpumask_t fallback_doms; |
| 6840 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6841 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6842 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6843 | * For now this just excludes isolated cpus, but could be used to |
| 6844 | * exclude other special cases in the future. |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6845 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6846 | static int arch_init_sched_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6847 | { |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6848 | int err; |
| 6849 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6850 | ndoms_cur = 1; |
| 6851 | doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL); |
| 6852 | if (!doms_cur) |
| 6853 | doms_cur = &fallback_doms; |
| 6854 | cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map); |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6855 | err = build_sched_domains(doms_cur); |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6856 | register_sched_domain_sysctl(); |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6857 | |
| 6858 | return err; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6859 | } |
| 6860 | |
| 6861 | static void arch_destroy_sched_domains(const cpumask_t *cpu_map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6863 | free_sched_groups(cpu_map); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6866 | /* |
| 6867 | * Detach sched domains from a group of cpus specified in cpu_map |
| 6868 | * These cpus will now be attached to the NULL domain |
| 6869 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 6870 | static void detach_destroy_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6871 | { |
| 6872 | int i; |
| 6873 | |
Milton Miller | 6382bc9 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 6874 | unregister_sched_domain_sysctl(); |
| 6875 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6876 | for_each_cpu_mask(i, *cpu_map) |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 6877 | cpu_attach_domain(NULL, &def_root_domain, i); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6878 | synchronize_sched(); |
| 6879 | arch_destroy_sched_domains(cpu_map); |
| 6880 | } |
| 6881 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6882 | /* |
| 6883 | * Partition sched domains as specified by the 'ndoms_new' |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6884 | * cpumasks in the array doms_new[] of cpumasks. This compares |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6885 | * doms_new[] to the current sched domain partitioning, doms_cur[]. |
| 6886 | * It destroys each deleted domain and builds each new domain. |
| 6887 | * |
| 6888 | * '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] | 6889 | * The masks don't intersect (don't overlap.) We should setup one |
| 6890 | * sched domain for each mask. CPUs not in any of the cpumasks will |
| 6891 | * not be load balanced. If the same cpumask appears both in the |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6892 | * current 'doms_cur' domains and in the new 'doms_new', we can leave |
| 6893 | * it as it is. |
| 6894 | * |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6895 | * The passed in 'doms_new' should be kmalloc'd. This routine takes |
| 6896 | * 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] | 6897 | * failed the kmalloc call, then it can pass in doms_new == NULL, |
| 6898 | * and partition_sched_domains() will fallback to the single partition |
| 6899 | * 'fallback_doms'. |
| 6900 | * |
| 6901 | * Call with hotplug lock held |
| 6902 | */ |
| 6903 | void partition_sched_domains(int ndoms_new, cpumask_t *doms_new) |
| 6904 | { |
| 6905 | int i, j; |
| 6906 | |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 6907 | lock_doms_cur(); |
| 6908 | |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6909 | /* always unregister in case we don't destroy any domains */ |
| 6910 | unregister_sched_domain_sysctl(); |
| 6911 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6912 | if (doms_new == NULL) { |
| 6913 | ndoms_new = 1; |
| 6914 | doms_new = &fallback_doms; |
| 6915 | cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map); |
| 6916 | } |
| 6917 | |
| 6918 | /* Destroy deleted domains */ |
| 6919 | for (i = 0; i < ndoms_cur; i++) { |
| 6920 | for (j = 0; j < ndoms_new; j++) { |
| 6921 | if (cpus_equal(doms_cur[i], doms_new[j])) |
| 6922 | goto match1; |
| 6923 | } |
| 6924 | /* no match - a current sched domain not in new doms_new[] */ |
| 6925 | detach_destroy_domains(doms_cur + i); |
| 6926 | match1: |
| 6927 | ; |
| 6928 | } |
| 6929 | |
| 6930 | /* Build new domains */ |
| 6931 | for (i = 0; i < ndoms_new; i++) { |
| 6932 | for (j = 0; j < ndoms_cur; j++) { |
| 6933 | if (cpus_equal(doms_new[i], doms_cur[j])) |
| 6934 | goto match2; |
| 6935 | } |
| 6936 | /* no match - add a new doms_new */ |
| 6937 | build_sched_domains(doms_new + i); |
| 6938 | match2: |
| 6939 | ; |
| 6940 | } |
| 6941 | |
| 6942 | /* Remember the new sched domains */ |
| 6943 | if (doms_cur != &fallback_doms) |
| 6944 | kfree(doms_cur); |
| 6945 | doms_cur = doms_new; |
| 6946 | ndoms_cur = ndoms_new; |
Milton Miller | 7378547 | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 6947 | |
| 6948 | register_sched_domain_sysctl(); |
Srivatsa Vaddagiri | a183561 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 6949 | |
| 6950 | unlock_doms_cur(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 6951 | } |
| 6952 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6953 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Adrian Bunk | 6707de00 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 6954 | static int arch_reinit_sched_domains(void) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6955 | { |
| 6956 | int err; |
| 6957 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 6958 | get_online_cpus(); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6959 | detach_destroy_domains(&cpu_online_map); |
| 6960 | err = arch_init_sched_domains(&cpu_online_map); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 6961 | put_online_cpus(); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6962 | |
| 6963 | return err; |
| 6964 | } |
| 6965 | |
| 6966 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) |
| 6967 | { |
| 6968 | int ret; |
| 6969 | |
| 6970 | if (buf[0] != '0' && buf[0] != '1') |
| 6971 | return -EINVAL; |
| 6972 | |
| 6973 | if (smt) |
| 6974 | sched_smt_power_savings = (buf[0] == '1'); |
| 6975 | else |
| 6976 | sched_mc_power_savings = (buf[0] == '1'); |
| 6977 | |
| 6978 | ret = arch_reinit_sched_domains(); |
| 6979 | |
| 6980 | return ret ? ret : count; |
| 6981 | } |
| 6982 | |
Adrian Bunk | 6707de00 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 6983 | #ifdef CONFIG_SCHED_MC |
| 6984 | static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page) |
| 6985 | { |
| 6986 | return sprintf(page, "%u\n", sched_mc_power_savings); |
| 6987 | } |
| 6988 | static ssize_t sched_mc_power_savings_store(struct sys_device *dev, |
| 6989 | const char *buf, size_t count) |
| 6990 | { |
| 6991 | return sched_power_savings_store(buf, count, 0); |
| 6992 | } |
| 6993 | static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show, |
| 6994 | sched_mc_power_savings_store); |
| 6995 | #endif |
| 6996 | |
| 6997 | #ifdef CONFIG_SCHED_SMT |
| 6998 | static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page) |
| 6999 | { |
| 7000 | return sprintf(page, "%u\n", sched_smt_power_savings); |
| 7001 | } |
| 7002 | static ssize_t sched_smt_power_savings_store(struct sys_device *dev, |
| 7003 | const char *buf, size_t count) |
| 7004 | { |
| 7005 | return sched_power_savings_store(buf, count, 1); |
| 7006 | } |
| 7007 | static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show, |
| 7008 | sched_smt_power_savings_store); |
| 7009 | #endif |
| 7010 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7011 | int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) |
| 7012 | { |
| 7013 | int err = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7014 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 7015 | #ifdef CONFIG_SCHED_SMT |
| 7016 | if (smt_capable()) |
| 7017 | err = sysfs_create_file(&cls->kset.kobj, |
| 7018 | &attr_sched_smt_power_savings.attr); |
| 7019 | #endif |
| 7020 | #ifdef CONFIG_SCHED_MC |
| 7021 | if (!err && mc_capable()) |
| 7022 | err = sysfs_create_file(&cls->kset.kobj, |
| 7023 | &attr_sched_mc_power_savings.attr); |
| 7024 | #endif |
| 7025 | return err; |
| 7026 | } |
| 7027 | #endif |
| 7028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7029 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7030 | * Force a reinitialization of the sched domains hierarchy. The domains |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7031 | * and groups cannot be updated in place without racing with the balancing |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 7032 | * code, so we temporarily attach all running cpus to the NULL domain |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7033 | * which will prevent rebalancing while the sched domains are recalculated. |
| 7034 | */ |
| 7035 | static int update_sched_domains(struct notifier_block *nfb, |
| 7036 | unsigned long action, void *hcpu) |
| 7037 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | switch (action) { |
| 7039 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7040 | case CPU_UP_PREPARE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | case CPU_DOWN_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7042 | case CPU_DOWN_PREPARE_FROZEN: |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7043 | detach_destroy_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7044 | return NOTIFY_OK; |
| 7045 | |
| 7046 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7047 | case CPU_UP_CANCELED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7048 | case CPU_DOWN_FAILED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7049 | case CPU_DOWN_FAILED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7050 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7051 | case CPU_ONLINE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7052 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 7053 | case CPU_DEAD_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7054 | /* |
| 7055 | * Fall through and re-initialise the domains. |
| 7056 | */ |
| 7057 | break; |
| 7058 | default: |
| 7059 | return NOTIFY_DONE; |
| 7060 | } |
| 7061 | |
| 7062 | /* The hotplug lock is already held by cpu_up/cpu_down */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7063 | arch_init_sched_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7064 | |
| 7065 | return NOTIFY_OK; |
| 7066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7067 | |
| 7068 | void __init sched_init_smp(void) |
| 7069 | { |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7070 | cpumask_t non_isolated_cpus; |
| 7071 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7072 | get_online_cpus(); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 7073 | arch_init_sched_domains(&cpu_online_map); |
Nathan Lynch | e5e5673 | 2007-01-10 23:15:28 -0800 | [diff] [blame] | 7074 | cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7075 | if (cpus_empty(non_isolated_cpus)) |
| 7076 | cpu_set(smp_processor_id(), non_isolated_cpus); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7077 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7078 | /* XXX: Theoretical race here - CPU may be hotplugged now */ |
| 7079 | hotcpu_notifier(update_sched_domains, 0); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 7080 | |
| 7081 | /* Move init over to a non-isolated CPU */ |
| 7082 | if (set_cpus_allowed(current, non_isolated_cpus) < 0) |
| 7083 | BUG(); |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 7084 | sched_init_granularity(); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7085 | |
| 7086 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7087 | if (nr_cpu_ids == 1) |
| 7088 | return; |
| 7089 | |
| 7090 | lb_monitor_task = kthread_create(load_balance_monitor, NULL, |
| 7091 | "group_balance"); |
| 7092 | if (!IS_ERR(lb_monitor_task)) { |
| 7093 | lb_monitor_task->flags |= PF_NOFREEZE; |
| 7094 | wake_up_process(lb_monitor_task); |
| 7095 | } else { |
| 7096 | printk(KERN_ERR "Could not create load balance monitor thread" |
| 7097 | "(error = %ld) \n", PTR_ERR(lb_monitor_task)); |
| 7098 | } |
| 7099 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7100 | } |
| 7101 | #else |
| 7102 | void __init sched_init_smp(void) |
| 7103 | { |
Ingo Molnar | 19978ca | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 7104 | sched_init_granularity(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7105 | } |
| 7106 | #endif /* CONFIG_SMP */ |
| 7107 | |
| 7108 | int in_sched_functions(unsigned long addr) |
| 7109 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7110 | return in_lock_functions(addr) || |
| 7111 | (addr >= (unsigned long)__sched_text_start |
| 7112 | && addr < (unsigned long)__sched_text_end); |
| 7113 | } |
| 7114 | |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 7115 | 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] | 7116 | { |
| 7117 | cfs_rq->tasks_timeline = RB_ROOT; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7118 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7119 | cfs_rq->rq = rq; |
| 7120 | #endif |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 7121 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7122 | } |
| 7123 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7124 | static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) |
| 7125 | { |
| 7126 | struct rt_prio_array *array; |
| 7127 | int i; |
| 7128 | |
| 7129 | array = &rt_rq->active; |
| 7130 | for (i = 0; i < MAX_RT_PRIO; i++) { |
| 7131 | INIT_LIST_HEAD(array->queue + i); |
| 7132 | __clear_bit(i, array->bitmap); |
| 7133 | } |
| 7134 | /* delimiter for bitsearch: */ |
| 7135 | __set_bit(MAX_RT_PRIO, array->bitmap); |
| 7136 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7137 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 7138 | rt_rq->highest_prio = MAX_RT_PRIO; |
| 7139 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7140 | #ifdef CONFIG_SMP |
| 7141 | rt_rq->rt_nr_migratory = 0; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7142 | rt_rq->overloaded = 0; |
| 7143 | #endif |
| 7144 | |
| 7145 | rt_rq->rt_time = 0; |
| 7146 | rt_rq->rt_throttled = 0; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7147 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7148 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7149 | rt_rq->rt_nr_boosted = 0; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7150 | rt_rq->rq = rq; |
| 7151 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7152 | } |
| 7153 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7154 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7155 | static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg, |
| 7156 | struct cfs_rq *cfs_rq, struct sched_entity *se, |
| 7157 | int cpu, int add) |
| 7158 | { |
| 7159 | tg->cfs_rq[cpu] = cfs_rq; |
| 7160 | init_cfs_rq(cfs_rq, rq); |
| 7161 | cfs_rq->tg = tg; |
| 7162 | if (add) |
| 7163 | list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list); |
| 7164 | |
| 7165 | tg->se[cpu] = se; |
| 7166 | se->cfs_rq = &rq->cfs; |
| 7167 | se->my_q = cfs_rq; |
| 7168 | se->load.weight = tg->shares; |
| 7169 | se->load.inv_weight = div64_64(1ULL<<32, se->load.weight); |
| 7170 | se->parent = NULL; |
| 7171 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7172 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7173 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7174 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7175 | static void init_tg_rt_entry(struct rq *rq, struct task_group *tg, |
| 7176 | struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, |
| 7177 | int cpu, int add) |
| 7178 | { |
| 7179 | tg->rt_rq[cpu] = rt_rq; |
| 7180 | init_rt_rq(rt_rq, rq); |
| 7181 | rt_rq->tg = tg; |
| 7182 | rt_rq->rt_se = rt_se; |
| 7183 | if (add) |
| 7184 | list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list); |
| 7185 | |
| 7186 | tg->rt_se[cpu] = rt_se; |
| 7187 | rt_se->rt_rq = &rq->rt; |
| 7188 | rt_se->my_q = rt_rq; |
| 7189 | rt_se->parent = NULL; |
| 7190 | INIT_LIST_HEAD(&rt_se->run_list); |
| 7191 | } |
| 7192 | #endif |
| 7193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | void __init sched_init(void) |
| 7195 | { |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 7196 | int highest_cpu = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7197 | int i, j; |
| 7198 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7199 | #ifdef CONFIG_SMP |
| 7200 | init_defrootdomain(); |
| 7201 | #endif |
| 7202 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7203 | #ifdef CONFIG_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7204 | list_add(&init_task_group.list, &task_groups); |
| 7205 | #endif |
| 7206 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 7207 | for_each_possible_cpu(i) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 7208 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7209 | |
| 7210 | rq = cpu_rq(i); |
| 7211 | spin_lock_init(&rq->lock); |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 7212 | lockdep_set_class(&rq->lock, &rq->rq_lock_key); |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 7213 | rq->nr_running = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7214 | rq->clock = 1; |
| 7215 | init_cfs_rq(&rq->cfs, rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7216 | init_rt_rq(&rq->rt, rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7217 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7218 | init_task_group.shares = init_task_group_load; |
| 7219 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
| 7220 | init_tg_cfs_entry(rq, &init_task_group, |
| 7221 | &per_cpu(init_cfs_rq, i), |
| 7222 | &per_cpu(init_sched_entity, i), i, 1); |
| 7223 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7224 | #endif |
| 7225 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7226 | init_task_group.rt_runtime = |
| 7227 | sysctl_sched_rt_runtime * NSEC_PER_USEC; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7228 | INIT_LIST_HEAD(&rq->leaf_rt_rq_list); |
| 7229 | init_tg_rt_entry(rq, &init_task_group, |
| 7230 | &per_cpu(init_rt_rq, i), |
| 7231 | &per_cpu(init_sched_rt_entity, i), i, 1); |
| 7232 | #endif |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7233 | rq->rt_period_expire = 0; |
Peter Zijlstra | 48d5e25 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 7234 | rq->rt_throttled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7235 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7236 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) |
| 7237 | rq->cpu_load[j] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7238 | #ifdef CONFIG_SMP |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 7239 | rq->sd = NULL; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 7240 | rq->rd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7241 | rq->active_balance = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7242 | rq->next_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7243 | rq->push_cpu = 0; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 7244 | rq->cpu = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | rq->migration_thread = NULL; |
| 7246 | INIT_LIST_HEAD(&rq->migration_queue); |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 7247 | rq_attach_root(rq, &def_root_domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7248 | #endif |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7249 | init_rq_hrtick(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7250 | atomic_set(&rq->nr_iowait, 0); |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 7251 | highest_cpu = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7252 | } |
| 7253 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 7254 | set_load_weight(&init_task); |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 7255 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 7256 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 7257 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); |
| 7258 | #endif |
| 7259 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 7260 | #ifdef CONFIG_SMP |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 7261 | nr_cpu_ids = highest_cpu + 1; |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 7262 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL); |
| 7263 | #endif |
| 7264 | |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 7265 | #ifdef CONFIG_RT_MUTEXES |
| 7266 | plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); |
| 7267 | #endif |
| 7268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7269 | /* |
| 7270 | * The boot idle thread does lazy MMU switching as well: |
| 7271 | */ |
| 7272 | atomic_inc(&init_mm.mm_count); |
| 7273 | enter_lazy_tlb(&init_mm, current); |
| 7274 | |
| 7275 | /* |
| 7276 | * Make us the idle thread. Technically, schedule() should not be |
| 7277 | * called from this thread, however somewhere below it might be, |
| 7278 | * but because we are the idle thread, we just pick up running again |
| 7279 | * when this runqueue becomes "idle". |
| 7280 | */ |
| 7281 | init_idle(current, smp_processor_id()); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7282 | /* |
| 7283 | * During early bootup we pretend to be a normal task: |
| 7284 | */ |
| 7285 | current->sched_class = &fair_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7286 | } |
| 7287 | |
| 7288 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 7289 | void __might_sleep(char *file, int line) |
| 7290 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 7291 | #ifdef in_atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7292 | static unsigned long prev_jiffy; /* ratelimiting */ |
| 7293 | |
| 7294 | if ((in_atomic() || irqs_disabled()) && |
| 7295 | system_state == SYSTEM_RUNNING && !oops_in_progress) { |
| 7296 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 7297 | return; |
| 7298 | prev_jiffy = jiffies; |
Ingo Molnar | 91368d7 | 2006-03-23 03:00:54 -0800 | [diff] [blame] | 7299 | printk(KERN_ERR "BUG: sleeping function called from invalid" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | " context at %s:%d\n", file, line); |
| 7301 | printk("in_atomic():%d, irqs_disabled():%d\n", |
| 7302 | in_atomic(), irqs_disabled()); |
Peter Zijlstra | a4c410f | 2006-12-06 20:37:21 -0800 | [diff] [blame] | 7303 | debug_show_held_locks(current); |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 7304 | if (irqs_disabled()) |
| 7305 | print_irqtrace_events(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7306 | dump_stack(); |
| 7307 | } |
| 7308 | #endif |
| 7309 | } |
| 7310 | EXPORT_SYMBOL(__might_sleep); |
| 7311 | #endif |
| 7312 | |
| 7313 | #ifdef CONFIG_MAGIC_SYSRQ |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 7314 | static void normalize_task(struct rq *rq, struct task_struct *p) |
| 7315 | { |
| 7316 | int on_rq; |
| 7317 | update_rq_clock(rq); |
| 7318 | on_rq = p->se.on_rq; |
| 7319 | if (on_rq) |
| 7320 | deactivate_task(rq, p, 0); |
| 7321 | __setscheduler(rq, p, SCHED_NORMAL, 0); |
| 7322 | if (on_rq) { |
| 7323 | activate_task(rq, p, 0); |
| 7324 | resched_task(rq->curr); |
| 7325 | } |
| 7326 | } |
| 7327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7328 | void normalize_rt_tasks(void) |
| 7329 | { |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 7330 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7331 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 7332 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7333 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7334 | read_lock_irqsave(&tasklist_lock, flags); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 7335 | do_each_thread(g, p) { |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 7336 | /* |
| 7337 | * Only normalize user tasks: |
| 7338 | */ |
| 7339 | if (!p->mm) |
| 7340 | continue; |
| 7341 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7342 | p->se.exec_start = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 7343 | #ifdef CONFIG_SCHEDSTATS |
| 7344 | p->se.wait_start = 0; |
| 7345 | p->se.sleep_start = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7346 | p->se.block_start = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 7347 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7348 | task_rq(p)->clock = 0; |
| 7349 | |
| 7350 | if (!rt_task(p)) { |
| 7351 | /* |
| 7352 | * Renice negative nice level userspace |
| 7353 | * tasks back to 0: |
| 7354 | */ |
| 7355 | if (TASK_NICE(p) < 0 && p->mm) |
| 7356 | set_user_nice(p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7357 | continue; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 7358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7360 | spin_lock(&p->pi_lock); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 7361 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7362 | |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 7363 | normalize_task(rq, p); |
Andi Kleen | 3a5e4dc | 2007-10-15 17:00:15 +0200 | [diff] [blame] | 7364 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 7365 | __task_rq_unlock(rq); |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7366 | spin_unlock(&p->pi_lock); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 7367 | } while_each_thread(g, p); |
| 7368 | |
Peter Zijlstra | 4cf5d77 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7369 | read_unlock_irqrestore(&tasklist_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7370 | } |
| 7371 | |
| 7372 | #endif /* CONFIG_MAGIC_SYSRQ */ |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 7373 | |
| 7374 | #ifdef CONFIG_IA64 |
| 7375 | /* |
| 7376 | * These functions are only useful for the IA64 MCA handling. |
| 7377 | * |
| 7378 | * They can only be called when the whole system has been |
| 7379 | * stopped - every CPU needs to be quiescent, and no scheduling |
| 7380 | * activity can take place. Using them for anything else would |
| 7381 | * be a serious bug, and as a result, they aren't even visible |
| 7382 | * under any other configuration. |
| 7383 | */ |
| 7384 | |
| 7385 | /** |
| 7386 | * curr_task - return the current task for a given cpu. |
| 7387 | * @cpu: the processor in question. |
| 7388 | * |
| 7389 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 7390 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 7391 | struct task_struct *curr_task(int cpu) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 7392 | { |
| 7393 | return cpu_curr(cpu); |
| 7394 | } |
| 7395 | |
| 7396 | /** |
| 7397 | * set_curr_task - set the current task for a given cpu. |
| 7398 | * @cpu: the processor in question. |
| 7399 | * @p: the task pointer to set. |
| 7400 | * |
| 7401 | * Description: This function must only be used when non-maskable interrupts |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7402 | * are serviced on a separate stack. It allows the architecture to switch the |
| 7403 | * 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] | 7404 | * must be called with all CPU's synchronized, and interrupts disabled, the |
| 7405 | * and caller must save the original value of the current task (see |
| 7406 | * curr_task() above) and restore that value before reenabling interrupts and |
| 7407 | * re-starting the system. |
| 7408 | * |
| 7409 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 7410 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 7411 | void set_curr_task(int cpu, struct task_struct *p) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 7412 | { |
| 7413 | cpu_curr(cpu) = p; |
| 7414 | } |
| 7415 | |
| 7416 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7417 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7418 | #ifdef CONFIG_GROUP_SCHED |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7419 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7420 | #if defined CONFIG_FAIR_GROUP_SCHED && defined CONFIG_SMP |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7421 | /* |
| 7422 | * distribute shares of all task groups among their schedulable entities, |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7423 | * to reflect load distribution across cpus. |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7424 | */ |
| 7425 | static int rebalance_shares(struct sched_domain *sd, int this_cpu) |
| 7426 | { |
| 7427 | struct cfs_rq *cfs_rq; |
| 7428 | struct rq *rq = cpu_rq(this_cpu); |
| 7429 | cpumask_t sdspan = sd->span; |
| 7430 | int balanced = 1; |
| 7431 | |
| 7432 | /* Walk thr' all the task groups that we have */ |
| 7433 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
| 7434 | int i; |
| 7435 | unsigned long total_load = 0, total_shares; |
| 7436 | struct task_group *tg = cfs_rq->tg; |
| 7437 | |
| 7438 | /* Gather total task load of this group across cpus */ |
| 7439 | for_each_cpu_mask(i, sdspan) |
| 7440 | total_load += tg->cfs_rq[i]->load.weight; |
| 7441 | |
Ingo Molnar | 0eab914 | 2008-01-25 21:08:19 +0100 | [diff] [blame] | 7442 | /* Nothing to do if this group has no load */ |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7443 | if (!total_load) |
| 7444 | continue; |
| 7445 | |
| 7446 | /* |
| 7447 | * tg->shares represents the number of cpu shares the task group |
| 7448 | * is eligible to hold on a single cpu. On N cpus, it is |
| 7449 | * eligible to hold (N * tg->shares) number of cpu shares. |
| 7450 | */ |
| 7451 | total_shares = tg->shares * cpus_weight(sdspan); |
| 7452 | |
| 7453 | /* |
| 7454 | * redistribute total_shares across cpus as per the task load |
| 7455 | * distribution. |
| 7456 | */ |
| 7457 | for_each_cpu_mask(i, sdspan) { |
| 7458 | unsigned long local_load, local_shares; |
| 7459 | |
| 7460 | local_load = tg->cfs_rq[i]->load.weight; |
| 7461 | local_shares = (local_load * total_shares) / total_load; |
| 7462 | if (!local_shares) |
| 7463 | local_shares = MIN_GROUP_SHARES; |
| 7464 | if (local_shares == tg->se[i]->load.weight) |
| 7465 | continue; |
| 7466 | |
| 7467 | spin_lock_irq(&cpu_rq(i)->lock); |
| 7468 | set_se_shares(tg->se[i], local_shares); |
| 7469 | spin_unlock_irq(&cpu_rq(i)->lock); |
| 7470 | balanced = 0; |
| 7471 | } |
| 7472 | } |
| 7473 | |
| 7474 | return balanced; |
| 7475 | } |
| 7476 | |
| 7477 | /* |
| 7478 | * How frequently should we rebalance_shares() across cpus? |
| 7479 | * |
| 7480 | * The more frequently we rebalance shares, the more accurate is the fairness |
| 7481 | * of cpu bandwidth distribution between task groups. However higher frequency |
| 7482 | * also implies increased scheduling overhead. |
| 7483 | * |
| 7484 | * sysctl_sched_min_bal_int_shares represents the minimum interval between |
| 7485 | * consecutive calls to rebalance_shares() in the same sched domain. |
| 7486 | * |
| 7487 | * sysctl_sched_max_bal_int_shares represents the maximum interval between |
| 7488 | * consecutive calls to rebalance_shares() in the same sched domain. |
| 7489 | * |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 7490 | * These settings allows for the appropriate trade-off between accuracy of |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7491 | * fairness and the associated overhead. |
| 7492 | * |
| 7493 | */ |
| 7494 | |
| 7495 | /* default: 8ms, units: milliseconds */ |
| 7496 | const_debug unsigned int sysctl_sched_min_bal_int_shares = 8; |
| 7497 | |
| 7498 | /* default: 128ms, units: milliseconds */ |
| 7499 | const_debug unsigned int sysctl_sched_max_bal_int_shares = 128; |
| 7500 | |
| 7501 | /* kernel thread that runs rebalance_shares() periodically */ |
| 7502 | static int load_balance_monitor(void *unused) |
| 7503 | { |
| 7504 | unsigned int timeout = sysctl_sched_min_bal_int_shares; |
| 7505 | struct sched_param schedparm; |
| 7506 | int ret; |
| 7507 | |
| 7508 | /* |
| 7509 | * We don't want this thread's execution to be limited by the shares |
| 7510 | * assigned to default group (init_task_group). Hence make it run |
| 7511 | * as a SCHED_RR RT task at the lowest priority. |
| 7512 | */ |
| 7513 | schedparm.sched_priority = 1; |
| 7514 | ret = sched_setscheduler(current, SCHED_RR, &schedparm); |
| 7515 | if (ret) |
| 7516 | printk(KERN_ERR "Couldn't set SCHED_RR policy for load balance" |
| 7517 | " monitor thread (error = %d) \n", ret); |
| 7518 | |
| 7519 | while (!kthread_should_stop()) { |
| 7520 | int i, cpu, balanced = 1; |
| 7521 | |
| 7522 | /* Prevent cpus going down or coming up */ |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7523 | get_online_cpus(); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7524 | /* lockout changes to doms_cur[] array */ |
| 7525 | lock_doms_cur(); |
| 7526 | /* |
| 7527 | * Enter a rcu read-side critical section to safely walk rq->sd |
| 7528 | * chain on various cpus and to walk task group list |
| 7529 | * (rq->leaf_cfs_rq_list) in rebalance_shares(). |
| 7530 | */ |
| 7531 | rcu_read_lock(); |
| 7532 | |
| 7533 | for (i = 0; i < ndoms_cur; i++) { |
| 7534 | cpumask_t cpumap = doms_cur[i]; |
| 7535 | struct sched_domain *sd = NULL, *sd_prev = NULL; |
| 7536 | |
| 7537 | cpu = first_cpu(cpumap); |
| 7538 | |
| 7539 | /* Find the highest domain at which to balance shares */ |
| 7540 | for_each_domain(cpu, sd) { |
| 7541 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 7542 | continue; |
| 7543 | sd_prev = sd; |
| 7544 | } |
| 7545 | |
| 7546 | sd = sd_prev; |
| 7547 | /* sd == NULL? No load balance reqd in this domain */ |
| 7548 | if (!sd) |
| 7549 | continue; |
| 7550 | |
| 7551 | balanced &= rebalance_shares(sd, cpu); |
| 7552 | } |
| 7553 | |
| 7554 | rcu_read_unlock(); |
| 7555 | |
| 7556 | unlock_doms_cur(); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 7557 | put_online_cpus(); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7558 | |
| 7559 | if (!balanced) |
| 7560 | timeout = sysctl_sched_min_bal_int_shares; |
| 7561 | else if (timeout < sysctl_sched_max_bal_int_shares) |
| 7562 | timeout *= 2; |
| 7563 | |
| 7564 | msleep_interruptible(timeout); |
| 7565 | } |
| 7566 | |
| 7567 | return 0; |
| 7568 | } |
| 7569 | #endif /* CONFIG_SMP */ |
| 7570 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7571 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 7572 | static void free_fair_sched_group(struct task_group *tg) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7573 | { |
| 7574 | int i; |
| 7575 | |
| 7576 | for_each_possible_cpu(i) { |
| 7577 | if (tg->cfs_rq) |
| 7578 | kfree(tg->cfs_rq[i]); |
| 7579 | if (tg->se) |
| 7580 | kfree(tg->se[i]); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7581 | } |
| 7582 | |
| 7583 | kfree(tg->cfs_rq); |
| 7584 | kfree(tg->se); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7585 | } |
| 7586 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7587 | static int alloc_fair_sched_group(struct task_group *tg) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7588 | { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7589 | struct cfs_rq *cfs_rq; |
| 7590 | struct sched_entity *se; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7591 | struct rq *rq; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7592 | int i; |
| 7593 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7594 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7595 | if (!tg->cfs_rq) |
| 7596 | goto err; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7597 | tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7598 | if (!tg->se) |
| 7599 | goto err; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7600 | |
| 7601 | tg->shares = NICE_0_LOAD; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7602 | |
| 7603 | for_each_possible_cpu(i) { |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7604 | rq = cpu_rq(i); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7605 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7606 | cfs_rq = kmalloc_node(sizeof(struct cfs_rq), |
| 7607 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7608 | if (!cfs_rq) |
| 7609 | goto err; |
| 7610 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7611 | se = kmalloc_node(sizeof(struct sched_entity), |
| 7612 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7613 | if (!se) |
| 7614 | goto err; |
| 7615 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7616 | init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7617 | } |
| 7618 | |
| 7619 | return 1; |
| 7620 | |
| 7621 | err: |
| 7622 | return 0; |
| 7623 | } |
| 7624 | |
| 7625 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) |
| 7626 | { |
| 7627 | list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list, |
| 7628 | &cpu_rq(cpu)->leaf_cfs_rq_list); |
| 7629 | } |
| 7630 | |
| 7631 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 7632 | { |
| 7633 | list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list); |
| 7634 | } |
| 7635 | #else |
| 7636 | static inline void free_fair_sched_group(struct task_group *tg) |
| 7637 | { |
| 7638 | } |
| 7639 | |
| 7640 | static inline int alloc_fair_sched_group(struct task_group *tg) |
| 7641 | { |
| 7642 | return 1; |
| 7643 | } |
| 7644 | |
| 7645 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) |
| 7646 | { |
| 7647 | } |
| 7648 | |
| 7649 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 7650 | { |
| 7651 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7652 | #endif |
| 7653 | |
| 7654 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7655 | static void free_rt_sched_group(struct task_group *tg) |
| 7656 | { |
| 7657 | int i; |
| 7658 | |
| 7659 | for_each_possible_cpu(i) { |
| 7660 | if (tg->rt_rq) |
| 7661 | kfree(tg->rt_rq[i]); |
| 7662 | if (tg->rt_se) |
| 7663 | kfree(tg->rt_se[i]); |
| 7664 | } |
| 7665 | |
| 7666 | kfree(tg->rt_rq); |
| 7667 | kfree(tg->rt_se); |
| 7668 | } |
| 7669 | |
| 7670 | static int alloc_rt_sched_group(struct task_group *tg) |
| 7671 | { |
| 7672 | struct rt_rq *rt_rq; |
| 7673 | struct sched_rt_entity *rt_se; |
| 7674 | struct rq *rq; |
| 7675 | int i; |
| 7676 | |
| 7677 | tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL); |
| 7678 | if (!tg->rt_rq) |
| 7679 | goto err; |
| 7680 | tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL); |
| 7681 | if (!tg->rt_se) |
| 7682 | goto err; |
| 7683 | |
| 7684 | tg->rt_runtime = 0; |
| 7685 | |
| 7686 | for_each_possible_cpu(i) { |
| 7687 | rq = cpu_rq(i); |
| 7688 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7689 | rt_rq = kmalloc_node(sizeof(struct rt_rq), |
| 7690 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
| 7691 | if (!rt_rq) |
| 7692 | goto err; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7693 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7694 | rt_se = kmalloc_node(sizeof(struct sched_rt_entity), |
| 7695 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); |
| 7696 | if (!rt_se) |
| 7697 | goto err; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7698 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7699 | init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7700 | } |
| 7701 | |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7702 | return 1; |
| 7703 | |
| 7704 | err: |
| 7705 | return 0; |
| 7706 | } |
| 7707 | |
| 7708 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) |
| 7709 | { |
| 7710 | list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list, |
| 7711 | &cpu_rq(cpu)->leaf_rt_rq_list); |
| 7712 | } |
| 7713 | |
| 7714 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) |
| 7715 | { |
| 7716 | list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list); |
| 7717 | } |
| 7718 | #else |
| 7719 | static inline void free_rt_sched_group(struct task_group *tg) |
| 7720 | { |
| 7721 | } |
| 7722 | |
| 7723 | static inline int alloc_rt_sched_group(struct task_group *tg) |
| 7724 | { |
| 7725 | return 1; |
| 7726 | } |
| 7727 | |
| 7728 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) |
| 7729 | { |
| 7730 | } |
| 7731 | |
| 7732 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) |
| 7733 | { |
| 7734 | } |
| 7735 | #endif |
| 7736 | |
| 7737 | static void free_sched_group(struct task_group *tg) |
| 7738 | { |
| 7739 | free_fair_sched_group(tg); |
| 7740 | free_rt_sched_group(tg); |
| 7741 | kfree(tg); |
| 7742 | } |
| 7743 | |
| 7744 | /* allocate runqueue etc for a new task group */ |
| 7745 | struct task_group *sched_create_group(void) |
| 7746 | { |
| 7747 | struct task_group *tg; |
| 7748 | unsigned long flags; |
| 7749 | int i; |
| 7750 | |
| 7751 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); |
| 7752 | if (!tg) |
| 7753 | return ERR_PTR(-ENOMEM); |
| 7754 | |
| 7755 | if (!alloc_fair_sched_group(tg)) |
| 7756 | goto err; |
| 7757 | |
| 7758 | if (!alloc_rt_sched_group(tg)) |
| 7759 | goto err; |
| 7760 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7761 | spin_lock_irqsave(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7762 | for_each_possible_cpu(i) { |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7763 | register_fair_sched_group(tg, i); |
| 7764 | register_rt_sched_group(tg, i); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7765 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7766 | list_add_rcu(&tg->list, &task_groups); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7767 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7768 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7769 | return tg; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7770 | |
| 7771 | err: |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7772 | free_sched_group(tg); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7773 | return ERR_PTR(-ENOMEM); |
| 7774 | } |
| 7775 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7776 | /* rcu callback to free various structures associated with a task group */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7777 | static void free_sched_group_rcu(struct rcu_head *rhp) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7778 | { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7779 | /* now it should be safe to free those cfs_rqs */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7780 | free_sched_group(container_of(rhp, struct task_group, rcu)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7781 | } |
| 7782 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7783 | /* Destroy runqueue etc associated with a task group */ |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7784 | void sched_destroy_group(struct task_group *tg) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7785 | { |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7786 | unsigned long flags; |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7787 | int i; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7788 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7789 | spin_lock_irqsave(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7790 | for_each_possible_cpu(i) { |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7791 | unregister_fair_sched_group(tg, i); |
| 7792 | unregister_rt_sched_group(tg, i); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7793 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7794 | list_del_rcu(&tg->list); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7795 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7796 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7797 | /* wait for possible concurrent references to cfs_rqs complete */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7798 | call_rcu(&tg->rcu, free_sched_group_rcu); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7799 | } |
| 7800 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7801 | /* change task's runqueue when it moves between groups. |
Ingo Molnar | 3a25201 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 7802 | * The caller of this function should have put the task in its new group |
| 7803 | * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to |
| 7804 | * reflect its new group. |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7805 | */ |
| 7806 | void sched_move_task(struct task_struct *tsk) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7807 | { |
| 7808 | int on_rq, running; |
| 7809 | unsigned long flags; |
| 7810 | struct rq *rq; |
| 7811 | |
| 7812 | rq = task_rq_lock(tsk, &flags); |
| 7813 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7814 | update_rq_clock(rq); |
| 7815 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 7816 | running = task_current(rq, tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7817 | on_rq = tsk->se.on_rq; |
| 7818 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 7819 | if (on_rq) { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7820 | dequeue_task(rq, tsk, 0); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 7821 | if (unlikely(running)) |
| 7822 | tsk->sched_class->put_prev_task(rq, tsk); |
| 7823 | } |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7824 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7825 | set_task_rq(tsk, task_cpu(tsk)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7826 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 7827 | if (on_rq) { |
| 7828 | if (unlikely(running)) |
| 7829 | tsk->sched_class->set_curr_task(rq); |
Dmitry Adamushko | 7074bad | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7830 | enqueue_task(rq, tsk, 0); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 7831 | } |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7832 | |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7833 | task_rq_unlock(rq, &flags); |
| 7834 | } |
| 7835 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7836 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7837 | /* rq->lock to be locked by caller */ |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7838 | static void set_se_shares(struct sched_entity *se, unsigned long shares) |
| 7839 | { |
| 7840 | struct cfs_rq *cfs_rq = se->cfs_rq; |
| 7841 | struct rq *rq = cfs_rq->rq; |
| 7842 | int on_rq; |
| 7843 | |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7844 | if (!shares) |
| 7845 | shares = MIN_GROUP_SHARES; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7846 | |
| 7847 | on_rq = se->on_rq; |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7848 | if (on_rq) { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7849 | dequeue_entity(cfs_rq, se, 0); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7850 | dec_cpu_load(rq, se->load.weight); |
| 7851 | } |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7852 | |
| 7853 | se->load.weight = shares; |
| 7854 | se->load.inv_weight = div64_64((1ULL<<32), shares); |
| 7855 | |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7856 | if (on_rq) { |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7857 | enqueue_entity(cfs_rq, se, 0); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7858 | inc_cpu_load(rq, se->load.weight); |
| 7859 | } |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7860 | } |
| 7861 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7862 | static DEFINE_MUTEX(shares_mutex); |
| 7863 | |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7864 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7865 | { |
| 7866 | int i; |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7867 | unsigned long flags; |
Ingo Molnar | c61935f | 2008-01-22 11:24:58 +0100 | [diff] [blame] | 7868 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7869 | mutex_lock(&shares_mutex); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7870 | if (tg->shares == shares) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7871 | goto done; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7872 | |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7873 | if (shares < MIN_GROUP_SHARES) |
| 7874 | shares = MIN_GROUP_SHARES; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7875 | |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7876 | /* |
| 7877 | * Prevent any load balance activity (rebalance_shares, |
| 7878 | * load_balance_fair) from referring to this group first, |
| 7879 | * by taking it off the rq->leaf_cfs_rq_list on each cpu. |
| 7880 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7881 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7882 | for_each_possible_cpu(i) |
| 7883 | unregister_fair_sched_group(tg, i); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7884 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 6b2d770 | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 7885 | |
| 7886 | /* wait for any ongoing reference to this group to finish */ |
| 7887 | synchronize_sched(); |
| 7888 | |
| 7889 | /* |
| 7890 | * Now we are free to modify the group's share on each cpu |
| 7891 | * w/o tripping rebalance_share or load_balance_fair. |
| 7892 | */ |
| 7893 | tg->shares = shares; |
| 7894 | for_each_possible_cpu(i) { |
| 7895 | spin_lock_irq(&cpu_rq(i)->lock); |
| 7896 | set_se_shares(tg->se[i], shares); |
| 7897 | spin_unlock_irq(&cpu_rq(i)->lock); |
| 7898 | } |
| 7899 | |
| 7900 | /* |
| 7901 | * Enable load balance activity on this group, by inserting it back on |
| 7902 | * each cpu's rq->leaf_cfs_rq_list. |
| 7903 | */ |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7904 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7905 | for_each_possible_cpu(i) |
| 7906 | register_fair_sched_group(tg, i); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7907 | spin_unlock_irqrestore(&task_group_lock, flags); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7908 | done: |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7909 | mutex_unlock(&shares_mutex); |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 7910 | return 0; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 7911 | } |
| 7912 | |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7913 | unsigned long sched_group_shares(struct task_group *tg) |
| 7914 | { |
| 7915 | return tg->shares; |
| 7916 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7917 | #endif |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7918 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7919 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7920 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7921 | * Ensure that the real time constraints are schedulable. |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7922 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7923 | static DEFINE_MUTEX(rt_constraints_mutex); |
| 7924 | |
| 7925 | static unsigned long to_ratio(u64 period, u64 runtime) |
| 7926 | { |
| 7927 | if (runtime == RUNTIME_INF) |
| 7928 | return 1ULL << 16; |
| 7929 | |
| 7930 | runtime *= (1ULL << 16); |
| 7931 | div64_64(runtime, period); |
| 7932 | return runtime; |
| 7933 | } |
| 7934 | |
| 7935 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7936 | { |
| 7937 | struct task_group *tgi; |
| 7938 | unsigned long total = 0; |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7939 | unsigned long global_ratio = |
| 7940 | to_ratio(sysctl_sched_rt_period, |
| 7941 | sysctl_sched_rt_runtime < 0 ? |
| 7942 | RUNTIME_INF : sysctl_sched_rt_runtime); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7943 | |
| 7944 | rcu_read_lock(); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7945 | list_for_each_entry_rcu(tgi, &task_groups, list) { |
| 7946 | if (tgi == tg) |
| 7947 | continue; |
| 7948 | |
| 7949 | total += to_ratio(period, tgi->rt_runtime); |
| 7950 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7951 | rcu_read_unlock(); |
| 7952 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7953 | return total + to_ratio(period, runtime) < global_ratio; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7954 | } |
| 7955 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7956 | 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] | 7957 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7958 | u64 rt_runtime, rt_period; |
| 7959 | int err = 0; |
| 7960 | |
| 7961 | rt_period = sysctl_sched_rt_period * NSEC_PER_USEC; |
| 7962 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; |
| 7963 | if (rt_runtime_us == -1) |
| 7964 | rt_runtime = rt_period; |
| 7965 | |
| 7966 | mutex_lock(&rt_constraints_mutex); |
| 7967 | if (!__rt_schedulable(tg, rt_period, rt_runtime)) { |
| 7968 | err = -EINVAL; |
| 7969 | goto unlock; |
| 7970 | } |
| 7971 | if (rt_runtime_us == -1) |
| 7972 | rt_runtime = RUNTIME_INF; |
| 7973 | tg->rt_runtime = rt_runtime; |
| 7974 | unlock: |
| 7975 | mutex_unlock(&rt_constraints_mutex); |
| 7976 | |
| 7977 | return err; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 7978 | } |
| 7979 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 7980 | long sched_group_rt_runtime(struct task_group *tg) |
| 7981 | { |
| 7982 | u64 rt_runtime_us; |
| 7983 | |
| 7984 | if (tg->rt_runtime == RUNTIME_INF) |
| 7985 | return -1; |
| 7986 | |
| 7987 | rt_runtime_us = tg->rt_runtime; |
| 7988 | do_div(rt_runtime_us, NSEC_PER_USEC); |
| 7989 | return rt_runtime_us; |
| 7990 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7991 | #endif |
| 7992 | #endif /* CONFIG_GROUP_SCHED */ |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7993 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 7994 | #ifdef CONFIG_CGROUP_SCHED |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7995 | |
| 7996 | /* return corresponding task_group object of a cgroup */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 7997 | static inline struct task_group *cgroup_tg(struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 7998 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 7999 | return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id), |
| 8000 | struct task_group, css); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8001 | } |
| 8002 | |
| 8003 | static struct cgroup_subsys_state * |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8004 | cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8005 | { |
| 8006 | struct task_group *tg; |
| 8007 | |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8008 | if (!cgrp->parent) { |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8009 | /* This is early initialization for the top cgroup */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8010 | init_task_group.css.cgroup = cgrp; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8011 | return &init_task_group.css; |
| 8012 | } |
| 8013 | |
| 8014 | /* we support only 1-level deep hierarchical scheduler atm */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8015 | if (cgrp->parent->parent) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8016 | return ERR_PTR(-EINVAL); |
| 8017 | |
| 8018 | tg = sched_create_group(); |
| 8019 | if (IS_ERR(tg)) |
| 8020 | return ERR_PTR(-ENOMEM); |
| 8021 | |
| 8022 | /* Bind the cgroup to task_group object we just created */ |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8023 | tg->css.cgroup = cgrp; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8024 | |
| 8025 | return &tg->css; |
| 8026 | } |
| 8027 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8028 | static void |
| 8029 | cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8030 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8031 | struct task_group *tg = cgroup_tg(cgrp); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8032 | |
| 8033 | sched_destroy_group(tg); |
| 8034 | } |
| 8035 | |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8036 | static int |
| 8037 | cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
| 8038 | struct task_struct *tsk) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8039 | { |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame^] | 8040 | #ifdef CONFIG_RT_GROUP_SCHED |
| 8041 | /* Don't accept realtime tasks when there is no way for them to run */ |
| 8042 | if (rt_task(tsk) && cgroup_tg(cgrp)->rt_runtime == 0) |
| 8043 | return -EINVAL; |
| 8044 | #else |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8045 | /* We don't support RT-tasks being in separate groups */ |
| 8046 | if (tsk->sched_class != &fair_sched_class) |
| 8047 | return -EINVAL; |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame^] | 8048 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8049 | |
| 8050 | return 0; |
| 8051 | } |
| 8052 | |
| 8053 | static void |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8054 | cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8055 | struct cgroup *old_cont, struct task_struct *tsk) |
| 8056 | { |
| 8057 | sched_move_task(tsk); |
| 8058 | } |
| 8059 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8060 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8061 | static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype, |
| 8062 | u64 shareval) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8063 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8064 | return sched_group_set_shares(cgroup_tg(cgrp), shareval); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8065 | } |
| 8066 | |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8067 | static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8068 | { |
Paul Menage | 2b01dfe | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 8069 | struct task_group *tg = cgroup_tg(cgrp); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8070 | |
| 8071 | return (u64) tg->shares; |
| 8072 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8073 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8074 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8075 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8076 | static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft, |
| 8077 | struct file *file, |
| 8078 | const char __user *userbuf, |
| 8079 | size_t nbytes, loff_t *unused_ppos) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8080 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8081 | char buffer[64]; |
| 8082 | int retval = 0; |
| 8083 | s64 val; |
| 8084 | char *end; |
| 8085 | |
| 8086 | if (!nbytes) |
| 8087 | return -EINVAL; |
| 8088 | if (nbytes >= sizeof(buffer)) |
| 8089 | return -E2BIG; |
| 8090 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 8091 | return -EFAULT; |
| 8092 | |
| 8093 | buffer[nbytes] = 0; /* nul-terminate */ |
| 8094 | |
| 8095 | /* strip newline if necessary */ |
| 8096 | if (nbytes && (buffer[nbytes-1] == '\n')) |
| 8097 | buffer[nbytes-1] = 0; |
| 8098 | val = simple_strtoll(buffer, &end, 0); |
| 8099 | if (*end) |
| 8100 | return -EINVAL; |
| 8101 | |
| 8102 | /* Pass to subsystem */ |
| 8103 | retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val); |
| 8104 | if (!retval) |
| 8105 | retval = nbytes; |
| 8106 | return retval; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8107 | } |
| 8108 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8109 | static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft, |
| 8110 | struct file *file, |
| 8111 | char __user *buf, size_t nbytes, |
| 8112 | loff_t *ppos) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8113 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8114 | char tmp[64]; |
| 8115 | long val = sched_group_rt_runtime(cgroup_tg(cgrp)); |
| 8116 | int len = sprintf(tmp, "%ld\n", val); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8117 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8118 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8119 | } |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8120 | #endif |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8121 | |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8122 | static struct cftype cpu_files[] = { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8123 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8124 | { |
| 8125 | .name = "shares", |
| 8126 | .read_uint = cpu_shares_read_uint, |
| 8127 | .write_uint = cpu_shares_write_uint, |
| 8128 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8129 | #endif |
| 8130 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8131 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 8132 | .name = "rt_runtime_us", |
| 8133 | .read = cpu_rt_runtime_read, |
| 8134 | .write = cpu_rt_runtime_write, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8135 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8136 | #endif |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8137 | }; |
| 8138 | |
| 8139 | static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont) |
| 8140 | { |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8141 | return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files)); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8142 | } |
| 8143 | |
| 8144 | struct cgroup_subsys cpu_cgroup_subsys = { |
Ingo Molnar | 38605ca | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 8145 | .name = "cpu", |
| 8146 | .create = cpu_cgroup_create, |
| 8147 | .destroy = cpu_cgroup_destroy, |
| 8148 | .can_attach = cpu_cgroup_can_attach, |
| 8149 | .attach = cpu_cgroup_attach, |
| 8150 | .populate = cpu_cgroup_populate, |
| 8151 | .subsys_id = cpu_cgroup_subsys_id, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 8152 | .early_init = 1, |
| 8153 | }; |
| 8154 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 8155 | #endif /* CONFIG_CGROUP_SCHED */ |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8156 | |
| 8157 | #ifdef CONFIG_CGROUP_CPUACCT |
| 8158 | |
| 8159 | /* |
| 8160 | * CPU accounting code for task groups. |
| 8161 | * |
| 8162 | * Based on the work by Paul Menage (menage@google.com) and Balbir Singh |
| 8163 | * (balbir@in.ibm.com). |
| 8164 | */ |
| 8165 | |
| 8166 | /* track cpu usage of a group of tasks */ |
| 8167 | struct cpuacct { |
| 8168 | struct cgroup_subsys_state css; |
| 8169 | /* cpuusage holds pointer to a u64-type object on every cpu */ |
| 8170 | u64 *cpuusage; |
| 8171 | }; |
| 8172 | |
| 8173 | struct cgroup_subsys cpuacct_subsys; |
| 8174 | |
| 8175 | /* return cpu accounting group corresponding to this container */ |
| 8176 | static inline struct cpuacct *cgroup_ca(struct cgroup *cont) |
| 8177 | { |
| 8178 | return container_of(cgroup_subsys_state(cont, cpuacct_subsys_id), |
| 8179 | struct cpuacct, css); |
| 8180 | } |
| 8181 | |
| 8182 | /* return cpu accounting group to which this task belongs */ |
| 8183 | static inline struct cpuacct *task_ca(struct task_struct *tsk) |
| 8184 | { |
| 8185 | return container_of(task_subsys_state(tsk, cpuacct_subsys_id), |
| 8186 | struct cpuacct, css); |
| 8187 | } |
| 8188 | |
| 8189 | /* create a new cpu accounting group */ |
| 8190 | static struct cgroup_subsys_state *cpuacct_create( |
| 8191 | struct cgroup_subsys *ss, struct cgroup *cont) |
| 8192 | { |
| 8193 | struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL); |
| 8194 | |
| 8195 | if (!ca) |
| 8196 | return ERR_PTR(-ENOMEM); |
| 8197 | |
| 8198 | ca->cpuusage = alloc_percpu(u64); |
| 8199 | if (!ca->cpuusage) { |
| 8200 | kfree(ca); |
| 8201 | return ERR_PTR(-ENOMEM); |
| 8202 | } |
| 8203 | |
| 8204 | return &ca->css; |
| 8205 | } |
| 8206 | |
| 8207 | /* destroy an existing cpu accounting group */ |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8208 | static void |
| 8209 | cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cont) |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 8210 | { |
| 8211 | struct cpuacct *ca = cgroup_ca(cont); |
| 8212 | |
| 8213 | free_percpu(ca->cpuusage); |
| 8214 | kfree(ca); |
| 8215 | } |
| 8216 | |
| 8217 | /* return total cpu usage (in nanoseconds) of a group */ |
| 8218 | static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft) |
| 8219 | { |
| 8220 | struct cpuacct *ca = cgroup_ca(cont); |
| 8221 | u64 totalcpuusage = 0; |
| 8222 | int i; |
| 8223 | |
| 8224 | for_each_possible_cpu(i) { |
| 8225 | u64 *cpuusage = percpu_ptr(ca->cpuusage, i); |
| 8226 | |
| 8227 | /* |
| 8228 | * Take rq->lock to make 64-bit addition safe on 32-bit |
| 8229 | * platforms. |
| 8230 | */ |
| 8231 | spin_lock_irq(&cpu_rq(i)->lock); |
| 8232 | totalcpuusage += *cpuusage; |
| 8233 | spin_unlock_irq(&cpu_rq(i)->lock); |
| 8234 | } |
| 8235 | |
| 8236 | return totalcpuusage; |
| 8237 | } |
| 8238 | |
| 8239 | static struct cftype files[] = { |
| 8240 | { |
| 8241 | .name = "usage", |
| 8242 | .read_uint = cpuusage_read, |
| 8243 | }, |
| 8244 | }; |
| 8245 | |
| 8246 | static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cont) |
| 8247 | { |
| 8248 | return cgroup_add_files(cont, ss, files, ARRAY_SIZE(files)); |
| 8249 | } |
| 8250 | |
| 8251 | /* |
| 8252 | * charge this task's execution time to its accounting group. |
| 8253 | * |
| 8254 | * called with rq->lock held. |
| 8255 | */ |
| 8256 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime) |
| 8257 | { |
| 8258 | struct cpuacct *ca; |
| 8259 | |
| 8260 | if (!cpuacct_subsys.active) |
| 8261 | return; |
| 8262 | |
| 8263 | ca = task_ca(tsk); |
| 8264 | if (ca) { |
| 8265 | u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk)); |
| 8266 | |
| 8267 | *cpuusage += cputime; |
| 8268 | } |
| 8269 | } |
| 8270 | |
| 8271 | struct cgroup_subsys cpuacct_subsys = { |
| 8272 | .name = "cpuacct", |
| 8273 | .create = cpuacct_create, |
| 8274 | .destroy = cpuacct_destroy, |
| 8275 | .populate = cpuacct_populate, |
| 8276 | .subsys_id = cpuacct_subsys_id, |
| 8277 | }; |
| 8278 | #endif /* CONFIG_CGROUP_CPUACCT */ |