stop_machine.h 4.0 KB

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