irq_work.c 4.5 KB

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