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