irq_work.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  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_FLAGS;
  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. #ifdef CONFIG_SMP
  52. /*
  53. * Enqueue the irq_work @work on @cpu unless it's already pending
  54. * somewhere.
  55. *
  56. * Can be re-enqueued while the callback is still in progress.
  57. */
  58. bool irq_work_queue_on(struct irq_work *work, int cpu)
  59. {
  60. /* All work should have been flushed before going offline */
  61. WARN_ON_ONCE(cpu_is_offline(cpu));
  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. return true;
  70. }
  71. EXPORT_SYMBOL_GPL(irq_work_queue_on);
  72. #endif
  73. /* Enqueue the irq work @work on the current CPU */
  74. bool irq_work_queue(struct irq_work *work)
  75. {
  76. /* Only queue if not already pending */
  77. if (!irq_work_claim(work))
  78. return false;
  79. /* Queue the entry and raise the IPI if needed. */
  80. preempt_disable();
  81. /* If the work is "lazy", handle it from next tick if any */
  82. if (work->flags & IRQ_WORK_LAZY) {
  83. if (llist_add(&work->llnode, &__get_cpu_var(lazy_list)) &&
  84. tick_nohz_tick_stopped())
  85. arch_irq_work_raise();
  86. } else {
  87. if (llist_add(&work->llnode, &__get_cpu_var(raised_list)))
  88. arch_irq_work_raise();
  89. }
  90. preempt_enable();
  91. return true;
  92. }
  93. EXPORT_SYMBOL_GPL(irq_work_queue);
  94. bool irq_work_needs_cpu(void)
  95. {
  96. struct llist_head *raised, *lazy;
  97. raised = &__get_cpu_var(raised_list);
  98. lazy = &__get_cpu_var(lazy_list);
  99. if (llist_empty(raised) && llist_empty(lazy))
  100. return false;
  101. /* All work should have been flushed before going offline */
  102. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  103. return true;
  104. }
  105. static void irq_work_run_list(struct llist_head *list)
  106. {
  107. unsigned long flags;
  108. struct irq_work *work;
  109. struct llist_node *llnode;
  110. BUG_ON(!irqs_disabled());
  111. if (llist_empty(list))
  112. return;
  113. llnode = llist_del_all(list);
  114. while (llnode != NULL) {
  115. work = llist_entry(llnode, struct irq_work, llnode);
  116. llnode = llist_next(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(&__get_cpu_var(raised_list));
  141. irq_work_run_list(&__get_cpu_var(lazy_list));
  142. }
  143. EXPORT_SYMBOL_GPL(irq_work_run);
  144. /*
  145. * Synchronize against the irq_work @entry, ensures the entry is not
  146. * currently in use.
  147. */
  148. void irq_work_sync(struct irq_work *work)
  149. {
  150. WARN_ON_ONCE(irqs_disabled());
  151. while (work->flags & IRQ_WORK_BUSY)
  152. cpu_relax();
  153. }
  154. EXPORT_SYMBOL_GPL(irq_work_sync);