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