irq_work.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/cpu.h>
  15. #include <linux/notifier.h>
  16. #include <asm/processor.h>
  17. /*
  18. * An entry can be in one of four states:
  19. *
  20. * free NULL, 0 -> {claimed} : free to be used
  21. * claimed NULL, 3 -> {pending} : claimed to be enqueued
  22. * pending next, 3 -> {busy} : queued, pending callback
  23. * busy NULL, 2 -> {free, claimed} : callback in progress, can be claimed
  24. */
  25. #define IRQ_WORK_PENDING 1UL
  26. #define IRQ_WORK_BUSY 2UL
  27. #define IRQ_WORK_FLAGS 3UL
  28. static DEFINE_PER_CPU(struct llist_head, irq_work_list);
  29. /*
  30. * Claim the entry so that no one else will poke at it.
  31. */
  32. static bool irq_work_claim(struct irq_work *work)
  33. {
  34. unsigned long flags, oflags, nflags;
  35. /*
  36. * Start with our best wish as a premise but only trust any
  37. * flag value after cmpxchg() result.
  38. */
  39. flags = work->flags & ~IRQ_WORK_PENDING;
  40. for (;;) {
  41. nflags = flags | IRQ_WORK_FLAGS;
  42. oflags = cmpxchg(&work->flags, flags, nflags);
  43. if (oflags == flags)
  44. break;
  45. if (oflags & IRQ_WORK_PENDING)
  46. return false;
  47. flags = oflags;
  48. cpu_relax();
  49. }
  50. return true;
  51. }
  52. void __weak arch_irq_work_raise(void)
  53. {
  54. /*
  55. * Lame architectures will get the timer tick callback
  56. */
  57. }
  58. /*
  59. * Queue the entry and raise the IPI if needed.
  60. */
  61. static void __irq_work_queue(struct irq_work *work)
  62. {
  63. bool empty;
  64. preempt_disable();
  65. empty = llist_add(&work->llnode, &__get_cpu_var(irq_work_list));
  66. /* The list was empty, raise self-interrupt to start processing. */
  67. if (empty)
  68. arch_irq_work_raise();
  69. preempt_enable();
  70. }
  71. /*
  72. * Enqueue the irq_work @entry, returns true on success, failure when the
  73. * @entry was already enqueued by someone else.
  74. *
  75. * Can be re-enqueued while the callback is still in progress.
  76. */
  77. bool irq_work_queue(struct irq_work *work)
  78. {
  79. if (!irq_work_claim(work)) {
  80. /*
  81. * Already enqueued, can't do!
  82. */
  83. return false;
  84. }
  85. __irq_work_queue(work);
  86. return true;
  87. }
  88. EXPORT_SYMBOL_GPL(irq_work_queue);
  89. bool irq_work_needs_cpu(void)
  90. {
  91. struct llist_head *this_list;
  92. this_list = &__get_cpu_var(irq_work_list);
  93. if (llist_empty(this_list))
  94. return false;
  95. /* All work should have been flushed before going offline */
  96. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  97. return true;
  98. }
  99. static void __irq_work_run(void)
  100. {
  101. struct irq_work *work;
  102. struct llist_head *this_list;
  103. struct llist_node *llnode;
  104. this_list = &__get_cpu_var(irq_work_list);
  105. if (llist_empty(this_list))
  106. return;
  107. BUG_ON(!irqs_disabled());
  108. llnode = llist_del_all(this_list);
  109. while (llnode != NULL) {
  110. work = llist_entry(llnode, struct irq_work, llnode);
  111. llnode = llist_next(llnode);
  112. /*
  113. * Clear the PENDING bit, after this point the @work
  114. * can be re-used.
  115. * Make it immediately visible so that other CPUs trying
  116. * to claim that work don't rely on us to handle their data
  117. * while we are in the middle of the func.
  118. */
  119. xchg(&work->flags, IRQ_WORK_BUSY);
  120. work->func(work);
  121. /*
  122. * Clear the BUSY bit and return to the free state if
  123. * no-one else claimed it meanwhile.
  124. */
  125. (void)cmpxchg(&work->flags, IRQ_WORK_BUSY, 0);
  126. }
  127. }
  128. /*
  129. * Run the irq_work entries on this cpu. Requires to be ran from hardirq
  130. * context with local IRQs disabled.
  131. */
  132. void irq_work_run(void)
  133. {
  134. BUG_ON(!in_irq());
  135. __irq_work_run();
  136. }
  137. EXPORT_SYMBOL_GPL(irq_work_run);
  138. /*
  139. * Synchronize against the irq_work @entry, ensures the entry is not
  140. * currently in use.
  141. */
  142. void irq_work_sync(struct irq_work *work)
  143. {
  144. WARN_ON_ONCE(irqs_disabled());
  145. while (work->flags & IRQ_WORK_BUSY)
  146. cpu_relax();
  147. }
  148. EXPORT_SYMBOL_GPL(irq_work_sync);
  149. #ifdef CONFIG_HOTPLUG_CPU
  150. static int irq_work_cpu_notify(struct notifier_block *self,
  151. unsigned long action, void *hcpu)
  152. {
  153. long cpu = (long)hcpu;
  154. switch (action) {
  155. case CPU_DYING:
  156. /* Called from stop_machine */
  157. if (WARN_ON_ONCE(cpu != smp_processor_id()))
  158. break;
  159. __irq_work_run();
  160. break;
  161. default:
  162. break;
  163. }
  164. return NOTIFY_OK;
  165. }
  166. static struct notifier_block cpu_notify;
  167. static __init int irq_work_init_cpu_notifier(void)
  168. {
  169. cpu_notify.notifier_call = irq_work_cpu_notify;
  170. cpu_notify.priority = 0;
  171. register_cpu_notifier(&cpu_notify);
  172. return 0;
  173. }
  174. device_initcall(irq_work_init_cpu_notifier);
  175. #endif /* CONFIG_HOTPLUG_CPU */