11da177e4SLinus Torvalds /* CPU control. 21da177e4SLinus Torvalds * (C) 2001, 2002, 2003, 2004 Rusty Russell 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This code is licenced under the GPL. 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds #include <linux/proc_fs.h> 71da177e4SLinus Torvalds #include <linux/smp.h> 81da177e4SLinus Torvalds #include <linux/init.h> 91da177e4SLinus Torvalds #include <linux/notifier.h> 101da177e4SLinus Torvalds #include <linux/sched.h> 111da177e4SLinus Torvalds #include <linux/unistd.h> 121da177e4SLinus Torvalds #include <linux/cpu.h> 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/kthread.h> 151da177e4SLinus Torvalds #include <linux/stop_machine.h> 1681615b62SIngo Molnar #include <linux/mutex.h> 175a0e3ad6STejun Heo #include <linux/gfp.h> 181da177e4SLinus Torvalds 1998a79d6aSRusty Russell #ifdef CONFIG_SMP 20b3199c02SRusty Russell /* Serializes the updates to cpu_online_mask, cpu_present_mask */ 21aa953877SLinus Torvalds static DEFINE_MUTEX(cpu_add_remove_lock); 221da177e4SLinus Torvalds 23bd5349cfSNeil Brown static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain); 241da177e4SLinus Torvalds 25e3920fb4SRafael J. Wysocki /* If set, cpu_up and cpu_down will return -EBUSY and do nothing. 26e3920fb4SRafael J. Wysocki * Should always be manipulated under cpu_add_remove_lock 27e3920fb4SRafael J. Wysocki */ 28e3920fb4SRafael J. Wysocki static int cpu_hotplug_disabled; 29e3920fb4SRafael J. Wysocki 30d221938cSGautham R Shenoy static struct { 31d221938cSGautham R Shenoy struct task_struct *active_writer; 32d221938cSGautham R Shenoy struct mutex lock; /* Synchronizes accesses to refcount, */ 33d221938cSGautham R Shenoy /* 34d221938cSGautham R Shenoy * Also blocks the new readers during 35d221938cSGautham R Shenoy * an ongoing cpu hotplug operation. 36d221938cSGautham R Shenoy */ 37d221938cSGautham R Shenoy int refcount; 3831950eb6SLinus Torvalds } cpu_hotplug = { 3931950eb6SLinus Torvalds .active_writer = NULL, 4031950eb6SLinus Torvalds .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock), 4131950eb6SLinus Torvalds .refcount = 0, 4231950eb6SLinus Torvalds }; 43d221938cSGautham R Shenoy 44d221938cSGautham R Shenoy #ifdef CONFIG_HOTPLUG_CPU 45a9d9baa1SAshok Raj 4686ef5c9aSGautham R Shenoy void get_online_cpus(void) 47a9d9baa1SAshok Raj { 48d221938cSGautham R Shenoy might_sleep(); 49d221938cSGautham R Shenoy if (cpu_hotplug.active_writer == current) 50aa953877SLinus Torvalds return; 51d221938cSGautham R Shenoy mutex_lock(&cpu_hotplug.lock); 52d221938cSGautham R Shenoy cpu_hotplug.refcount++; 53d221938cSGautham R Shenoy mutex_unlock(&cpu_hotplug.lock); 54d221938cSGautham R Shenoy 55a9d9baa1SAshok Raj } 5686ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(get_online_cpus); 57a9d9baa1SAshok Raj 5886ef5c9aSGautham R Shenoy void put_online_cpus(void) 59a9d9baa1SAshok Raj { 60d221938cSGautham R Shenoy if (cpu_hotplug.active_writer == current) 61aa953877SLinus Torvalds return; 62d221938cSGautham R Shenoy mutex_lock(&cpu_hotplug.lock); 63d2ba7e2aSOleg Nesterov if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer)) 64d2ba7e2aSOleg Nesterov wake_up_process(cpu_hotplug.active_writer); 65d221938cSGautham R Shenoy mutex_unlock(&cpu_hotplug.lock); 66d221938cSGautham R Shenoy 67a9d9baa1SAshok Raj } 6886ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(put_online_cpus); 69a9d9baa1SAshok Raj 70a9d9baa1SAshok Raj #endif /* CONFIG_HOTPLUG_CPU */ 7190d45d17SAshok Raj 72d221938cSGautham R Shenoy /* 73d221938cSGautham R Shenoy * The following two API's must be used when attempting 74b3199c02SRusty Russell * to serialize the updates to cpu_online_mask, cpu_present_mask. 75d221938cSGautham R Shenoy */ 76d221938cSGautham R Shenoy void cpu_maps_update_begin(void) 77d221938cSGautham R Shenoy { 78d221938cSGautham R Shenoy mutex_lock(&cpu_add_remove_lock); 79d221938cSGautham R Shenoy } 80d221938cSGautham R Shenoy 81d221938cSGautham R Shenoy void cpu_maps_update_done(void) 82d221938cSGautham R Shenoy { 83d221938cSGautham R Shenoy mutex_unlock(&cpu_add_remove_lock); 84d221938cSGautham R Shenoy } 85d221938cSGautham R Shenoy 86d221938cSGautham R Shenoy /* 87d221938cSGautham R Shenoy * This ensures that the hotplug operation can begin only when the 88d221938cSGautham R Shenoy * refcount goes to zero. 89d221938cSGautham R Shenoy * 90d221938cSGautham R Shenoy * Note that during a cpu-hotplug operation, the new readers, if any, 91d221938cSGautham R Shenoy * will be blocked by the cpu_hotplug.lock 92d221938cSGautham R Shenoy * 93d2ba7e2aSOleg Nesterov * Since cpu_hotplug_begin() is always called after invoking 94d2ba7e2aSOleg Nesterov * cpu_maps_update_begin(), we can be sure that only one writer is active. 95d221938cSGautham R Shenoy * 96d221938cSGautham R Shenoy * Note that theoretically, there is a possibility of a livelock: 97d221938cSGautham R Shenoy * - Refcount goes to zero, last reader wakes up the sleeping 98d221938cSGautham R Shenoy * writer. 99d221938cSGautham R Shenoy * - Last reader unlocks the cpu_hotplug.lock. 100d221938cSGautham R Shenoy * - A new reader arrives at this moment, bumps up the refcount. 101d221938cSGautham R Shenoy * - The writer acquires the cpu_hotplug.lock finds the refcount 102d221938cSGautham R Shenoy * non zero and goes to sleep again. 103d221938cSGautham R Shenoy * 104d221938cSGautham R Shenoy * However, this is very difficult to achieve in practice since 10586ef5c9aSGautham R Shenoy * get_online_cpus() not an api which is called all that often. 106d221938cSGautham R Shenoy * 107d221938cSGautham R Shenoy */ 108d221938cSGautham R Shenoy static void cpu_hotplug_begin(void) 109d221938cSGautham R Shenoy { 110d221938cSGautham R Shenoy cpu_hotplug.active_writer = current; 111d2ba7e2aSOleg Nesterov 112d2ba7e2aSOleg Nesterov for (;;) { 113d2ba7e2aSOleg Nesterov mutex_lock(&cpu_hotplug.lock); 114d2ba7e2aSOleg Nesterov if (likely(!cpu_hotplug.refcount)) 115d2ba7e2aSOleg Nesterov break; 116d2ba7e2aSOleg Nesterov __set_current_state(TASK_UNINTERRUPTIBLE); 117d221938cSGautham R Shenoy mutex_unlock(&cpu_hotplug.lock); 118d221938cSGautham R Shenoy schedule(); 119d221938cSGautham R Shenoy } 120d221938cSGautham R Shenoy } 121d221938cSGautham R Shenoy 122d221938cSGautham R Shenoy static void cpu_hotplug_done(void) 123d221938cSGautham R Shenoy { 124d221938cSGautham R Shenoy cpu_hotplug.active_writer = NULL; 125d221938cSGautham R Shenoy mutex_unlock(&cpu_hotplug.lock); 126d221938cSGautham R Shenoy } 1271da177e4SLinus Torvalds /* Need to know about CPUs going up/down? */ 128f7b16c10SSam Ravnborg int __ref register_cpu_notifier(struct notifier_block *nb) 1291da177e4SLinus Torvalds { 130bd5349cfSNeil Brown int ret; 131d221938cSGautham R Shenoy cpu_maps_update_begin(); 132bd5349cfSNeil Brown ret = raw_notifier_chain_register(&cpu_chain, nb); 133d221938cSGautham R Shenoy cpu_maps_update_done(); 134bd5349cfSNeil Brown return ret; 1351da177e4SLinus Torvalds } 13665edc68cSChandra Seetharaman 137*e9fb7631SAkinobu Mita static int __cpu_notify(unsigned long val, void *v, int nr_to_call, 138*e9fb7631SAkinobu Mita int *nr_calls) 139*e9fb7631SAkinobu Mita { 140*e9fb7631SAkinobu Mita return __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call, 141*e9fb7631SAkinobu Mita nr_calls); 142*e9fb7631SAkinobu Mita } 143*e9fb7631SAkinobu Mita 144*e9fb7631SAkinobu Mita static int cpu_notify(unsigned long val, void *v) 145*e9fb7631SAkinobu Mita { 146*e9fb7631SAkinobu Mita return __cpu_notify(val, v, -1, NULL); 147*e9fb7631SAkinobu Mita } 148*e9fb7631SAkinobu Mita 149*e9fb7631SAkinobu Mita static void cpu_notify_nofail(unsigned long val, void *v) 150*e9fb7631SAkinobu Mita { 151*e9fb7631SAkinobu Mita int err; 152*e9fb7631SAkinobu Mita 153*e9fb7631SAkinobu Mita err = cpu_notify(val, v); 154*e9fb7631SAkinobu Mita BUG_ON(err == NOTIFY_BAD); 155*e9fb7631SAkinobu Mita } 156*e9fb7631SAkinobu Mita 15765edc68cSChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU 15865edc68cSChandra Seetharaman 1591da177e4SLinus Torvalds EXPORT_SYMBOL(register_cpu_notifier); 1601da177e4SLinus Torvalds 1619647155fSSam Ravnborg void __ref unregister_cpu_notifier(struct notifier_block *nb) 1621da177e4SLinus Torvalds { 163d221938cSGautham R Shenoy cpu_maps_update_begin(); 164bd5349cfSNeil Brown raw_notifier_chain_unregister(&cpu_chain, nb); 165d221938cSGautham R Shenoy cpu_maps_update_done(); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_cpu_notifier); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds static inline void check_for_tasks(int cpu) 1701da177e4SLinus Torvalds { 1711da177e4SLinus Torvalds struct task_struct *p; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 1741da177e4SLinus Torvalds for_each_process(p) { 17511854247SPeter Zijlstra if (task_cpu(p) == cpu && p->state == TASK_RUNNING && 1761da177e4SLinus Torvalds (!cputime_eq(p->utime, cputime_zero) || 1771da177e4SLinus Torvalds !cputime_eq(p->stime, cputime_zero))) 1789d3cfc4cSFrans Pop printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d " 1799d3cfc4cSFrans Pop "(state = %ld, flags = %x)\n", 180ba25f9dcSPavel Emelyanov p->comm, task_pid_nr(p), cpu, 181ba25f9dcSPavel Emelyanov p->state, p->flags); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 186db912f96SAvi Kivity struct take_cpu_down_param { 1876a1bdc1bSOleg Nesterov struct task_struct *caller; 188db912f96SAvi Kivity unsigned long mod; 189db912f96SAvi Kivity void *hcpu; 190db912f96SAvi Kivity }; 191db912f96SAvi Kivity 1921da177e4SLinus Torvalds /* Take this CPU down. */ 193514a20a5SSam Ravnborg static int __ref take_cpu_down(void *_param) 1941da177e4SLinus Torvalds { 195db912f96SAvi Kivity struct take_cpu_down_param *param = _param; 1966a1bdc1bSOleg Nesterov unsigned int cpu = (unsigned long)param->hcpu; 1971da177e4SLinus Torvalds int err; 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds /* Ensure this CPU doesn't handle any more interrupts. */ 2001da177e4SLinus Torvalds err = __cpu_disable(); 2011da177e4SLinus Torvalds if (err < 0) 202f3705136SZwane Mwaikambo return err; 203f3705136SZwane Mwaikambo 204*e9fb7631SAkinobu Mita cpu_notify(CPU_DYING | param->mod, param->hcpu); 2053ba35573SManfred Spraul 2066a1bdc1bSOleg Nesterov if (task_cpu(param->caller) == cpu) 2076a1bdc1bSOleg Nesterov move_task_off_dead_cpu(cpu, param->caller); 2081da177e4SLinus Torvalds /* Force idle task to run as soon as we yield: it should 2091da177e4SLinus Torvalds immediately notice cpu is offline and die quickly. */ 2101da177e4SLinus Torvalds sched_idle_next(); 211f3705136SZwane Mwaikambo return 0; 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 214e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */ 215514a20a5SSam Ravnborg static int __ref _cpu_down(unsigned int cpu, int tasks_frozen) 2161da177e4SLinus Torvalds { 217e7407dccSHeiko Carstens int err, nr_calls = 0; 218e7407dccSHeiko Carstens void *hcpu = (void *)(long)cpu; 2198bb78442SRafael J. Wysocki unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; 220db912f96SAvi Kivity struct take_cpu_down_param tcd_param = { 2216a1bdc1bSOleg Nesterov .caller = current, 222db912f96SAvi Kivity .mod = mod, 223db912f96SAvi Kivity .hcpu = hcpu, 224db912f96SAvi Kivity }; 2251da177e4SLinus Torvalds 226e3920fb4SRafael J. Wysocki if (num_online_cpus() == 1) 227e3920fb4SRafael J. Wysocki return -EBUSY; 2281da177e4SLinus Torvalds 229e3920fb4SRafael J. Wysocki if (!cpu_online(cpu)) 230e3920fb4SRafael J. Wysocki return -EINVAL; 2311da177e4SLinus Torvalds 232d221938cSGautham R Shenoy cpu_hotplug_begin(); 2339ee349adSXiaotian Feng set_cpu_active(cpu, false); 234*e9fb7631SAkinobu Mita err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls); 2351da177e4SLinus Torvalds if (err == NOTIFY_BAD) { 2366ad4c188SPeter Zijlstra set_cpu_active(cpu, true); 2376ad4c188SPeter Zijlstra 238a0d8cdb6SAkinobu Mita nr_calls--; 239*e9fb7631SAkinobu Mita __cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL); 2401da177e4SLinus Torvalds printk("%s: attempt to take down CPU %u failed\n", 241af1f16d0SHarvey Harrison __func__, cpu); 242baaca49fSGautham R Shenoy err = -EINVAL; 243baaca49fSGautham R Shenoy goto out_release; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 246e0b582ecSRusty Russell err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu)); 24704321587SRusty Russell if (err) { 2486ad4c188SPeter Zijlstra set_cpu_active(cpu, true); 2491da177e4SLinus Torvalds /* CPU didn't die: tell everyone. Can't complain. */ 250*e9fb7631SAkinobu Mita cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu); 2511da177e4SLinus Torvalds 2526a1bdc1bSOleg Nesterov goto out_release; 2531da177e4SLinus Torvalds } 25404321587SRusty Russell BUG_ON(cpu_online(cpu)); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /* Wait for it to sleep (leaving idle task). */ 2571da177e4SLinus Torvalds while (!idle_cpu(cpu)) 2581da177e4SLinus Torvalds yield(); 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds /* This actually kills the CPU. */ 2611da177e4SLinus Torvalds __cpu_die(cpu); 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds /* CPU is completely dead: tell everyone. Too late to complain. */ 264*e9fb7631SAkinobu Mita cpu_notify_nofail(CPU_DEAD | mod, hcpu); 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds check_for_tasks(cpu); 2671da177e4SLinus Torvalds 268baaca49fSGautham R Shenoy out_release: 269d221938cSGautham R Shenoy cpu_hotplug_done(); 270*e9fb7631SAkinobu Mita if (!err) 271*e9fb7631SAkinobu Mita cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu); 272e3920fb4SRafael J. Wysocki return err; 273e3920fb4SRafael J. Wysocki } 274e3920fb4SRafael J. Wysocki 275514a20a5SSam Ravnborg int __ref cpu_down(unsigned int cpu) 276e3920fb4SRafael J. Wysocki { 2779ea09af3SHeiko Carstens int err; 278e3920fb4SRafael J. Wysocki 279d221938cSGautham R Shenoy cpu_maps_update_begin(); 280e761b772SMax Krasnyansky 281e761b772SMax Krasnyansky if (cpu_hotplug_disabled) { 282e3920fb4SRafael J. Wysocki err = -EBUSY; 283e761b772SMax Krasnyansky goto out; 284e761b772SMax Krasnyansky } 285e761b772SMax Krasnyansky 2868bb78442SRafael J. Wysocki err = _cpu_down(cpu, 0); 287e3920fb4SRafael J. Wysocki 288e761b772SMax Krasnyansky out: 289d221938cSGautham R Shenoy cpu_maps_update_done(); 2901da177e4SLinus Torvalds return err; 2911da177e4SLinus Torvalds } 292b62b8ef9SZhang Rui EXPORT_SYMBOL(cpu_down); 2931da177e4SLinus Torvalds #endif /*CONFIG_HOTPLUG_CPU*/ 2941da177e4SLinus Torvalds 295e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */ 2968bb78442SRafael J. Wysocki static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen) 2971da177e4SLinus Torvalds { 298baaca49fSGautham R Shenoy int ret, nr_calls = 0; 2991da177e4SLinus Torvalds void *hcpu = (void *)(long)cpu; 3008bb78442SRafael J. Wysocki unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; 3011da177e4SLinus Torvalds 302e3920fb4SRafael J. Wysocki if (cpu_online(cpu) || !cpu_present(cpu)) 303e3920fb4SRafael J. Wysocki return -EINVAL; 30490d45d17SAshok Raj 305d221938cSGautham R Shenoy cpu_hotplug_begin(); 306*e9fb7631SAkinobu Mita ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls); 3071da177e4SLinus Torvalds if (ret == NOTIFY_BAD) { 308a0d8cdb6SAkinobu Mita nr_calls--; 3091da177e4SLinus Torvalds printk("%s: attempt to bring up CPU %u failed\n", 310af1f16d0SHarvey Harrison __func__, cpu); 3111da177e4SLinus Torvalds ret = -EINVAL; 3121da177e4SLinus Torvalds goto out_notify; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* Arch-specific enabling code. */ 3161da177e4SLinus Torvalds ret = __cpu_up(cpu); 3171da177e4SLinus Torvalds if (ret != 0) 3181da177e4SLinus Torvalds goto out_notify; 3196978c705SEric Sesterhenn BUG_ON(!cpu_online(cpu)); 3201da177e4SLinus Torvalds 3212b17fa50SRusty Russell set_cpu_active(cpu, true); 322279ef6bbSDmitry Adamushko 3231da177e4SLinus Torvalds /* Now call notifier in preparation. */ 324*e9fb7631SAkinobu Mita cpu_notify(CPU_ONLINE | mod, hcpu); 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds out_notify: 3271da177e4SLinus Torvalds if (ret != 0) 328*e9fb7631SAkinobu Mita __cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL); 329d221938cSGautham R Shenoy cpu_hotplug_done(); 330e3920fb4SRafael J. Wysocki 3311da177e4SLinus Torvalds return ret; 3321da177e4SLinus Torvalds } 333e3920fb4SRafael J. Wysocki 334b282b6f8SGautham R Shenoy int __cpuinit cpu_up(unsigned int cpu) 335e3920fb4SRafael J. Wysocki { 336e3920fb4SRafael J. Wysocki int err = 0; 337cf23422bSminskey guo 338cf23422bSminskey guo #ifdef CONFIG_MEMORY_HOTPLUG 339cf23422bSminskey guo int nid; 340cf23422bSminskey guo pg_data_t *pgdat; 341cf23422bSminskey guo #endif 342cf23422bSminskey guo 343e0b582ecSRusty Russell if (!cpu_possible(cpu)) { 34473e753a5SKAMEZAWA Hiroyuki printk(KERN_ERR "can't online cpu %d because it is not " 34573e753a5SKAMEZAWA Hiroyuki "configured as may-hotadd at boot time\n", cpu); 34687d5e023SChen Gong #if defined(CONFIG_IA64) 34773e753a5SKAMEZAWA Hiroyuki printk(KERN_ERR "please check additional_cpus= boot " 34873e753a5SKAMEZAWA Hiroyuki "parameter\n"); 34973e753a5SKAMEZAWA Hiroyuki #endif 35073e753a5SKAMEZAWA Hiroyuki return -EINVAL; 35173e753a5SKAMEZAWA Hiroyuki } 352e3920fb4SRafael J. Wysocki 353cf23422bSminskey guo #ifdef CONFIG_MEMORY_HOTPLUG 354cf23422bSminskey guo nid = cpu_to_node(cpu); 355cf23422bSminskey guo if (!node_online(nid)) { 356cf23422bSminskey guo err = mem_online_node(nid); 357cf23422bSminskey guo if (err) 358cf23422bSminskey guo return err; 359cf23422bSminskey guo } 360cf23422bSminskey guo 361cf23422bSminskey guo pgdat = NODE_DATA(nid); 362cf23422bSminskey guo if (!pgdat) { 363cf23422bSminskey guo printk(KERN_ERR 364cf23422bSminskey guo "Can't online cpu %d due to NULL pgdat\n", cpu); 365cf23422bSminskey guo return -ENOMEM; 366cf23422bSminskey guo } 367cf23422bSminskey guo 3684eaf3f64SHaicheng Li if (pgdat->node_zonelists->_zonerefs->zone == NULL) { 3694eaf3f64SHaicheng Li mutex_lock(&zonelists_mutex); 3701f522509SHaicheng Li build_all_zonelists(NULL); 3714eaf3f64SHaicheng Li mutex_unlock(&zonelists_mutex); 3724eaf3f64SHaicheng Li } 373cf23422bSminskey guo #endif 374cf23422bSminskey guo 375d221938cSGautham R Shenoy cpu_maps_update_begin(); 376e761b772SMax Krasnyansky 377e761b772SMax Krasnyansky if (cpu_hotplug_disabled) { 378e3920fb4SRafael J. Wysocki err = -EBUSY; 379e761b772SMax Krasnyansky goto out; 380e761b772SMax Krasnyansky } 381e761b772SMax Krasnyansky 3828bb78442SRafael J. Wysocki err = _cpu_up(cpu, 0); 383e3920fb4SRafael J. Wysocki 384e761b772SMax Krasnyansky out: 385d221938cSGautham R Shenoy cpu_maps_update_done(); 386e3920fb4SRafael J. Wysocki return err; 387e3920fb4SRafael J. Wysocki } 388e3920fb4SRafael J. Wysocki 389f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP 390e0b582ecSRusty Russell static cpumask_var_t frozen_cpus; 391e3920fb4SRafael J. Wysocki 392e3920fb4SRafael J. Wysocki int disable_nonboot_cpus(void) 393e3920fb4SRafael J. Wysocki { 39469575d38SShane Wang int cpu, first_cpu, error; 395e3920fb4SRafael J. Wysocki 396d221938cSGautham R Shenoy cpu_maps_update_begin(); 397e0b582ecSRusty Russell first_cpu = cpumask_first(cpu_online_mask); 3989ee349adSXiaotian Feng /* 3999ee349adSXiaotian Feng * We take down all of the non-boot CPUs in one shot to avoid races 400e3920fb4SRafael J. Wysocki * with the userspace trying to use the CPU hotplug at the same time 401e3920fb4SRafael J. Wysocki */ 402e0b582ecSRusty Russell cpumask_clear(frozen_cpus); 4036ad4c188SPeter Zijlstra 404e3920fb4SRafael J. Wysocki printk("Disabling non-boot CPUs ...\n"); 405e3920fb4SRafael J. Wysocki for_each_online_cpu(cpu) { 406e3920fb4SRafael J. Wysocki if (cpu == first_cpu) 407e3920fb4SRafael J. Wysocki continue; 4088bb78442SRafael J. Wysocki error = _cpu_down(cpu, 1); 409feae3203SMike Travis if (!error) 410e0b582ecSRusty Russell cpumask_set_cpu(cpu, frozen_cpus); 411feae3203SMike Travis else { 412e3920fb4SRafael J. Wysocki printk(KERN_ERR "Error taking CPU%d down: %d\n", 413e3920fb4SRafael J. Wysocki cpu, error); 414e3920fb4SRafael J. Wysocki break; 415e3920fb4SRafael J. Wysocki } 416e3920fb4SRafael J. Wysocki } 41786886e55SJoseph Cihula 418e3920fb4SRafael J. Wysocki if (!error) { 419e3920fb4SRafael J. Wysocki BUG_ON(num_online_cpus() > 1); 420e3920fb4SRafael J. Wysocki /* Make sure the CPUs won't be enabled by someone else */ 421e3920fb4SRafael J. Wysocki cpu_hotplug_disabled = 1; 422e3920fb4SRafael J. Wysocki } else { 423e1d9fd2eSIngo Molnar printk(KERN_ERR "Non-boot CPUs are not disabled\n"); 424e3920fb4SRafael J. Wysocki } 425d221938cSGautham R Shenoy cpu_maps_update_done(); 426e3920fb4SRafael J. Wysocki return error; 427e3920fb4SRafael J. Wysocki } 428e3920fb4SRafael J. Wysocki 429d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_begin(void) 430d0af9eedSSuresh Siddha { 431d0af9eedSSuresh Siddha } 432d0af9eedSSuresh Siddha 433d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_end(void) 434d0af9eedSSuresh Siddha { 435d0af9eedSSuresh Siddha } 436d0af9eedSSuresh Siddha 437fa7303e2SSam Ravnborg void __ref enable_nonboot_cpus(void) 438e3920fb4SRafael J. Wysocki { 439e3920fb4SRafael J. Wysocki int cpu, error; 440e3920fb4SRafael J. Wysocki 441e3920fb4SRafael J. Wysocki /* Allow everyone to use the CPU hotplug again */ 442d221938cSGautham R Shenoy cpu_maps_update_begin(); 443e3920fb4SRafael J. Wysocki cpu_hotplug_disabled = 0; 444e0b582ecSRusty Russell if (cpumask_empty(frozen_cpus)) 4451d64b9cbSRafael J. Wysocki goto out; 446e3920fb4SRafael J. Wysocki 447e3920fb4SRafael J. Wysocki printk("Enabling non-boot CPUs ...\n"); 448d0af9eedSSuresh Siddha 449d0af9eedSSuresh Siddha arch_enable_nonboot_cpus_begin(); 450d0af9eedSSuresh Siddha 451e0b582ecSRusty Russell for_each_cpu(cpu, frozen_cpus) { 4528bb78442SRafael J. Wysocki error = _cpu_up(cpu, 1); 453e3920fb4SRafael J. Wysocki if (!error) { 454e3920fb4SRafael J. Wysocki printk("CPU%d is up\n", cpu); 455e3920fb4SRafael J. Wysocki continue; 456e3920fb4SRafael J. Wysocki } 4571d64b9cbSRafael J. Wysocki printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error); 458e3920fb4SRafael J. Wysocki } 459d0af9eedSSuresh Siddha 460d0af9eedSSuresh Siddha arch_enable_nonboot_cpus_end(); 461d0af9eedSSuresh Siddha 462e0b582ecSRusty Russell cpumask_clear(frozen_cpus); 4631d64b9cbSRafael J. Wysocki out: 464d221938cSGautham R Shenoy cpu_maps_update_done(); 465e3920fb4SRafael J. Wysocki } 466e0b582ecSRusty Russell 467e0b582ecSRusty Russell static int alloc_frozen_cpus(void) 468e0b582ecSRusty Russell { 469e0b582ecSRusty Russell if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO)) 470e0b582ecSRusty Russell return -ENOMEM; 471e0b582ecSRusty Russell return 0; 472e0b582ecSRusty Russell } 473e0b582ecSRusty Russell core_initcall(alloc_frozen_cpus); 474f3de4be9SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP_SMP */ 47568f4f1ecSMax Krasnyansky 476e545a614SManfred Spraul /** 477e545a614SManfred Spraul * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers 478e545a614SManfred Spraul * @cpu: cpu that just started 479e545a614SManfred Spraul * 480e545a614SManfred Spraul * This function calls the cpu_chain notifiers with CPU_STARTING. 481e545a614SManfred Spraul * It must be called by the arch code on the new cpu, before the new cpu 482e545a614SManfred Spraul * enables interrupts and before the "boot" cpu returns from __cpu_up(). 483e545a614SManfred Spraul */ 48484196414SAl Viro void __cpuinit notify_cpu_starting(unsigned int cpu) 485e545a614SManfred Spraul { 486e545a614SManfred Spraul unsigned long val = CPU_STARTING; 487e545a614SManfred Spraul 488e545a614SManfred Spraul #ifdef CONFIG_PM_SLEEP_SMP 489e0b582ecSRusty Russell if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus)) 490e545a614SManfred Spraul val = CPU_STARTING_FROZEN; 491e545a614SManfred Spraul #endif /* CONFIG_PM_SLEEP_SMP */ 492*e9fb7631SAkinobu Mita cpu_notify(val, (void *)(long)cpu); 493e545a614SManfred Spraul } 494e545a614SManfred Spraul 49568f4f1ecSMax Krasnyansky #endif /* CONFIG_SMP */ 496b8d317d1SMike Travis 497e56b3bc7SLinus Torvalds /* 498e56b3bc7SLinus Torvalds * cpu_bit_bitmap[] is a special, "compressed" data structure that 499e56b3bc7SLinus Torvalds * represents all NR_CPUS bits binary values of 1<<nr. 500e56b3bc7SLinus Torvalds * 501e0b582ecSRusty Russell * It is used by cpumask_of() to get a constant address to a CPU 502e56b3bc7SLinus Torvalds * mask value that has a single bit set only. 503e56b3bc7SLinus Torvalds */ 504b8d317d1SMike Travis 505e56b3bc7SLinus Torvalds /* cpu_bit_bitmap[0] is empty - so we can back into it */ 506e56b3bc7SLinus Torvalds #define MASK_DECLARE_1(x) [x+1][0] = 1UL << (x) 507e56b3bc7SLinus Torvalds #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1) 508e56b3bc7SLinus Torvalds #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2) 509e56b3bc7SLinus Torvalds #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4) 510b8d317d1SMike Travis 511e56b3bc7SLinus Torvalds const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = { 512b8d317d1SMike Travis 513e56b3bc7SLinus Torvalds MASK_DECLARE_8(0), MASK_DECLARE_8(8), 514e56b3bc7SLinus Torvalds MASK_DECLARE_8(16), MASK_DECLARE_8(24), 515e56b3bc7SLinus Torvalds #if BITS_PER_LONG > 32 516e56b3bc7SLinus Torvalds MASK_DECLARE_8(32), MASK_DECLARE_8(40), 517e56b3bc7SLinus Torvalds MASK_DECLARE_8(48), MASK_DECLARE_8(56), 518b8d317d1SMike Travis #endif 519b8d317d1SMike Travis }; 520e56b3bc7SLinus Torvalds EXPORT_SYMBOL_GPL(cpu_bit_bitmap); 5212d3854a3SRusty Russell 5222d3854a3SRusty Russell const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL; 5232d3854a3SRusty Russell EXPORT_SYMBOL(cpu_all_bits); 524b3199c02SRusty Russell 525b3199c02SRusty Russell #ifdef CONFIG_INIT_ALL_POSSIBLE 526b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly 527b3199c02SRusty Russell = CPU_BITS_ALL; 528b3199c02SRusty Russell #else 529b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly; 530b3199c02SRusty Russell #endif 531b3199c02SRusty Russell const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits); 532b3199c02SRusty Russell EXPORT_SYMBOL(cpu_possible_mask); 533b3199c02SRusty Russell 534b3199c02SRusty Russell static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly; 535b3199c02SRusty Russell const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits); 536b3199c02SRusty Russell EXPORT_SYMBOL(cpu_online_mask); 537b3199c02SRusty Russell 538b3199c02SRusty Russell static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly; 539b3199c02SRusty Russell const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits); 540b3199c02SRusty Russell EXPORT_SYMBOL(cpu_present_mask); 541b3199c02SRusty Russell 542b3199c02SRusty Russell static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly; 543b3199c02SRusty Russell const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits); 544b3199c02SRusty Russell EXPORT_SYMBOL(cpu_active_mask); 5453fa41520SRusty Russell 5463fa41520SRusty Russell void set_cpu_possible(unsigned int cpu, bool possible) 5473fa41520SRusty Russell { 5483fa41520SRusty Russell if (possible) 5493fa41520SRusty Russell cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits)); 5503fa41520SRusty Russell else 5513fa41520SRusty Russell cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits)); 5523fa41520SRusty Russell } 5533fa41520SRusty Russell 5543fa41520SRusty Russell void set_cpu_present(unsigned int cpu, bool present) 5553fa41520SRusty Russell { 5563fa41520SRusty Russell if (present) 5573fa41520SRusty Russell cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits)); 5583fa41520SRusty Russell else 5593fa41520SRusty Russell cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits)); 5603fa41520SRusty Russell } 5613fa41520SRusty Russell 5623fa41520SRusty Russell void set_cpu_online(unsigned int cpu, bool online) 5633fa41520SRusty Russell { 5643fa41520SRusty Russell if (online) 5653fa41520SRusty Russell cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits)); 5663fa41520SRusty Russell else 5673fa41520SRusty Russell cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits)); 5683fa41520SRusty Russell } 5693fa41520SRusty Russell 5703fa41520SRusty Russell void set_cpu_active(unsigned int cpu, bool active) 5713fa41520SRusty Russell { 5723fa41520SRusty Russell if (active) 5733fa41520SRusty Russell cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits)); 5743fa41520SRusty Russell else 5753fa41520SRusty Russell cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits)); 5763fa41520SRusty Russell } 5773fa41520SRusty Russell 5783fa41520SRusty Russell void init_cpu_present(const struct cpumask *src) 5793fa41520SRusty Russell { 5803fa41520SRusty Russell cpumask_copy(to_cpumask(cpu_present_bits), src); 5813fa41520SRusty Russell } 5823fa41520SRusty Russell 5833fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src) 5843fa41520SRusty Russell { 5853fa41520SRusty Russell cpumask_copy(to_cpumask(cpu_possible_bits), src); 5863fa41520SRusty Russell } 5873fa41520SRusty Russell 5883fa41520SRusty Russell void init_cpu_online(const struct cpumask *src) 5893fa41520SRusty Russell { 5903fa41520SRusty Russell cpumask_copy(to_cpumask(cpu_online_bits), src); 5913fa41520SRusty Russell } 592