stop_machine.h 4.1 KB

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