spurious.c 12 KB

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