stop_machine.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. unsigned long caller;
  24. void *arg;
  25. struct cpu_stop_done *done;
  26. };
  27. int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg);
  28. int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg);
  29. bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
  30. struct cpu_stop_work *work_buf);
  31. void stop_machine_park(int cpu);
  32. void stop_machine_unpark(int cpu);
  33. void stop_machine_yield(const struct cpumask *cpumask);
  34. extern void print_stop_info(const char *log_lvl, struct task_struct *task);
  35. #else /* CONFIG_SMP */
  36. #include <linux/workqueue.h>
  37. struct cpu_stop_work {
  38. struct work_struct work;
  39. cpu_stop_fn_t fn;
  40. void *arg;
  41. };
  42. static inline int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
  43. {
  44. int ret = -ENOENT;
  45. preempt_disable();
  46. if (cpu == smp_processor_id())
  47. ret = fn(arg);
  48. preempt_enable();
  49. return ret;
  50. }
  51. static void stop_one_cpu_nowait_workfn(struct work_struct *work)
  52. {
  53. struct cpu_stop_work *stwork =
  54. container_of(work, struct cpu_stop_work, work);
  55. preempt_disable();
  56. stwork->fn(stwork->arg);
  57. preempt_enable();
  58. }
  59. static inline bool stop_one_cpu_nowait(unsigned int cpu,
  60. cpu_stop_fn_t fn, void *arg,
  61. struct cpu_stop_work *work_buf)
  62. {
  63. if (cpu == smp_processor_id()) {
  64. INIT_WORK(&work_buf->work, stop_one_cpu_nowait_workfn);
  65. work_buf->fn = fn;
  66. work_buf->arg = arg;
  67. schedule_work(&work_buf->work);
  68. return true;
  69. }
  70. return false;
  71. }
  72. static inline void print_stop_info(const char *log_lvl, struct task_struct *task) { }
  73. #endif /* CONFIG_SMP */
  74. /*
  75. * stop_machine "Bogolock": stop the entire machine, disable
  76. * interrupts. This is a very heavy lock, which is equivalent to
  77. * grabbing every spinlock (and more). So the "read" side to such a
  78. * lock is anything which disables preemption.
  79. */
  80. #if defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
  81. /**
  82. * stop_machine: freeze the machine on all CPUs and run this function
  83. * @fn: the function to run
  84. * @data: the data ptr for the @fn()
  85. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  86. *
  87. * Description: This causes a thread to be scheduled on every cpu,
  88. * each of which disables interrupts. The result is that no one is
  89. * holding a spinlock or inside any other preempt-disabled region when
  90. * @fn() runs.
  91. *
  92. * This can be thought of as a very heavy write lock, equivalent to
  93. * grabbing every spinlock in the kernel.
  94. *
  95. * Protects against CPU hotplug.
  96. */
  97. int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
  98. /**
  99. * stop_machine_cpuslocked: freeze the machine on all CPUs and run this function
  100. * @fn: the function to run
  101. * @data: the data ptr for the @fn()
  102. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  103. *
  104. * Same as above. Must be called from with in a cpus_read_lock() protected
  105. * region. Avoids nested calls to cpus_read_lock().
  106. */
  107. int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
  108. /**
  109. * stop_core_cpuslocked: - stop all threads on just one core
  110. * @cpu: any cpu in the targeted core
  111. * @fn: the function to run
  112. * @data: the data ptr for @fn()
  113. *
  114. * Same as above, but instead of every CPU, only the logical CPUs of a
  115. * single core are affected.
  116. *
  117. * Context: Must be called from within a cpus_read_lock() protected region.
  118. *
  119. * Return: 0 if all executions of @fn returned 0, any non zero return
  120. * value if any returned non zero.
  121. */
  122. int stop_core_cpuslocked(unsigned int cpu, cpu_stop_fn_t fn, void *data);
  123. int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  124. const struct cpumask *cpus);
  125. #else /* CONFIG_SMP || CONFIG_HOTPLUG_CPU */
  126. static __always_inline int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data,
  127. const struct cpumask *cpus)
  128. {
  129. unsigned long flags;
  130. int ret;
  131. local_irq_save(flags);
  132. ret = fn(data);
  133. local_irq_restore(flags);
  134. return ret;
  135. }
  136. static __always_inline int
  137. stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
  138. {
  139. return stop_machine_cpuslocked(fn, data, cpus);
  140. }
  141. static __always_inline int
  142. stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  143. const struct cpumask *cpus)
  144. {
  145. return stop_machine(fn, data, cpus);
  146. }
  147. #endif /* CONFIG_SMP || CONFIG_HOTPLUG_CPU */
  148. #endif /* _LINUX_STOP_MACHINE */