stop_machine.c 4.8 KB

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