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 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 18 | /* Serializes the updates to cpu_online_map, cpu_present_map */ |
Linus Torvalds | aa95387 | 2006-07-23 12:12:16 -0700 | [diff] [blame] | 19 | static DEFINE_MUTEX(cpu_add_remove_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Neil Brown | bd5349c | 2006-10-17 00:10:35 -0700 | [diff] [blame] | 21 | static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 23 | /* If set, cpu_up and cpu_down will return -EBUSY and do nothing. |
| 24 | * Should always be manipulated under cpu_add_remove_lock |
| 25 | */ |
| 26 | static int cpu_hotplug_disabled; |
| 27 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 28 | static struct { |
| 29 | struct task_struct *active_writer; |
| 30 | struct mutex lock; /* Synchronizes accesses to refcount, */ |
| 31 | /* |
| 32 | * Also blocks the new readers during |
| 33 | * an ongoing cpu hotplug operation. |
| 34 | */ |
| 35 | int refcount; |
| 36 | wait_queue_head_t writer_queue; |
| 37 | } cpu_hotplug; |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 38 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 39 | #define writer_exists() (cpu_hotplug.active_writer != NULL) |
| 40 | |
| 41 | void __init cpu_hotplug_init(void) |
| 42 | { |
| 43 | cpu_hotplug.active_writer = NULL; |
| 44 | mutex_init(&cpu_hotplug.lock); |
| 45 | cpu_hotplug.refcount = 0; |
| 46 | init_waitqueue_head(&cpu_hotplug.writer_queue); |
| 47 | } |
| 48 | |
| 49 | #ifdef CONFIG_HOTPLUG_CPU |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 50 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 51 | void get_online_cpus(void) |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 52 | { |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 53 | might_sleep(); |
| 54 | if (cpu_hotplug.active_writer == current) |
Linus Torvalds | aa95387 | 2006-07-23 12:12:16 -0700 | [diff] [blame] | 55 | return; |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 56 | mutex_lock(&cpu_hotplug.lock); |
| 57 | cpu_hotplug.refcount++; |
| 58 | mutex_unlock(&cpu_hotplug.lock); |
| 59 | |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 60 | } |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 61 | EXPORT_SYMBOL_GPL(get_online_cpus); |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 62 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 63 | void put_online_cpus(void) |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 64 | { |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 65 | if (cpu_hotplug.active_writer == current) |
Linus Torvalds | aa95387 | 2006-07-23 12:12:16 -0700 | [diff] [blame] | 66 | return; |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 67 | mutex_lock(&cpu_hotplug.lock); |
| 68 | cpu_hotplug.refcount--; |
| 69 | |
| 70 | if (unlikely(writer_exists()) && !cpu_hotplug.refcount) |
| 71 | wake_up(&cpu_hotplug.writer_queue); |
| 72 | |
| 73 | mutex_unlock(&cpu_hotplug.lock); |
| 74 | |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 75 | } |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 76 | EXPORT_SYMBOL_GPL(put_online_cpus); |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 77 | |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 78 | #endif /* CONFIG_HOTPLUG_CPU */ |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 79 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 80 | /* |
| 81 | * The following two API's must be used when attempting |
| 82 | * to serialize the updates to cpu_online_map, cpu_present_map. |
| 83 | */ |
| 84 | void cpu_maps_update_begin(void) |
| 85 | { |
| 86 | mutex_lock(&cpu_add_remove_lock); |
| 87 | } |
| 88 | |
| 89 | void cpu_maps_update_done(void) |
| 90 | { |
| 91 | mutex_unlock(&cpu_add_remove_lock); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * This ensures that the hotplug operation can begin only when the |
| 96 | * refcount goes to zero. |
| 97 | * |
| 98 | * Note that during a cpu-hotplug operation, the new readers, if any, |
| 99 | * will be blocked by the cpu_hotplug.lock |
| 100 | * |
| 101 | * Since cpu_maps_update_begin is always called after invoking |
| 102 | * cpu_maps_update_begin, we can be sure that only one writer is active. |
| 103 | * |
| 104 | * Note that theoretically, there is a possibility of a livelock: |
| 105 | * - Refcount goes to zero, last reader wakes up the sleeping |
| 106 | * writer. |
| 107 | * - Last reader unlocks the cpu_hotplug.lock. |
| 108 | * - A new reader arrives at this moment, bumps up the refcount. |
| 109 | * - The writer acquires the cpu_hotplug.lock finds the refcount |
| 110 | * non zero and goes to sleep again. |
| 111 | * |
| 112 | * However, this is very difficult to achieve in practice since |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 113 | * 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] | 114 | * |
| 115 | */ |
| 116 | static void cpu_hotplug_begin(void) |
| 117 | { |
| 118 | DECLARE_WAITQUEUE(wait, current); |
| 119 | |
| 120 | mutex_lock(&cpu_hotplug.lock); |
| 121 | |
| 122 | cpu_hotplug.active_writer = current; |
| 123 | add_wait_queue_exclusive(&cpu_hotplug.writer_queue, &wait); |
| 124 | while (cpu_hotplug.refcount) { |
| 125 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 126 | mutex_unlock(&cpu_hotplug.lock); |
| 127 | schedule(); |
| 128 | mutex_lock(&cpu_hotplug.lock); |
| 129 | } |
| 130 | remove_wait_queue_locked(&cpu_hotplug.writer_queue, &wait); |
| 131 | } |
| 132 | |
| 133 | static void cpu_hotplug_done(void) |
| 134 | { |
| 135 | cpu_hotplug.active_writer = NULL; |
| 136 | mutex_unlock(&cpu_hotplug.lock); |
| 137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* Need to know about CPUs going up/down? */ |
Chandra Seetharaman | 65edc68 | 2006-06-27 02:54:08 -0700 | [diff] [blame] | 139 | int __cpuinit register_cpu_notifier(struct notifier_block *nb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Neil Brown | bd5349c | 2006-10-17 00:10:35 -0700 | [diff] [blame] | 141 | int ret; |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 142 | cpu_maps_update_begin(); |
Neil Brown | bd5349c | 2006-10-17 00:10:35 -0700 | [diff] [blame] | 143 | ret = raw_notifier_chain_register(&cpu_chain, nb); |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 144 | cpu_maps_update_done(); |
Neil Brown | bd5349c | 2006-10-17 00:10:35 -0700 | [diff] [blame] | 145 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
Chandra Seetharaman | 65edc68 | 2006-06-27 02:54:08 -0700 | [diff] [blame] | 147 | |
| 148 | #ifdef CONFIG_HOTPLUG_CPU |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | EXPORT_SYMBOL(register_cpu_notifier); |
| 151 | |
Sam Ravnborg | 9647155 | 2008-04-29 00:58:48 -0700 | [diff] [blame^] | 152 | void __ref unregister_cpu_notifier(struct notifier_block *nb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 154 | cpu_maps_update_begin(); |
Neil Brown | bd5349c | 2006-10-17 00:10:35 -0700 | [diff] [blame] | 155 | raw_notifier_chain_unregister(&cpu_chain, nb); |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 156 | cpu_maps_update_done(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | EXPORT_SYMBOL(unregister_cpu_notifier); |
| 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | static inline void check_for_tasks(int cpu) |
| 161 | { |
| 162 | struct task_struct *p; |
| 163 | |
| 164 | write_lock_irq(&tasklist_lock); |
| 165 | for_each_process(p) { |
| 166 | if (task_cpu(p) == cpu && |
| 167 | (!cputime_eq(p->utime, cputime_zero) || |
| 168 | !cputime_eq(p->stime, cputime_zero))) |
| 169 | printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\ |
Heiko Carstens | e7407dc | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 170 | (state = %ld, flags = %x) \n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 171 | p->comm, task_pid_nr(p), cpu, |
| 172 | p->state, p->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | write_unlock_irq(&tasklist_lock); |
| 175 | } |
| 176 | |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 177 | struct take_cpu_down_param { |
| 178 | unsigned long mod; |
| 179 | void *hcpu; |
| 180 | }; |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* Take this CPU down. */ |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 183 | static int take_cpu_down(void *_param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 185 | struct take_cpu_down_param *param = _param; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | int err; |
| 187 | |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 188 | raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod, |
| 189 | param->hcpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* Ensure this CPU doesn't handle any more interrupts. */ |
| 191 | err = __cpu_disable(); |
| 192 | if (err < 0) |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 193 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 195 | /* Force idle task to run as soon as we yield: it should |
| 196 | immediately notice cpu is offline and die quickly. */ |
| 197 | sched_idle_next(); |
| 198 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 201 | /* Requires cpu_add_remove_lock to be held */ |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 202 | static int _cpu_down(unsigned int cpu, int tasks_frozen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Heiko Carstens | e7407dc | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 204 | int err, nr_calls = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | struct task_struct *p; |
| 206 | cpumask_t old_allowed, tmp; |
Heiko Carstens | e7407dc | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 207 | void *hcpu = (void *)(long)cpu; |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 208 | unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 209 | struct take_cpu_down_param tcd_param = { |
| 210 | .mod = mod, |
| 211 | .hcpu = hcpu, |
| 212 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 214 | if (num_online_cpus() == 1) |
| 215 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 217 | if (!cpu_online(cpu)) |
| 218 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 220 | cpu_hotplug_begin(); |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 221 | err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod, |
Heiko Carstens | e7407dc | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 222 | hcpu, -1, &nr_calls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (err == NOTIFY_BAD) { |
Akinobu Mita | a0d8cdb | 2007-10-18 03:05:12 -0700 | [diff] [blame] | 224 | nr_calls--; |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 225 | __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod, |
| 226 | hcpu, nr_calls, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | printk("%s: attempt to take down CPU %u failed\n", |
| 228 | __FUNCTION__, cpu); |
Gautham R Shenoy | baaca49 | 2007-05-09 02:34:03 -0700 | [diff] [blame] | 229 | err = -EINVAL; |
| 230 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* Ensure that we are not runnable on dying cpu */ |
| 234 | old_allowed = current->cpus_allowed; |
Mike Travis | f70316d | 2008-04-04 18:11:06 -0700 | [diff] [blame] | 235 | cpus_setall(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | cpu_clear(cpu, tmp); |
Mike Travis | f70316d | 2008-04-04 18:11:06 -0700 | [diff] [blame] | 237 | set_cpus_allowed_ptr(current, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Avi Kivity | db912f9 | 2007-05-24 12:23:10 +0300 | [diff] [blame] | 239 | p = __stop_machine_run(take_cpu_down, &tcd_param, cpu); |
Linus Torvalds | aa95387 | 2006-07-23 12:12:16 -0700 | [diff] [blame] | 240 | |
Satoru Takeuchi | 8fa1d7d | 2006-10-28 10:38:57 -0700 | [diff] [blame] | 241 | if (IS_ERR(p) || cpu_online(cpu)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /* CPU didn't die: tell everyone. Can't complain. */ |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 243 | if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod, |
Heiko Carstens | e7407dc | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 244 | hcpu) == NOTIFY_BAD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | BUG(); |
| 246 | |
Satoru Takeuchi | 8fa1d7d | 2006-10-28 10:38:57 -0700 | [diff] [blame] | 247 | if (IS_ERR(p)) { |
| 248 | err = PTR_ERR(p); |
| 249 | goto out_allowed; |
| 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | goto out_thread; |
Satoru Takeuchi | 8fa1d7d | 2006-10-28 10:38:57 -0700 | [diff] [blame] | 252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | /* Wait for it to sleep (leaving idle task). */ |
| 255 | while (!idle_cpu(cpu)) |
| 256 | yield(); |
| 257 | |
| 258 | /* This actually kills the CPU. */ |
| 259 | __cpu_die(cpu); |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | /* CPU is completely dead: tell everyone. Too late to complain. */ |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 262 | if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod, |
| 263 | hcpu) == NOTIFY_BAD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | BUG(); |
| 265 | |
| 266 | check_for_tasks(cpu); |
| 267 | |
| 268 | out_thread: |
| 269 | err = kthread_stop(p); |
| 270 | out_allowed: |
Mike Travis | f70316d | 2008-04-04 18:11:06 -0700 | [diff] [blame] | 271 | set_cpus_allowed_ptr(current, &old_allowed); |
Gautham R Shenoy | baaca49 | 2007-05-09 02:34:03 -0700 | [diff] [blame] | 272 | out_release: |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 273 | cpu_hotplug_done(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 274 | return err; |
| 275 | } |
| 276 | |
| 277 | int cpu_down(unsigned int cpu) |
| 278 | { |
| 279 | int err = 0; |
| 280 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 281 | cpu_maps_update_begin(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 282 | if (cpu_hotplug_disabled) |
| 283 | err = -EBUSY; |
| 284 | else |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 285 | err = _cpu_down(cpu, 0); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 286 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 287 | cpu_maps_update_done(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return err; |
| 289 | } |
| 290 | #endif /*CONFIG_HOTPLUG_CPU*/ |
| 291 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 292 | /* Requires cpu_add_remove_lock to be held */ |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 293 | static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Gautham R Shenoy | baaca49 | 2007-05-09 02:34:03 -0700 | [diff] [blame] | 295 | int ret, nr_calls = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | void *hcpu = (void *)(long)cpu; |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 297 | unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 299 | if (cpu_online(cpu) || !cpu_present(cpu)) |
| 300 | return -EINVAL; |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 301 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 302 | cpu_hotplug_begin(); |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 303 | 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] | 304 | -1, &nr_calls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | if (ret == NOTIFY_BAD) { |
Akinobu Mita | a0d8cdb | 2007-10-18 03:05:12 -0700 | [diff] [blame] | 306 | nr_calls--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | printk("%s: attempt to bring up CPU %u failed\n", |
| 308 | __FUNCTION__, cpu); |
| 309 | ret = -EINVAL; |
| 310 | goto out_notify; |
| 311 | } |
| 312 | |
| 313 | /* Arch-specific enabling code. */ |
| 314 | ret = __cpu_up(cpu); |
| 315 | if (ret != 0) |
| 316 | goto out_notify; |
Eric Sesterhenn | 6978c70 | 2006-03-24 18:45:21 +0100 | [diff] [blame] | 317 | BUG_ON(!cpu_online(cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | /* Now call notifier in preparation. */ |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 320 | raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | out_notify: |
| 323 | if (ret != 0) |
Gautham R Shenoy | baaca49 | 2007-05-09 02:34:03 -0700 | [diff] [blame] | 324 | __raw_notifier_call_chain(&cpu_chain, |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 325 | CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL); |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 326 | cpu_hotplug_done(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return ret; |
| 329 | } |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 330 | |
Gautham R Shenoy | b282b6f | 2007-01-10 23:15:34 -0800 | [diff] [blame] | 331 | int __cpuinit cpu_up(unsigned int cpu) |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 332 | { |
| 333 | int err = 0; |
KAMEZAWA Hiroyuki | 73e753a | 2007-10-18 23:40:47 -0700 | [diff] [blame] | 334 | if (!cpu_isset(cpu, cpu_possible_map)) { |
| 335 | printk(KERN_ERR "can't online cpu %d because it is not " |
| 336 | "configured as may-hotadd at boot time\n", cpu); |
| 337 | #if defined(CONFIG_IA64) || defined(CONFIG_X86_64) || defined(CONFIG_S390) |
| 338 | printk(KERN_ERR "please check additional_cpus= boot " |
| 339 | "parameter\n"); |
| 340 | #endif |
| 341 | return -EINVAL; |
| 342 | } |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 343 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 344 | cpu_maps_update_begin(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 345 | if (cpu_hotplug_disabled) |
| 346 | err = -EBUSY; |
| 347 | else |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 348 | err = _cpu_up(cpu, 0); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 349 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 350 | cpu_maps_update_done(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 351 | return err; |
| 352 | } |
| 353 | |
Rafael J. Wysocki | f3de4be | 2007-08-30 23:56:29 -0700 | [diff] [blame] | 354 | #ifdef CONFIG_PM_SLEEP_SMP |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 355 | static cpumask_t frozen_cpus; |
| 356 | |
| 357 | int disable_nonboot_cpus(void) |
| 358 | { |
Ingo Molnar | e1d9fd2 | 2006-12-23 16:55:29 +0100 | [diff] [blame] | 359 | int cpu, first_cpu, error = 0; |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 360 | |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 361 | cpu_maps_update_begin(); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 362 | first_cpu = first_cpu(cpu_online_map); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 363 | /* We take down all of the non-boot CPUs in one shot to avoid races |
| 364 | * with the userspace trying to use the CPU hotplug at the same time |
| 365 | */ |
| 366 | cpus_clear(frozen_cpus); |
| 367 | printk("Disabling non-boot CPUs ...\n"); |
| 368 | for_each_online_cpu(cpu) { |
| 369 | if (cpu == first_cpu) |
| 370 | continue; |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 371 | error = _cpu_down(cpu, 1); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 372 | if (!error) { |
| 373 | cpu_set(cpu, frozen_cpus); |
| 374 | printk("CPU%d is down\n", cpu); |
| 375 | } else { |
| 376 | printk(KERN_ERR "Error taking CPU%d down: %d\n", |
| 377 | cpu, error); |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | if (!error) { |
| 382 | BUG_ON(num_online_cpus() > 1); |
| 383 | /* Make sure the CPUs won't be enabled by someone else */ |
| 384 | cpu_hotplug_disabled = 1; |
| 385 | } else { |
Ingo Molnar | e1d9fd2 | 2006-12-23 16:55:29 +0100 | [diff] [blame] | 386 | printk(KERN_ERR "Non-boot CPUs are not disabled\n"); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 387 | } |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 388 | cpu_maps_update_done(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 389 | return error; |
| 390 | } |
| 391 | |
Sam Ravnborg | fa7303e | 2008-02-08 04:21:55 -0800 | [diff] [blame] | 392 | void __ref enable_nonboot_cpus(void) |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 393 | { |
| 394 | int cpu, error; |
| 395 | |
| 396 | /* Allow everyone to use the CPU hotplug again */ |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 397 | cpu_maps_update_begin(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 398 | cpu_hotplug_disabled = 0; |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 399 | if (cpus_empty(frozen_cpus)) |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 400 | goto out; |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 401 | |
| 402 | printk("Enabling non-boot CPUs ...\n"); |
| 403 | for_each_cpu_mask(cpu, frozen_cpus) { |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 404 | error = _cpu_up(cpu, 1); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 405 | if (!error) { |
| 406 | printk("CPU%d is up\n", cpu); |
| 407 | continue; |
| 408 | } |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 409 | 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] | 410 | } |
| 411 | cpus_clear(frozen_cpus); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 412 | out: |
Gautham R Shenoy | d221938 | 2008-01-25 21:08:01 +0100 | [diff] [blame] | 413 | cpu_maps_update_done(); |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 414 | } |
Rafael J. Wysocki | f3de4be | 2007-08-30 23:56:29 -0700 | [diff] [blame] | 415 | #endif /* CONFIG_PM_SLEEP_SMP */ |