irq_work.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <asm/processor.h>
  15. /*
  16. * An entry can be in one of four states:
  17. *
  18. * free NULL, 0 -> {claimed} : free to be used
  19. * claimed NULL, 3 -> {pending} : claimed to be enqueued
  20. * pending next, 3 -> {busy} : queued, pending callback
  21. * busy NULL, 2 -> {free, claimed} : callback in progress, can be claimed
  22. */
  23. #define IRQ_WORK_PENDING 1UL
  24. #define IRQ_WORK_BUSY 2UL
  25. #define IRQ_WORK_FLAGS 3UL
  26. static DEFINE_PER_CPU(struct llist_head, irq_work_list);
  27. /*
  28. * Claim the entry so that no one else will poke at it.
  29. */
  30. static bool irq_work_claim(struct irq_work *work)
  31. {
  32. unsigned long flags, oflags, nflags;
  33. /*
  34. * Start with our best wish as a premise but only trust any
  35. * flag value after cmpxchg() result.
  36. */
  37. flags = work->flags & ~IRQ_WORK_PENDING;
  38. for (;;) {
  39. nflags = flags | IRQ_WORK_FLAGS;
  40. oflags = cmpxchg(&work->flags, flags, nflags);
  41. if (oflags == flags)
  42. break;
  43. if (oflags & IRQ_WORK_PENDING)
  44. return false;
  45. flags = oflags;
  46. cpu_relax();
  47. }
  48. return true;
  49. }
  50. void __weak arch_irq_work_raise(void)
  51. {
  52. /*
  53. * Lame architectures will get the timer tick callback
  54. */
  55. }
  56. /*
  57. * Queue the entry and raise the IPI if needed.
  58. */
  59. static void __irq_work_queue(struct irq_work *work)
  60. {
  61. bool empty;
  62. preempt_disable();
  63. empty = llist_add(&work->llnode, &__get_cpu_var(irq_work_list));
  64. /* The list was empty, raise self-interrupt to start processing. */
  65. if (empty)
  66. arch_irq_work_raise();
  67. preempt_enable();
  68. }
  69. /*
  70. * Enqueue the irq_work @entry, returns true on success, failure when the
  71. * @entry was already enqueued by someone else.
  72. *
  73. * Can be re-enqueued while the callback is still in progress.
  74. */
  75. bool irq_work_queue(struct irq_work *work)
  76. {
  77. if (!irq_work_claim(work)) {
  78. /*
  79. * Already enqueued, can't do!
  80. */
  81. return false;
  82. }
  83. __irq_work_queue(work);
  84. return true;
  85. }
  86. EXPORT_SYMBOL_GPL(irq_work_queue);
  87. /*
  88. * Run the irq_work entries on this cpu. Requires to be ran from hardirq
  89. * context with local IRQs disabled.
  90. */
  91. void irq_work_run(void)
  92. {
  93. struct irq_work *work;
  94. struct llist_head *this_list;
  95. struct llist_node *llnode;
  96. this_list = &__get_cpu_var(irq_work_list);
  97. if (llist_empty(this_list))
  98. return;
  99. BUG_ON(!in_irq());
  100. BUG_ON(!irqs_disabled());
  101. llnode = llist_del_all(this_list);
  102. while (llnode != NULL) {
  103. work = llist_entry(llnode, struct irq_work, llnode);
  104. llnode = llist_next(llnode);
  105. /*
  106. * Clear the PENDING bit, after this point the @work
  107. * can be re-used.
  108. * Make it immediately visible so that other CPUs trying
  109. * to claim that work don't rely on us to handle their data
  110. * while we are in the middle of the func.
  111. */
  112. xchg(&work->flags, IRQ_WORK_BUSY);
  113. work->func(work);
  114. /*
  115. * Clear the BUSY bit and return to the free state if
  116. * no-one else claimed it meanwhile.
  117. */
  118. (void)cmpxchg(&work->flags, IRQ_WORK_BUSY, 0);
  119. }
  120. }
  121. EXPORT_SYMBOL_GPL(irq_work_run);
  122. /*
  123. * Synchronize against the irq_work @entry, ensures the entry is not
  124. * currently in use.
  125. */
  126. void irq_work_sync(struct irq_work *work)
  127. {
  128. WARN_ON_ONCE(irqs_disabled());
  129. while (work->flags & IRQ_WORK_BUSY)
  130. cpu_relax();
  131. }
  132. EXPORT_SYMBOL_GPL(irq_work_sync);