stop_machine.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 struct completion finished;
  37. static DEFINE_MUTEX(lock);
  38. static void set_state(enum stopmachine_state newstate)
  39. {
  40. /* Reset ack counter. */
  41. atomic_set(&thread_ack, num_threads);
  42. smp_wmb();
  43. state = newstate;
  44. }
  45. /* Last one to ack a state moves to the next state. */
  46. static void ack_state(void)
  47. {
  48. if (atomic_dec_and_test(&thread_ack)) {
  49. /* If we're the last one to ack the EXIT, we're finished. */
  50. if (state == STOPMACHINE_EXIT)
  51. complete(&finished);
  52. else
  53. set_state(state + 1);
  54. }
  55. }
  56. /* This is the actual thread which stops the CPU. It exits by itself rather
  57. * than waiting for kthread_stop(), because it's easier for hotplug CPU. */
  58. static int stop_cpu(struct stop_machine_data *smdata)
  59. {
  60. enum stopmachine_state curstate = STOPMACHINE_NONE;
  61. /* Simple state machine */
  62. do {
  63. /* Chill out and ensure we re-read stopmachine_state. */
  64. cpu_relax();
  65. if (state != curstate) {
  66. curstate = state;
  67. switch (curstate) {
  68. case STOPMACHINE_DISABLE_IRQ:
  69. local_irq_disable();
  70. hard_irq_disable();
  71. break;
  72. case STOPMACHINE_RUN:
  73. /* |= allows error detection if functions on
  74. * multiple CPUs. */
  75. smdata->fnret |= smdata->fn(smdata->data);
  76. break;
  77. default:
  78. break;
  79. }
  80. ack_state();
  81. }
  82. } while (curstate != STOPMACHINE_EXIT);
  83. local_irq_enable();
  84. do_exit(0);
  85. }
  86. /* Callback for CPUs which aren't supposed to do anything. */
  87. static int chill(void *unused)
  88. {
  89. return 0;
  90. }
  91. int __stop_machine(int (*fn)(void *), void *data, const cpumask_t *cpus)
  92. {
  93. int i, err;
  94. struct stop_machine_data active, idle;
  95. struct task_struct **threads;
  96. active.fn = fn;
  97. active.data = data;
  98. active.fnret = 0;
  99. idle.fn = chill;
  100. idle.data = NULL;
  101. /* This could be too big for stack on large machines. */
  102. threads = kcalloc(NR_CPUS, sizeof(threads[0]), GFP_KERNEL);
  103. if (!threads)
  104. return -ENOMEM;
  105. /* Set up initial state. */
  106. mutex_lock(&lock);
  107. init_completion(&finished);
  108. num_threads = num_online_cpus();
  109. set_state(STOPMACHINE_PREPARE);
  110. for_each_online_cpu(i) {
  111. struct stop_machine_data *smdata = &idle;
  112. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  113. if (!cpus) {
  114. if (i == first_cpu(cpu_online_map))
  115. smdata = &active;
  116. } else {
  117. if (cpu_isset(i, *cpus))
  118. smdata = &active;
  119. }
  120. threads[i] = kthread_create((void *)stop_cpu, smdata, "kstop%u",
  121. i);
  122. if (IS_ERR(threads[i])) {
  123. err = PTR_ERR(threads[i]);
  124. threads[i] = NULL;
  125. goto kill_threads;
  126. }
  127. /* Place it onto correct cpu. */
  128. kthread_bind(threads[i], i);
  129. /* Make it highest prio. */
  130. if (sched_setscheduler_nocheck(threads[i], SCHED_FIFO, &param))
  131. BUG();
  132. }
  133. /* We've created all the threads. Wake them all: hold this CPU so one
  134. * doesn't hit this CPU until we're ready. */
  135. get_cpu();
  136. for_each_online_cpu(i)
  137. wake_up_process(threads[i]);
  138. /* This will release the thread on our CPU. */
  139. put_cpu();
  140. wait_for_completion(&finished);
  141. mutex_unlock(&lock);
  142. kfree(threads);
  143. return active.fnret;
  144. kill_threads:
  145. for_each_online_cpu(i)
  146. if (threads[i])
  147. kthread_stop(threads[i]);
  148. mutex_unlock(&lock);
  149. kfree(threads);
  150. return err;
  151. }
  152. int stop_machine(int (*fn)(void *), void *data, const cpumask_t *cpus)
  153. {
  154. int ret;
  155. /* No CPUs can come up or down during this. */
  156. get_online_cpus();
  157. ret = __stop_machine(fn, data, cpus);
  158. put_online_cpus();
  159. return ret;
  160. }
  161. EXPORT_SYMBOL_GPL(stop_machine);