irq_work.c 3.1 KB

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