irq_work.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra
  3. *
  4. * Provides a framework for enqueueing and running callbacks from hardirq
  5. * context. The enqueueing is NMI-safe.
  6. */
  7. #include <linux/bug.h>
  8. #include <linux/kernel.h>
  9. #include <linux/export.h>
  10. #include <linux/irq_work.h>
  11. #include <linux/percpu.h>
  12. #include <linux/hardirq.h>
  13. #include <linux/irqflags.h>
  14. #include <linux/sched.h>
  15. #include <linux/tick.h>
  16. #include <linux/cpu.h>
  17. #include <linux/notifier.h>
  18. #include <linux/smp.h>
  19. #include <asm/processor.h>
  20. static DEFINE_PER_CPU(struct llist_head, raised_list);
  21. static DEFINE_PER_CPU(struct llist_head, lazy_list);
  22. /*
  23. * Claim the entry so that no one else will poke at it.
  24. */
  25. static bool irq_work_claim(struct irq_work *work)
  26. {
  27. unsigned long flags, oflags, nflags;
  28. /*
  29. * Start with our best wish as a premise but only trust any
  30. * flag value after cmpxchg() result.
  31. */
  32. flags = work->flags & ~IRQ_WORK_PENDING;
  33. for (;;) {
  34. nflags = flags | IRQ_WORK_CLAIMED;
  35. oflags = cmpxchg(&work->flags, flags, nflags);
  36. if (oflags == flags)
  37. break;
  38. if (oflags & IRQ_WORK_PENDING)
  39. return false;
  40. flags = oflags;
  41. cpu_relax();
  42. }
  43. return true;
  44. }
  45. void __weak arch_irq_work_raise(void)
  46. {
  47. /*
  48. * Lame architectures will get the timer tick callback
  49. */
  50. }
  51. /*
  52. * Enqueue the irq_work @work on @cpu unless it's already pending
  53. * somewhere.
  54. *
  55. * Can be re-enqueued while the callback is still in progress.
  56. */
  57. bool irq_work_queue_on(struct irq_work *work, int cpu)
  58. {
  59. /* All work should have been flushed before going offline */
  60. WARN_ON_ONCE(cpu_is_offline(cpu));
  61. #ifdef CONFIG_SMP
  62. /* Arch remote IPI send/receive backend aren't NMI safe */
  63. WARN_ON_ONCE(in_nmi());
  64. /* Only queue if not already pending */
  65. if (!irq_work_claim(work))
  66. return false;
  67. if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  68. arch_send_call_function_single_ipi(cpu);
  69. #else /* #ifdef CONFIG_SMP */
  70. irq_work_queue(work);
  71. #endif /* #else #ifdef CONFIG_SMP */
  72. return true;
  73. }
  74. /* Enqueue the irq work @work on the current CPU */
  75. bool irq_work_queue(struct irq_work *work)
  76. {
  77. /* Only queue if not already pending */
  78. if (!irq_work_claim(work))
  79. return false;
  80. /* Queue the entry and raise the IPI if needed. */
  81. preempt_disable();
  82. /* If the work is "lazy", handle it from next tick if any */
  83. if (work->flags & IRQ_WORK_LAZY) {
  84. if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  85. tick_nohz_tick_stopped())
  86. arch_irq_work_raise();
  87. } else {
  88. if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  89. arch_irq_work_raise();
  90. }
  91. preempt_enable();
  92. return true;
  93. }
  94. EXPORT_SYMBOL_GPL(irq_work_queue);
  95. bool irq_work_needs_cpu(void)
  96. {
  97. struct llist_head *raised, *lazy;
  98. raised = this_cpu_ptr(&raised_list);
  99. lazy = this_cpu_ptr(&lazy_list);
  100. if (llist_empty(raised) || arch_irq_work_has_interrupt())
  101. if (llist_empty(lazy))
  102. return false;
  103. /* All work should have been flushed before going offline */
  104. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  105. return true;
  106. }
  107. static void irq_work_run_list(struct llist_head *list)
  108. {
  109. struct irq_work *work, *tmp;
  110. struct llist_node *llnode;
  111. unsigned long flags;
  112. BUG_ON(!irqs_disabled());
  113. if (llist_empty(list))
  114. return;
  115. llnode = llist_del_all(list);
  116. llist_for_each_entry_safe(work, tmp, llnode, llnode) {
  117. /*
  118. * Clear the PENDING bit, after this point the @work
  119. * can be re-used.
  120. * Make it immediately visible so that other CPUs trying
  121. * to claim that work don't rely on us to handle their data
  122. * while we are in the middle of the func.
  123. */
  124. flags = work->flags & ~IRQ_WORK_PENDING;
  125. xchg(&work->flags, flags);
  126. work->func(work);
  127. /*
  128. * Clear the BUSY bit and return to the free state if
  129. * no-one else claimed it meanwhile.
  130. */
  131. (void)cmpxchg(&work->flags, flags, flags & ~IRQ_WORK_BUSY);
  132. }
  133. }
  134. /*
  135. * hotplug calls this through:
  136. * hotplug_cfd() -> flush_smp_call_function_queue()
  137. */
  138. void irq_work_run(void)
  139. {
  140. irq_work_run_list(this_cpu_ptr(&raised_list));
  141. irq_work_run_list(this_cpu_ptr(&lazy_list));
  142. }
  143. EXPORT_SYMBOL_GPL(irq_work_run);
  144. void irq_work_tick(void)
  145. {
  146. struct llist_head *raised = this_cpu_ptr(&raised_list);
  147. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  148. irq_work_run_list(raised);
  149. irq_work_run_list(this_cpu_ptr(&lazy_list));
  150. }
  151. /*
  152. * Synchronize against the irq_work @entry, ensures the entry is not
  153. * currently in use.
  154. */
  155. void irq_work_sync(struct irq_work *work)
  156. {
  157. lockdep_assert_irqs_enabled();
  158. while (work->flags & IRQ_WORK_BUSY)
  159. cpu_relax();
  160. }
  161. EXPORT_SYMBOL_GPL(irq_work_sync);