irq_work.c 3.1 KB

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