irq_work.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. bool irq_work_queue(struct irq_work *work)
  57. {
  58. /* Only queue if not already pending */
  59. if (!irq_work_claim(work))
  60. return false;
  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. return true;
  75. }
  76. EXPORT_SYMBOL_GPL(irq_work_queue);
  77. bool irq_work_needs_cpu(void)
  78. {
  79. struct llist_head *this_list;
  80. this_list = &__get_cpu_var(irq_work_list);
  81. if (llist_empty(this_list))
  82. return false;
  83. /* All work should have been flushed before going offline */
  84. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  85. return true;
  86. }
  87. static void __irq_work_run(void)
  88. {
  89. unsigned long flags;
  90. struct irq_work *work;
  91. struct llist_head *this_list;
  92. struct llist_node *llnode;
  93. /*
  94. * Reset the "raised" state right before we check the list because
  95. * an NMI may enqueue after we find the list empty from the runner.
  96. */
  97. __this_cpu_write(irq_work_raised, 0);
  98. barrier();
  99. this_list = &__get_cpu_var(irq_work_list);
  100. if (llist_empty(this_list))
  101. return;
  102. BUG_ON(!irqs_disabled());
  103. llnode = llist_del_all(this_list);
  104. while (llnode != NULL) {
  105. work = llist_entry(llnode, struct irq_work, llnode);
  106. llnode = llist_next(llnode);
  107. /*
  108. * Clear the PENDING bit, after this point the @work
  109. * can be re-used.
  110. * Make it immediately visible so that other CPUs trying
  111. * to claim that work don't rely on us to handle their data
  112. * while we are in the middle of the func.
  113. */
  114. flags = work->flags & ~IRQ_WORK_PENDING;
  115. xchg(&work->flags, flags);
  116. work->func(work);
  117. /*
  118. * Clear the BUSY bit and return to the free state if
  119. * no-one else claimed it meanwhile.
  120. */
  121. (void)cmpxchg(&work->flags, flags, flags & ~IRQ_WORK_BUSY);
  122. }
  123. }
  124. /*
  125. * Run the irq_work entries on this cpu. Requires to be ran from hardirq
  126. * context with local IRQs disabled.
  127. */
  128. void irq_work_run(void)
  129. {
  130. BUG_ON(!in_irq());
  131. __irq_work_run();
  132. }
  133. EXPORT_SYMBOL_GPL(irq_work_run);
  134. /*
  135. * Synchronize against the irq_work @entry, ensures the entry is not
  136. * currently in use.
  137. */
  138. void irq_work_sync(struct irq_work *work)
  139. {
  140. WARN_ON_ONCE(irqs_disabled());
  141. while (work->flags & IRQ_WORK_BUSY)
  142. cpu_relax();
  143. }
  144. EXPORT_SYMBOL_GPL(irq_work_sync);
  145. #ifdef CONFIG_HOTPLUG_CPU
  146. static int irq_work_cpu_notify(struct notifier_block *self,
  147. unsigned long action, void *hcpu)
  148. {
  149. long cpu = (long)hcpu;
  150. switch (action) {
  151. case CPU_DYING:
  152. /* Called from stop_machine */
  153. if (WARN_ON_ONCE(cpu != smp_processor_id()))
  154. break;
  155. __irq_work_run();
  156. break;
  157. default:
  158. break;
  159. }
  160. return NOTIFY_OK;
  161. }
  162. static struct notifier_block cpu_notify;
  163. static __init int irq_work_init_cpu_notifier(void)
  164. {
  165. cpu_notify.notifier_call = irq_work_cpu_notify;
  166. cpu_notify.priority = 0;
  167. register_cpu_notifier(&cpu_notify);
  168. return 0;
  169. }
  170. device_initcall(irq_work_init_cpu_notifier);
  171. #endif /* CONFIG_HOTPLUG_CPU */