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> 13cb79295eSAnton Vorontsov #include <linux/oom.h> 14cb79295eSAnton Vorontsov #include <linux/rcupdate.h> 159984de1aSPaul Gortmaker #include <linux/export.h> 16e4cc2f87SAnton Vorontsov #include <linux/bug.h> 171da177e4SLinus Torvalds #include <linux/kthread.h> 181da177e4SLinus Torvalds #include <linux/stop_machine.h> 1981615b62SIngo Molnar #include <linux/mutex.h> 205a0e3ad6STejun Heo #include <linux/gfp.h> 2179cfbdfaSSrivatsa S. Bhat #include <linux/suspend.h> 22a19423b9SGautham R. Shenoy #include <linux/lockdep.h> 23345527b1SPreeti U Murthy #include <linux/tick.h> 24a8994181SThomas Gleixner #include <linux/irq.h> 25bb3632c6STodd E Brandt #include <trace/events/power.h> 261da177e4SLinus Torvalds 2738498a67SThomas Gleixner #include "smpboot.h" 2838498a67SThomas Gleixner 2998a79d6aSRusty Russell #ifdef CONFIG_SMP 30b3199c02SRusty Russell /* Serializes the updates to cpu_online_mask, cpu_present_mask */ 31aa953877SLinus Torvalds static DEFINE_MUTEX(cpu_add_remove_lock); 321da177e4SLinus Torvalds 3379a6cdebSLai Jiangshan /* 3493ae4f97SSrivatsa S. Bhat * The following two APIs (cpu_maps_update_begin/done) must be used when 3593ae4f97SSrivatsa S. Bhat * attempting to serialize the updates to cpu_online_mask & cpu_present_mask. 3693ae4f97SSrivatsa S. Bhat * The APIs cpu_notifier_register_begin/done() must be used to protect CPU 3793ae4f97SSrivatsa S. Bhat * hotplug callback (un)registration performed using __register_cpu_notifier() 3893ae4f97SSrivatsa S. Bhat * or __unregister_cpu_notifier(). 3979a6cdebSLai Jiangshan */ 4079a6cdebSLai Jiangshan void cpu_maps_update_begin(void) 4179a6cdebSLai Jiangshan { 4279a6cdebSLai Jiangshan mutex_lock(&cpu_add_remove_lock); 4379a6cdebSLai Jiangshan } 4493ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(cpu_notifier_register_begin); 4579a6cdebSLai Jiangshan 4679a6cdebSLai Jiangshan void cpu_maps_update_done(void) 4779a6cdebSLai Jiangshan { 4879a6cdebSLai Jiangshan mutex_unlock(&cpu_add_remove_lock); 4979a6cdebSLai Jiangshan } 5093ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(cpu_notifier_register_done); 5179a6cdebSLai Jiangshan 525c113fbeSDaniel J Blueman static RAW_NOTIFIER_HEAD(cpu_chain); 531da177e4SLinus Torvalds 54e3920fb4SRafael J. Wysocki /* If set, cpu_up and cpu_down will return -EBUSY and do nothing. 55e3920fb4SRafael J. Wysocki * Should always be manipulated under cpu_add_remove_lock 56e3920fb4SRafael J. Wysocki */ 57e3920fb4SRafael J. Wysocki static int cpu_hotplug_disabled; 58e3920fb4SRafael J. Wysocki 5979a6cdebSLai Jiangshan #ifdef CONFIG_HOTPLUG_CPU 6079a6cdebSLai Jiangshan 61d221938cSGautham R Shenoy static struct { 62d221938cSGautham R Shenoy struct task_struct *active_writer; 6387af9e7fSDavid Hildenbrand /* wait queue to wake up the active_writer */ 6487af9e7fSDavid Hildenbrand wait_queue_head_t wq; 6587af9e7fSDavid Hildenbrand /* verifies that no writer will get active while readers are active */ 6687af9e7fSDavid Hildenbrand struct mutex lock; 67d221938cSGautham R Shenoy /* 68d221938cSGautham R Shenoy * Also blocks the new readers during 69d221938cSGautham R Shenoy * an ongoing cpu hotplug operation. 70d221938cSGautham R Shenoy */ 7187af9e7fSDavid Hildenbrand atomic_t refcount; 72a19423b9SGautham R. Shenoy 73a19423b9SGautham R. Shenoy #ifdef CONFIG_DEBUG_LOCK_ALLOC 74a19423b9SGautham R. Shenoy struct lockdep_map dep_map; 75a19423b9SGautham R. Shenoy #endif 7631950eb6SLinus Torvalds } cpu_hotplug = { 7731950eb6SLinus Torvalds .active_writer = NULL, 7887af9e7fSDavid Hildenbrand .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq), 7931950eb6SLinus Torvalds .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock), 80a19423b9SGautham R. Shenoy #ifdef CONFIG_DEBUG_LOCK_ALLOC 81a19423b9SGautham R. Shenoy .dep_map = {.name = "cpu_hotplug.lock" }, 82a19423b9SGautham R. Shenoy #endif 8331950eb6SLinus Torvalds }; 84d221938cSGautham R Shenoy 85a19423b9SGautham R. Shenoy /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */ 86a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map) 87dd56af42SPaul E. McKenney #define cpuhp_lock_acquire_tryread() \ 88dd56af42SPaul E. McKenney lock_map_acquire_tryread(&cpu_hotplug.dep_map) 89a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map) 90a19423b9SGautham R. Shenoy #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map) 91a19423b9SGautham R. Shenoy 9262db99f4SPaul E. McKenney 9386ef5c9aSGautham R Shenoy void get_online_cpus(void) 94a9d9baa1SAshok Raj { 95d221938cSGautham R Shenoy might_sleep(); 96d221938cSGautham R Shenoy if (cpu_hotplug.active_writer == current) 97aa953877SLinus Torvalds return; 98a19423b9SGautham R. Shenoy cpuhp_lock_acquire_read(); 99d221938cSGautham R Shenoy mutex_lock(&cpu_hotplug.lock); 10087af9e7fSDavid Hildenbrand atomic_inc(&cpu_hotplug.refcount); 101d221938cSGautham R Shenoy mutex_unlock(&cpu_hotplug.lock); 102a9d9baa1SAshok Raj } 10386ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(get_online_cpus); 104a9d9baa1SAshok Raj 105dd56af42SPaul E. McKenney bool try_get_online_cpus(void) 106dd56af42SPaul E. McKenney { 107dd56af42SPaul E. McKenney if (cpu_hotplug.active_writer == current) 108dd56af42SPaul E. McKenney return true; 109dd56af42SPaul E. McKenney if (!mutex_trylock(&cpu_hotplug.lock)) 110dd56af42SPaul E. McKenney return false; 111dd56af42SPaul E. McKenney cpuhp_lock_acquire_tryread(); 11287af9e7fSDavid Hildenbrand atomic_inc(&cpu_hotplug.refcount); 113dd56af42SPaul E. McKenney mutex_unlock(&cpu_hotplug.lock); 114dd56af42SPaul E. McKenney return true; 115dd56af42SPaul E. McKenney } 116dd56af42SPaul E. McKenney EXPORT_SYMBOL_GPL(try_get_online_cpus); 117dd56af42SPaul E. McKenney 11886ef5c9aSGautham R Shenoy void put_online_cpus(void) 119a9d9baa1SAshok Raj { 12087af9e7fSDavid Hildenbrand int refcount; 12187af9e7fSDavid Hildenbrand 122d221938cSGautham R Shenoy if (cpu_hotplug.active_writer == current) 123aa953877SLinus Torvalds return; 124075663d1SSrivatsa S. Bhat 12587af9e7fSDavid Hildenbrand refcount = atomic_dec_return(&cpu_hotplug.refcount); 12687af9e7fSDavid Hildenbrand if (WARN_ON(refcount < 0)) /* try to fix things up */ 12787af9e7fSDavid Hildenbrand atomic_inc(&cpu_hotplug.refcount); 128075663d1SSrivatsa S. Bhat 12987af9e7fSDavid Hildenbrand if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq)) 13087af9e7fSDavid Hildenbrand wake_up(&cpu_hotplug.wq); 13187af9e7fSDavid Hildenbrand 132a19423b9SGautham R. Shenoy cpuhp_lock_release(); 133d221938cSGautham R Shenoy 134a9d9baa1SAshok Raj } 13586ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(put_online_cpus); 136a9d9baa1SAshok Raj 137d221938cSGautham R Shenoy /* 138d221938cSGautham R Shenoy * This ensures that the hotplug operation can begin only when the 139d221938cSGautham R Shenoy * refcount goes to zero. 140d221938cSGautham R Shenoy * 141d221938cSGautham R Shenoy * Note that during a cpu-hotplug operation, the new readers, if any, 142d221938cSGautham R Shenoy * will be blocked by the cpu_hotplug.lock 143d221938cSGautham R Shenoy * 144d2ba7e2aSOleg Nesterov * Since cpu_hotplug_begin() is always called after invoking 145d2ba7e2aSOleg Nesterov * cpu_maps_update_begin(), we can be sure that only one writer is active. 146d221938cSGautham R Shenoy * 147d221938cSGautham R Shenoy * Note that theoretically, there is a possibility of a livelock: 148d221938cSGautham R Shenoy * - Refcount goes to zero, last reader wakes up the sleeping 149d221938cSGautham R Shenoy * writer. 150d221938cSGautham R Shenoy * - Last reader unlocks the cpu_hotplug.lock. 151d221938cSGautham R Shenoy * - A new reader arrives at this moment, bumps up the refcount. 152d221938cSGautham R Shenoy * - The writer acquires the cpu_hotplug.lock finds the refcount 153d221938cSGautham R Shenoy * non zero and goes to sleep again. 154d221938cSGautham R Shenoy * 155d221938cSGautham R Shenoy * However, this is very difficult to achieve in practice since 15686ef5c9aSGautham R Shenoy * get_online_cpus() not an api which is called all that often. 157d221938cSGautham R Shenoy * 158d221938cSGautham R Shenoy */ 159b9d10be7SToshi Kani void cpu_hotplug_begin(void) 160d221938cSGautham R Shenoy { 16187af9e7fSDavid Hildenbrand DEFINE_WAIT(wait); 162d2ba7e2aSOleg Nesterov 16387af9e7fSDavid Hildenbrand cpu_hotplug.active_writer = current; 164a19423b9SGautham R. Shenoy cpuhp_lock_acquire(); 16587af9e7fSDavid Hildenbrand 166d2ba7e2aSOleg Nesterov for (;;) { 167d2ba7e2aSOleg Nesterov mutex_lock(&cpu_hotplug.lock); 16887af9e7fSDavid Hildenbrand prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE); 16987af9e7fSDavid Hildenbrand if (likely(!atomic_read(&cpu_hotplug.refcount))) 170d2ba7e2aSOleg Nesterov break; 171d221938cSGautham R Shenoy mutex_unlock(&cpu_hotplug.lock); 172d221938cSGautham R Shenoy schedule(); 173d221938cSGautham R Shenoy } 17487af9e7fSDavid Hildenbrand finish_wait(&cpu_hotplug.wq, &wait); 175d221938cSGautham R Shenoy } 176d221938cSGautham R Shenoy 177b9d10be7SToshi Kani void cpu_hotplug_done(void) 178d221938cSGautham R Shenoy { 179d221938cSGautham R Shenoy cpu_hotplug.active_writer = NULL; 180d221938cSGautham R Shenoy mutex_unlock(&cpu_hotplug.lock); 181a19423b9SGautham R. Shenoy cpuhp_lock_release(); 182d221938cSGautham R Shenoy } 18379a6cdebSLai Jiangshan 18416e53dbfSSrivatsa S. Bhat /* 18516e53dbfSSrivatsa S. Bhat * Wait for currently running CPU hotplug operations to complete (if any) and 18616e53dbfSSrivatsa S. Bhat * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects 18716e53dbfSSrivatsa S. Bhat * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the 18816e53dbfSSrivatsa S. Bhat * hotplug path before performing hotplug operations. So acquiring that lock 18916e53dbfSSrivatsa S. Bhat * guarantees mutual exclusion from any currently running hotplug operations. 19016e53dbfSSrivatsa S. Bhat */ 19116e53dbfSSrivatsa S. Bhat void cpu_hotplug_disable(void) 19216e53dbfSSrivatsa S. Bhat { 19316e53dbfSSrivatsa S. Bhat cpu_maps_update_begin(); 19489af7ba5SVitaly Kuznetsov cpu_hotplug_disabled++; 19516e53dbfSSrivatsa S. Bhat cpu_maps_update_done(); 19616e53dbfSSrivatsa S. Bhat } 19732145c46SVitaly Kuznetsov EXPORT_SYMBOL_GPL(cpu_hotplug_disable); 19816e53dbfSSrivatsa S. Bhat 19916e53dbfSSrivatsa S. Bhat void cpu_hotplug_enable(void) 20016e53dbfSSrivatsa S. Bhat { 20116e53dbfSSrivatsa S. Bhat cpu_maps_update_begin(); 20289af7ba5SVitaly Kuznetsov WARN_ON(--cpu_hotplug_disabled < 0); 20316e53dbfSSrivatsa S. Bhat cpu_maps_update_done(); 20416e53dbfSSrivatsa S. Bhat } 20532145c46SVitaly Kuznetsov EXPORT_SYMBOL_GPL(cpu_hotplug_enable); 206b9d10be7SToshi Kani #endif /* CONFIG_HOTPLUG_CPU */ 20779a6cdebSLai Jiangshan 2081da177e4SLinus Torvalds /* Need to know about CPUs going up/down? */ 20971cf5aeeSMathias Krause int register_cpu_notifier(struct notifier_block *nb) 2101da177e4SLinus Torvalds { 211bd5349cfSNeil Brown int ret; 212d221938cSGautham R Shenoy cpu_maps_update_begin(); 213bd5349cfSNeil Brown ret = raw_notifier_chain_register(&cpu_chain, nb); 214d221938cSGautham R Shenoy cpu_maps_update_done(); 215bd5349cfSNeil Brown return ret; 2161da177e4SLinus Torvalds } 21765edc68cSChandra Seetharaman 21871cf5aeeSMathias Krause int __register_cpu_notifier(struct notifier_block *nb) 21993ae4f97SSrivatsa S. Bhat { 22093ae4f97SSrivatsa S. Bhat return raw_notifier_chain_register(&cpu_chain, nb); 22193ae4f97SSrivatsa S. Bhat } 22293ae4f97SSrivatsa S. Bhat 223e9fb7631SAkinobu Mita static int __cpu_notify(unsigned long val, void *v, int nr_to_call, 224e9fb7631SAkinobu Mita int *nr_calls) 225e9fb7631SAkinobu Mita { 226e6bde73bSAkinobu Mita int ret; 227e6bde73bSAkinobu Mita 228e6bde73bSAkinobu Mita ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call, 229e9fb7631SAkinobu Mita nr_calls); 230e6bde73bSAkinobu Mita 231e6bde73bSAkinobu Mita return notifier_to_errno(ret); 232e9fb7631SAkinobu Mita } 233e9fb7631SAkinobu Mita 234e9fb7631SAkinobu Mita static int cpu_notify(unsigned long val, void *v) 235e9fb7631SAkinobu Mita { 236e9fb7631SAkinobu Mita return __cpu_notify(val, v, -1, NULL); 237e9fb7631SAkinobu Mita } 238e9fb7631SAkinobu Mita 23900b9b0afSLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU 24000b9b0afSLinus Torvalds 241e9fb7631SAkinobu Mita static void cpu_notify_nofail(unsigned long val, void *v) 242e9fb7631SAkinobu Mita { 24300b9b0afSLinus Torvalds BUG_ON(cpu_notify(val, v)); 244e9fb7631SAkinobu Mita } 2451da177e4SLinus Torvalds EXPORT_SYMBOL(register_cpu_notifier); 24693ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__register_cpu_notifier); 2471da177e4SLinus Torvalds 24871cf5aeeSMathias Krause void unregister_cpu_notifier(struct notifier_block *nb) 2491da177e4SLinus Torvalds { 250d221938cSGautham R Shenoy cpu_maps_update_begin(); 251bd5349cfSNeil Brown raw_notifier_chain_unregister(&cpu_chain, nb); 252d221938cSGautham R Shenoy cpu_maps_update_done(); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_cpu_notifier); 2551da177e4SLinus Torvalds 25671cf5aeeSMathias Krause void __unregister_cpu_notifier(struct notifier_block *nb) 25793ae4f97SSrivatsa S. Bhat { 25893ae4f97SSrivatsa S. Bhat raw_notifier_chain_unregister(&cpu_chain, nb); 25993ae4f97SSrivatsa S. Bhat } 26093ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__unregister_cpu_notifier); 26193ae4f97SSrivatsa S. Bhat 262e4cc2f87SAnton Vorontsov /** 263e4cc2f87SAnton Vorontsov * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU 264e4cc2f87SAnton Vorontsov * @cpu: a CPU id 265e4cc2f87SAnton Vorontsov * 266e4cc2f87SAnton Vorontsov * This function walks all processes, finds a valid mm struct for each one and 267e4cc2f87SAnton Vorontsov * then clears a corresponding bit in mm's cpumask. While this all sounds 268e4cc2f87SAnton Vorontsov * trivial, there are various non-obvious corner cases, which this function 269e4cc2f87SAnton Vorontsov * tries to solve in a safe manner. 270e4cc2f87SAnton Vorontsov * 271e4cc2f87SAnton Vorontsov * Also note that the function uses a somewhat relaxed locking scheme, so it may 272e4cc2f87SAnton Vorontsov * be called only for an already offlined CPU. 273e4cc2f87SAnton Vorontsov */ 274cb79295eSAnton Vorontsov void clear_tasks_mm_cpumask(int cpu) 275cb79295eSAnton Vorontsov { 276cb79295eSAnton Vorontsov struct task_struct *p; 277cb79295eSAnton Vorontsov 278cb79295eSAnton Vorontsov /* 279cb79295eSAnton Vorontsov * This function is called after the cpu is taken down and marked 280cb79295eSAnton Vorontsov * offline, so its not like new tasks will ever get this cpu set in 281cb79295eSAnton Vorontsov * their mm mask. -- Peter Zijlstra 282cb79295eSAnton Vorontsov * Thus, we may use rcu_read_lock() here, instead of grabbing 283cb79295eSAnton Vorontsov * full-fledged tasklist_lock. 284cb79295eSAnton Vorontsov */ 285e4cc2f87SAnton Vorontsov WARN_ON(cpu_online(cpu)); 286cb79295eSAnton Vorontsov rcu_read_lock(); 287cb79295eSAnton Vorontsov for_each_process(p) { 288cb79295eSAnton Vorontsov struct task_struct *t; 289cb79295eSAnton Vorontsov 290e4cc2f87SAnton Vorontsov /* 291e4cc2f87SAnton Vorontsov * Main thread might exit, but other threads may still have 292e4cc2f87SAnton Vorontsov * a valid mm. Find one. 293e4cc2f87SAnton Vorontsov */ 294cb79295eSAnton Vorontsov t = find_lock_task_mm(p); 295cb79295eSAnton Vorontsov if (!t) 296cb79295eSAnton Vorontsov continue; 297cb79295eSAnton Vorontsov cpumask_clear_cpu(cpu, mm_cpumask(t->mm)); 298cb79295eSAnton Vorontsov task_unlock(t); 299cb79295eSAnton Vorontsov } 300cb79295eSAnton Vorontsov rcu_read_unlock(); 301cb79295eSAnton Vorontsov } 302cb79295eSAnton Vorontsov 303b728ca06SKirill Tkhai static inline void check_for_tasks(int dead_cpu) 3041da177e4SLinus Torvalds { 305b728ca06SKirill Tkhai struct task_struct *g, *p; 3061da177e4SLinus Torvalds 307*a75a6068SOleg Nesterov read_lock(&tasklist_lock); 308*a75a6068SOleg Nesterov for_each_process_thread(g, p) { 309b728ca06SKirill Tkhai if (!p->on_rq) 310b728ca06SKirill Tkhai continue; 311b728ca06SKirill Tkhai /* 312b728ca06SKirill Tkhai * We do the check with unlocked task_rq(p)->lock. 313b728ca06SKirill Tkhai * Order the reading to do not warn about a task, 314b728ca06SKirill Tkhai * which was running on this cpu in the past, and 315b728ca06SKirill Tkhai * it's just been woken on another cpu. 316b728ca06SKirill Tkhai */ 317b728ca06SKirill Tkhai rmb(); 318b728ca06SKirill Tkhai if (task_cpu(p) != dead_cpu) 319b728ca06SKirill Tkhai continue; 320b728ca06SKirill Tkhai 32184117da5SFabian Frederick pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n", 322b728ca06SKirill Tkhai p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags); 323*a75a6068SOleg Nesterov } 324*a75a6068SOleg Nesterov read_unlock(&tasklist_lock); 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 327db912f96SAvi Kivity struct take_cpu_down_param { 328db912f96SAvi Kivity unsigned long mod; 329db912f96SAvi Kivity void *hcpu; 330db912f96SAvi Kivity }; 331db912f96SAvi Kivity 3321da177e4SLinus Torvalds /* Take this CPU down. */ 33371cf5aeeSMathias Krause static int take_cpu_down(void *_param) 3341da177e4SLinus Torvalds { 335db912f96SAvi Kivity struct take_cpu_down_param *param = _param; 3361da177e4SLinus Torvalds int err; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds /* Ensure this CPU doesn't handle any more interrupts. */ 3391da177e4SLinus Torvalds err = __cpu_disable(); 3401da177e4SLinus Torvalds if (err < 0) 341f3705136SZwane Mwaikambo return err; 342f3705136SZwane Mwaikambo 343e9fb7631SAkinobu Mita cpu_notify(CPU_DYING | param->mod, param->hcpu); 34452c063d1SThomas Gleixner /* Give up timekeeping duties */ 34552c063d1SThomas Gleixner tick_handover_do_timer(); 34614e568e7SThomas Gleixner /* Park the stopper thread */ 34714e568e7SThomas Gleixner kthread_park(current); 348f3705136SZwane Mwaikambo return 0; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 351e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */ 35271cf5aeeSMathias Krause static int _cpu_down(unsigned int cpu, int tasks_frozen) 3531da177e4SLinus Torvalds { 354e7407dccSHeiko Carstens int err, nr_calls = 0; 355e7407dccSHeiko Carstens void *hcpu = (void *)(long)cpu; 3568bb78442SRafael J. Wysocki unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; 357db912f96SAvi Kivity struct take_cpu_down_param tcd_param = { 358db912f96SAvi Kivity .mod = mod, 359db912f96SAvi Kivity .hcpu = hcpu, 360db912f96SAvi Kivity }; 3611da177e4SLinus Torvalds 362e3920fb4SRafael J. Wysocki if (num_online_cpus() == 1) 363e3920fb4SRafael J. Wysocki return -EBUSY; 3641da177e4SLinus Torvalds 365e3920fb4SRafael J. Wysocki if (!cpu_online(cpu)) 366e3920fb4SRafael J. Wysocki return -EINVAL; 3671da177e4SLinus Torvalds 368d221938cSGautham R Shenoy cpu_hotplug_begin(); 3694d51985eSMichael Rodriguez 370e9fb7631SAkinobu Mita err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls); 371e6bde73bSAkinobu Mita if (err) { 372a0d8cdb6SAkinobu Mita nr_calls--; 373e9fb7631SAkinobu Mita __cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL); 37484117da5SFabian Frederick pr_warn("%s: attempt to take down CPU %u failed\n", 375af1f16d0SHarvey Harrison __func__, cpu); 376baaca49fSGautham R Shenoy goto out_release; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3796acce3efSPeter Zijlstra /* 3806acce3efSPeter Zijlstra * By now we've cleared cpu_active_mask, wait for all preempt-disabled 3816acce3efSPeter Zijlstra * and RCU users of this state to go away such that all new such users 3826acce3efSPeter Zijlstra * will observe it. 3836acce3efSPeter Zijlstra * 3846acce3efSPeter Zijlstra * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might 385779de6ceSPaul E. McKenney * not imply sync_sched(), so wait for both. 386106dd5afSMichael wang * 387106dd5afSMichael wang * Do sync before park smpboot threads to take care the rcu boost case. 3886acce3efSPeter Zijlstra */ 389779de6ceSPaul E. McKenney if (IS_ENABLED(CONFIG_PREEMPT)) 390779de6ceSPaul E. McKenney synchronize_rcu_mult(call_rcu, call_rcu_sched); 391779de6ceSPaul E. McKenney else 3926acce3efSPeter Zijlstra synchronize_rcu(); 3936acce3efSPeter Zijlstra 394106dd5afSMichael wang smpboot_park_threads(cpu); 395106dd5afSMichael wang 3966acce3efSPeter Zijlstra /* 397a8994181SThomas Gleixner * Prevent irq alloc/free while the dying cpu reorganizes the 398a8994181SThomas Gleixner * interrupt affinities. 399a8994181SThomas Gleixner */ 400a8994181SThomas Gleixner irq_lock_sparse(); 401a8994181SThomas Gleixner 402a8994181SThomas Gleixner /* 4036acce3efSPeter Zijlstra * So now all preempt/rcu users must observe !cpu_active(). 4046acce3efSPeter Zijlstra */ 4057eeb088eSOleg Nesterov err = stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu)); 40604321587SRusty Russell if (err) { 4071da177e4SLinus Torvalds /* CPU didn't die: tell everyone. Can't complain. */ 408e9fb7631SAkinobu Mita cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu); 409a8994181SThomas Gleixner irq_unlock_sparse(); 4106a1bdc1bSOleg Nesterov goto out_release; 4111da177e4SLinus Torvalds } 41204321587SRusty Russell BUG_ON(cpu_online(cpu)); 4131da177e4SLinus Torvalds 41448c5ccaeSPeter Zijlstra /* 41548c5ccaeSPeter Zijlstra * The migration_call() CPU_DYING callback will have removed all 41648c5ccaeSPeter Zijlstra * runnable tasks from the cpu, there's only the idle task left now 41748c5ccaeSPeter Zijlstra * that the migration thread is done doing the stop_machine thing. 41851a96c77SPeter Zijlstra * 41951a96c77SPeter Zijlstra * Wait for the stop thread to go away. 42048c5ccaeSPeter Zijlstra */ 421528a25b0SPaul E. McKenney while (!per_cpu(cpu_dead_idle, cpu)) 42251a96c77SPeter Zijlstra cpu_relax(); 423528a25b0SPaul E. McKenney smp_mb(); /* Read from cpu_dead_idle before __cpu_die(). */ 424528a25b0SPaul E. McKenney per_cpu(cpu_dead_idle, cpu) = false; 4251da177e4SLinus Torvalds 426a8994181SThomas Gleixner /* Interrupts are moved away from the dying cpu, reenable alloc/free */ 427a8994181SThomas Gleixner irq_unlock_sparse(); 428a8994181SThomas Gleixner 429345527b1SPreeti U Murthy hotplug_cpu__broadcast_tick_pull(cpu); 4301da177e4SLinus Torvalds /* This actually kills the CPU. */ 4311da177e4SLinus Torvalds __cpu_die(cpu); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds /* CPU is completely dead: tell everyone. Too late to complain. */ 434a49b116dSThomas Gleixner tick_cleanup_dead_cpu(cpu); 435e9fb7631SAkinobu Mita cpu_notify_nofail(CPU_DEAD | mod, hcpu); 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds check_for_tasks(cpu); 4381da177e4SLinus Torvalds 439baaca49fSGautham R Shenoy out_release: 440d221938cSGautham R Shenoy cpu_hotplug_done(); 441e9fb7631SAkinobu Mita if (!err) 442e9fb7631SAkinobu Mita cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu); 443e3920fb4SRafael J. Wysocki return err; 444e3920fb4SRafael J. Wysocki } 445e3920fb4SRafael J. Wysocki 44671cf5aeeSMathias Krause int cpu_down(unsigned int cpu) 447e3920fb4SRafael J. Wysocki { 4489ea09af3SHeiko Carstens int err; 449e3920fb4SRafael J. Wysocki 450d221938cSGautham R Shenoy cpu_maps_update_begin(); 451e761b772SMax Krasnyansky 452e761b772SMax Krasnyansky if (cpu_hotplug_disabled) { 453e3920fb4SRafael J. Wysocki err = -EBUSY; 454e761b772SMax Krasnyansky goto out; 455e761b772SMax Krasnyansky } 456e761b772SMax Krasnyansky 4578bb78442SRafael J. Wysocki err = _cpu_down(cpu, 0); 458e3920fb4SRafael J. Wysocki 459e761b772SMax Krasnyansky out: 460d221938cSGautham R Shenoy cpu_maps_update_done(); 4611da177e4SLinus Torvalds return err; 4621da177e4SLinus Torvalds } 463b62b8ef9SZhang Rui EXPORT_SYMBOL(cpu_down); 4641da177e4SLinus Torvalds #endif /*CONFIG_HOTPLUG_CPU*/ 4651da177e4SLinus Torvalds 46600df35f9SPaul E. McKenney /* 46700df35f9SPaul E. McKenney * Unpark per-CPU smpboot kthreads at CPU-online time. 46800df35f9SPaul E. McKenney */ 46900df35f9SPaul E. McKenney static int smpboot_thread_call(struct notifier_block *nfb, 47000df35f9SPaul E. McKenney unsigned long action, void *hcpu) 47100df35f9SPaul E. McKenney { 47200df35f9SPaul E. McKenney int cpu = (long)hcpu; 47300df35f9SPaul E. McKenney 47400df35f9SPaul E. McKenney switch (action & ~CPU_TASKS_FROZEN) { 47500df35f9SPaul E. McKenney 47664eaf974SPaul E. McKenney case CPU_DOWN_FAILED: 47700df35f9SPaul E. McKenney case CPU_ONLINE: 47800df35f9SPaul E. McKenney smpboot_unpark_threads(cpu); 47900df35f9SPaul E. McKenney break; 48000df35f9SPaul E. McKenney 48100df35f9SPaul E. McKenney default: 48200df35f9SPaul E. McKenney break; 48300df35f9SPaul E. McKenney } 48400df35f9SPaul E. McKenney 48500df35f9SPaul E. McKenney return NOTIFY_OK; 48600df35f9SPaul E. McKenney } 48700df35f9SPaul E. McKenney 48800df35f9SPaul E. McKenney static struct notifier_block smpboot_thread_notifier = { 48900df35f9SPaul E. McKenney .notifier_call = smpboot_thread_call, 49000df35f9SPaul E. McKenney .priority = CPU_PRI_SMPBOOT, 49100df35f9SPaul E. McKenney }; 49200df35f9SPaul E. McKenney 493927da9dfSPaul Gortmaker void smpboot_thread_init(void) 49400df35f9SPaul E. McKenney { 49500df35f9SPaul E. McKenney register_cpu_notifier(&smpboot_thread_notifier); 49600df35f9SPaul E. McKenney } 49700df35f9SPaul E. McKenney 498e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */ 4990db0628dSPaul Gortmaker static int _cpu_up(unsigned int cpu, int tasks_frozen) 5001da177e4SLinus Torvalds { 501baaca49fSGautham R Shenoy int ret, nr_calls = 0; 5021da177e4SLinus Torvalds void *hcpu = (void *)(long)cpu; 5038bb78442SRafael J. Wysocki unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; 5043bb5d2eeSSuresh Siddha struct task_struct *idle; 5051da177e4SLinus Torvalds 506d221938cSGautham R Shenoy cpu_hotplug_begin(); 50738498a67SThomas Gleixner 5085e5041f3SYasuaki Ishimatsu if (cpu_online(cpu) || !cpu_present(cpu)) { 5095e5041f3SYasuaki Ishimatsu ret = -EINVAL; 5105e5041f3SYasuaki Ishimatsu goto out; 5115e5041f3SYasuaki Ishimatsu } 5125e5041f3SYasuaki Ishimatsu 5133bb5d2eeSSuresh Siddha idle = idle_thread_get(cpu); 5143bb5d2eeSSuresh Siddha if (IS_ERR(idle)) { 5153bb5d2eeSSuresh Siddha ret = PTR_ERR(idle); 51638498a67SThomas Gleixner goto out; 5173bb5d2eeSSuresh Siddha } 51838498a67SThomas Gleixner 519f97f8f06SThomas Gleixner ret = smpboot_create_threads(cpu); 520f97f8f06SThomas Gleixner if (ret) 521f97f8f06SThomas Gleixner goto out; 522f97f8f06SThomas Gleixner 523e9fb7631SAkinobu Mita ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls); 524e6bde73bSAkinobu Mita if (ret) { 525a0d8cdb6SAkinobu Mita nr_calls--; 52684117da5SFabian Frederick pr_warn("%s: attempt to bring up CPU %u failed\n", 527af1f16d0SHarvey Harrison __func__, cpu); 5281da177e4SLinus Torvalds goto out_notify; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds /* Arch-specific enabling code. */ 5323bb5d2eeSSuresh Siddha ret = __cpu_up(cpu, idle); 533a8994181SThomas Gleixner 5341da177e4SLinus Torvalds if (ret != 0) 5351da177e4SLinus Torvalds goto out_notify; 5366978c705SEric Sesterhenn BUG_ON(!cpu_online(cpu)); 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds /* Now call notifier in preparation. */ 539e9fb7631SAkinobu Mita cpu_notify(CPU_ONLINE | mod, hcpu); 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds out_notify: 5421da177e4SLinus Torvalds if (ret != 0) 543e9fb7631SAkinobu Mita __cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL); 54438498a67SThomas Gleixner out: 545d221938cSGautham R Shenoy cpu_hotplug_done(); 546e3920fb4SRafael J. Wysocki 5471da177e4SLinus Torvalds return ret; 5481da177e4SLinus Torvalds } 549e3920fb4SRafael J. Wysocki 5500db0628dSPaul Gortmaker int cpu_up(unsigned int cpu) 551e3920fb4SRafael J. Wysocki { 552e3920fb4SRafael J. Wysocki int err = 0; 553cf23422bSminskey guo 554e0b582ecSRusty Russell if (!cpu_possible(cpu)) { 55584117da5SFabian Frederick pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n", 55684117da5SFabian Frederick cpu); 55787d5e023SChen Gong #if defined(CONFIG_IA64) 55884117da5SFabian Frederick pr_err("please check additional_cpus= boot parameter\n"); 55973e753a5SKAMEZAWA Hiroyuki #endif 56073e753a5SKAMEZAWA Hiroyuki return -EINVAL; 56173e753a5SKAMEZAWA Hiroyuki } 562e3920fb4SRafael J. Wysocki 56301b0f197SToshi Kani err = try_online_node(cpu_to_node(cpu)); 564cf23422bSminskey guo if (err) 565cf23422bSminskey guo return err; 566cf23422bSminskey guo 567d221938cSGautham R Shenoy cpu_maps_update_begin(); 568e761b772SMax Krasnyansky 569e761b772SMax Krasnyansky if (cpu_hotplug_disabled) { 570e3920fb4SRafael J. Wysocki err = -EBUSY; 571e761b772SMax Krasnyansky goto out; 572e761b772SMax Krasnyansky } 573e761b772SMax Krasnyansky 5748bb78442SRafael J. Wysocki err = _cpu_up(cpu, 0); 575e3920fb4SRafael J. Wysocki 576e761b772SMax Krasnyansky out: 577d221938cSGautham R Shenoy cpu_maps_update_done(); 578e3920fb4SRafael J. Wysocki return err; 579e3920fb4SRafael J. Wysocki } 580a513f6baSPaul E. McKenney EXPORT_SYMBOL_GPL(cpu_up); 581e3920fb4SRafael J. Wysocki 582f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP 583e0b582ecSRusty Russell static cpumask_var_t frozen_cpus; 584e3920fb4SRafael J. Wysocki 585e3920fb4SRafael J. Wysocki int disable_nonboot_cpus(void) 586e3920fb4SRafael J. Wysocki { 587e9a5f426SRafael J. Wysocki int cpu, first_cpu, error = 0; 588e3920fb4SRafael J. Wysocki 589d221938cSGautham R Shenoy cpu_maps_update_begin(); 590e0b582ecSRusty Russell first_cpu = cpumask_first(cpu_online_mask); 5919ee349adSXiaotian Feng /* 5929ee349adSXiaotian Feng * We take down all of the non-boot CPUs in one shot to avoid races 593e3920fb4SRafael J. Wysocki * with the userspace trying to use the CPU hotplug at the same time 594e3920fb4SRafael J. Wysocki */ 595e0b582ecSRusty Russell cpumask_clear(frozen_cpus); 5966ad4c188SPeter Zijlstra 59784117da5SFabian Frederick pr_info("Disabling non-boot CPUs ...\n"); 598e3920fb4SRafael J. Wysocki for_each_online_cpu(cpu) { 599e3920fb4SRafael J. Wysocki if (cpu == first_cpu) 600e3920fb4SRafael J. Wysocki continue; 601bb3632c6STodd E Brandt trace_suspend_resume(TPS("CPU_OFF"), cpu, true); 6028bb78442SRafael J. Wysocki error = _cpu_down(cpu, 1); 603bb3632c6STodd E Brandt trace_suspend_resume(TPS("CPU_OFF"), cpu, false); 604feae3203SMike Travis if (!error) 605e0b582ecSRusty Russell cpumask_set_cpu(cpu, frozen_cpus); 606feae3203SMike Travis else { 60784117da5SFabian Frederick pr_err("Error taking CPU%d down: %d\n", cpu, error); 608e3920fb4SRafael J. Wysocki break; 609e3920fb4SRafael J. Wysocki } 610e3920fb4SRafael J. Wysocki } 61186886e55SJoseph Cihula 61289af7ba5SVitaly Kuznetsov if (!error) 613e3920fb4SRafael J. Wysocki BUG_ON(num_online_cpus() > 1); 61489af7ba5SVitaly Kuznetsov else 61584117da5SFabian Frederick pr_err("Non-boot CPUs are not disabled\n"); 61689af7ba5SVitaly Kuznetsov 61789af7ba5SVitaly Kuznetsov /* 61889af7ba5SVitaly Kuznetsov * Make sure the CPUs won't be enabled by someone else. We need to do 61989af7ba5SVitaly Kuznetsov * this even in case of failure as all disable_nonboot_cpus() users are 62089af7ba5SVitaly Kuznetsov * supposed to do enable_nonboot_cpus() on the failure path. 62189af7ba5SVitaly Kuznetsov */ 62289af7ba5SVitaly Kuznetsov cpu_hotplug_disabled++; 62389af7ba5SVitaly Kuznetsov 624d221938cSGautham R Shenoy cpu_maps_update_done(); 625e3920fb4SRafael J. Wysocki return error; 626e3920fb4SRafael J. Wysocki } 627e3920fb4SRafael J. Wysocki 628d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_begin(void) 629d0af9eedSSuresh Siddha { 630d0af9eedSSuresh Siddha } 631d0af9eedSSuresh Siddha 632d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_end(void) 633d0af9eedSSuresh Siddha { 634d0af9eedSSuresh Siddha } 635d0af9eedSSuresh Siddha 63671cf5aeeSMathias Krause void enable_nonboot_cpus(void) 637e3920fb4SRafael J. Wysocki { 638e3920fb4SRafael J. Wysocki int cpu, error; 639e3920fb4SRafael J. Wysocki 640e3920fb4SRafael J. Wysocki /* Allow everyone to use the CPU hotplug again */ 641d221938cSGautham R Shenoy cpu_maps_update_begin(); 64289af7ba5SVitaly Kuznetsov WARN_ON(--cpu_hotplug_disabled < 0); 643e0b582ecSRusty Russell if (cpumask_empty(frozen_cpus)) 6441d64b9cbSRafael J. Wysocki goto out; 645e3920fb4SRafael J. Wysocki 64684117da5SFabian Frederick pr_info("Enabling non-boot CPUs ...\n"); 647d0af9eedSSuresh Siddha 648d0af9eedSSuresh Siddha arch_enable_nonboot_cpus_begin(); 649d0af9eedSSuresh Siddha 650e0b582ecSRusty Russell for_each_cpu(cpu, frozen_cpus) { 651bb3632c6STodd E Brandt trace_suspend_resume(TPS("CPU_ON"), cpu, true); 6528bb78442SRafael J. Wysocki error = _cpu_up(cpu, 1); 653bb3632c6STodd E Brandt trace_suspend_resume(TPS("CPU_ON"), cpu, false); 654e3920fb4SRafael J. Wysocki if (!error) { 65584117da5SFabian Frederick pr_info("CPU%d is up\n", cpu); 656e3920fb4SRafael J. Wysocki continue; 657e3920fb4SRafael J. Wysocki } 65884117da5SFabian Frederick pr_warn("Error taking CPU%d up: %d\n", cpu, error); 659e3920fb4SRafael J. Wysocki } 660d0af9eedSSuresh Siddha 661d0af9eedSSuresh Siddha arch_enable_nonboot_cpus_end(); 662d0af9eedSSuresh Siddha 663e0b582ecSRusty Russell cpumask_clear(frozen_cpus); 6641d64b9cbSRafael J. Wysocki out: 665d221938cSGautham R Shenoy cpu_maps_update_done(); 666e3920fb4SRafael J. Wysocki } 667e0b582ecSRusty Russell 668d7268a31SFenghua Yu static int __init alloc_frozen_cpus(void) 669e0b582ecSRusty Russell { 670e0b582ecSRusty Russell if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO)) 671e0b582ecSRusty Russell return -ENOMEM; 672e0b582ecSRusty Russell return 0; 673e0b582ecSRusty Russell } 674e0b582ecSRusty Russell core_initcall(alloc_frozen_cpus); 67579cfbdfaSSrivatsa S. Bhat 67679cfbdfaSSrivatsa S. Bhat /* 67779cfbdfaSSrivatsa S. Bhat * When callbacks for CPU hotplug notifications are being executed, we must 67879cfbdfaSSrivatsa S. Bhat * ensure that the state of the system with respect to the tasks being frozen 67979cfbdfaSSrivatsa S. Bhat * or not, as reported by the notification, remains unchanged *throughout the 68079cfbdfaSSrivatsa S. Bhat * duration* of the execution of the callbacks. 68179cfbdfaSSrivatsa S. Bhat * Hence we need to prevent the freezer from racing with regular CPU hotplug. 68279cfbdfaSSrivatsa S. Bhat * 68379cfbdfaSSrivatsa S. Bhat * This synchronization is implemented by mutually excluding regular CPU 68479cfbdfaSSrivatsa S. Bhat * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/ 68579cfbdfaSSrivatsa S. Bhat * Hibernate notifications. 68679cfbdfaSSrivatsa S. Bhat */ 68779cfbdfaSSrivatsa S. Bhat static int 68879cfbdfaSSrivatsa S. Bhat cpu_hotplug_pm_callback(struct notifier_block *nb, 68979cfbdfaSSrivatsa S. Bhat unsigned long action, void *ptr) 69079cfbdfaSSrivatsa S. Bhat { 69179cfbdfaSSrivatsa S. Bhat switch (action) { 69279cfbdfaSSrivatsa S. Bhat 69379cfbdfaSSrivatsa S. Bhat case PM_SUSPEND_PREPARE: 69479cfbdfaSSrivatsa S. Bhat case PM_HIBERNATION_PREPARE: 69516e53dbfSSrivatsa S. Bhat cpu_hotplug_disable(); 69679cfbdfaSSrivatsa S. Bhat break; 69779cfbdfaSSrivatsa S. Bhat 69879cfbdfaSSrivatsa S. Bhat case PM_POST_SUSPEND: 69979cfbdfaSSrivatsa S. Bhat case PM_POST_HIBERNATION: 70016e53dbfSSrivatsa S. Bhat cpu_hotplug_enable(); 70179cfbdfaSSrivatsa S. Bhat break; 70279cfbdfaSSrivatsa S. Bhat 70379cfbdfaSSrivatsa S. Bhat default: 70479cfbdfaSSrivatsa S. Bhat return NOTIFY_DONE; 70579cfbdfaSSrivatsa S. Bhat } 70679cfbdfaSSrivatsa S. Bhat 70779cfbdfaSSrivatsa S. Bhat return NOTIFY_OK; 70879cfbdfaSSrivatsa S. Bhat } 70979cfbdfaSSrivatsa S. Bhat 71079cfbdfaSSrivatsa S. Bhat 711d7268a31SFenghua Yu static int __init cpu_hotplug_pm_sync_init(void) 71279cfbdfaSSrivatsa S. Bhat { 7136e32d479SFenghua Yu /* 7146e32d479SFenghua Yu * cpu_hotplug_pm_callback has higher priority than x86 7156e32d479SFenghua Yu * bsp_pm_callback which depends on cpu_hotplug_pm_callback 7166e32d479SFenghua Yu * to disable cpu hotplug to avoid cpu hotplug race. 7176e32d479SFenghua Yu */ 71879cfbdfaSSrivatsa S. Bhat pm_notifier(cpu_hotplug_pm_callback, 0); 71979cfbdfaSSrivatsa S. Bhat return 0; 72079cfbdfaSSrivatsa S. Bhat } 72179cfbdfaSSrivatsa S. Bhat core_initcall(cpu_hotplug_pm_sync_init); 72279cfbdfaSSrivatsa S. Bhat 723f3de4be9SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP_SMP */ 72468f4f1ecSMax Krasnyansky 725e545a614SManfred Spraul /** 726e545a614SManfred Spraul * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers 727e545a614SManfred Spraul * @cpu: cpu that just started 728e545a614SManfred Spraul * 729e545a614SManfred Spraul * This function calls the cpu_chain notifiers with CPU_STARTING. 730e545a614SManfred Spraul * It must be called by the arch code on the new cpu, before the new cpu 731e545a614SManfred Spraul * enables interrupts and before the "boot" cpu returns from __cpu_up(). 732e545a614SManfred Spraul */ 7330db0628dSPaul Gortmaker void notify_cpu_starting(unsigned int cpu) 734e545a614SManfred Spraul { 735e545a614SManfred Spraul unsigned long val = CPU_STARTING; 736e545a614SManfred Spraul 737e545a614SManfred Spraul #ifdef CONFIG_PM_SLEEP_SMP 738e0b582ecSRusty Russell if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus)) 739e545a614SManfred Spraul val = CPU_STARTING_FROZEN; 740e545a614SManfred Spraul #endif /* CONFIG_PM_SLEEP_SMP */ 741e9fb7631SAkinobu Mita cpu_notify(val, (void *)(long)cpu); 742e545a614SManfred Spraul } 743e545a614SManfred Spraul 74468f4f1ecSMax Krasnyansky #endif /* CONFIG_SMP */ 745b8d317d1SMike Travis 746e56b3bc7SLinus Torvalds /* 747e56b3bc7SLinus Torvalds * cpu_bit_bitmap[] is a special, "compressed" data structure that 748e56b3bc7SLinus Torvalds * represents all NR_CPUS bits binary values of 1<<nr. 749e56b3bc7SLinus Torvalds * 750e0b582ecSRusty Russell * It is used by cpumask_of() to get a constant address to a CPU 751e56b3bc7SLinus Torvalds * mask value that has a single bit set only. 752e56b3bc7SLinus Torvalds */ 753b8d317d1SMike Travis 754e56b3bc7SLinus Torvalds /* cpu_bit_bitmap[0] is empty - so we can back into it */ 7554d51985eSMichael Rodriguez #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x)) 756e56b3bc7SLinus Torvalds #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1) 757e56b3bc7SLinus Torvalds #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2) 758e56b3bc7SLinus Torvalds #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4) 759b8d317d1SMike Travis 760e56b3bc7SLinus Torvalds const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = { 761b8d317d1SMike Travis 762e56b3bc7SLinus Torvalds MASK_DECLARE_8(0), MASK_DECLARE_8(8), 763e56b3bc7SLinus Torvalds MASK_DECLARE_8(16), MASK_DECLARE_8(24), 764e56b3bc7SLinus Torvalds #if BITS_PER_LONG > 32 765e56b3bc7SLinus Torvalds MASK_DECLARE_8(32), MASK_DECLARE_8(40), 766e56b3bc7SLinus Torvalds MASK_DECLARE_8(48), MASK_DECLARE_8(56), 767b8d317d1SMike Travis #endif 768b8d317d1SMike Travis }; 769e56b3bc7SLinus Torvalds EXPORT_SYMBOL_GPL(cpu_bit_bitmap); 7702d3854a3SRusty Russell 7712d3854a3SRusty Russell const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL; 7722d3854a3SRusty Russell EXPORT_SYMBOL(cpu_all_bits); 773b3199c02SRusty Russell 774b3199c02SRusty Russell #ifdef CONFIG_INIT_ALL_POSSIBLE 775b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly 776b3199c02SRusty Russell = CPU_BITS_ALL; 777b3199c02SRusty Russell #else 778b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly; 779b3199c02SRusty Russell #endif 780b3199c02SRusty Russell const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits); 781b3199c02SRusty Russell EXPORT_SYMBOL(cpu_possible_mask); 782b3199c02SRusty Russell 783b3199c02SRusty Russell static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly; 784b3199c02SRusty Russell const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits); 785b3199c02SRusty Russell EXPORT_SYMBOL(cpu_online_mask); 786b3199c02SRusty Russell 787b3199c02SRusty Russell static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly; 788b3199c02SRusty Russell const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits); 789b3199c02SRusty Russell EXPORT_SYMBOL(cpu_present_mask); 790b3199c02SRusty Russell 791b3199c02SRusty Russell static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly; 792b3199c02SRusty Russell const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits); 793b3199c02SRusty Russell EXPORT_SYMBOL(cpu_active_mask); 7943fa41520SRusty Russell 7953fa41520SRusty Russell void set_cpu_possible(unsigned int cpu, bool possible) 7963fa41520SRusty Russell { 7973fa41520SRusty Russell if (possible) 7983fa41520SRusty Russell cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits)); 7993fa41520SRusty Russell else 8003fa41520SRusty Russell cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits)); 8013fa41520SRusty Russell } 8023fa41520SRusty Russell 8033fa41520SRusty Russell void set_cpu_present(unsigned int cpu, bool present) 8043fa41520SRusty Russell { 8053fa41520SRusty Russell if (present) 8063fa41520SRusty Russell cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits)); 8073fa41520SRusty Russell else 8083fa41520SRusty Russell cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits)); 8093fa41520SRusty Russell } 8103fa41520SRusty Russell 8113fa41520SRusty Russell void set_cpu_online(unsigned int cpu, bool online) 8123fa41520SRusty Russell { 8136acbfb96SLai Jiangshan if (online) { 8143fa41520SRusty Russell cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits)); 8156acbfb96SLai Jiangshan cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits)); 8166acbfb96SLai Jiangshan } else { 8173fa41520SRusty Russell cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits)); 8183fa41520SRusty Russell } 8196acbfb96SLai Jiangshan } 8203fa41520SRusty Russell 8213fa41520SRusty Russell void set_cpu_active(unsigned int cpu, bool active) 8223fa41520SRusty Russell { 8233fa41520SRusty Russell if (active) 8243fa41520SRusty Russell cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits)); 8253fa41520SRusty Russell else 8263fa41520SRusty Russell cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits)); 8273fa41520SRusty Russell } 8283fa41520SRusty Russell 8293fa41520SRusty Russell void init_cpu_present(const struct cpumask *src) 8303fa41520SRusty Russell { 8313fa41520SRusty Russell cpumask_copy(to_cpumask(cpu_present_bits), src); 8323fa41520SRusty Russell } 8333fa41520SRusty Russell 8343fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src) 8353fa41520SRusty Russell { 8363fa41520SRusty Russell cpumask_copy(to_cpumask(cpu_possible_bits), src); 8373fa41520SRusty Russell } 8383fa41520SRusty Russell 8393fa41520SRusty Russell void init_cpu_online(const struct cpumask *src) 8403fa41520SRusty Russell { 8413fa41520SRusty Russell cpumask_copy(to_cpumask(cpu_online_bits), src); 8423fa41520SRusty Russell } 843