stop_machine.c 4.9 KB

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