PCI/AER: Use IRQF_NO_THREAD on aer_irq
Subsystems (5)
PATCH 1/1 PCI/AER: Use IRQF_NO_THREAD on aer_irq
On PREEMPT_RT, currently both aer_irq and aer_isr run in separate threads, at the same FIFO priority. This can lead to the aer_isr thread starving the aer_irq thread, particularly if multi_error_valid causes a scan of all devices, and multiple errors are raised during the scan.
On !PREEMPT_RT, or if aer_irq runs at a higher priority than aer_isr, these errors can be queued as single-error events as they happen. But if aer_irq can't run until aer_isr finishes, by that time the multi event bit will be set again, causing a new scan and an infinite loop.
---
I'm seeing this on a particular ARM server when using /sys/bus/pci/rescan, though the internal reporter sometimes saw it happen on boot as well. On !PREEMPT_RT, or with this patch, a finite number of errors are emitted and the scan completes.
--- drivers/pci/pcie/aer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pci/pcie/aer.c +2 -1
@@ -1671,7 +1671,8 @@ static int aer_probe(struct pcie_device *dev)set_service_data (dev, rpc); status =devm_request_threaded_irq (device, dev->irq, aer_irq, aer_isr, -IRQF_SHARED ,"aerdrv" , dev); +IRQF_NO_THREAD |IRQF_SHARED , +"aerdrv" , dev);if (status) {pci_err (port,"request AER IRQ %d failed\n" , dev->irq);return status;
Signature · 1 line
2.47.1
Quoted 1 line · Crystal Wood
On
PREEMPT_RT, currently bothaer_irqandaer_isrrun in separate threads,
My understanding is that if request_threaded_irq() is passed both a non-NULL handler and a non-NULL thread_fn, the former runs in hardirq context and the latter in kthread context. Even on PREEMPT_RT.
So how can aer_irq() and aer_isr() ever both run in kthread context? Am I missing something?
Quoted 3 lines
at the same FIFO priority. This can lead to the
aer_isrthread starving theaer_irqthread, particularly ifmulti_error_validcauses a scan of all devices, and multiple errors are raised during the scan.
I'm not seeing aer_isr() waiting on a spinlock, so how can it be starved?
Thanks,
Lukas
Quoted 9 lines · Lukas Wunner
Quoted 1 line · Crystal Wood
On
PREEMPT_RT, currently bothaer_irqandaer_isrrun in separate threads,My understanding is that if
request_threaded_irq()is passed both a non-NULL handler and a non-NULLthread_fn, the former runs in hardirq context and the latter in kthread context. Even onPREEMPT_RT.So how can
aer_irq()andaer_isr()ever both run in kthread context? Am I missing something?
They are both threaded. See commit 2a1d3ab8986d1b2 ("genirq: Handle force threading of irqs with primary and thread handler")
Quoted 6 lines
Quoted 4 lines
at the same FIFO priority. This can lead to the
aer_isrthread starving theaer_irqthread, particularly ifmulti_error_validcauses a scan of all devices, and multiple errors are raised during the scan.I'm not seeing
aer_isr()waiting on a spinlock, so how can it be starved?
It's not about locks... Maybe starvation is too strong of a word since aer_irq does eventually get to run, just too late to avert yet another multi error that starts the whole thing over again.
-Crystal
Quoted 9 lines · Crystal Wood
On
PREEMPT_RT, currently bothaer_irqandaer_isrrun in separate threads, at the same FIFO priority. This can lead to theaer_isrthread starving theaer_irqthread, particularly ifmulti_error_validcauses a scan of all devices, and multiple errors are raised during the scan.On !PREEMPT_RT, or if
aer_irqruns at a higher priority thanaer_isr, these errors can be queued as single-error events as they happen. But ifaer_irqcan't run untilaer_isrfinishes, by that time the multi event bit will be set again, causing a new scan and an infinite loop.
So if aer_irq is too slow we get new "work" pilled up? Is it because there is a timing constrains how long until the error needs to be acknowledged?
Another way would be to let the secondary handler run at a slightly lower priority than the primary handler. In this case making the primary non-threaded should not cause any harm.
Quoted 15 lines
Signed-off-by Crystal Wood <[email protected]>---
I'm seeing this on a particular ARM server when using/sys/bus/pci/rescan, though the internal reporter sometimes saw it happen on boot as well. On !PREEMPT_RT, or with this patch, a finite number of errors are emitted and the scan completes.--- drivers/pci/pcie/aer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/pci/pcie/aer.c +0
-0
drivers/pci/pcie/aer.c static int aer_probe(struct pcie_device *dev) · line 1675
Quoted 6 lines
drivers/pci/pcie/aer.c +2
-1@@ -1671,7 +1671,8 @@ static int aer_probe(struct pcie_device *dev)set_service_data (dev, rpc); status =devm_request_threaded_irq (device, dev->irq, aer_irq, aer_isr, -IRQF_SHARED ,"aerdrv" , dev); +IRQF_NO_THREAD |IRQF_SHARED , +"aerdrv" , dev);
I'm not sure if this works with IRQF_SHARED. Your primary handler is IRQF_SHARED + IRQF_NO_THREAD and another shared handler which is forced-threaded will have IRQF_SHARED + IRQF_ONESHOT.
If the core does not complain, all good. Worst case might be the shared ONESHOT lets your primary handler starve. It would be nice if you could check if you have shared handler here (I have no aer I three boxes I checked).
drivers/pci/pcie/aer.c static int aer_probe(struct pcie_device *dev) · line 1678
Quoted 3 lines
drivers/pci/pcie/aer.c +0
-0if (status) {pci_err (port,"request AER IRQ %d failed\n" , dev->irq);return status;
Sebastian
Quoted 6 lines · Sebastian Andrzej Siewior
Quoted 5 lines · Crystal Wood
On
PREEMPT_RT, currently bothaer_irqandaer_isrrun in separate threads, at the same FIFO priority. This can lead to theaer_isrthread starving theaer_irqthread, particularly ifmulti_error_validcauses a scan of all devices, and multiple errors are raised during the scan.
Quoted 8 lines
Quoted 4 lines
On !PREEMPT_RT, or if
aer_irqruns at a higher priority thanaer_isr, these errors can be queued as single-error events as they happen. But ifaer_irqcan't run untilaer_isrfinishes, by that time the multi event bit will be set again, causing a new scan and an infinite loop.So if
aer_irqis too slow we get new "work" pilled up? Is it because there is a timing constrains how long until the error needs to be acknowledged?
Since v6.16, AER supports rate limiting. It's unclear which kernel version Crystal is using, but if it's older than v6.16, it may be worth retrying with a newer release to see if that solves the problem.
Quoted 3 lines
Another way would be to let the secondary handler run at a slightly lower priority than the primary handler. In this case making the primary non-threaded should not cause any harm.
Why isn't the secondary handler always assigned a lower priority by default? I think a lot of drivers are built on the assumption that the primary handler is scheduled sooner than the secondary handler.
E.g. the native PCIe hotplug driver (drivers/pci/hotplug/pciehp_hpc.c) uses the primary handler to pick up Command Completed interrupts and will then wake the secondary handler, which is waiting in pcie_wait_cmd(). The secondary handler uses a timeout of 1 sec to ensure forward progress in case the hardware never signals Command Completed (e.g. if the hotplug port itself was hot-removed).
In extreme cases, the primary handler may not run within 1 sec to wake the secondary handler. The secondary handler will then run into the timeout and issue an error message (but should otherwise react gracefully).
My point is that keeping both at the same priority by default provokes such situations more easily, so assigning a higher default priority to the primary handler would seem prudent.
Quoted 4 lines
Quoted 4 lines
+++
b/drivers/pci/pcie/aer.cdrivers/pci/pcie/aer.c +0
-0@@ -1671,7 +1671,8 @@ static int aer_probe(struct pcie_device *dev)set_service_data (dev, rpc);
Quoted 12 lines
drivers/pci/pcie/aer.c static int aer_probe(struct pcie_device *dev) · line 1675
Quoted 4 lines
drivers/pci/pcie/aer.c +2
-1status =devm_request_threaded_irq (device, dev->irq, aer_irq, aer_isr, -IRQF_SHARED ,"aerdrv" , dev); +IRQF_NO_THREAD |IRQF_SHARED , +"aerdrv" , dev);I'm not sure if this works with
IRQF_SHARED. Your primary handler isIRQF_SHARED+IRQF_NO_THREADand another shared handler which is forced-threaded will haveIRQF_SHARED+IRQF_ONESHOT.
If the core does not complain, all good. Worst case might be the shared ONESHOT lets your primary handler starve. It would be nice if you could check if you have shared handler here (I have no aer I three boxes I checked).
Yes, interrupt sharing can happen if the Root Port uses legacy INTx interrupts. In that case other port services such as hotplug, bandwidth control, PME or DPC may use the same interrupt.
Thanks,
Lukas
Quoted 19 lines · Lukas Wunner
Quoted 5 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Crystal Wood
+++
b/drivers/pci/pcie/aer.cdrivers/pci/pcie/aer.c +0
-0@@ -1671,7 +1671,8 @@ static int aer_probe(struct pcie_device *dev)set_service_data (dev, rpc);Quoted 12 lines
drivers/pci/pcie/aer.c static int aer_probe(struct pcie_device *dev) · line 1675
Quoted 4 lines
drivers/pci/pcie/aer.c +2
-1status =devm_request_threaded_irq (device, dev->irq, aer_irq, aer_isr, -IRQF_SHARED ,"aerdrv" , dev); +IRQF_NO_THREAD |IRQF_SHARED , +"aerdrv" , dev);I'm not sure if this works with
IRQF_SHARED. Your primary handler isIRQF_SHARED+IRQF_NO_THREADand another shared handler which is forced-threaded will haveIRQF_SHARED+IRQF_ONESHOT.
If the core does not complain, all good. Worst case might be the shared ONESHOT lets your primary handler starve. It would be nice if you could check if you have shared handler here (I have no aer I three boxes I checked).
Quoted 3 lines
Yes, interrupt sharing can happen if the Root Port uses legacy INTx interrupts. In that case other port services such as hotplug, bandwidth control, PME or DPC may use the same interrupt.
I should add that none of these other port service drivers use IRQF_ONESHOT. They're all IRQF_SHARED only.
Thanks,
Lukas
Sebastian Andrzej Siewior
maintainerQuoted 20 lines · Lukas Wunner
Quoted 19 lines · Lukas Wunner
Quoted 17 lines · Sebastian Andrzej Siewior
drivers/pci/pcie/aer.c static int aer_probe(struct pcie_device *dev) · line 1675
Quoted 8 lines · Crystal Wood
+++
b/drivers/pci/pcie/aer.cdrivers/pci/pcie/aer.c +2
-1@@ -1671,7 +1671,8 @@ static int aer_probe(struct pcie_device *dev)set_service_data (dev, rpc); status =devm_request_threaded_irq (device, dev->irq, aer_irq, aer_isr, -IRQF_SHARED ,"aerdrv" , dev); +IRQF_NO_THREAD |IRQF_SHARED , +"aerdrv" , dev);I'm not sure if this works with
IRQF_SHARED. Your primary handler isIRQF_SHARED+IRQF_NO_THREADand another shared handler which is forced-threaded will haveIRQF_SHARED+IRQF_ONESHOT.
If the core does not complain, all good. Worst case might be the shared ONESHOT lets your primary handler starve. It would be nice if you could check if you have shared handler here (I have no aer I three boxes I checked).
Quoted 6 lines
Quoted 3 lines
Yes, interrupt sharing can happen if the Root Port uses legacy INTx interrupts. In that case other port services such as hotplug, bandwidth control, PME or DPC may use the same interrupt.
I should add that none of these other port service drivers use
IRQF_ONESHOT. They're allIRQF_SHAREDonly.
Yes. But. If they get forced-threaded then we have a primary handler irq_default_primary_handler() as the hardirq handler. This one would be marked IRQF_ONESHOT. The other primary handler would be aer_irq() in this case. As long as all (or none) are forced-threaded then it is fine. In this case it could be a miss match, I'm not sure.
Quoted 3 lines
Thanks,
Lukas
Sebastian
Sebastian Andrzej Siewior
maintainerQuoted 4 lines · Lukas Wunner
Since v6.16, AER supports rate limiting. It's unclear which kernel version Crystal is using, but if it's older than v6.16, it may be worth retrying with a newer release to see if that solves the problem.
Where is this rate limiting coming from?
Quoted 8 lines
Quoted 3 lines
Another way would be to let the secondary handler run at a slightly lower priority than the primary handler. In this case making the primary non-threaded should not cause any harm.
Why isn't the secondary handler always assigned a lower priority by default? I think a lot of drivers are built on the assumption that the primary handler is scheduled sooner than the secondary handler.
Well, that is the first time I see that someone made that assumption.
Quoted 6 lines
E.g. the native PCIe hotplug driver (
drivers/pci/hotplug/pciehp_hpc.c) uses the primary handler to pick up Command Completed interrupts and will then wake the secondary handler, which is waiting inpcie_wait_cmd(). The secondary handler uses a timeout of 1 sec to ensure forward progress in case the hardware never signals Command Completed (e.g. if the hotplug port itself was hot-removed).
If it is waiting then everything is good. It would be only problematic if it busy-polls.
Quoted 8 lines
In extreme cases, the primary handler may not run within 1 sec to wake the secondary handler. The secondary handler will then run into the timeout and issue an error message (but should otherwise react gracefully).
My point is that keeping both at the same priority by default provokes such situations more easily, so assigning a higher default priority to the primary handler would seem prudent.
Okay but the secondary should be one less than the primary. The primary is in the middle priority "MAX_RT_PRIO / 2". It should not be preferred over other forced-threaded handler just because it has also a secondary handler. The secondary should run after all primary handler are done. This would also mirror the !RT case.
Quoted 17 lines
Quoted 4 lines
Quoted 4 lines
+++
b/drivers/pci/pcie/aer.cdrivers/pci/pcie/aer.c +0
-0@@ -1671,7 +1671,8 @@ static int aer_probe(struct pcie_device *dev)set_service_data (dev, rpc);Quoted 12 lines
drivers/pci/pcie/aer.c static int aer_probe(struct pcie_device *dev) · line 1675
Quoted 4 lines
drivers/pci/pcie/aer.c +2
-1status =devm_request_threaded_irq (device, dev->irq, aer_irq, aer_isr, -IRQF_SHARED ,"aerdrv" , dev); +IRQF_NO_THREAD |IRQF_SHARED , +"aerdrv" , dev);I'm not sure if this works with
IRQF_SHARED. Your primary handler isIRQF_SHARED+IRQF_NO_THREADand another shared handler which is forced-threaded will haveIRQF_SHARED+IRQF_ONESHOT.
If the core does not complain, all good. Worst case might be the shared ONESHOT lets your primary handler starve. It would be nice if you could check if you have shared handler here (I have no aer I three boxes I checked).
Quoted 3 lines
Yes, interrupt sharing can happen if the Root Port uses legacy INTx interrupts. In that case other port services such as hotplug, bandwidth control, PME or DPC may use the same interrupt.
So this sounds like it is not going to work then, or is it?
Quoted 3 lines
Thanks,
Lukas
Sebastian
Quoted 7 lines · Lukas Wunner
Quoted 6 lines · Sebastian Andrzej Siewior
Quoted 5 lines · Crystal Wood
On
PREEMPT_RT, currently bothaer_irqandaer_isrrun in separate threads, at the same FIFO priority. This can lead to theaer_isrthread starving theaer_irqthread, particularly ifmulti_error_validcauses a scan of all devices, and multiple errors are raised during the scan.
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
On !PREEMPT_RT, or if
aer_irqruns at a higher priority thanaer_isr, these errors can be queued as single-error events as they happen. But ifaer_irqcan't run untilaer_isrfinishes, by that time the multi event bit will be set again, causing a new scan and an infinite loop.So if
aer_irqis too slow we get new "work" pilled up? Is it because there is a timing constrains how long until the error needs to be acknowledged?
The error needs to be cleared before the next error happens, or else the hardware will set the "Multiple ERR_COR Received" bit. If that bit is set, then aer_isr can't rely on the error source ID register, so it scans through all devices looking for errors -- and for some reason, on this system, accessing the error registers (or any config space above 0x400, even though there are capabilities located there) generates an Unsupported Request Error (but returns valid data).
Since this happens more than once, without aer_irq preempting, it causes another multi error and we get stuck in a loop.
Quoted 4 lines
Since v6.16, AER supports rate limiting. It's unclear which kernel version Crystal is using, but if it's older than v6.16, it may be worth retrying with a newer release to see if that solves the problem.
The problem shows in top-of-tree. The messages are ratelimited, but the problem isn't from the messages. It still does the scan.
Quoted 8 lines
Quoted 3 lines
Another way would be to let the secondary handler run at a slightly lower priority than the primary handler. In this case making the primary non-threaded should not cause any harm.
Why isn't the secondary handler always assigned a lower priority by default? I think a lot of drivers are built on the assumption that the primary handler is scheduled sooner than the secondary handler.
That also works, and I agree it's more intuitive.
Quoted 17 lines
Quoted 4 lines
Quoted 4 lines
+++
b/drivers/pci/pcie/aer.cdrivers/pci/pcie/aer.c +0
-0@@ -1671,7 +1671,8 @@ static int aer_probe(struct pcie_device *dev)set_service_data (dev, rpc);Quoted 12 lines
drivers/pci/pcie/aer.c static int aer_probe(struct pcie_device *dev) · line 1675
Quoted 4 lines
drivers/pci/pcie/aer.c +2
-1status =devm_request_threaded_irq (device, dev->irq, aer_irq, aer_isr, -IRQF_SHARED ,"aerdrv" , dev); +IRQF_NO_THREAD |IRQF_SHARED , +"aerdrv" , dev);I'm not sure if this works with
IRQF_SHARED. Your primary handler isIRQF_SHARED+IRQF_NO_THREADand another shared handler which is forced-threaded will haveIRQF_SHARED+IRQF_ONESHOT.
If the core does not complain, all good. Worst case might be the shared ONESHOT lets your primary handler starve. It would be nice if you could check if you have shared handler here (I have no aer I three boxes I checked).
Quoted 3 lines
Yes, interrupt sharing can happen if the Root Port uses legacy INTx interrupts. In that case other port services such as hotplug, bandwidth control, PME or DPC may use the same interrupt.
It's shared, but with another explicitly threaded interrupt. This is with the patch applied:
root 778 0.0 0.0 0 0 ? S Sep02 0:00 [irq/87-aerdrv] root 779 0.0 0.0 0 0 ? S Sep02 0:00 [irq/87-pciehp] root 780 0.0 0.0 0 0 ? S Sep02 0:00 [irq/87-s-pciehp]
If it were shared with a oneshot irq (forced or otherwise) wouldn't that have already been a mismatch?
-Crystal
Quoted 6 lines · Crystal Wood
Quoted 5 lines · Lukas Wunner
Quoted 3 lines · Sebastian Andrzej Siewior
Another way would be to let the secondary handler run at a slightly lower priority than the primary handler. In this case making the primary non-threaded should not cause any harm.
Quoted 6 lines
Quoted 4 lines
Why isn't the secondary handler always assigned a lower priority by default? I think a lot of drivers are built on the assumption that the primary handler is scheduled sooner than the secondary handler.
That also works, and I agree it's more intuitive.
Could you test whether the patch below fixes the issue for you?
-- >8 --
include/linux/sched.h +1 -0
@@ -1894,6 +1894,7 @@ static inline int task_nice(const struct task_struct *p)extern int sched_setscheduler (struct task_struct *,int ,const struct sched_param *);extern int sched_setscheduler_nocheck (struct task_struct *,int ,const struct sched_param *);extern void sched_set_fifo (struct task_struct *p); +extern void sched_set_fifo_minus_one (struct task_struct *p);extern void sched_set_fifo_low (struct task_struct *p);extern void sched_set_normal (struct task_struct *p,int nice);extern int sched_setattr (struct task_struct *,const struct sched_attr *);
kernel/irq/manage.c +4 -1
@@ -1239,7 +1239,10 @@ static int irq_thread(void *data)irq_thread_set_ready (desc, action); -sched_set_fifo (current); +if (action->handler == irq_forced_secondary_handler) +sched_set_fifo_minus_one (current); +else +sched_set_fifo (current);if (force_irqthreads () &&test_bit (IRQTF_FORCED_THREAD , &action->thread_flags))
kernel/sched/syscalls.c +9 -0
@@ -847,6 +847,15 @@ void sched_set_fifo(struct task_struct *p)EXPORT_SYMBOL_GPL (sched_set_fifo);/* +* Secondary IRQ handler has slightly lower priority than primary IRQ handler. +*/ +void sched_set_fifo_minus_one (struct task_struct *p) +{ +struct sched_param sp = { .sched_priority =MAX_RT_PRIO /2 -1 }; +WARN_ON_ONCE (sched_setscheduler_nocheck (p,SCHED_FIFO , &sp) !=0 ); +} + +/* * For when you don't much care about FIFO, but want to be above SCHED_NORMAL. */ void sched_set_fifo_low (struct task_struct *p)
Sebastian Andrzej Siewior
maintainerkernel/sched/syscalls.c void sched_set_fifo(struct task_struct *p) · line 850
Quoted 7 lines · Lukas Wunner
kernel/sched/syscalls.c +1
-0@@ -847,6 +847,15 @@ void sched_set_fifo(struct task_struct *p)EXPORT_SYMBOL_GPL (sched_set_fifo);/* +* Secondary IRQ handler has slightly lower priority than primary IRQ handler.
For tasks that should be sched_set_fifo() but require not to preempt another sched_set_fifo() task.
Quoted 11 lines
kernel/sched/syscalls.c +8
-0+*/ +void sched_set_fifo_minus_one (struct task_struct *p) +{ +struct sched_param sp = { .sched_priority =MAX_RT_PRIO /2 -1 }; +WARN_ON_ONCE (sched_setscheduler_nocheck (p,SCHED_FIFO , &sp) !=0 ); +} + +/* * For when you don't much care about FIFO, but want to be above SCHED_NORMAL. */ void sched_set_fifo_low (struct task_struct *p)
Quoted 11 lines · Sebastian Andrzej Siewior
kernel/sched/syscalls.c void sched_set_fifo(struct task_struct *p) · line 850
Quoted 7 lines · Lukas Wunner
kernel/sched/syscalls.c +1
-0@@ -847,6 +847,15 @@ void sched_set_fifo(struct task_struct *p)EXPORT_SYMBOL_GPL (sched_set_fifo);/* +* Secondary IRQ handler has slightly lower priority than primary IRQ handler. For tasks that should be
sched_set_fifo()but require not to preempt anothersched_set_fifo()task.
SCHED_FIFO already means that tasks of the same priority won't preempt each other. I'd word it as:
For tasks that must be preemptible by a sched_set_fifo() task, such as to simulate the behavior of a non-PREEMPT_RT system where the sched_set_fifo() task is a hard interrupt.
-Crystal