stop_machine.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* Copyright 2005 Rusty Russell rusty@rustcorp.com.au IBM Corporation.
  2. * GPL v2 and any later version.
  3. */
  4. #include <linux/cpu.h>
  5. #include <linux/err.h>
  6. #include <linux/kthread.h>
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/stop_machine.h>
  10. #include <linux/syscalls.h>
  11. #include <linux/interrupt.h>
  12. #include <asm/atomic.h>
  13. #include <asm/semaphore.h>
  14. #include <asm/uaccess.h>
  15. /* Since we effect priority and affinity (both of which are visible
  16. * to, and settable by outside processes) we do indirection via a
  17. * kthread. */
  18. /* Thread to stop each CPU in user context. */
  19. enum stopmachine_state {
  20. STOPMACHINE_WAIT,
  21. STOPMACHINE_PREPARE,
  22. STOPMACHINE_DISABLE_IRQ,
  23. STOPMACHINE_EXIT,
  24. };
  25. static enum stopmachine_state stopmachine_state;
  26. static unsigned int stopmachine_num_threads;
  27. static atomic_t stopmachine_thread_ack;
  28. static DECLARE_MUTEX(stopmachine_mutex);
  29. static int stopmachine(void *cpu)
  30. {
  31. int irqs_disabled = 0;
  32. int prepared = 0;
  33. set_cpus_allowed(current, cpumask_of_cpu((int)(long)cpu));
  34. /* Ack: we are alive */
  35. smp_mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */
  36. atomic_inc(&stopmachine_thread_ack);
  37. /* Simple state machine */
  38. while (stopmachine_state != STOPMACHINE_EXIT) {
  39. if (stopmachine_state == STOPMACHINE_DISABLE_IRQ
  40. && !irqs_disabled) {
  41. local_irq_disable();
  42. hard_irq_disable();
  43. irqs_disabled = 1;
  44. /* Ack: irqs disabled. */
  45. smp_mb(); /* Must read state first. */
  46. atomic_inc(&stopmachine_thread_ack);
  47. } else if (stopmachine_state == STOPMACHINE_PREPARE
  48. && !prepared) {
  49. /* Everyone is in place, hold CPU. */
  50. preempt_disable();
  51. prepared = 1;
  52. smp_mb(); /* Must read state first. */
  53. atomic_inc(&stopmachine_thread_ack);
  54. }
  55. /* Yield in first stage: migration threads need to
  56. * help our sisters onto their CPUs. */
  57. if (!prepared && !irqs_disabled)
  58. yield();
  59. else
  60. cpu_relax();
  61. }
  62. /* Ack: we are exiting. */
  63. smp_mb(); /* Must read state first. */
  64. atomic_inc(&stopmachine_thread_ack);
  65. if (irqs_disabled)
  66. local_irq_enable();
  67. if (prepared)
  68. preempt_enable();
  69. return 0;
  70. }
  71. /* Change the thread state */
  72. static void stopmachine_set_state(enum stopmachine_state state)
  73. {
  74. atomic_set(&stopmachine_thread_ack, 0);
  75. smp_wmb();
  76. stopmachine_state = state;
  77. while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
  78. cpu_relax();
  79. }
  80. static int stop_machine(void)
  81. {
  82. int i, ret = 0;
  83. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  84. /* One high-prio thread per cpu. We'll do this one. */
  85. sched_setscheduler(current, SCHED_FIFO, &param);
  86. atomic_set(&stopmachine_thread_ack, 0);
  87. stopmachine_num_threads = 0;
  88. stopmachine_state = STOPMACHINE_WAIT;
  89. for_each_online_cpu(i) {
  90. if (i == raw_smp_processor_id())
  91. continue;
  92. ret = kernel_thread(stopmachine, (void *)(long)i,CLONE_KERNEL);
  93. if (ret < 0)
  94. break;
  95. stopmachine_num_threads++;
  96. }
  97. /* Wait for them all to come to life. */
  98. while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
  99. yield();
  100. /* If some failed, kill them all. */
  101. if (ret < 0) {
  102. stopmachine_set_state(STOPMACHINE_EXIT);
  103. return ret;
  104. }
  105. /* Now they are all started, make them hold the CPUs, ready. */
  106. preempt_disable();
  107. stopmachine_set_state(STOPMACHINE_PREPARE);
  108. /* Make them disable irqs. */
  109. local_irq_disable();
  110. hard_irq_disable();
  111. stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);
  112. return 0;
  113. }
  114. static void restart_machine(void)
  115. {
  116. stopmachine_set_state(STOPMACHINE_EXIT);
  117. local_irq_enable();
  118. preempt_enable_no_resched();
  119. }
  120. struct stop_machine_data
  121. {
  122. int (*fn)(void *);
  123. void *data;
  124. struct completion done;
  125. };
  126. static int do_stop(void *_smdata)
  127. {
  128. struct stop_machine_data *smdata = _smdata;
  129. int ret;
  130. ret = stop_machine();
  131. if (ret == 0) {
  132. ret = smdata->fn(smdata->data);
  133. restart_machine();
  134. }
  135. /* We're done: you can kthread_stop us now */
  136. complete(&smdata->done);
  137. /* Wait for kthread_stop */
  138. set_current_state(TASK_INTERRUPTIBLE);
  139. while (!kthread_should_stop()) {
  140. schedule();
  141. set_current_state(TASK_INTERRUPTIBLE);
  142. }
  143. __set_current_state(TASK_RUNNING);
  144. return ret;
  145. }
  146. struct task_struct *__stop_machine_run(int (*fn)(void *), void *data,
  147. unsigned int cpu)
  148. {
  149. struct stop_machine_data smdata;
  150. struct task_struct *p;
  151. smdata.fn = fn;
  152. smdata.data = data;
  153. init_completion(&smdata.done);
  154. down(&stopmachine_mutex);
  155. /* If they don't care which CPU fn runs on, bind to any online one. */
  156. if (cpu == NR_CPUS)
  157. cpu = raw_smp_processor_id();
  158. p = kthread_create(do_stop, &smdata, "kstopmachine");
  159. if (!IS_ERR(p)) {
  160. kthread_bind(p, cpu);
  161. wake_up_process(p);
  162. wait_for_completion(&smdata.done);
  163. }
  164. up(&stopmachine_mutex);
  165. return p;
  166. }
  167. int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
  168. {
  169. struct task_struct *p;
  170. int ret;
  171. /* No CPUs can come up or down during this. */
  172. lock_cpu_hotplug();
  173. p = __stop_machine_run(fn, data, cpu);
  174. if (!IS_ERR(p))
  175. ret = kthread_stop(p);
  176. else
  177. ret = PTR_ERR(p);
  178. unlock_cpu_hotplug();
  179. return ret;
  180. }
  181. EXPORT_SYMBOL_GPL(stop_machine_run);