138498a67SThomas Gleixner /* 238498a67SThomas Gleixner * Common SMP CPU bringup/teardown functions 338498a67SThomas Gleixner */ 4f97f8f06SThomas Gleixner #include <linux/cpu.h> 529d5e047SThomas Gleixner #include <linux/err.h> 629d5e047SThomas Gleixner #include <linux/smp.h> 738498a67SThomas Gleixner #include <linux/init.h> 8f97f8f06SThomas Gleixner #include <linux/list.h> 9f97f8f06SThomas Gleixner #include <linux/slab.h> 1029d5e047SThomas Gleixner #include <linux/sched.h> 11f97f8f06SThomas Gleixner #include <linux/export.h> 1229d5e047SThomas Gleixner #include <linux/percpu.h> 13f97f8f06SThomas Gleixner #include <linux/kthread.h> 14f97f8f06SThomas Gleixner #include <linux/smpboot.h> 1538498a67SThomas Gleixner 1638498a67SThomas Gleixner #include "smpboot.h" 1738498a67SThomas Gleixner 183180d89bSPaul E. McKenney #ifdef CONFIG_SMP 193180d89bSPaul E. McKenney 2029d5e047SThomas Gleixner #ifdef CONFIG_GENERIC_SMP_IDLE_THREAD 2129d5e047SThomas Gleixner /* 2229d5e047SThomas Gleixner * For the hotplug case we keep the task structs around and reuse 2329d5e047SThomas Gleixner * them. 2429d5e047SThomas Gleixner */ 2529d5e047SThomas Gleixner static DEFINE_PER_CPU(struct task_struct *, idle_threads); 2629d5e047SThomas Gleixner 270db0628dSPaul Gortmaker struct task_struct *idle_thread_get(unsigned int cpu) 2829d5e047SThomas Gleixner { 2929d5e047SThomas Gleixner struct task_struct *tsk = per_cpu(idle_threads, cpu); 3029d5e047SThomas Gleixner 3129d5e047SThomas Gleixner if (!tsk) 323bb5d2eeSSuresh Siddha return ERR_PTR(-ENOMEM); 3329d5e047SThomas Gleixner init_idle(tsk, cpu); 3429d5e047SThomas Gleixner return tsk; 3529d5e047SThomas Gleixner } 3629d5e047SThomas Gleixner 3729d5e047SThomas Gleixner void __init idle_thread_set_boot_cpu(void) 3829d5e047SThomas Gleixner { 3929d5e047SThomas Gleixner per_cpu(idle_threads, smp_processor_id()) = current; 4029d5e047SThomas Gleixner } 4129d5e047SThomas Gleixner 424a70d2d9SSrivatsa S. Bhat /** 434a70d2d9SSrivatsa S. Bhat * idle_init - Initialize the idle thread for a cpu 444a70d2d9SSrivatsa S. Bhat * @cpu: The cpu for which the idle thread should be initialized 454a70d2d9SSrivatsa S. Bhat * 464a70d2d9SSrivatsa S. Bhat * Creates the thread if it does not exist. 474a70d2d9SSrivatsa S. Bhat */ 483bb5d2eeSSuresh Siddha static inline void idle_init(unsigned int cpu) 493bb5d2eeSSuresh Siddha { 503bb5d2eeSSuresh Siddha struct task_struct *tsk = per_cpu(idle_threads, cpu); 513bb5d2eeSSuresh Siddha 523bb5d2eeSSuresh Siddha if (!tsk) { 533bb5d2eeSSuresh Siddha tsk = fork_idle(cpu); 543bb5d2eeSSuresh Siddha if (IS_ERR(tsk)) 553bb5d2eeSSuresh Siddha pr_err("SMP: fork_idle() failed for CPU %u\n", cpu); 563bb5d2eeSSuresh Siddha else 573bb5d2eeSSuresh Siddha per_cpu(idle_threads, cpu) = tsk; 583bb5d2eeSSuresh Siddha } 593bb5d2eeSSuresh Siddha } 603bb5d2eeSSuresh Siddha 6129d5e047SThomas Gleixner /** 624a70d2d9SSrivatsa S. Bhat * idle_threads_init - Initialize idle threads for all cpus 6329d5e047SThomas Gleixner */ 643bb5d2eeSSuresh Siddha void __init idle_threads_init(void) 6529d5e047SThomas Gleixner { 66ee74d132SSrivatsa S. Bhat unsigned int cpu, boot_cpu; 67ee74d132SSrivatsa S. Bhat 68ee74d132SSrivatsa S. Bhat boot_cpu = smp_processor_id(); 6929d5e047SThomas Gleixner 703bb5d2eeSSuresh Siddha for_each_possible_cpu(cpu) { 71ee74d132SSrivatsa S. Bhat if (cpu != boot_cpu) 723bb5d2eeSSuresh Siddha idle_init(cpu); 7329d5e047SThomas Gleixner } 7429d5e047SThomas Gleixner } 7529d5e047SThomas Gleixner #endif 76f97f8f06SThomas Gleixner 773180d89bSPaul E. McKenney #endif /* #ifdef CONFIG_SMP */ 783180d89bSPaul E. McKenney 79f97f8f06SThomas Gleixner static LIST_HEAD(hotplug_threads); 80f97f8f06SThomas Gleixner static DEFINE_MUTEX(smpboot_threads_lock); 81f97f8f06SThomas Gleixner 82f97f8f06SThomas Gleixner struct smpboot_thread_data { 83f97f8f06SThomas Gleixner unsigned int cpu; 84f97f8f06SThomas Gleixner unsigned int status; 85f97f8f06SThomas Gleixner struct smp_hotplug_thread *ht; 86f97f8f06SThomas Gleixner }; 87f97f8f06SThomas Gleixner 88f97f8f06SThomas Gleixner enum { 89f97f8f06SThomas Gleixner HP_THREAD_NONE = 0, 90f97f8f06SThomas Gleixner HP_THREAD_ACTIVE, 91f97f8f06SThomas Gleixner HP_THREAD_PARKED, 92f97f8f06SThomas Gleixner }; 93f97f8f06SThomas Gleixner 94f97f8f06SThomas Gleixner /** 95f97f8f06SThomas Gleixner * smpboot_thread_fn - percpu hotplug thread loop function 96f97f8f06SThomas Gleixner * @data: thread data pointer 97f97f8f06SThomas Gleixner * 98f97f8f06SThomas Gleixner * Checks for thread stop and park conditions. Calls the necessary 99f97f8f06SThomas Gleixner * setup, cleanup, park and unpark functions for the registered 100f97f8f06SThomas Gleixner * thread. 101f97f8f06SThomas Gleixner * 102f97f8f06SThomas Gleixner * Returns 1 when the thread should exit, 0 otherwise. 103f97f8f06SThomas Gleixner */ 104f97f8f06SThomas Gleixner static int smpboot_thread_fn(void *data) 105f97f8f06SThomas Gleixner { 106f97f8f06SThomas Gleixner struct smpboot_thread_data *td = data; 107f97f8f06SThomas Gleixner struct smp_hotplug_thread *ht = td->ht; 108f97f8f06SThomas Gleixner 109f97f8f06SThomas Gleixner while (1) { 110f97f8f06SThomas Gleixner set_current_state(TASK_INTERRUPTIBLE); 111f97f8f06SThomas Gleixner preempt_disable(); 112f97f8f06SThomas Gleixner if (kthread_should_stop()) { 113*7d4d2696SPeter Zijlstra __set_current_state(TASK_RUNNING); 114f97f8f06SThomas Gleixner preempt_enable(); 115f97f8f06SThomas Gleixner if (ht->cleanup) 116f97f8f06SThomas Gleixner ht->cleanup(td->cpu, cpu_online(td->cpu)); 117f97f8f06SThomas Gleixner kfree(td); 118f97f8f06SThomas Gleixner return 0; 119f97f8f06SThomas Gleixner } 120f97f8f06SThomas Gleixner 121f97f8f06SThomas Gleixner if (kthread_should_park()) { 122f97f8f06SThomas Gleixner __set_current_state(TASK_RUNNING); 123f97f8f06SThomas Gleixner preempt_enable(); 124f97f8f06SThomas Gleixner if (ht->park && td->status == HP_THREAD_ACTIVE) { 125f97f8f06SThomas Gleixner BUG_ON(td->cpu != smp_processor_id()); 126f97f8f06SThomas Gleixner ht->park(td->cpu); 127f97f8f06SThomas Gleixner td->status = HP_THREAD_PARKED; 128f97f8f06SThomas Gleixner } 129f97f8f06SThomas Gleixner kthread_parkme(); 130f97f8f06SThomas Gleixner /* We might have been woken for stop */ 131f97f8f06SThomas Gleixner continue; 132f97f8f06SThomas Gleixner } 133f97f8f06SThomas Gleixner 134dc893e19SArnd Bergmann BUG_ON(td->cpu != smp_processor_id()); 135f97f8f06SThomas Gleixner 136f97f8f06SThomas Gleixner /* Check for state change setup */ 137f97f8f06SThomas Gleixner switch (td->status) { 138f97f8f06SThomas Gleixner case HP_THREAD_NONE: 139*7d4d2696SPeter Zijlstra __set_current_state(TASK_RUNNING); 140f97f8f06SThomas Gleixner preempt_enable(); 141f97f8f06SThomas Gleixner if (ht->setup) 142f97f8f06SThomas Gleixner ht->setup(td->cpu); 143f97f8f06SThomas Gleixner td->status = HP_THREAD_ACTIVE; 144*7d4d2696SPeter Zijlstra continue; 145*7d4d2696SPeter Zijlstra 146f97f8f06SThomas Gleixner case HP_THREAD_PARKED: 147*7d4d2696SPeter Zijlstra __set_current_state(TASK_RUNNING); 148f97f8f06SThomas Gleixner preempt_enable(); 149f97f8f06SThomas Gleixner if (ht->unpark) 150f97f8f06SThomas Gleixner ht->unpark(td->cpu); 151f97f8f06SThomas Gleixner td->status = HP_THREAD_ACTIVE; 152*7d4d2696SPeter Zijlstra continue; 153f97f8f06SThomas Gleixner } 154f97f8f06SThomas Gleixner 155f97f8f06SThomas Gleixner if (!ht->thread_should_run(td->cpu)) { 156*7d4d2696SPeter Zijlstra preempt_enable_no_resched(); 157f97f8f06SThomas Gleixner schedule(); 158f97f8f06SThomas Gleixner } else { 159*7d4d2696SPeter Zijlstra __set_current_state(TASK_RUNNING); 160f97f8f06SThomas Gleixner preempt_enable(); 161f97f8f06SThomas Gleixner ht->thread_fn(td->cpu); 162f97f8f06SThomas Gleixner } 163f97f8f06SThomas Gleixner } 164f97f8f06SThomas Gleixner } 165f97f8f06SThomas Gleixner 166f97f8f06SThomas Gleixner static int 167f97f8f06SThomas Gleixner __smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu) 168f97f8f06SThomas Gleixner { 169f97f8f06SThomas Gleixner struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu); 170f97f8f06SThomas Gleixner struct smpboot_thread_data *td; 171f97f8f06SThomas Gleixner 172f97f8f06SThomas Gleixner if (tsk) 173f97f8f06SThomas Gleixner return 0; 174f97f8f06SThomas Gleixner 175f97f8f06SThomas Gleixner td = kzalloc_node(sizeof(*td), GFP_KERNEL, cpu_to_node(cpu)); 176f97f8f06SThomas Gleixner if (!td) 177f97f8f06SThomas Gleixner return -ENOMEM; 178f97f8f06SThomas Gleixner td->cpu = cpu; 179f97f8f06SThomas Gleixner td->ht = ht; 180f97f8f06SThomas Gleixner 181f97f8f06SThomas Gleixner tsk = kthread_create_on_cpu(smpboot_thread_fn, td, cpu, 182f97f8f06SThomas Gleixner ht->thread_comm); 183f97f8f06SThomas Gleixner if (IS_ERR(tsk)) { 184f97f8f06SThomas Gleixner kfree(td); 185f97f8f06SThomas Gleixner return PTR_ERR(tsk); 186f97f8f06SThomas Gleixner } 187f97f8f06SThomas Gleixner get_task_struct(tsk); 188f97f8f06SThomas Gleixner *per_cpu_ptr(ht->store, cpu) = tsk; 189f2530dc7SThomas Gleixner if (ht->create) { 190f2530dc7SThomas Gleixner /* 191f2530dc7SThomas Gleixner * Make sure that the task has actually scheduled out 192f2530dc7SThomas Gleixner * into park position, before calling the create 193f2530dc7SThomas Gleixner * callback. At least the migration thread callback 194f2530dc7SThomas Gleixner * requires that the task is off the runqueue. 195f2530dc7SThomas Gleixner */ 196f2530dc7SThomas Gleixner if (!wait_task_inactive(tsk, TASK_PARKED)) 197f2530dc7SThomas Gleixner WARN_ON(1); 198f2530dc7SThomas Gleixner else 1997d7e499fSThomas Gleixner ht->create(cpu); 200f2530dc7SThomas Gleixner } 201f97f8f06SThomas Gleixner return 0; 202f97f8f06SThomas Gleixner } 203f97f8f06SThomas Gleixner 204f97f8f06SThomas Gleixner int smpboot_create_threads(unsigned int cpu) 205f97f8f06SThomas Gleixner { 206f97f8f06SThomas Gleixner struct smp_hotplug_thread *cur; 207f97f8f06SThomas Gleixner int ret = 0; 208f97f8f06SThomas Gleixner 209f97f8f06SThomas Gleixner mutex_lock(&smpboot_threads_lock); 210f97f8f06SThomas Gleixner list_for_each_entry(cur, &hotplug_threads, list) { 211f97f8f06SThomas Gleixner ret = __smpboot_create_thread(cur, cpu); 212f97f8f06SThomas Gleixner if (ret) 213f97f8f06SThomas Gleixner break; 214f97f8f06SThomas Gleixner } 215f97f8f06SThomas Gleixner mutex_unlock(&smpboot_threads_lock); 216f97f8f06SThomas Gleixner return ret; 217f97f8f06SThomas Gleixner } 218f97f8f06SThomas Gleixner 219f97f8f06SThomas Gleixner static void smpboot_unpark_thread(struct smp_hotplug_thread *ht, unsigned int cpu) 220f97f8f06SThomas Gleixner { 221f97f8f06SThomas Gleixner struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu); 222f97f8f06SThomas Gleixner 22346c498c2SThomas Gleixner if (ht->pre_unpark) 22446c498c2SThomas Gleixner ht->pre_unpark(cpu); 225f97f8f06SThomas Gleixner kthread_unpark(tsk); 226f97f8f06SThomas Gleixner } 227f97f8f06SThomas Gleixner 228f97f8f06SThomas Gleixner void smpboot_unpark_threads(unsigned int cpu) 229f97f8f06SThomas Gleixner { 230f97f8f06SThomas Gleixner struct smp_hotplug_thread *cur; 231f97f8f06SThomas Gleixner 232f97f8f06SThomas Gleixner mutex_lock(&smpboot_threads_lock); 233f97f8f06SThomas Gleixner list_for_each_entry(cur, &hotplug_threads, list) 234f97f8f06SThomas Gleixner smpboot_unpark_thread(cur, cpu); 235f97f8f06SThomas Gleixner mutex_unlock(&smpboot_threads_lock); 236f97f8f06SThomas Gleixner } 237f97f8f06SThomas Gleixner 238f97f8f06SThomas Gleixner static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu) 239f97f8f06SThomas Gleixner { 240f97f8f06SThomas Gleixner struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu); 241f97f8f06SThomas Gleixner 2427d7e499fSThomas Gleixner if (tsk && !ht->selfparking) 243f97f8f06SThomas Gleixner kthread_park(tsk); 244f97f8f06SThomas Gleixner } 245f97f8f06SThomas Gleixner 246f97f8f06SThomas Gleixner void smpboot_park_threads(unsigned int cpu) 247f97f8f06SThomas Gleixner { 248f97f8f06SThomas Gleixner struct smp_hotplug_thread *cur; 249f97f8f06SThomas Gleixner 250f97f8f06SThomas Gleixner mutex_lock(&smpboot_threads_lock); 251f97f8f06SThomas Gleixner list_for_each_entry_reverse(cur, &hotplug_threads, list) 252f97f8f06SThomas Gleixner smpboot_park_thread(cur, cpu); 253f97f8f06SThomas Gleixner mutex_unlock(&smpboot_threads_lock); 254f97f8f06SThomas Gleixner } 255f97f8f06SThomas Gleixner 256f97f8f06SThomas Gleixner static void smpboot_destroy_threads(struct smp_hotplug_thread *ht) 257f97f8f06SThomas Gleixner { 258f97f8f06SThomas Gleixner unsigned int cpu; 259f97f8f06SThomas Gleixner 260f97f8f06SThomas Gleixner /* We need to destroy also the parked threads of offline cpus */ 261f97f8f06SThomas Gleixner for_each_possible_cpu(cpu) { 262f97f8f06SThomas Gleixner struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu); 263f97f8f06SThomas Gleixner 264f97f8f06SThomas Gleixner if (tsk) { 265f97f8f06SThomas Gleixner kthread_stop(tsk); 266f97f8f06SThomas Gleixner put_task_struct(tsk); 267f97f8f06SThomas Gleixner *per_cpu_ptr(ht->store, cpu) = NULL; 268f97f8f06SThomas Gleixner } 269f97f8f06SThomas Gleixner } 270f97f8f06SThomas Gleixner } 271f97f8f06SThomas Gleixner 272f97f8f06SThomas Gleixner /** 273f97f8f06SThomas Gleixner * smpboot_register_percpu_thread - Register a per_cpu thread related to hotplug 274f97f8f06SThomas Gleixner * @plug_thread: Hotplug thread descriptor 275f97f8f06SThomas Gleixner * 276f97f8f06SThomas Gleixner * Creates and starts the threads on all online cpus. 277f97f8f06SThomas Gleixner */ 278f97f8f06SThomas Gleixner int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread) 279f97f8f06SThomas Gleixner { 280f97f8f06SThomas Gleixner unsigned int cpu; 281f97f8f06SThomas Gleixner int ret = 0; 282f97f8f06SThomas Gleixner 283f97f8f06SThomas Gleixner mutex_lock(&smpboot_threads_lock); 284f97f8f06SThomas Gleixner for_each_online_cpu(cpu) { 285f97f8f06SThomas Gleixner ret = __smpboot_create_thread(plug_thread, cpu); 286f97f8f06SThomas Gleixner if (ret) { 287f97f8f06SThomas Gleixner smpboot_destroy_threads(plug_thread); 288f97f8f06SThomas Gleixner goto out; 289f97f8f06SThomas Gleixner } 290f97f8f06SThomas Gleixner smpboot_unpark_thread(plug_thread, cpu); 291f97f8f06SThomas Gleixner } 292f97f8f06SThomas Gleixner list_add(&plug_thread->list, &hotplug_threads); 293f97f8f06SThomas Gleixner out: 294f97f8f06SThomas Gleixner mutex_unlock(&smpboot_threads_lock); 295f97f8f06SThomas Gleixner return ret; 296f97f8f06SThomas Gleixner } 297f97f8f06SThomas Gleixner EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread); 298f97f8f06SThomas Gleixner 299f97f8f06SThomas Gleixner /** 300f97f8f06SThomas Gleixner * smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug 301f97f8f06SThomas Gleixner * @plug_thread: Hotplug thread descriptor 302f97f8f06SThomas Gleixner * 303f97f8f06SThomas Gleixner * Stops all threads on all possible cpus. 304f97f8f06SThomas Gleixner */ 305f97f8f06SThomas Gleixner void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread) 306f97f8f06SThomas Gleixner { 307f97f8f06SThomas Gleixner get_online_cpus(); 308f97f8f06SThomas Gleixner mutex_lock(&smpboot_threads_lock); 309f97f8f06SThomas Gleixner list_del(&plug_thread->list); 310f97f8f06SThomas Gleixner smpboot_destroy_threads(plug_thread); 311f97f8f06SThomas Gleixner mutex_unlock(&smpboot_threads_lock); 312f97f8f06SThomas Gleixner put_online_cpus(); 313f97f8f06SThomas Gleixner } 314f97f8f06SThomas Gleixner EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread); 315