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