stop_machine.c 5.0 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/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. cpumask_of_cpu_ptr(cpumask, (int)(long)cpu);
  32. set_cpus_allowed_ptr(current, cpumask);
  33. /* Ack: we are alive */
  34. smp_mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */
  35. atomic_inc(&stopmachine_thread_ack);
  36. /* Simple state machine */
  37. while (stopmachine_state != STOPMACHINE_EXIT) {
  38. if (stopmachine_state == STOPMACHINE_DISABLE_IRQ
  39. && !irqs_disabled) {
  40. local_irq_disable();
  41. hard_irq_disable();
  42. irqs_disabled = 1;
  43. /* Ack: irqs disabled. */
  44. smp_mb(); /* Must read state first. */
  45. atomic_inc(&stopmachine_thread_ack);
  46. } else if (stopmachine_state == STOPMACHINE_PREPARE
  47. && !prepared) {
  48. /* Everyone is in place, hold CPU. */
  49. preempt_disable();
  50. prepared = 1;
  51. smp_mb(); /* Must read state first. */
  52. atomic_inc(&stopmachine_thread_ack);
  53. }
  54. /* Yield in first stage: migration threads need to
  55. * help our sisters onto their CPUs. */
  56. if (!prepared && !irqs_disabled)
  57. yield();
  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. cpu_relax();
  96. }
  97. /* If some failed, kill them all. */
  98. if (ret < 0) {
  99. stopmachine_set_state(STOPMACHINE_EXIT);
  100. return ret;
  101. }
  102. /* Now they are all started, make them hold the CPUs, ready. */
  103. preempt_disable();
  104. stopmachine_set_state(STOPMACHINE_PREPARE);
  105. /* Make them disable irqs. */
  106. local_irq_disable();
  107. hard_irq_disable();
  108. stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);
  109. return 0;
  110. }
  111. static void restart_machine(void)
  112. {
  113. stopmachine_set_state(STOPMACHINE_EXIT);
  114. local_irq_enable();
  115. preempt_enable_no_resched();
  116. }
  117. struct stop_machine_data {
  118. int (*fn)(void *);
  119. void *data;
  120. struct completion done;
  121. };
  122. static int do_stop(void *_smdata)
  123. {
  124. struct stop_machine_data *smdata = _smdata;
  125. int ret;
  126. ret = stop_machine();
  127. if (ret == 0) {
  128. ret = smdata->fn(smdata->data);
  129. restart_machine();
  130. }
  131. /* We're done: you can kthread_stop us now */
  132. complete(&smdata->done);
  133. /* Wait for kthread_stop */
  134. set_current_state(TASK_INTERRUPTIBLE);
  135. while (!kthread_should_stop()) {
  136. schedule();
  137. set_current_state(TASK_INTERRUPTIBLE);
  138. }
  139. __set_current_state(TASK_RUNNING);
  140. return ret;
  141. }
  142. struct task_struct *__stop_machine_run(int (*fn)(void *), void *data,
  143. unsigned int cpu)
  144. {
  145. static DEFINE_MUTEX(stopmachine_mutex);
  146. struct stop_machine_data smdata;
  147. struct task_struct *p;
  148. smdata.fn = fn;
  149. smdata.data = data;
  150. init_completion(&smdata.done);
  151. mutex_lock(&stopmachine_mutex);
  152. /* If they don't care which CPU fn runs on, bind to any online one. */
  153. if (cpu == NR_CPUS)
  154. cpu = raw_smp_processor_id();
  155. p = kthread_create(do_stop, &smdata, "kstopmachine");
  156. if (!IS_ERR(p)) {
  157. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  158. /* One high-prio thread per cpu. We'll do this one. */
  159. sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
  160. kthread_bind(p, cpu);
  161. wake_up_process(p);
  162. wait_for_completion(&smdata.done);
  163. }
  164. mutex_unlock(&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. get_online_cpus();
  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. put_online_cpus();
  179. return ret;
  180. }
  181. EXPORT_SYMBOL_GPL(stop_machine_run);