stop_machine.c 4.2 KB

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