Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* CPU control. |
| 2 | * (C) 2001, 2002, 2003, 2004 Rusty Russell |
| 3 | * |
| 4 | * This code is licenced under the GPL. |
| 5 | */ |
| 6 | #include <linux/proc_fs.h> |
| 7 | #include <linux/smp.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/notifier.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/unistd.h> |
| 12 | #include <linux/cpu.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kthread.h> |
| 15 | #include <linux/stop_machine.h> |
Ingo Molnar | 81615b62 | 2006-06-26 00:24:32 -0700 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Max Krasnyansky | 68f4f1e | 2008-05-29 11:17:02 -0700 | [diff] [blame] | 18 | /* |
| 19 | * Represents all cpu's present in the system |
| 20 | * In systems capable of hotplug, this map could dynamically grow |
| 21 | * as new cpu's are detected in the system via any platform specific |
| 22 | * method, such as ACPI for e.g. |
| 23 | */ |
| 24 | cpumask_t cpu_present_map __read_mostly; |
| 25 | EXPORT_SYMBOL(cpu_present_map); |
| 26 | |
| 27 | #ifndef CONFIG_SMP |
| 28 | |
| 29 | /* |
| 30 | * Represents all cpu's that are currently online. |
| 31 | */ |
| 32 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; |
| 33 | EXPORT_SYMBOL(cpu_online_map); |
| 34 | |
| 35 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; |
| 36 | EXPORT_SYMBOL(cpu_possible_map); |
| 37 | |
| 38 | #else /* CONFIG_SMP */ |
| 39 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 40 | /* Serializes the updates to cpu_online_map, cpu_present_map */ |
Linus Torvalds | aa95387 | 2006-07-23 12:12:16 -0700 | [diff] [blame] | 41 | static DEFINE_MUTEX(cpu_add_remove_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Neil Brown | bd5349c | 2006-10-17 00:10:35 -0700 | [diff] [blame] | 43 | static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 45 | /* If set, cpu_up and cpu_down will return -EBUSY and do nothing. |
| 46 | * Should always be manipulated under cpu_add_remove_lock |
| 47 | */ |
| 48 | static int cpu_hotplug_disabled; |
| 49 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 50 | static struct { |
| 51 | struct task_struct *active_writer; |
| 52 | struct mutex lock; /* Synchronizes accesses to refcount, */ |
| 53 | /* |
| 54 | * Also blocks the new readers during |
| 55 | * an ongoing cpu hotplug operation. |
| 56 | */ |
| 57 | int refcount; |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 58 | } cpu_hotplug; |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 59 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 60 | void __init cpu_hotplug_init(void) |
| 61 | { |
| 62 | cpu_hotplug.active_writer = NULL; |
| 63 | mutex_init(&cpu_hotplug.lock); |
| 64 | cpu_hotplug.refcount = 0; |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Max Krasnyansky | e761b77 | 2008-07-15 04:43:49 -0700 | [diff] [blame] | 67 | cpumask_t cpu_active_map; |
| 68 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 69 | #ifdef CONFIG_HOTPLUG_CPU |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 70 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 71 | void get_online_cpus(void) |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 72 | { |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 73 | might_sleep(); |
| 74 | if (cpu_hotplug.active_writer == current) |
Linus Torvalds | aa95387 | 2006-07-23 12:12:16 -0700 | [diff] [blame] | 75 | return; |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 76 | mutex_lock(&cpu_hotplug.lock); |
| 77 | cpu_hotplug.refcount++; |
| 78 | mutex_unlock(&cpu_hotplug.lock); |
| 79 | |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 80 | } |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 81 | EXPORT_SYMBOL_GPL(get_online_cpus); |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 82 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 83 | void put_online_cpus(void) |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 84 | { |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 85 | if (cpu_hotplug.active_writer == current) |
Linus Torvalds | aa95387 | 2006-07-23 12:12:16 -0700 | [diff] [blame] | 86 | return; |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 87 | mutex_lock(&cpu_hotplug.lock); |
Oleg Nesterov | d2ba7e2 | 2008-04-29 01:00:29 -0700 | [diff] [blame] | 88 | if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer)) |
| 89 | wake_up_process(cpu_hotplug.active_writer); |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 90 | mutex_unlock(&cpu_hotplug.lock); |
| 91 | |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 92 | } |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 93 | EXPORT_SYMBOL_GPL(put_online_cpus); |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 94 | |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 95 | #endif /* CONFIG_HOTPLUG_CPU */ |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 96 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 97 | /* |
| 98 | * The following two API's must be used when attempting |
| 99 | * to serialize the updates to cpu_online_map, cpu_present_map. |
| 100 | */ |
| 101 | void cpu_maps_update_begin(void) |
| 102 | { |
| 103 | mutex_lock(&cpu_add_remove_lock); |
| 104 | } |
| 105 | |
| 106 | void cpu_maps_update_done(void) |
| 107 | { |
| 108 | mutex_unlock(&cpu_add_remove_lock); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * This ensures that the hotplug operation can begin only when the |
| 113 | * refcount goes to zero. |
| 114 | * |
| 115 | * Note that during a cpu-hotplug operation, the new readers, if any, |
| 116 | * will be blocked by the cpu_hotplug.lock |
| 117 | * |
Oleg Nesterov | d2ba7e2 | 2008-04-29 01:00:29 -0700 | [diff] [blame] | 118 | * Since cpu_hotplug_begin() is always called after invoking |
| 119 | * cpu_maps_update_begin(), we can be sure that only one writer is active. |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 120 | * |
| 121 | * Note that theoretically, there is a possibility of a livelock: |
| 122 | * - Refcount goes to zero, last reader wakes up the sleeping |
| 123 | * writer. |
| 124 | * - Last reader unlocks the cpu_hotplug.lock. |
| 125 | * - A new reader arrives at this moment, bumps up the refcount. |
| 126 | * - The writer acquires the cpu_hotplug.lock finds the refcount |
| 127 | * non zero and goes to sleep again. |
| 128 | * |
| 129 | * However, this is very difficult to achieve in practice since |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 130 | * get_online_cpus() not an api which is called all that often. |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 131 | * |
| 132 | */ |
| 133 | static void cpu_hotplug_begin(void) |
| 134 | { |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 135 | cpu_hotplug.active_writer = current; |
Oleg Nesterov | d2ba7e2 | 2008-04-29 01:00:29 -0700 | [diff] [blame] | 136 | |
| 137 | for (;;) { |
| 138 | mutex_lock(&cpu_hotplug.lock); |
| 139 | if (likely(!cpu_hotplug.refcount)) |
| 140 | break; |
| 141 | __set_current_state(TASK_UNINTERRUPTIBLE); |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 142 | mutex_unlock(&cpu_hotplug.lock); |
| 143 | schedule(); |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 144 | } |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void cpu_hotplug_done(void) |
| 148 | { |
| 149 | cpu_hotplug.active_writer = NULL; |
| 150 | mutex_unlock(&cpu_hotplug.lock); |
| 151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* Need to know about CPUs going up/down? */ |
Sam Ravnborg | f7b16c1 | 2008-04-29 00:58:51 -0700 | [diff] [blame] | 153 | int __ref register_cpu_notifier(struct notifier_block *nb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Neil Brown | bd5349c | 2006-10-17 00:10:35 -0700 | [diff] [blame] | 155 | int ret; |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 156 | cpu_maps_update_begin(); |
Neil Brown | bd5349c | 2006-10-17 00:10:35 -0700 | [diff] [blame] | 157 | ret = raw_notifier_chain_register(&cpu_chain, nb); |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 158 | cpu_maps_update_done(); |
Neil Brown | bd5349c | 2006-10-17 00:10:35 -0700 | [diff] [blame] | 159 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
Chandra Seetharaman | 65edc68 | 2006-06-27 02:54:08 -0700 | [diff] [blame] | 161 | |
| 162 | #ifdef CONFIG_HOTPLUG_CPU |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | EXPORT_SYMBOL(register_cpu_notifier); |
| 165 | |
Sam Ravnborg | 9647155 | 2008-04-29 00:58:48 -0700 | [diff] [blame] | 166 | void __ref unregister_cpu_notifier(struct notifier_block *nb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 168 | cpu_maps_update_begin(); |
Neil Brown | bd5349c | 2006-10-17 00:10:35 -0700 | [diff] [blame] | 169 | raw_notifier_chain_unregister(&cpu_chain, nb); |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 170 | cpu_maps_update_done(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | EXPORT_SYMBOL(unregister_cpu_notifier); |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | static inline void check_for_tasks(int cpu) |
| 175 | { |
| 176 | struct task_struct *p; |
| 177 | |
| 178 | write_lock_irq(&tasklist_lock); |
| 179 | for_each_process(p) { |
| 180 | if (task_cpu(p) == cpu && |
| 181 | (!cputime_eq(p->utime, cputime_zero) || |
| 182 | !cputime_eq(p->stime, cputime_zero))) |
| 183 | printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\ |
Heiko Carstens | e7407dc | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 184 | (state = %ld, flags = %x) \n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 185 | p->comm, task_pid_nr(p), cpu, |
| 186 | p->state, p->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | write_unlock_irq(&tasklist_lock); |
| 189 | } |
| 190 | |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 191 | struct take_cpu_down_param { |
| 192 | unsigned long mod; |
| 193 | void *hcpu; |
| 194 | }; |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | /* Take this CPU down. */ |
Sam Ravnborg | 514a20a | 2008-04-29 00:58:50 -0700 | [diff] [blame] | 197 | static int __ref take_cpu_down(void *_param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 199 | struct take_cpu_down_param *param = _param; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | int err; |
| 201 | |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 202 | raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod, |
| 203 | param->hcpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /* Ensure this CPU doesn't handle any more interrupts. */ |
| 205 | err = __cpu_disable(); |
| 206 | if (err < 0) |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 207 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 209 | /* Force idle task to run as soon as we yield: it should |
| 210 | immediately notice cpu is offline and die quickly. */ |
| 211 | sched_idle_next(); |
| 212 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 215 | /* Requires cpu_add_remove_lock to be held */ |
Sam Ravnborg | 514a20a | 2008-04-29 00:58:50 -0700 | [diff] [blame] | 216 | static int __ref _cpu_down(unsigned int cpu, int tasks_frozen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Heiko Carstens | e7407dc | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 218 | int err, nr_calls = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | struct task_struct *p; |
| 220 | cpumask_t old_allowed, tmp; |
Heiko Carstens | e7407dc | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 221 | void *hcpu = (void *)(long)cpu; |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 222 | unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 223 | struct take_cpu_down_param tcd_param = { |
| 224 | .mod = mod, |
| 225 | .hcpu = hcpu, |
| 226 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 228 | if (num_online_cpus() == 1) |
| 229 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 231 | if (!cpu_online(cpu)) |
| 232 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 234 | cpu_hotplug_begin(); |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 235 | err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod, |
Heiko Carstens | e7407dc | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 236 | hcpu, -1, &nr_calls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | if (err == NOTIFY_BAD) { |
Akinobu Mita | a0d8cdb | 2007-10-18 03:05:12 -0700 | [diff] [blame] | 238 | nr_calls--; |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 239 | __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod, |
| 240 | hcpu, nr_calls, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | printk("%s: attempt to take down CPU %u failed\n", |
Harvey Harrison | af1f16d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 242 | __func__, cpu); |
Gautham R Shenoy | baaca49 | 2007-05-09 02:34:03 -0700 | [diff] [blame] | 243 | err = -EINVAL; |
| 244 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* Ensure that we are not runnable on dying cpu */ |
| 248 | old_allowed = current->cpus_allowed; |
Mike Travis | f70316d | 2008-04-04 18:11:06 -0700 | [diff] [blame] | 249 | cpus_setall(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | cpu_clear(cpu, tmp); |
Mike Travis | f70316d | 2008-04-04 18:11:06 -0700 | [diff] [blame] | 251 | set_cpus_allowed_ptr(current, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 253 | p = __stop_machine_run(take_cpu_down, &tcd_param, cpu); |
Linus Torvalds | aa95387 | 2006-07-23 12:12:16 -0700 | [diff] [blame] | 254 | |
Satoru Takeuchi | 8fa1d7d | 2006-10-28 10:38:57 -0700 | [diff] [blame] | 255 | if (IS_ERR(p) || cpu_online(cpu)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | /* CPU didn't die: tell everyone. Can't complain. */ |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 257 | if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod, |
Heiko Carstens | e7407dc | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 258 | hcpu) == NOTIFY_BAD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | BUG(); |
| 260 | |
Satoru Takeuchi | 8fa1d7d | 2006-10-28 10:38:57 -0700 | [diff] [blame] | 261 | if (IS_ERR(p)) { |
| 262 | err = PTR_ERR(p); |
| 263 | goto out_allowed; |
| 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | goto out_thread; |
Satoru Takeuchi | 8fa1d7d | 2006-10-28 10:38:57 -0700 | [diff] [blame] | 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | /* Wait for it to sleep (leaving idle task). */ |
| 269 | while (!idle_cpu(cpu)) |
| 270 | yield(); |
| 271 | |
| 272 | /* This actually kills the CPU. */ |
| 273 | __cpu_die(cpu); |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | /* CPU is completely dead: tell everyone. Too late to complain. */ |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 276 | if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod, |
| 277 | hcpu) == NOTIFY_BAD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | BUG(); |
| 279 | |
| 280 | check_for_tasks(cpu); |
| 281 | |
| 282 | out_thread: |
| 283 | err = kthread_stop(p); |
| 284 | out_allowed: |
Mike Travis | f70316d | 2008-04-04 18:11:06 -0700 | [diff] [blame] | 285 | set_cpus_allowed_ptr(current, &old_allowed); |
Gautham R Shenoy | baaca49 | 2007-05-09 02:34:03 -0700 | [diff] [blame] | 286 | out_release: |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 287 | cpu_hotplug_done(); |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 288 | if (!err) { |
| 289 | if (raw_notifier_call_chain(&cpu_chain, CPU_POST_DEAD | mod, |
| 290 | hcpu) == NOTIFY_BAD) |
| 291 | BUG(); |
| 292 | } |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 293 | return err; |
| 294 | } |
| 295 | |
Sam Ravnborg | 514a20a | 2008-04-29 00:58:50 -0700 | [diff] [blame] | 296 | int __ref cpu_down(unsigned int cpu) |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 297 | { |
| 298 | int err = 0; |
| 299 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 300 | cpu_maps_update_begin(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 301 | |
Max Krasnyansky | e761b77 | 2008-07-15 04:43:49 -0700 | [diff] [blame] | 302 | if (cpu_hotplug_disabled) { |
| 303 | err = -EBUSY; |
| 304 | goto out; |
| 305 | } |
| 306 | |
| 307 | cpu_clear(cpu, cpu_active_map); |
| 308 | |
Max Krasnyansky | 39b0fad | 2008-07-15 20:56:26 -0700 | [diff] [blame] | 309 | /* |
| 310 | * Make sure the all cpus did the reschedule and are not |
| 311 | * using stale version of the cpu_active_map. |
| 312 | * This is not strictly necessary becuase stop_machine() |
| 313 | * that we run down the line already provides the required |
| 314 | * synchronization. But it's really a side effect and we do not |
| 315 | * want to depend on the innards of the stop_machine here. |
| 316 | */ |
| 317 | synchronize_sched(); |
| 318 | |
Max Krasnyansky | e761b77 | 2008-07-15 04:43:49 -0700 | [diff] [blame] | 319 | err = _cpu_down(cpu, 0); |
| 320 | |
| 321 | if (cpu_online(cpu)) |
| 322 | cpu_set(cpu, cpu_active_map); |
| 323 | |
| 324 | out: |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 325 | cpu_maps_update_done(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | return err; |
| 327 | } |
Zhang Rui | b62b8ef | 2008-04-29 02:35:56 -0400 | [diff] [blame] | 328 | EXPORT_SYMBOL(cpu_down); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | #endif /*CONFIG_HOTPLUG_CPU*/ |
| 330 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 331 | /* Requires cpu_add_remove_lock to be held */ |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 332 | static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Gautham R Shenoy | baaca49 | 2007-05-09 02:34:03 -0700 | [diff] [blame] | 334 | int ret, nr_calls = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | void *hcpu = (void *)(long)cpu; |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 336 | unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 338 | if (cpu_online(cpu) || !cpu_present(cpu)) |
| 339 | return -EINVAL; |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 340 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 341 | cpu_hotplug_begin(); |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 342 | ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu, |
Gautham R Shenoy | baaca49 | 2007-05-09 02:34:03 -0700 | [diff] [blame] | 343 | -1, &nr_calls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | if (ret == NOTIFY_BAD) { |
Akinobu Mita | a0d8cdb | 2007-10-18 03:05:12 -0700 | [diff] [blame] | 345 | nr_calls--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | printk("%s: attempt to bring up CPU %u failed\n", |
Harvey Harrison | af1f16d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 347 | __func__, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | ret = -EINVAL; |
| 349 | goto out_notify; |
| 350 | } |
| 351 | |
| 352 | /* Arch-specific enabling code. */ |
| 353 | ret = __cpu_up(cpu); |
| 354 | if (ret != 0) |
| 355 | goto out_notify; |
Eric Sesterhenn | 6978c70 | 2006-03-24 18:45:21 +0100 | [diff] [blame] | 356 | BUG_ON(!cpu_online(cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | /* Now call notifier in preparation. */ |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 359 | raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | out_notify: |
| 362 | if (ret != 0) |
Gautham R Shenoy | baaca49 | 2007-05-09 02:34:03 -0700 | [diff] [blame] | 363 | __raw_notifier_call_chain(&cpu_chain, |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 364 | CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL); |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 365 | cpu_hotplug_done(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return ret; |
| 368 | } |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 369 | |
Gautham R Shenoy | b282b6f | 2007-01-10 23:15:34 -0800 | [diff] [blame] | 370 | int __cpuinit cpu_up(unsigned int cpu) |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 371 | { |
| 372 | int err = 0; |
KAMEZAWA Hiroyuki | 73e753a | 2007-10-18 23:40:47 -0700 | [diff] [blame] | 373 | if (!cpu_isset(cpu, cpu_possible_map)) { |
| 374 | printk(KERN_ERR "can't online cpu %d because it is not " |
| 375 | "configured as may-hotadd at boot time\n", cpu); |
| 376 | #if defined(CONFIG_IA64) || defined(CONFIG_X86_64) || defined(CONFIG_S390) |
| 377 | printk(KERN_ERR "please check additional_cpus= boot " |
| 378 | "parameter\n"); |
| 379 | #endif |
| 380 | return -EINVAL; |
| 381 | } |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 382 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 383 | cpu_maps_update_begin(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 384 | |
Max Krasnyansky | e761b77 | 2008-07-15 04:43:49 -0700 | [diff] [blame] | 385 | if (cpu_hotplug_disabled) { |
| 386 | err = -EBUSY; |
| 387 | goto out; |
| 388 | } |
| 389 | |
| 390 | err = _cpu_up(cpu, 0); |
| 391 | |
| 392 | if (cpu_online(cpu)) |
| 393 | cpu_set(cpu, cpu_active_map); |
| 394 | |
| 395 | out: |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 396 | cpu_maps_update_done(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 397 | return err; |
| 398 | } |
| 399 | |
Rafael J. Wysocki | f3de4be | 2007-08-30 23:56:29 -0700 | [diff] [blame] | 400 | #ifdef CONFIG_PM_SLEEP_SMP |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 401 | static cpumask_t frozen_cpus; |
| 402 | |
| 403 | int disable_nonboot_cpus(void) |
| 404 | { |
Ingo Molnar | e1d9fd2 | 2006-12-23 16:55:29 +0100 | [diff] [blame] | 405 | int cpu, first_cpu, error = 0; |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 406 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 407 | cpu_maps_update_begin(); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 408 | first_cpu = first_cpu(cpu_online_map); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 409 | /* We take down all of the non-boot CPUs in one shot to avoid races |
| 410 | * with the userspace trying to use the CPU hotplug at the same time |
| 411 | */ |
| 412 | cpus_clear(frozen_cpus); |
| 413 | printk("Disabling non-boot CPUs ...\n"); |
| 414 | for_each_online_cpu(cpu) { |
| 415 | if (cpu == first_cpu) |
| 416 | continue; |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 417 | error = _cpu_down(cpu, 1); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 418 | if (!error) { |
| 419 | cpu_set(cpu, frozen_cpus); |
| 420 | printk("CPU%d is down\n", cpu); |
| 421 | } else { |
| 422 | printk(KERN_ERR "Error taking CPU%d down: %d\n", |
| 423 | cpu, error); |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | if (!error) { |
| 428 | BUG_ON(num_online_cpus() > 1); |
| 429 | /* Make sure the CPUs won't be enabled by someone else */ |
| 430 | cpu_hotplug_disabled = 1; |
| 431 | } else { |
Ingo Molnar | e1d9fd2 | 2006-12-23 16:55:29 +0100 | [diff] [blame] | 432 | printk(KERN_ERR "Non-boot CPUs are not disabled\n"); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 433 | } |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 434 | cpu_maps_update_done(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 435 | return error; |
| 436 | } |
| 437 | |
Sam Ravnborg | fa7303e | 2008-02-08 04:21:55 -0800 | [diff] [blame] | 438 | void __ref enable_nonboot_cpus(void) |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 439 | { |
| 440 | int cpu, error; |
| 441 | |
| 442 | /* Allow everyone to use the CPU hotplug again */ |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 443 | cpu_maps_update_begin(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 444 | cpu_hotplug_disabled = 0; |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 445 | if (cpus_empty(frozen_cpus)) |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 446 | goto out; |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 447 | |
| 448 | printk("Enabling non-boot CPUs ...\n"); |
Mike Travis | 363ab6f | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 449 | for_each_cpu_mask_nr(cpu, frozen_cpus) { |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 450 | error = _cpu_up(cpu, 1); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 451 | if (!error) { |
| 452 | printk("CPU%d is up\n", cpu); |
| 453 | continue; |
| 454 | } |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 455 | printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 456 | } |
| 457 | cpus_clear(frozen_cpus); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 458 | out: |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 459 | cpu_maps_update_done(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 460 | } |
Rafael J. Wysocki | f3de4be | 2007-08-30 23:56:29 -0700 | [diff] [blame] | 461 | #endif /* CONFIG_PM_SLEEP_SMP */ |
Max Krasnyansky | 68f4f1e | 2008-05-29 11:17:02 -0700 | [diff] [blame] | 462 | |
| 463 | #endif /* CONFIG_SMP */ |
Mike Travis | b8d317d | 2008-07-24 18:21:29 -0700 | [diff] [blame^] | 464 | |
| 465 | #ifndef CONFIG_HAVE_CPUMASK_OF_CPU_MAP |
| 466 | /* 64 bits of zeros, for initializers. */ |
| 467 | #if BITS_PER_LONG == 32 |
| 468 | #define Z64 0, 0 |
| 469 | #else |
| 470 | #define Z64 0 |
| 471 | #endif |
| 472 | |
| 473 | /* Initializer macros. */ |
| 474 | #define CMI0(n) { .bits = { 1UL << (n) } } |
| 475 | #define CMI(n, ...) { .bits = { __VA_ARGS__, 1UL << ((n) % BITS_PER_LONG) } } |
| 476 | |
| 477 | #define CMI8(n, ...) \ |
| 478 | CMI((n), __VA_ARGS__), CMI((n)+1, __VA_ARGS__), \ |
| 479 | CMI((n)+2, __VA_ARGS__), CMI((n)+3, __VA_ARGS__), \ |
| 480 | CMI((n)+4, __VA_ARGS__), CMI((n)+5, __VA_ARGS__), \ |
| 481 | CMI((n)+6, __VA_ARGS__), CMI((n)+7, __VA_ARGS__) |
| 482 | |
| 483 | #if BITS_PER_LONG == 32 |
| 484 | #define CMI64(n, ...) \ |
| 485 | CMI8((n), __VA_ARGS__), CMI8((n)+8, __VA_ARGS__), \ |
| 486 | CMI8((n)+16, __VA_ARGS__), CMI8((n)+24, __VA_ARGS__), \ |
| 487 | CMI8((n)+32, 0, __VA_ARGS__), CMI8((n)+40, 0, __VA_ARGS__), \ |
| 488 | CMI8((n)+48, 0, __VA_ARGS__), CMI8((n)+56, 0, __VA_ARGS__) |
| 489 | #else |
| 490 | #define CMI64(n, ...) \ |
| 491 | CMI8((n), __VA_ARGS__), CMI8((n)+8, __VA_ARGS__), \ |
| 492 | CMI8((n)+16, __VA_ARGS__), CMI8((n)+24, __VA_ARGS__), \ |
| 493 | CMI8((n)+32, __VA_ARGS__), CMI8((n)+40, __VA_ARGS__), \ |
| 494 | CMI8((n)+48, __VA_ARGS__), CMI8((n)+56, __VA_ARGS__) |
| 495 | #endif |
| 496 | |
| 497 | #define CMI256(n, ...) \ |
| 498 | CMI64((n), __VA_ARGS__), CMI64((n)+64, Z64, __VA_ARGS__), \ |
| 499 | CMI64((n)+128, Z64, Z64, __VA_ARGS__), \ |
| 500 | CMI64((n)+192, Z64, Z64, Z64, __VA_ARGS__) |
| 501 | #define Z256 Z64, Z64, Z64, Z64 |
| 502 | |
| 503 | #define CMI1024(n, ...) \ |
| 504 | CMI256((n), __VA_ARGS__), \ |
| 505 | CMI256((n)+256, Z256, __VA_ARGS__), \ |
| 506 | CMI256((n)+512, Z256, Z256, __VA_ARGS__), \ |
| 507 | CMI256((n)+768, Z256, Z256, Z256, __VA_ARGS__) |
| 508 | #define Z1024 Z256, Z256, Z256, Z256 |
| 509 | |
| 510 | /* We want this statically initialized, just to be safe. We try not |
| 511 | * to waste too much space, either. */ |
| 512 | static const cpumask_t cpumask_map[] = { |
| 513 | CMI0(0), CMI0(1), CMI0(2), CMI0(3), |
| 514 | #if NR_CPUS > 4 |
| 515 | CMI0(4), CMI0(5), CMI0(6), CMI0(7), |
| 516 | #endif |
| 517 | #if NR_CPUS > 8 |
| 518 | CMI0(8), CMI0(9), CMI0(10), CMI0(11), |
| 519 | CMI0(12), CMI0(13), CMI0(14), CMI0(15), |
| 520 | #endif |
| 521 | #if NR_CPUS > 16 |
| 522 | CMI0(16), CMI0(17), CMI0(18), CMI0(19), |
| 523 | CMI0(20), CMI0(21), CMI0(22), CMI0(23), |
| 524 | CMI0(24), CMI0(25), CMI0(26), CMI0(27), |
| 525 | CMI0(28), CMI0(29), CMI0(30), CMI0(31), |
| 526 | #endif |
| 527 | #if NR_CPUS > 32 |
| 528 | #if BITS_PER_LONG == 32 |
| 529 | CMI(32, 0), CMI(33, 0), CMI(34, 0), CMI(35, 0), |
| 530 | CMI(36, 0), CMI(37, 0), CMI(38, 0), CMI(39, 0), |
| 531 | CMI(40, 0), CMI(41, 0), CMI(42, 0), CMI(43, 0), |
| 532 | CMI(44, 0), CMI(45, 0), CMI(46, 0), CMI(47, 0), |
| 533 | CMI(48, 0), CMI(49, 0), CMI(50, 0), CMI(51, 0), |
| 534 | CMI(52, 0), CMI(53, 0), CMI(54, 0), CMI(55, 0), |
| 535 | CMI(56, 0), CMI(57, 0), CMI(58, 0), CMI(59, 0), |
| 536 | CMI(60, 0), CMI(61, 0), CMI(62, 0), CMI(63, 0), |
| 537 | #else |
| 538 | CMI0(32), CMI0(33), CMI0(34), CMI0(35), |
| 539 | CMI0(36), CMI0(37), CMI0(38), CMI0(39), |
| 540 | CMI0(40), CMI0(41), CMI0(42), CMI0(43), |
| 541 | CMI0(44), CMI0(45), CMI0(46), CMI0(47), |
| 542 | CMI0(48), CMI0(49), CMI0(50), CMI0(51), |
| 543 | CMI0(52), CMI0(53), CMI0(54), CMI0(55), |
| 544 | CMI0(56), CMI0(57), CMI0(58), CMI0(59), |
| 545 | CMI0(60), CMI0(61), CMI0(62), CMI0(63), |
| 546 | #endif /* BITS_PER_LONG == 64 */ |
| 547 | #endif |
| 548 | #if NR_CPUS > 64 |
| 549 | CMI64(64, Z64), |
| 550 | #endif |
| 551 | #if NR_CPUS > 128 |
| 552 | CMI64(128, Z64, Z64), CMI64(192, Z64, Z64, Z64), |
| 553 | #endif |
| 554 | #if NR_CPUS > 256 |
| 555 | CMI256(256, Z256), |
| 556 | #endif |
| 557 | #if NR_CPUS > 512 |
| 558 | CMI256(512, Z256, Z256), CMI256(768, Z256, Z256, Z256), |
| 559 | #endif |
| 560 | #if NR_CPUS > 1024 |
| 561 | CMI1024(1024, Z1024), |
| 562 | #endif |
| 563 | #if NR_CPUS > 2048 |
| 564 | CMI1024(2048, Z1024, Z1024), CMI1024(3072, Z1024, Z1024, Z1024), |
| 565 | #endif |
| 566 | #if NR_CPUS > 4096 |
| 567 | #error NR_CPUS too big. Fix initializers or set CONFIG_HAVE_CPUMASK_OF_CPU_MAP |
| 568 | #endif |
| 569 | }; |
| 570 | |
| 571 | const cpumask_t *cpumask_of_cpu_map = cpumask_map; |
| 572 | #endif /* !CONFIG_HAVE_CPUMASK_OF_CPU_MAP */ |