stop_machine.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. static struct workqueue_struct *stop_machine_wq;
  38. static struct stop_machine_data active, idle;
  39. static const cpumask_t *active_cpus;
  40. static void *stop_machine_work;
  41. static void set_state(enum stopmachine_state newstate)
  42. {
  43. /* Reset ack counter. */
  44. atomic_set(&thread_ack, num_threads);
  45. smp_wmb();
  46. state = newstate;
  47. }
  48. /* Last one to ack a state moves to the next state. */
  49. static void ack_state(void)
  50. {
  51. if (atomic_dec_and_test(&thread_ack))
  52. set_state(state + 1);
  53. }
  54. /* This is the actual function which stops the CPU. It runs
  55. * in the context of a dedicated stopmachine workqueue. */
  56. static void stop_cpu(struct work_struct *unused)
  57. {
  58. enum stopmachine_state curstate = STOPMACHINE_NONE;
  59. struct stop_machine_data *smdata = &idle;
  60. int cpu = smp_processor_id();
  61. int err;
  62. if (!active_cpus) {
  63. if (cpu == cpumask_first(cpu_online_mask))
  64. smdata = &active;
  65. } else {
  66. if (cpumask_test_cpu(cpu, active_cpus))
  67. smdata = &active;
  68. }
  69. /* Simple state machine */
  70. do {
  71. /* Chill out and ensure we re-read stopmachine_state. */
  72. cpu_relax();
  73. if (state != curstate) {
  74. curstate = state;
  75. switch (curstate) {
  76. case STOPMACHINE_DISABLE_IRQ:
  77. local_irq_disable();
  78. hard_irq_disable();
  79. break;
  80. case STOPMACHINE_RUN:
  81. /* On multiple CPUs only a single error code
  82. * is needed to tell that something failed. */
  83. err = smdata->fn(smdata->data);
  84. if (err)
  85. smdata->fnret = err;
  86. break;
  87. default:
  88. break;
  89. }
  90. ack_state();
  91. }
  92. } while (curstate != STOPMACHINE_EXIT);
  93. local_irq_enable();
  94. }
  95. /* Callback for CPUs which aren't supposed to do anything. */
  96. static int chill(void *unused)
  97. {
  98. return 0;
  99. }
  100. int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
  101. {
  102. struct work_struct *sm_work;
  103. int i, ret;
  104. /* Set up initial state. */
  105. mutex_lock(&lock);
  106. num_threads = num_online_cpus();
  107. active_cpus = cpus;
  108. active.fn = fn;
  109. active.data = data;
  110. active.fnret = 0;
  111. idle.fn = chill;
  112. idle.data = NULL;
  113. set_state(STOPMACHINE_PREPARE);
  114. /* Schedule the stop_cpu work on all cpus: hold this CPU so one
  115. * doesn't hit this CPU until we're ready. */
  116. get_cpu();
  117. for_each_online_cpu(i) {
  118. sm_work = percpu_ptr(stop_machine_work, i);
  119. INIT_WORK(sm_work, stop_cpu);
  120. queue_work_on(i, stop_machine_wq, sm_work);
  121. }
  122. /* This will release the thread on our CPU. */
  123. put_cpu();
  124. flush_workqueue(stop_machine_wq);
  125. ret = active.fnret;
  126. mutex_unlock(&lock);
  127. return ret;
  128. }
  129. int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
  130. {
  131. int ret;
  132. /* No CPUs can come up or down during this. */
  133. get_online_cpus();
  134. ret = __stop_machine(fn, data, cpus);
  135. put_online_cpus();
  136. return ret;
  137. }
  138. EXPORT_SYMBOL_GPL(stop_machine);
  139. static int __init stop_machine_init(void)
  140. {
  141. stop_machine_wq = create_rt_workqueue("kstop");
  142. stop_machine_work = alloc_percpu(struct work_struct);
  143. return 0;
  144. }
  145. core_initcall(stop_machine_init);