handle.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
  4. * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
  5. *
  6. * This file contains the core interrupt handling code. Detailed
  7. * information is available in Documentation/core-api/genericirq.rst
  8. *
  9. */
  10. #include <linux/irq.h>
  11. #include <linux/random.h>
  12. #include <linux/sched.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel_stat.h>
  15. #include <trace/events/irq.h>
  16. #include "internals.h"
  17. #ifdef CONFIG_GENERIC_IRQ_MULTI_HANDLER
  18. void (*handle_arch_irq)(struct pt_regs *) __ro_after_init;
  19. #endif
  20. /**
  21. * handle_bad_irq - handle spurious and unhandled irqs
  22. * @desc: description of the interrupt
  23. *
  24. * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
  25. */
  26. void handle_bad_irq(struct irq_desc *desc)
  27. {
  28. unsigned int irq = irq_desc_get_irq(desc);
  29. print_irq_desc(irq, desc);
  30. kstat_incr_irqs_this_cpu(desc);
  31. ack_bad_irq(irq);
  32. }
  33. EXPORT_SYMBOL_GPL(handle_bad_irq);
  34. /*
  35. * Special, empty irq handler:
  36. */
  37. irqreturn_t no_action(int cpl, void *dev_id)
  38. {
  39. return IRQ_NONE;
  40. }
  41. EXPORT_SYMBOL_GPL(no_action);
  42. static void warn_no_thread(unsigned int irq, struct irqaction *action)
  43. {
  44. if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))
  45. return;
  46. printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "
  47. "but no thread function available.", irq, action->name);
  48. }
  49. void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action)
  50. {
  51. /*
  52. * In case the thread crashed and was killed we just pretend that
  53. * we handled the interrupt. The hardirq handler has disabled the
  54. * device interrupt, so no irq storm is lurking.
  55. */
  56. if (action->thread->flags & PF_EXITING)
  57. return;
  58. /*
  59. * Wake up the handler thread for this action. If the
  60. * RUNTHREAD bit is already set, nothing to do.
  61. */
  62. if (test_and_set_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  63. return;
  64. /*
  65. * It's safe to OR the mask lockless here. We have only two
  66. * places which write to threads_oneshot: This code and the
  67. * irq thread.
  68. *
  69. * This code is the hard irq context and can never run on two
  70. * cpus in parallel. If it ever does we have more serious
  71. * problems than this bitmask.
  72. *
  73. * The irq threads of this irq which clear their "running" bit
  74. * in threads_oneshot are serialized via desc->lock against
  75. * each other and they are serialized against this code by
  76. * IRQS_INPROGRESS.
  77. *
  78. * Hard irq handler:
  79. *
  80. * spin_lock(desc->lock);
  81. * desc->state |= IRQS_INPROGRESS;
  82. * spin_unlock(desc->lock);
  83. * set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
  84. * desc->threads_oneshot |= mask;
  85. * spin_lock(desc->lock);
  86. * desc->state &= ~IRQS_INPROGRESS;
  87. * spin_unlock(desc->lock);
  88. *
  89. * irq thread:
  90. *
  91. * again:
  92. * spin_lock(desc->lock);
  93. * if (desc->state & IRQS_INPROGRESS) {
  94. * spin_unlock(desc->lock);
  95. * while(desc->state & IRQS_INPROGRESS)
  96. * cpu_relax();
  97. * goto again;
  98. * }
  99. * if (!test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  100. * desc->threads_oneshot &= ~mask;
  101. * spin_unlock(desc->lock);
  102. *
  103. * So either the thread waits for us to clear IRQS_INPROGRESS
  104. * or we are waiting in the flow handler for desc->lock to be
  105. * released before we reach this point. The thread also checks
  106. * IRQTF_RUNTHREAD under desc->lock. If set it leaves
  107. * threads_oneshot untouched and runs the thread another time.
  108. */
  109. desc->threads_oneshot |= action->thread_mask;
  110. /*
  111. * We increment the threads_active counter in case we wake up
  112. * the irq thread. The irq thread decrements the counter when
  113. * it returns from the handler or in the exit path and wakes
  114. * up waiters which are stuck in synchronize_irq() when the
  115. * active count becomes zero. synchronize_irq() is serialized
  116. * against this code (hard irq handler) via IRQS_INPROGRESS
  117. * like the finalize_oneshot() code. See comment above.
  118. */
  119. atomic_inc(&desc->threads_active);
  120. wake_up_process(action->thread);
  121. }
  122. irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc, unsigned int *flags)
  123. {
  124. irqreturn_t retval = IRQ_NONE;
  125. unsigned int irq = desc->irq_data.irq;
  126. struct irqaction *action;
  127. record_irq_time(desc);
  128. for_each_action_of_desc(desc, action) {
  129. irqreturn_t res;
  130. /*
  131. * If this IRQ would be threaded under force_irqthreads, mark it so.
  132. */
  133. if (irq_settings_can_thread(desc) &&
  134. !(action->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT)))
  135. lockdep_hardirq_threaded();
  136. trace_irq_handler_entry(irq, action);
  137. res = action->handler(irq, action->dev_id);
  138. trace_irq_handler_exit(irq, action, res);
  139. if (WARN_ONCE(!irqs_disabled(),"irq %u handler %pS enabled interrupts\n",
  140. irq, action->handler))
  141. local_irq_disable();
  142. switch (res) {
  143. case IRQ_WAKE_THREAD:
  144. /*
  145. * Catch drivers which return WAKE_THREAD but
  146. * did not set up a thread function
  147. */
  148. if (unlikely(!action->thread_fn)) {
  149. warn_no_thread(irq, action);
  150. break;
  151. }
  152. __irq_wake_thread(desc, action);
  153. fallthrough; /* to add to randomness */
  154. case IRQ_HANDLED:
  155. *flags |= action->flags;
  156. break;
  157. default:
  158. break;
  159. }
  160. retval |= res;
  161. }
  162. return retval;
  163. }
  164. irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
  165. {
  166. irqreturn_t retval;
  167. unsigned int flags = 0;
  168. retval = __handle_irq_event_percpu(desc, &flags);
  169. add_interrupt_randomness(desc->irq_data.irq, flags);
  170. if (!noirqdebug)
  171. note_interrupt(desc, retval);
  172. return retval;
  173. }
  174. irqreturn_t handle_irq_event(struct irq_desc *desc)
  175. {
  176. irqreturn_t ret;
  177. desc->istate &= ~IRQS_PENDING;
  178. irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  179. raw_spin_unlock(&desc->lock);
  180. ret = handle_irq_event_percpu(desc);
  181. raw_spin_lock(&desc->lock);
  182. irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  183. return ret;
  184. }
  185. #ifdef CONFIG_GENERIC_IRQ_MULTI_HANDLER
  186. int __init set_handle_irq(void (*handle_irq)(struct pt_regs *))
  187. {
  188. if (handle_arch_irq)
  189. return -EBUSY;
  190. handle_arch_irq = handle_irq;
  191. return 0;
  192. }
  193. #endif