Back to home page

LXR

 
 

    


0001 /*
0002  * Common SMP CPU bringup/teardown functions
0003  */
0004 #include <linux/cpu.h>
0005 #include <linux/err.h>
0006 #include <linux/smp.h>
0007 #include <linux/delay.h>
0008 #include <linux/init.h>
0009 #include <linux/list.h>
0010 #include <linux/slab.h>
0011 #include <linux/sched.h>
0012 #include <linux/export.h>
0013 #include <linux/percpu.h>
0014 #include <linux/kthread.h>
0015 #include <linux/smpboot.h>
0016 
0017 #include "smpboot.h"
0018 
0019 #ifdef CONFIG_SMP
0020 
0021 #ifdef CONFIG_GENERIC_SMP_IDLE_THREAD
0022 /*
0023  * For the hotplug case we keep the task structs around and reuse
0024  * them.
0025  */
0026 static DEFINE_PER_CPU(struct task_struct *, idle_threads);
0027 
0028 struct task_struct *idle_thread_get(unsigned int cpu)
0029 {
0030     struct task_struct *tsk = per_cpu(idle_threads, cpu);
0031 
0032     if (!tsk)
0033         return ERR_PTR(-ENOMEM);
0034     init_idle(tsk, cpu);
0035     return tsk;
0036 }
0037 
0038 void __init idle_thread_set_boot_cpu(void)
0039 {
0040     per_cpu(idle_threads, smp_processor_id()) = current;
0041 }
0042 
0043 /**
0044  * idle_init - Initialize the idle thread for a cpu
0045  * @cpu:    The cpu for which the idle thread should be initialized
0046  *
0047  * Creates the thread if it does not exist.
0048  */
0049 static inline void idle_init(unsigned int cpu)
0050 {
0051     struct task_struct *tsk = per_cpu(idle_threads, cpu);
0052 
0053     if (!tsk) {
0054         tsk = fork_idle(cpu);
0055         if (IS_ERR(tsk))
0056             pr_err("SMP: fork_idle() failed for CPU %u\n", cpu);
0057         else
0058             per_cpu(idle_threads, cpu) = tsk;
0059     }
0060 }
0061 
0062 /**
0063  * idle_threads_init - Initialize idle threads for all cpus
0064  */
0065 void __init idle_threads_init(void)
0066 {
0067     unsigned int cpu, boot_cpu;
0068 
0069     boot_cpu = smp_processor_id();
0070 
0071     for_each_possible_cpu(cpu) {
0072         if (cpu != boot_cpu)
0073             idle_init(cpu);
0074     }
0075 }
0076 #endif
0077 
0078 #endif /* #ifdef CONFIG_SMP */
0079 
0080 static LIST_HEAD(hotplug_threads);
0081 static DEFINE_MUTEX(smpboot_threads_lock);
0082 
0083 struct smpboot_thread_data {
0084     unsigned int            cpu;
0085     unsigned int            status;
0086     struct smp_hotplug_thread   *ht;
0087 };
0088 
0089 enum {
0090     HP_THREAD_NONE = 0,
0091     HP_THREAD_ACTIVE,
0092     HP_THREAD_PARKED,
0093 };
0094 
0095 /**
0096  * smpboot_thread_fn - percpu hotplug thread loop function
0097  * @data:   thread data pointer
0098  *
0099  * Checks for thread stop and park conditions. Calls the necessary
0100  * setup, cleanup, park and unpark functions for the registered
0101  * thread.
0102  *
0103  * Returns 1 when the thread should exit, 0 otherwise.
0104  */
0105 static int smpboot_thread_fn(void *data)
0106 {
0107     struct smpboot_thread_data *td = data;
0108     struct smp_hotplug_thread *ht = td->ht;
0109 
0110     while (1) {
0111         set_current_state(TASK_INTERRUPTIBLE);
0112         preempt_disable();
0113         if (kthread_should_stop()) {
0114             __set_current_state(TASK_RUNNING);
0115             preempt_enable();
0116             /* cleanup must mirror setup */
0117             if (ht->cleanup && td->status != HP_THREAD_NONE)
0118                 ht->cleanup(td->cpu, cpu_online(td->cpu));
0119             kfree(td);
0120             return 0;
0121         }
0122 
0123         if (kthread_should_park()) {
0124             __set_current_state(TASK_RUNNING);
0125             preempt_enable();
0126             if (ht->park && td->status == HP_THREAD_ACTIVE) {
0127                 BUG_ON(td->cpu != smp_processor_id());
0128                 ht->park(td->cpu);
0129                 td->status = HP_THREAD_PARKED;
0130             }
0131             kthread_parkme();
0132             /* We might have been woken for stop */
0133             continue;
0134         }
0135 
0136         BUG_ON(td->cpu != smp_processor_id());
0137 
0138         /* Check for state change setup */
0139         switch (td->status) {
0140         case HP_THREAD_NONE:
0141             __set_current_state(TASK_RUNNING);
0142             preempt_enable();
0143             if (ht->setup)
0144                 ht->setup(td->cpu);
0145             td->status = HP_THREAD_ACTIVE;
0146             continue;
0147 
0148         case HP_THREAD_PARKED:
0149             __set_current_state(TASK_RUNNING);
0150             preempt_enable();
0151             if (ht->unpark)
0152                 ht->unpark(td->cpu);
0153             td->status = HP_THREAD_ACTIVE;
0154             continue;
0155         }
0156 
0157         if (!ht->thread_should_run(td->cpu)) {
0158             preempt_enable_no_resched();
0159             schedule();
0160         } else {
0161             __set_current_state(TASK_RUNNING);
0162             preempt_enable();
0163             ht->thread_fn(td->cpu);
0164         }
0165     }
0166 }
0167 
0168 static int
0169 __smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
0170 {
0171     struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
0172     struct smpboot_thread_data *td;
0173 
0174     if (tsk)
0175         return 0;
0176 
0177     td = kzalloc_node(sizeof(*td), GFP_KERNEL, cpu_to_node(cpu));
0178     if (!td)
0179         return -ENOMEM;
0180     td->cpu = cpu;
0181     td->ht = ht;
0182 
0183     tsk = kthread_create_on_cpu(smpboot_thread_fn, td, cpu,
0184                     ht->thread_comm);
0185     if (IS_ERR(tsk)) {
0186         kfree(td);
0187         return PTR_ERR(tsk);
0188     }
0189     /*
0190      * Park the thread so that it could start right on the CPU
0191      * when it is available.
0192      */
0193     kthread_park(tsk);
0194     get_task_struct(tsk);
0195     *per_cpu_ptr(ht->store, cpu) = tsk;
0196     if (ht->create) {
0197         /*
0198          * Make sure that the task has actually scheduled out
0199          * into park position, before calling the create
0200          * callback. At least the migration thread callback
0201          * requires that the task is off the runqueue.
0202          */
0203         if (!wait_task_inactive(tsk, TASK_PARKED))
0204             WARN_ON(1);
0205         else
0206             ht->create(cpu);
0207     }
0208     return 0;
0209 }
0210 
0211 int smpboot_create_threads(unsigned int cpu)
0212 {
0213     struct smp_hotplug_thread *cur;
0214     int ret = 0;
0215 
0216     mutex_lock(&smpboot_threads_lock);
0217     list_for_each_entry(cur, &hotplug_threads, list) {
0218         ret = __smpboot_create_thread(cur, cpu);
0219         if (ret)
0220             break;
0221     }
0222     mutex_unlock(&smpboot_threads_lock);
0223     return ret;
0224 }
0225 
0226 static void smpboot_unpark_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
0227 {
0228     struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
0229 
0230     if (!ht->selfparking)
0231         kthread_unpark(tsk);
0232 }
0233 
0234 int smpboot_unpark_threads(unsigned int cpu)
0235 {
0236     struct smp_hotplug_thread *cur;
0237 
0238     mutex_lock(&smpboot_threads_lock);
0239     list_for_each_entry(cur, &hotplug_threads, list)
0240         if (cpumask_test_cpu(cpu, cur->cpumask))
0241             smpboot_unpark_thread(cur, cpu);
0242     mutex_unlock(&smpboot_threads_lock);
0243     return 0;
0244 }
0245 
0246 static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
0247 {
0248     struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
0249 
0250     if (tsk && !ht->selfparking)
0251         kthread_park(tsk);
0252 }
0253 
0254 int smpboot_park_threads(unsigned int cpu)
0255 {
0256     struct smp_hotplug_thread *cur;
0257 
0258     mutex_lock(&smpboot_threads_lock);
0259     list_for_each_entry_reverse(cur, &hotplug_threads, list)
0260         smpboot_park_thread(cur, cpu);
0261     mutex_unlock(&smpboot_threads_lock);
0262     return 0;
0263 }
0264 
0265 static void smpboot_destroy_threads(struct smp_hotplug_thread *ht)
0266 {
0267     unsigned int cpu;
0268 
0269     /* We need to destroy also the parked threads of offline cpus */
0270     for_each_possible_cpu(cpu) {
0271         struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
0272 
0273         if (tsk) {
0274             kthread_stop(tsk);
0275             put_task_struct(tsk);
0276             *per_cpu_ptr(ht->store, cpu) = NULL;
0277         }
0278     }
0279 }
0280 
0281 /**
0282  * smpboot_register_percpu_thread_cpumask - Register a per_cpu thread related
0283  *                      to hotplug
0284  * @plug_thread:    Hotplug thread descriptor
0285  * @cpumask:        The cpumask where threads run
0286  *
0287  * Creates and starts the threads on all online cpus.
0288  */
0289 int smpboot_register_percpu_thread_cpumask(struct smp_hotplug_thread *plug_thread,
0290                        const struct cpumask *cpumask)
0291 {
0292     unsigned int cpu;
0293     int ret = 0;
0294 
0295     if (!alloc_cpumask_var(&plug_thread->cpumask, GFP_KERNEL))
0296         return -ENOMEM;
0297     cpumask_copy(plug_thread->cpumask, cpumask);
0298 
0299     get_online_cpus();
0300     mutex_lock(&smpboot_threads_lock);
0301     for_each_online_cpu(cpu) {
0302         ret = __smpboot_create_thread(plug_thread, cpu);
0303         if (ret) {
0304             smpboot_destroy_threads(plug_thread);
0305             free_cpumask_var(plug_thread->cpumask);
0306             goto out;
0307         }
0308         if (cpumask_test_cpu(cpu, cpumask))
0309             smpboot_unpark_thread(plug_thread, cpu);
0310     }
0311     list_add(&plug_thread->list, &hotplug_threads);
0312 out:
0313     mutex_unlock(&smpboot_threads_lock);
0314     put_online_cpus();
0315     return ret;
0316 }
0317 EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread_cpumask);
0318 
0319 /**
0320  * smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug
0321  * @plug_thread:    Hotplug thread descriptor
0322  *
0323  * Stops all threads on all possible cpus.
0324  */
0325 void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
0326 {
0327     get_online_cpus();
0328     mutex_lock(&smpboot_threads_lock);
0329     list_del(&plug_thread->list);
0330     smpboot_destroy_threads(plug_thread);
0331     mutex_unlock(&smpboot_threads_lock);
0332     put_online_cpus();
0333     free_cpumask_var(plug_thread->cpumask);
0334 }
0335 EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);
0336 
0337 /**
0338  * smpboot_update_cpumask_percpu_thread - Adjust which per_cpu hotplug threads stay parked
0339  * @plug_thread:    Hotplug thread descriptor
0340  * @new:        Revised mask to use
0341  *
0342  * The cpumask field in the smp_hotplug_thread must not be updated directly
0343  * by the client, but only by calling this function.
0344  * This function can only be called on a registered smp_hotplug_thread.
0345  */
0346 int smpboot_update_cpumask_percpu_thread(struct smp_hotplug_thread *plug_thread,
0347                      const struct cpumask *new)
0348 {
0349     struct cpumask *old = plug_thread->cpumask;
0350     cpumask_var_t tmp;
0351     unsigned int cpu;
0352 
0353     if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
0354         return -ENOMEM;
0355 
0356     get_online_cpus();
0357     mutex_lock(&smpboot_threads_lock);
0358 
0359     /* Park threads that were exclusively enabled on the old mask. */
0360     cpumask_andnot(tmp, old, new);
0361     for_each_cpu_and(cpu, tmp, cpu_online_mask)
0362         smpboot_park_thread(plug_thread, cpu);
0363 
0364     /* Unpark threads that are exclusively enabled on the new mask. */
0365     cpumask_andnot(tmp, new, old);
0366     for_each_cpu_and(cpu, tmp, cpu_online_mask)
0367         smpboot_unpark_thread(plug_thread, cpu);
0368 
0369     cpumask_copy(old, new);
0370 
0371     mutex_unlock(&smpboot_threads_lock);
0372     put_online_cpus();
0373 
0374     free_cpumask_var(tmp);
0375 
0376     return 0;
0377 }
0378 EXPORT_SYMBOL_GPL(smpboot_update_cpumask_percpu_thread);
0379 
0380 static DEFINE_PER_CPU(atomic_t, cpu_hotplug_state) = ATOMIC_INIT(CPU_POST_DEAD);
0381 
0382 /*
0383  * Called to poll specified CPU's state, for example, when waiting for
0384  * a CPU to come online.
0385  */
0386 int cpu_report_state(int cpu)
0387 {
0388     return atomic_read(&per_cpu(cpu_hotplug_state, cpu));
0389 }
0390 
0391 /*
0392  * If CPU has died properly, set its state to CPU_UP_PREPARE and
0393  * return success.  Otherwise, return -EBUSY if the CPU died after
0394  * cpu_wait_death() timed out.  And yet otherwise again, return -EAGAIN
0395  * if cpu_wait_death() timed out and the CPU still hasn't gotten around
0396  * to dying.  In the latter two cases, the CPU might not be set up
0397  * properly, but it is up to the arch-specific code to decide.
0398  * Finally, -EIO indicates an unanticipated problem.
0399  *
0400  * Note that it is permissible to omit this call entirely, as is
0401  * done in architectures that do no CPU-hotplug error checking.
0402  */
0403 int cpu_check_up_prepare(int cpu)
0404 {
0405     if (!IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
0406         atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_UP_PREPARE);
0407         return 0;
0408     }
0409 
0410     switch (atomic_read(&per_cpu(cpu_hotplug_state, cpu))) {
0411 
0412     case CPU_POST_DEAD:
0413 
0414         /* The CPU died properly, so just start it up again. */
0415         atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_UP_PREPARE);
0416         return 0;
0417 
0418     case CPU_DEAD_FROZEN:
0419 
0420         /*
0421          * Timeout during CPU death, so let caller know.
0422          * The outgoing CPU completed its processing, but after
0423          * cpu_wait_death() timed out and reported the error. The
0424          * caller is free to proceed, in which case the state
0425          * will be reset properly by cpu_set_state_online().
0426          * Proceeding despite this -EBUSY return makes sense
0427          * for systems where the outgoing CPUs take themselves
0428          * offline, with no post-death manipulation required from
0429          * a surviving CPU.
0430          */
0431         return -EBUSY;
0432 
0433     case CPU_BROKEN:
0434 
0435         /*
0436          * The most likely reason we got here is that there was
0437          * a timeout during CPU death, and the outgoing CPU never
0438          * did complete its processing.  This could happen on
0439          * a virtualized system if the outgoing VCPU gets preempted
0440          * for more than five seconds, and the user attempts to
0441          * immediately online that same CPU.  Trying again later
0442          * might return -EBUSY above, hence -EAGAIN.
0443          */
0444         return -EAGAIN;
0445 
0446     default:
0447 
0448         /* Should not happen.  Famous last words. */
0449         return -EIO;
0450     }
0451 }
0452 
0453 /*
0454  * Mark the specified CPU online.
0455  *
0456  * Note that it is permissible to omit this call entirely, as is
0457  * done in architectures that do no CPU-hotplug error checking.
0458  */
0459 void cpu_set_state_online(int cpu)
0460 {
0461     (void)atomic_xchg(&per_cpu(cpu_hotplug_state, cpu), CPU_ONLINE);
0462 }
0463 
0464 #ifdef CONFIG_HOTPLUG_CPU
0465 
0466 /*
0467  * Wait for the specified CPU to exit the idle loop and die.
0468  */
0469 bool cpu_wait_death(unsigned int cpu, int seconds)
0470 {
0471     int jf_left = seconds * HZ;
0472     int oldstate;
0473     bool ret = true;
0474     int sleep_jf = 1;
0475 
0476     might_sleep();
0477 
0478     /* The outgoing CPU will normally get done quite quickly. */
0479     if (atomic_read(&per_cpu(cpu_hotplug_state, cpu)) == CPU_DEAD)
0480         goto update_state;
0481     udelay(5);
0482 
0483     /* But if the outgoing CPU dawdles, wait increasingly long times. */
0484     while (atomic_read(&per_cpu(cpu_hotplug_state, cpu)) != CPU_DEAD) {
0485         schedule_timeout_uninterruptible(sleep_jf);
0486         jf_left -= sleep_jf;
0487         if (jf_left <= 0)
0488             break;
0489         sleep_jf = DIV_ROUND_UP(sleep_jf * 11, 10);
0490     }
0491 update_state:
0492     oldstate = atomic_read(&per_cpu(cpu_hotplug_state, cpu));
0493     if (oldstate == CPU_DEAD) {
0494         /* Outgoing CPU died normally, update state. */
0495         smp_mb(); /* atomic_read() before update. */
0496         atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_POST_DEAD);
0497     } else {
0498         /* Outgoing CPU still hasn't died, set state accordingly. */
0499         if (atomic_cmpxchg(&per_cpu(cpu_hotplug_state, cpu),
0500                    oldstate, CPU_BROKEN) != oldstate)
0501             goto update_state;
0502         ret = false;
0503     }
0504     return ret;
0505 }
0506 
0507 /*
0508  * Called by the outgoing CPU to report its successful death.  Return
0509  * false if this report follows the surviving CPU's timing out.
0510  *
0511  * A separate "CPU_DEAD_FROZEN" is used when the surviving CPU
0512  * timed out.  This approach allows architectures to omit calls to
0513  * cpu_check_up_prepare() and cpu_set_state_online() without defeating
0514  * the next cpu_wait_death()'s polling loop.
0515  */
0516 bool cpu_report_death(void)
0517 {
0518     int oldstate;
0519     int newstate;
0520     int cpu = smp_processor_id();
0521 
0522     do {
0523         oldstate = atomic_read(&per_cpu(cpu_hotplug_state, cpu));
0524         if (oldstate != CPU_BROKEN)
0525             newstate = CPU_DEAD;
0526         else
0527             newstate = CPU_DEAD_FROZEN;
0528     } while (atomic_cmpxchg(&per_cpu(cpu_hotplug_state, cpu),
0529                 oldstate, newstate) != oldstate);
0530     return newstate == CPU_DEAD;
0531 }
0532 
0533 #endif /* #ifdef CONFIG_HOTPLUG_CPU */