stop_machine.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. else
  58. cpu_relax();
  59. }
  60. /* Ack: we are exiting. */
  61. smp_mb(); /* Must read state first. */
  62. atomic_inc(&stopmachine_thread_ack);
  63. if (irqs_disabled)
  64. local_irq_enable();
  65. if (prepared)
  66. preempt_enable();
  67. return 0;
  68. }
  69. /* Change the thread state */
  70. static void stopmachine_set_state(enum stopmachine_state state)
  71. {
  72. atomic_set(&stopmachine_thread_ack, 0);
  73. smp_wmb();
  74. stopmachine_state = state;
  75. while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
  76. cpu_relax();
  77. }
  78. static int stop_machine(void)
  79. {
  80. int i, ret = 0;
  81. atomic_set(&stopmachine_thread_ack, 0);
  82. stopmachine_num_threads = 0;
  83. stopmachine_state = STOPMACHINE_WAIT;
  84. for_each_online_cpu(i) {
  85. if (i == raw_smp_processor_id())
  86. continue;
  87. ret = kernel_thread(stopmachine, (void *)(long)i,CLONE_KERNEL);
  88. if (ret < 0)
  89. break;
  90. stopmachine_num_threads++;
  91. }
  92. /* Wait for them all to come to life. */
  93. while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
  94. yield();
  95. /* If some failed, kill them all. */
  96. if (ret < 0) {
  97. stopmachine_set_state(STOPMACHINE_EXIT);
  98. return ret;
  99. }
  100. /* Now they are all started, make them hold the CPUs, ready. */
  101. preempt_disable();
  102. stopmachine_set_state(STOPMACHINE_PREPARE);
  103. /* Make them disable irqs. */
  104. local_irq_disable();
  105. hard_irq_disable();
  106. stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);
  107. return 0;
  108. }
  109. static void restart_machine(void)
  110. {
  111. stopmachine_set_state(STOPMACHINE_EXIT);
  112. local_irq_enable();
  113. preempt_enable_no_resched();
  114. }
  115. struct stop_machine_data {
  116. int (*fn)(void *);
  117. void *data;
  118. struct completion done;
  119. };
  120. static int do_stop(void *_smdata)
  121. {
  122. struct stop_machine_data *smdata = _smdata;
  123. int ret;
  124. ret = stop_machine();
  125. if (ret == 0) {
  126. ret = smdata->fn(smdata->data);
  127. restart_machine();
  128. }
  129. /* We're done: you can kthread_stop us now */
  130. complete(&smdata->done);
  131. /* Wait for kthread_stop */
  132. set_current_state(TASK_INTERRUPTIBLE);
  133. while (!kthread_should_stop()) {
  134. schedule();
  135. set_current_state(TASK_INTERRUPTIBLE);
  136. }
  137. __set_current_state(TASK_RUNNING);
  138. return ret;
  139. }
  140. struct task_struct *__stop_machine_run(int (*fn)(void *), void *data,
  141. unsigned int cpu)
  142. {
  143. static DEFINE_MUTEX(stopmachine_mutex);
  144. struct stop_machine_data smdata;
  145. struct task_struct *p;
  146. smdata.fn = fn;
  147. smdata.data = data;
  148. init_completion(&smdata.done);
  149. mutex_lock(&stopmachine_mutex);
  150. /* If they don't care which CPU fn runs on, bind to any online one. */
  151. if (cpu == NR_CPUS)
  152. cpu = raw_smp_processor_id();
  153. p = kthread_create(do_stop, &smdata, "kstopmachine");
  154. if (!IS_ERR(p)) {
  155. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  156. /* One high-prio thread per cpu. We'll do this one. */
  157. sched_setscheduler(p, SCHED_FIFO, &param);
  158. kthread_bind(p, cpu);
  159. wake_up_process(p);
  160. wait_for_completion(&smdata.done);
  161. }
  162. mutex_unlock(&stopmachine_mutex);
  163. return p;
  164. }
  165. int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
  166. {
  167. struct task_struct *p;
  168. int ret;
  169. /* No CPUs can come up or down during this. */
  170. get_online_cpus();
  171. p = __stop_machine_run(fn, data, cpu);
  172. if (!IS_ERR(p))
  173. ret = kthread_stop(p);
  174. else
  175. ret = PTR_ERR(p);
  176. put_online_cpus();
  177. return ret;
  178. }
  179. EXPORT_SYMBOL_GPL(stop_machine_run);