stop_machine.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef _LINUX_STOP_MACHINE
  2. #define _LINUX_STOP_MACHINE
  3. #include <linux/cpu.h>
  4. #include <linux/cpumask.h>
  5. #include <linux/smp.h>
  6. #include <linux/list.h>
  7. #include <asm/system.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. void stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
  28. struct cpu_stop_work *work_buf);
  29. int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg);
  30. int try_stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg);
  31. #else /* CONFIG_SMP */
  32. #include <linux/workqueue.h>
  33. struct cpu_stop_work {
  34. struct work_struct work;
  35. cpu_stop_fn_t fn;
  36. void *arg;
  37. };
  38. static inline int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
  39. {
  40. int ret = -ENOENT;
  41. preempt_disable();
  42. if (cpu == smp_processor_id())
  43. ret = fn(arg);
  44. preempt_enable();
  45. return ret;
  46. }
  47. static void stop_one_cpu_nowait_workfn(struct work_struct *work)
  48. {
  49. struct cpu_stop_work *stwork =
  50. container_of(work, struct cpu_stop_work, work);
  51. preempt_disable();
  52. stwork->fn(stwork->arg);
  53. preempt_enable();
  54. }
  55. static inline void stop_one_cpu_nowait(unsigned int cpu,
  56. cpu_stop_fn_t fn, void *arg,
  57. struct cpu_stop_work *work_buf)
  58. {
  59. if (cpu == smp_processor_id()) {
  60. INIT_WORK(&work_buf->work, stop_one_cpu_nowait_workfn);
  61. work_buf->fn = fn;
  62. work_buf->arg = arg;
  63. schedule_work(&work_buf->work);
  64. }
  65. }
  66. static inline int stop_cpus(const struct cpumask *cpumask,
  67. cpu_stop_fn_t fn, void *arg)
  68. {
  69. if (cpumask_test_cpu(raw_smp_processor_id(), cpumask))
  70. return stop_one_cpu(raw_smp_processor_id(), fn, arg);
  71. return -ENOENT;
  72. }
  73. static inline int try_stop_cpus(const struct cpumask *cpumask,
  74. cpu_stop_fn_t fn, void *arg)
  75. {
  76. return stop_cpus(cpumask, fn, arg);
  77. }
  78. #endif /* CONFIG_SMP */
  79. /*
  80. * stop_machine "Bogolock": stop the entire machine, disable
  81. * interrupts. This is a very heavy lock, which is equivalent to
  82. * grabbing every spinlock (and more). So the "read" side to such a
  83. * lock is anything which disables preemption.
  84. */
  85. #if defined(CONFIG_STOP_MACHINE) && defined(CONFIG_SMP)
  86. /**
  87. * stop_machine: freeze the machine on all CPUs and run this function
  88. * @fn: the function to run
  89. * @data: the data ptr for the @fn()
  90. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  91. *
  92. * Description: This causes a thread to be scheduled on every cpu,
  93. * each of which disables interrupts. The result is that no one is
  94. * holding a spinlock or inside any other preempt-disabled region when
  95. * @fn() runs.
  96. *
  97. * This can be thought of as a very heavy write lock, equivalent to
  98. * grabbing every spinlock in the kernel. */
  99. int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus);
  100. /**
  101. * __stop_machine: freeze the machine on all CPUs and run this function
  102. * @fn: the function to run
  103. * @data: the data ptr for the @fn
  104. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  105. *
  106. * Description: This is a special version of the above, which assumes cpus
  107. * won't come or go while it's being called. Used by hotplug cpu.
  108. */
  109. int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus);
  110. int stop_machine_from_inactive_cpu(int (*fn)(void *), void *data,
  111. const struct cpumask *cpus);
  112. #else /* CONFIG_STOP_MACHINE && CONFIG_SMP */
  113. static inline int __stop_machine(int (*fn)(void *), void *data,
  114. const struct cpumask *cpus)
  115. {
  116. unsigned long flags;
  117. int ret;
  118. local_irq_save(flags);
  119. ret = fn(data);
  120. local_irq_restore(flags);
  121. return ret;
  122. }
  123. static inline int stop_machine(int (*fn)(void *), void *data,
  124. const struct cpumask *cpus)
  125. {
  126. return __stop_machine(fn, data, cpus);
  127. }
  128. static inline int stop_machine_from_inactive_cpu(int (*fn)(void *), void *data,
  129. const struct cpumask *cpus)
  130. {
  131. return __stop_machine(fn, data, cpus);
  132. }
  133. #endif /* CONFIG_STOP_MACHINE && CONFIG_SMP */
  134. #endif /* _LINUX_STOP_MACHINE */