stop_machine.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_STOP_MACHINE
  3. #define _LINUX_STOP_MACHINE
  4. #include <linux/cpu.h>
  5. #include <linux/cpumask.h>
  6. #include <linux/smp.h>
  7. #include <linux/list.h>
  8. /*
  9. * stop_cpu[s]() is simplistic per-cpu maximum priority cpu
  10. * monopolization mechanism. The caller can specify a non-sleeping
  11. * function to be executed on a single or multiple cpus preempting all
  12. * other processes and monopolizing those cpus until it finishes.
  13. *
  14. * Resources for this mechanism are preallocated when a cpu is brought
  15. * up and requests are guaranteed to be served as long as the target
  16. * cpus are online.
  17. */
  18. typedef int (*cpu_stop_fn_t)(void *arg);
  19. #ifdef CONFIG_SMP
  20. struct cpu_stop_work {
  21. struct list_head list; /* cpu_stopper->works */
  22. cpu_stop_fn_t fn;
  23. void *arg;
  24. struct cpu_stop_done *done;
  25. };
  26. int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg);
  27. int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg);
  28. bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
  29. struct cpu_stop_work *work_buf);
  30. int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg);
  31. int try_stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg);
  32. void stop_machine_park(int cpu);
  33. void stop_machine_unpark(int cpu);
  34. #else /* CONFIG_SMP */
  35. #include <linux/workqueue.h>
  36. struct cpu_stop_work {
  37. struct work_struct work;
  38. cpu_stop_fn_t fn;
  39. void *arg;
  40. };
  41. static inline int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
  42. {
  43. int ret = -ENOENT;
  44. preempt_disable();
  45. if (cpu == smp_processor_id())
  46. ret = fn(arg);
  47. preempt_enable();
  48. return ret;
  49. }
  50. static void stop_one_cpu_nowait_workfn(struct work_struct *work)
  51. {
  52. struct cpu_stop_work *stwork =
  53. container_of(work, struct cpu_stop_work, work);
  54. preempt_disable();
  55. stwork->fn(stwork->arg);
  56. preempt_enable();
  57. }
  58. static inline bool stop_one_cpu_nowait(unsigned int cpu,
  59. cpu_stop_fn_t fn, void *arg,
  60. struct cpu_stop_work *work_buf)
  61. {
  62. if (cpu == smp_processor_id()) {
  63. INIT_WORK(&work_buf->work, stop_one_cpu_nowait_workfn);
  64. work_buf->fn = fn;
  65. work_buf->arg = arg;
  66. schedule_work(&work_buf->work);
  67. return true;
  68. }
  69. return false;
  70. }
  71. static inline int stop_cpus(const struct cpumask *cpumask,
  72. cpu_stop_fn_t fn, void *arg)
  73. {
  74. if (cpumask_test_cpu(raw_smp_processor_id(), cpumask))
  75. return stop_one_cpu(raw_smp_processor_id(), fn, arg);
  76. return -ENOENT;
  77. }
  78. static inline int try_stop_cpus(const struct cpumask *cpumask,
  79. cpu_stop_fn_t fn, void *arg)
  80. {
  81. return stop_cpus(cpumask, fn, arg);
  82. }
  83. #endif /* CONFIG_SMP */
  84. /*
  85. * stop_machine "Bogolock": stop the entire machine, disable
  86. * interrupts. This is a very heavy lock, which is equivalent to
  87. * grabbing every spinlock (and more). So the "read" side to such a
  88. * lock is anything which disables preemption.
  89. */
  90. #if defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
  91. /**
  92. * stop_machine: freeze the machine on all CPUs and run this function
  93. * @fn: the function to run
  94. * @data: the data ptr for the @fn()
  95. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  96. *
  97. * Description: This causes a thread to be scheduled on every cpu,
  98. * each of which disables interrupts. The result is that no one is
  99. * holding a spinlock or inside any other preempt-disabled region when
  100. * @fn() runs.
  101. *
  102. * This can be thought of as a very heavy write lock, equivalent to
  103. * grabbing every spinlock in the kernel.
  104. *
  105. * Protects against CPU hotplug.
  106. */
  107. int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
  108. /**
  109. * stop_machine_cpuslocked: freeze the machine on all CPUs and run this function
  110. * @fn: the function to run
  111. * @data: the data ptr for the @fn()
  112. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  113. *
  114. * Same as above. Must be called from with in a cpus_read_lock() protected
  115. * region. Avoids nested calls to cpus_read_lock().
  116. */
  117. int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
  118. int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  119. const struct cpumask *cpus);
  120. #else /* CONFIG_SMP || CONFIG_HOTPLUG_CPU */
  121. static inline int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data,
  122. const struct cpumask *cpus)
  123. {
  124. unsigned long flags;
  125. int ret;
  126. local_irq_save(flags);
  127. ret = fn(data);
  128. local_irq_restore(flags);
  129. return ret;
  130. }
  131. static inline int stop_machine(cpu_stop_fn_t fn, void *data,
  132. const struct cpumask *cpus)
  133. {
  134. return stop_machine_cpuslocked(fn, data, cpus);
  135. }
  136. static inline int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  137. const struct cpumask *cpus)
  138. {
  139. return stop_machine(fn, data, cpus);
  140. }
  141. #endif /* CONFIG_SMP || CONFIG_HOTPLUG_CPU */
  142. #endif /* _LINUX_STOP_MACHINE */