spurious.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
  4. *
  5. * This file contains spurious interrupt handling.
  6. */
  7. #include <linux/jiffies.h>
  8. #include <linux/irq.h>
  9. #include <linux/module.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/timer.h>
  13. #include "internals.h"
  14. static int irqfixup __read_mostly;
  15. #define POLL_SPURIOUS_IRQ_INTERVAL (HZ/10)
  16. static void poll_spurious_irqs(struct timer_list *unused);
  17. static DEFINE_TIMER(poll_spurious_irq_timer, poll_spurious_irqs);
  18. static int irq_poll_cpu;
  19. static atomic_t irq_poll_active;
  20. /*
  21. * We wait here for a poller to finish.
  22. *
  23. * If the poll runs on this CPU, then we yell loudly and return
  24. * false. That will leave the interrupt line disabled in the worst
  25. * case, but it should never happen.
  26. *
  27. * We wait until the poller is done and then recheck disabled and
  28. * action (about to be disabled). Only if it's still active, we return
  29. * true and let the handler run.
  30. */
  31. bool irq_wait_for_poll(struct irq_desc *desc)
  32. __must_hold(&desc->lock)
  33. {
  34. if (WARN_ONCE(irq_poll_cpu == smp_processor_id(),
  35. "irq poll in progress on cpu %d for irq %d\n",
  36. smp_processor_id(), desc->irq_data.irq))
  37. return false;
  38. #ifdef CONFIG_SMP
  39. do {
  40. raw_spin_unlock(&desc->lock);
  41. while (irqd_irq_inprogress(&desc->irq_data))
  42. cpu_relax();
  43. raw_spin_lock(&desc->lock);
  44. } while (irqd_irq_inprogress(&desc->irq_data));
  45. /* Might have been disabled in meantime */
  46. return !irqd_irq_disabled(&desc->irq_data) && desc->action;
  47. #else
  48. return false;
  49. #endif
  50. }
  51. /*
  52. * Recovery handler for misrouted interrupts.
  53. */
  54. static int try_one_irq(struct irq_desc *desc, bool force)
  55. {
  56. irqreturn_t ret = IRQ_NONE;
  57. struct irqaction *action;
  58. raw_spin_lock(&desc->lock);
  59. /*
  60. * PER_CPU, nested thread interrupts and interrupts explicitly
  61. * marked polled are excluded from polling.
  62. */
  63. if (irq_settings_is_per_cpu(desc) ||
  64. irq_settings_is_nested_thread(desc) ||
  65. irq_settings_is_polled(desc))
  66. goto out;
  67. /*
  68. * Do not poll disabled interrupts unless the spurious
  69. * disabled poller asks explicitly.
  70. */
  71. if (irqd_irq_disabled(&desc->irq_data) && !force)
  72. goto out;
  73. /*
  74. * All handlers must agree on IRQF_SHARED, so we test just the
  75. * first.
  76. */
  77. action = desc->action;
  78. if (!action || !(action->flags & IRQF_SHARED) ||
  79. (action->flags & __IRQF_TIMER))
  80. goto out;
  81. /* Already running on another processor */
  82. if (irqd_irq_inprogress(&desc->irq_data)) {
  83. /*
  84. * Already running: If it is shared get the other
  85. * CPU to go looking for our mystery interrupt too
  86. */
  87. desc->istate |= IRQS_PENDING;
  88. goto out;
  89. }
  90. /* Mark it poll in progress */
  91. desc->istate |= IRQS_POLL_INPROGRESS;
  92. do {
  93. if (handle_irq_event(desc) == IRQ_HANDLED)
  94. ret = IRQ_HANDLED;
  95. /* Make sure that there is still a valid action */
  96. action = desc->action;
  97. } while ((desc->istate & IRQS_PENDING) && action);
  98. desc->istate &= ~IRQS_POLL_INPROGRESS;
  99. out:
  100. raw_spin_unlock(&desc->lock);
  101. return ret == IRQ_HANDLED;
  102. }
  103. static int misrouted_irq(int irq)
  104. {
  105. struct irq_desc *desc;
  106. int i, ok = 0;
  107. if (atomic_inc_return(&irq_poll_active) != 1)
  108. goto out;
  109. irq_poll_cpu = smp_processor_id();
  110. for_each_irq_desc(i, desc) {
  111. if (!i)
  112. continue;
  113. if (i == irq) /* Already tried */
  114. continue;
  115. if (try_one_irq(desc, false))
  116. ok = 1;
  117. }
  118. out:
  119. atomic_dec(&irq_poll_active);
  120. /* So the caller can adjust the irq error counts */
  121. return ok;
  122. }
  123. static void poll_spurious_irqs(struct timer_list *unused)
  124. {
  125. struct irq_desc *desc;
  126. int i;
  127. if (atomic_inc_return(&irq_poll_active) != 1)
  128. goto out;
  129. irq_poll_cpu = smp_processor_id();
  130. for_each_irq_desc(i, desc) {
  131. unsigned int state;
  132. if (!i)
  133. continue;
  134. /* Racy but it doesn't matter */
  135. state = desc->istate;
  136. barrier();
  137. if (!(state & IRQS_SPURIOUS_DISABLED))
  138. continue;
  139. local_irq_disable();
  140. try_one_irq(desc, true);
  141. local_irq_enable();
  142. }
  143. out:
  144. atomic_dec(&irq_poll_active);
  145. mod_timer(&poll_spurious_irq_timer,
  146. jiffies + POLL_SPURIOUS_IRQ_INTERVAL);
  147. }
  148. static inline int bad_action_ret(irqreturn_t action_ret)
  149. {
  150. unsigned int r = action_ret;
  151. if (likely(r <= (IRQ_HANDLED | IRQ_WAKE_THREAD)))
  152. return 0;
  153. return 1;
  154. }
  155. /*
  156. * If 99,900 of the previous 100,000 interrupts have not been handled
  157. * then assume that the IRQ is stuck in some manner. Drop a diagnostic
  158. * and try to turn the IRQ off.
  159. *
  160. * (The other 100-of-100,000 interrupts may have been a correctly
  161. * functioning device sharing an IRQ with the failing one)
  162. */
  163. static void __report_bad_irq(struct irq_desc *desc, irqreturn_t action_ret)
  164. {
  165. unsigned int irq = irq_desc_get_irq(desc);
  166. struct irqaction *action;
  167. unsigned long flags;
  168. if (bad_action_ret(action_ret)) {
  169. printk(KERN_ERR "irq event %d: bogus return value %x\n",
  170. irq, action_ret);
  171. } else {
  172. printk(KERN_ERR "irq %d: nobody cared (try booting with "
  173. "the \"irqpoll\" option)\n", irq);
  174. }
  175. dump_stack();
  176. printk(KERN_ERR "handlers:\n");
  177. /*
  178. * We need to take desc->lock here. note_interrupt() is called
  179. * w/o desc->lock held, but IRQ_PROGRESS set. We might race
  180. * with something else removing an action. It's ok to take
  181. * desc->lock here. See synchronize_irq().
  182. */
  183. raw_spin_lock_irqsave(&desc->lock, flags);
  184. for_each_action_of_desc(desc, action) {
  185. printk(KERN_ERR "[<%p>] %ps", action->handler, action->handler);
  186. if (action->thread_fn)
  187. printk(KERN_CONT " threaded [<%p>] %ps",
  188. action->thread_fn, action->thread_fn);
  189. printk(KERN_CONT "\n");
  190. }
  191. raw_spin_unlock_irqrestore(&desc->lock, flags);
  192. }
  193. static void report_bad_irq(struct irq_desc *desc, irqreturn_t action_ret)
  194. {
  195. static int count = 100;
  196. if (count > 0) {
  197. count--;
  198. __report_bad_irq(desc, action_ret);
  199. }
  200. }
  201. static inline int
  202. try_misrouted_irq(unsigned int irq, struct irq_desc *desc,
  203. irqreturn_t action_ret)
  204. {
  205. struct irqaction *action;
  206. if (!irqfixup)
  207. return 0;
  208. /* We didn't actually handle the IRQ - see if it was misrouted? */
  209. if (action_ret == IRQ_NONE)
  210. return 1;
  211. /*
  212. * But for 'irqfixup == 2' we also do it for handled interrupts if
  213. * they are marked as IRQF_IRQPOLL (or for irq zero, which is the
  214. * traditional PC timer interrupt.. Legacy)
  215. */
  216. if (irqfixup < 2)
  217. return 0;
  218. if (!irq)
  219. return 1;
  220. /*
  221. * Since we don't get the descriptor lock, "action" can
  222. * change under us. We don't really care, but we don't
  223. * want to follow a NULL pointer. So tell the compiler to
  224. * just load it once by using a barrier.
  225. */
  226. action = desc->action;
  227. barrier();
  228. return action && (action->flags & IRQF_IRQPOLL);
  229. }
  230. #define SPURIOUS_DEFERRED 0x80000000
  231. void note_interrupt(struct irq_desc *desc, irqreturn_t action_ret)
  232. {
  233. unsigned int irq;
  234. if (desc->istate & IRQS_POLL_INPROGRESS ||
  235. irq_settings_is_polled(desc))
  236. return;
  237. if (bad_action_ret(action_ret)) {
  238. report_bad_irq(desc, action_ret);
  239. return;
  240. }
  241. /*
  242. * We cannot call note_interrupt from the threaded handler
  243. * because we need to look at the compound of all handlers
  244. * (primary and threaded). Aside of that in the threaded
  245. * shared case we have no serialization against an incoming
  246. * hardware interrupt while we are dealing with a threaded
  247. * result.
  248. *
  249. * So in case a thread is woken, we just note the fact and
  250. * defer the analysis to the next hardware interrupt.
  251. *
  252. * The threaded handlers store whether they successfully
  253. * handled an interrupt and we check whether that number
  254. * changed versus the last invocation.
  255. *
  256. * We could handle all interrupts with the delayed by one
  257. * mechanism, but for the non forced threaded case we'd just
  258. * add pointless overhead to the straight hardirq interrupts
  259. * for the sake of a few lines less code.
  260. */
  261. if (action_ret & IRQ_WAKE_THREAD) {
  262. /*
  263. * There is a thread woken. Check whether one of the
  264. * shared primary handlers returned IRQ_HANDLED. If
  265. * not we defer the spurious detection to the next
  266. * interrupt.
  267. */
  268. if (action_ret == IRQ_WAKE_THREAD) {
  269. int handled;
  270. /*
  271. * We use bit 31 of thread_handled_last to
  272. * denote the deferred spurious detection
  273. * active. No locking necessary as
  274. * thread_handled_last is only accessed here
  275. * and we have the guarantee that hard
  276. * interrupts are not reentrant.
  277. */
  278. if (!(desc->threads_handled_last & SPURIOUS_DEFERRED)) {
  279. desc->threads_handled_last |= SPURIOUS_DEFERRED;
  280. return;
  281. }
  282. /*
  283. * Check whether one of the threaded handlers
  284. * returned IRQ_HANDLED since the last
  285. * interrupt happened.
  286. *
  287. * For simplicity we just set bit 31, as it is
  288. * set in threads_handled_last as well. So we
  289. * avoid extra masking. And we really do not
  290. * care about the high bits of the handled
  291. * count. We just care about the count being
  292. * different than the one we saw before.
  293. */
  294. handled = atomic_read(&desc->threads_handled);
  295. handled |= SPURIOUS_DEFERRED;
  296. if (handled != desc->threads_handled_last) {
  297. action_ret = IRQ_HANDLED;
  298. /*
  299. * Note: We keep the SPURIOUS_DEFERRED
  300. * bit set. We are handling the
  301. * previous invocation right now.
  302. * Keep it for the current one, so the
  303. * next hardware interrupt will
  304. * account for it.
  305. */
  306. desc->threads_handled_last = handled;
  307. } else {
  308. /*
  309. * None of the threaded handlers felt
  310. * responsible for the last interrupt
  311. *
  312. * We keep the SPURIOUS_DEFERRED bit
  313. * set in threads_handled_last as we
  314. * need to account for the current
  315. * interrupt as well.
  316. */
  317. action_ret = IRQ_NONE;
  318. }
  319. } else {
  320. /*
  321. * One of the primary handlers returned
  322. * IRQ_HANDLED. So we don't care about the
  323. * threaded handlers on the same line. Clear
  324. * the deferred detection bit.
  325. *
  326. * In theory we could/should check whether the
  327. * deferred bit is set and take the result of
  328. * the previous run into account here as
  329. * well. But it's really not worth the
  330. * trouble. If every other interrupt is
  331. * handled we never trigger the spurious
  332. * detector. And if this is just the one out
  333. * of 100k unhandled ones which is handled
  334. * then we merily delay the spurious detection
  335. * by one hard interrupt. Not a real problem.
  336. */
  337. desc->threads_handled_last &= ~SPURIOUS_DEFERRED;
  338. }
  339. }
  340. if (unlikely(action_ret == IRQ_NONE)) {
  341. /*
  342. * If we are seeing only the odd spurious IRQ caused by
  343. * bus asynchronicity then don't eventually trigger an error,
  344. * otherwise the counter becomes a doomsday timer for otherwise
  345. * working systems
  346. */
  347. if (time_after(jiffies, desc->last_unhandled + HZ/10))
  348. desc->irqs_unhandled = 1;
  349. else
  350. desc->irqs_unhandled++;
  351. desc->last_unhandled = jiffies;
  352. }
  353. irq = irq_desc_get_irq(desc);
  354. if (unlikely(try_misrouted_irq(irq, desc, action_ret))) {
  355. int ok = misrouted_irq(irq);
  356. if (action_ret == IRQ_NONE)
  357. desc->irqs_unhandled -= ok;
  358. }
  359. desc->irq_count++;
  360. if (likely(desc->irq_count < 100000))
  361. return;
  362. desc->irq_count = 0;
  363. if (unlikely(desc->irqs_unhandled > 99900)) {
  364. /*
  365. * The interrupt is stuck
  366. */
  367. __report_bad_irq(desc, action_ret);
  368. /*
  369. * Now kill the IRQ
  370. */
  371. printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
  372. desc->istate |= IRQS_SPURIOUS_DISABLED;
  373. desc->depth++;
  374. irq_disable(desc);
  375. mod_timer(&poll_spurious_irq_timer,
  376. jiffies + POLL_SPURIOUS_IRQ_INTERVAL);
  377. }
  378. desc->irqs_unhandled = 0;
  379. }
  380. bool noirqdebug __read_mostly;
  381. int noirqdebug_setup(char *str)
  382. {
  383. noirqdebug = 1;
  384. printk(KERN_INFO "IRQ lockup detection disabled\n");
  385. return 1;
  386. }
  387. __setup("noirqdebug", noirqdebug_setup);
  388. module_param(noirqdebug, bool, 0644);
  389. MODULE_PARM_DESC(noirqdebug, "Disable irq lockup detection when true");
  390. static int __init irqfixup_setup(char *str)
  391. {
  392. irqfixup = 1;
  393. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  394. printk(KERN_WARNING "This may impact system performance.\n");
  395. return 1;
  396. }
  397. __setup("irqfixup", irqfixup_setup);
  398. module_param(irqfixup, int, 0644);
  399. static int __init irqpoll_setup(char *str)
  400. {
  401. irqfixup = 2;
  402. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  403. "enabled\n");
  404. printk(KERN_WARNING "This may significantly impact system "
  405. "performance\n");
  406. return 1;
  407. }
  408. __setup("irqpoll", irqpoll_setup);