stop_machine.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Copyright 2008, 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. /* This controls the threads on each CPU. */
  15. enum stopmachine_state {
  16. /* Dummy starting state for thread. */
  17. STOPMACHINE_NONE,
  18. /* Awaiting everyone to be scheduled. */
  19. STOPMACHINE_PREPARE,
  20. /* Disable interrupts. */
  21. STOPMACHINE_DISABLE_IRQ,
  22. /* Run the function */
  23. STOPMACHINE_RUN,
  24. /* Exit */
  25. STOPMACHINE_EXIT,
  26. };
  27. static enum stopmachine_state state;
  28. struct stop_machine_data {
  29. int (*fn)(void *);
  30. void *data;
  31. int fnret;
  32. };
  33. /* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
  34. static unsigned int num_threads;
  35. static atomic_t thread_ack;
  36. static DEFINE_MUTEX(lock);
  37. /* setup_lock protects refcount, stop_machine_wq and stop_machine_work. */
  38. static DEFINE_MUTEX(setup_lock);
  39. /* Users of stop_machine. */
  40. static int refcount;
  41. static struct workqueue_struct *stop_machine_wq;
  42. static struct stop_machine_data active, idle;
  43. static const struct cpumask *active_cpus;
  44. static void *stop_machine_work;
  45. static void set_state(enum stopmachine_state newstate)
  46. {
  47. /* Reset ack counter. */
  48. atomic_set(&thread_ack, num_threads);
  49. smp_wmb();
  50. state = newstate;
  51. }
  52. /* Last one to ack a state moves to the next state. */
  53. static void ack_state(void)
  54. {
  55. if (atomic_dec_and_test(&thread_ack))
  56. set_state(state + 1);
  57. }
  58. /* This is the actual function which stops the CPU. It runs
  59. * in the context of a dedicated stopmachine workqueue. */
  60. static void stop_cpu(struct work_struct *unused)
  61. {
  62. enum stopmachine_state curstate = STOPMACHINE_NONE;
  63. struct stop_machine_data *smdata = &idle;
  64. int cpu = smp_processor_id();
  65. int err;
  66. if (!active_cpus) {
  67. if (cpu == cpumask_first(cpu_online_mask))
  68. smdata = &active;
  69. } else {
  70. if (cpumask_test_cpu(cpu, active_cpus))
  71. smdata = &active;
  72. }
  73. /* Simple state machine */
  74. do {
  75. /* Chill out and ensure we re-read stopmachine_state. */
  76. cpu_relax();
  77. if (state != curstate) {
  78. curstate = state;
  79. switch (curstate) {
  80. case STOPMACHINE_DISABLE_IRQ:
  81. local_irq_disable();
  82. hard_irq_disable();
  83. break;
  84. case STOPMACHINE_RUN:
  85. /* On multiple CPUs only a single error code
  86. * is needed to tell that something failed. */
  87. err = smdata->fn(smdata->data);
  88. if (err)
  89. smdata->fnret = err;
  90. break;
  91. default:
  92. break;
  93. }
  94. ack_state();
  95. }
  96. } while (curstate != STOPMACHINE_EXIT);
  97. local_irq_enable();
  98. }
  99. /* Callback for CPUs which aren't supposed to do anything. */
  100. static int chill(void *unused)
  101. {
  102. return 0;
  103. }
  104. int stop_machine_create(void)
  105. {
  106. mutex_lock(&setup_lock);
  107. if (refcount)
  108. goto done;
  109. stop_machine_wq = create_rt_workqueue("kstop");
  110. if (!stop_machine_wq)
  111. goto err_out;
  112. stop_machine_work = alloc_percpu(struct work_struct);
  113. if (!stop_machine_work)
  114. goto err_out;
  115. done:
  116. refcount++;
  117. mutex_unlock(&setup_lock);
  118. return 0;
  119. err_out:
  120. if (stop_machine_wq)
  121. destroy_workqueue(stop_machine_wq);
  122. mutex_unlock(&setup_lock);
  123. return -ENOMEM;
  124. }
  125. EXPORT_SYMBOL_GPL(stop_machine_create);
  126. void stop_machine_destroy(void)
  127. {
  128. mutex_lock(&setup_lock);
  129. refcount--;
  130. if (refcount)
  131. goto done;
  132. destroy_workqueue(stop_machine_wq);
  133. free_percpu(stop_machine_work);
  134. done:
  135. mutex_unlock(&setup_lock);
  136. }
  137. EXPORT_SYMBOL_GPL(stop_machine_destroy);
  138. int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
  139. {
  140. struct work_struct *sm_work;
  141. int i, ret;
  142. /* Set up initial state. */
  143. mutex_lock(&lock);
  144. num_threads = num_online_cpus();
  145. active_cpus = cpus;
  146. active.fn = fn;
  147. active.data = data;
  148. active.fnret = 0;
  149. idle.fn = chill;
  150. idle.data = NULL;
  151. set_state(STOPMACHINE_PREPARE);
  152. /* Schedule the stop_cpu work on all cpus: hold this CPU so one
  153. * doesn't hit this CPU until we're ready. */
  154. get_cpu();
  155. for_each_online_cpu(i) {
  156. sm_work = per_cpu_ptr(stop_machine_work, i);
  157. INIT_WORK(sm_work, stop_cpu);
  158. queue_work_on(i, stop_machine_wq, sm_work);
  159. }
  160. /* This will release the thread on our CPU. */
  161. put_cpu();
  162. flush_workqueue(stop_machine_wq);
  163. ret = active.fnret;
  164. mutex_unlock(&lock);
  165. return ret;
  166. }
  167. int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
  168. {
  169. int ret;
  170. ret = stop_machine_create();
  171. if (ret)
  172. return ret;
  173. /* No CPUs can come up or down during this. */
  174. get_online_cpus();
  175. ret = __stop_machine(fn, data, cpus);
  176. put_online_cpus();
  177. stop_machine_destroy();
  178. return ret;
  179. }
  180. EXPORT_SYMBOL_GPL(stop_machine);