PCI: pciehp: Re-read Slot Control when cached HPIE is clear
PATCH 1/1 PCI: pciehp: Re-read Slot Control when cached HPIE is clear
pciehp_isr() dismisses an interrupt as not ours (IRQ_NONE) when the Hot-Plug Interrupt Enable (HPIE) bit is clear in ctrl->slot_ctrl, the driver's cached copy of the Slot Control register.
This can drop hotplug events during PCIe hot-add. On an AMD EPYC platform, sometimes hot-adding an PCIe device delivered only the Presence Detect Changed (PDC) event while the Data Link Layer State Changed (DLSC) event was lost. Afterwards lspci still showed the Link State Changed bit latched in Slot Status, i.e. the DLSC interrupt was never serviced:
pcieport 0000:c0:03.4: pciehp: pending interrupts 0x0008 from Slot Status pcieport 0000:c0:03.4: pciehp: Slot(71): Card present
lspci -vvv after hot-add:
SltCtl: Enable: ... PresDet+ HPIrq+ LinkChg+
SltSta: Changed: MRL- PresDet- LinkState+
Only PDC (Slot Status 0x0008) was reported; LinkState+ (DLSC) stayed latched and unhandled. This will affect the device's next hot-plug operation, causing the state machine to become inconsistent.
The root cause is a stale cache. Platform firmware transiently clears HPIE in hardware and restores it shortly afterwards. While handling the PDC event the driver updates the slot indicators via pciehp_set_indicators(), which issues a Slot Control read-modify-write (pcie_do_write_cmd()). If that read occurs while firmware has HPIE cleared, HPIE=0 is read back and stored into ctrl->slot_ctrl. Firmware then restores HPIE=1 in hardware, but the cached HPIE=0 remains, so the following DLSC interrupt is misjudged as not ours and dropped.
The hardware HPIE bit is correct in this case, so consult hardware rather than the possibly stale cache. To keep the fast path cheap and avoid any behavioural change for low-power ports, only re-read Slot Control when the cache says HPIE is disabled and the port is in D0 (accessible and enabled). In a lower-power state the port may be inaccessible, so the cached value remains authoritative and the existing early return is preserved. Surprise removal is handled via PCI_POSSIBLE_ERROR().
This changes behaviour only in the previously broken case (D0, cached HPIE clear but hardware HPIE set). Poll mode, the shared-INTx early return while suspended, and the D3hot deferral to the IRQ thread are unchanged.
--- drivers/pci/hotplug/pciehp_hpc.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-)
drivers/pci/hotplug/pciehp_hpc.c +25 -4
@@ -628,13 +628,34 @@ static irqreturn_t pciehp_isr(int irq, void *dev_id)u16 status, events =0 ;/* -* Interrupts only occur in D3hot or shallower and only if enabled -* in the Slot Control register (PCIe r4.0, sec 6.7.3.4). +* Interrupts only occur in D3hot or shallower (PCIe r4.0, sec 6.7.3.4). */ -if (pdev->current_state == PCI_D3cold || - (!(ctrl->slot_ctrl &PCI_EXP_SLTCTL_HPIE ) && !pciehp_poll_mode)) +if (pdev->current_state == PCI_D3cold)return IRQ_NONE ; +/* +* Interrupts are only sent if enabled in the Slot Control register +* (PCIe r4.0, sec 6.7.3.4). ctrl->slot_ctrl caches that register, but +* the cached Hot-Plug Interrupt Enable bit can fall transiently out of +* sync with the hardware if platform firmware clears it behind the +* driver's back (e.g. concurrently with a Slot Control read-modify- +* write). While the port is in D0 it is accessible, so re-read the +* register from hardware before dismissing the interrupt as not ours. +* In a low-power state the port may be inaccessible and the cached +* value is authoritative. +*/ +if (!pciehp_poll_mode && !(ctrl->slot_ctrl &PCI_EXP_SLTCTL_HPIE )) { +u16 slot_ctrl; + +if (pdev->current_state !=PCI_D0 ) +return IRQ_NONE ; + +pcie_capability_read_word (pdev,PCI_EXP_SLTCTL , &slot_ctrl); +if (PCI_POSSIBLE_ERROR (slot_ctrl) || + !(slot_ctrl &PCI_EXP_SLTCTL_HPIE )) +return IRQ_NONE ; + } +/* * Keep the port accessible by holding a runtime PM ref on its parent. * Defer resume of the parent to the IRQ thread if it's suspended.
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] pci: TOCTOU race in config space read leading to master abort --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] pci: TOCTOU race in config space read leading to master abort
--
commit 7ceb468446bd25d57acbe78d0b28b124ca7b5902
Author: Zhu Qiyu <[email protected]>
PCI: pciehp: Re-read Slot Control when cached HPIE is clear
This commit addresses missed hot-plug events by re-reading the Slot Control register directly from hardware when the cached Hot-Plug Interrupt Enable (HPIE) bit is clear and the port is in the D0 state.
Quoted 4 lines
drivers/pci/hotplug/pciehp_hpc.c +0
-0
[ ... ]
Quoted 21 lines
drivers/pci/hotplug/pciehp_hpc.c +10
-4@@ -628,13 +628,34 @@ static irqreturn_t pciehp_isr(int irq, void *dev_id)u16 status, events =0 ;/* -* Interrupts only occur in D3hot or shallower and only if enabled -* in the Slot Control register (PCIe r4.0, sec 6.7.3.4). +* Interrupts only occur in D3hot or shallower (PCIe r4.0, sec 6.7.3.4). */ -if (pdev->current_state == PCI_D3cold || - (!(ctrl->slot_ctrl &PCI_EXP_SLTCTL_HPIE ) && !pciehp_poll_mode)) +if (pdev->current_state == PCI_D3cold)return IRQ_NONE ; +/* +* Interrupts are only sent if enabled in the Slot Control register +* (PCIe r4.0, sec 6.7.3.4). ctrl->slot_ctrl caches that register, but +* the cached Hot-Plug Interrupt Enable bit can fall transiently out of +* sync with the hardware if platform firmware clears it behind the +* driver's back (e.g. concurrently with a Slot Control read-modify- +* write). While the port is in D0 it is accessible, so re-read the +* register from hardware before dismissing the interrupt as not ours.
drivers/pci/hotplug/pciehp_hpc.c static irqreturn_t pciehp_isr(int irq, void *dev_id) · line 653
Quoted 10 lines
drivers/pci/hotplug/pciehp_hpc.c +10
-0+* In a low-power state the port may be inaccessible and the cached +* value is authoritative. +*/ +if (!pciehp_poll_mode && !(ctrl->slot_ctrl &PCI_EXP_SLTCTL_HPIE )) { +u16 slot_ctrl; + +if (pdev->current_state !=PCI_D0 ) +return IRQ_NONE ; + +pcie_capability_read_word (pdev,PCI_EXP_SLTCTL , &slot_ctrl);
[Severity: High]
Does this introduce a Time-of-Check to Time-of-Use (TOCTOU) race condition when reading the config space?
A concurrent pm_runtime_suspend operation powering down the device can race with pciehp_isr receiving an interrupt.
The lockless pdev->current_state != PCI_D0 check could pass just before the state is updated, leading to an unprotected pcie_capability_read_word after the device becomes inaccessible.
Reading the PCI config space of a suspended device can trigger an Unsupported Request (UR) or Completion Timeout. On some platforms (e.g. ARM64), this does not gracefully return 0xFFFF but instead triggers a Synchronous External Abort (SEA), leading to a kernel panic.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 PCI: pciehp: Avoid dropping hotplug interrupts due to stale HPIE
In reply to Zhu Qiyupciehp_isr() uses the Hot-Plug Interrupt Enable (HPIE) bit in ctrl->slot_ctrl to reject interrupts when hotplug interrupts are disabled. This check is needed because hotplug can share an interrupt with other sources, including native PME.
However, ctrl->slot_ctrl is updated from hardware on every Slot Control Register read-modify-write. A firmware change to HPIE can race with an unrelated driver operation, such as an LED indicator update:
1. The driver enables hotplug interrupts, setting both hardware HPIE and the cached HPIE bit to 1.
2. Firmware temporarily clears hardware HPIE; the cached bit remains 1.
3. pcie_do_write_cmd() reads Slot Control for an indicator update and observes HPIE=0. Since the command's mask excludes HPIE, it preserves the value read from hardware.
4. The driver updates ctrl->slot_ctrl and writes Slot Control. Both hardware and cached HPIE are now 0, although the driver did not explicitly disable hotplug interrupts.
5. Firmware restores hardware HPIE to 1 after the driver's write, but the cached bit remains 0.
6. A subsequent hotplug interrupt reaches pciehp_isr(), which sees cached HPIE=0 and returns IRQ_NONE without reading or clearing Slot Status, leaving the event pending.
ctrl_lock serializes driver commands, but cannot prevent this race because firmware does not acquire it.
Track the driver's HPIE setting separately. Update it under ctrl_lock only when the command's mask includes HPIE, before writing Slot Control. Use this setting in the ISR so unrelated hardware readbacks cannot override the driver's enable/disable state. This adds no configuration space accesses to the ISR and leaves the register writes and runtime PM flow unchanged.
Wait for command completion interrupts only when the software HPIE setting and the cached HPIE and CCIE bits are all set; otherwise poll. Also check cmd_busy when polling to recognize completions already consumed by the ISR.
This prevents stale-cache rejection of delivered interrupts, but does not restore hardware interrupt delivery. If firmware restores HPIE before the driver's write, that write may clear it again.
---
Changes in v2:
- Replace the Slot Control re-read in the ISR with a separate software HPIE setting. A parent runtime PM reference does not prevent the port itself from entering D3cold, so it cannot guarantee that the added configuration access is safe.
- Update the setting only for explicit HPIE commands, under ctrl_lock, using READ_ONCE()/WRITE_ONCE() for concurrent access. Keep the early interrupt-disable check and the existing ISR runtime PM flow.
- Require the software setting as well as cached HPIE/CCIE for command completion interrupt waits. Check cmd_busy when polling to recognize completions already consumed by the ISR.
- Explain the race step by step and clarify that the fix does not restore hardware interrupt delivery.
- Retitle the patch to describe the failure rather than the solution.
drivers/pci/hotplug/pciehp.h | 3 +++ drivers/pci/hotplug/pciehp_hpc.c | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-)
drivers/pci/hotplug/pciehp.h +3 -0
@@ -54,6 +54,8 @@ extern int pciehp_poll_time;* controller and disabled per spec recommendation (PCIe r5.0, appendix I * implementation note) * @slot_ctrl: cached copy of the Slot Control register +* @hpie_enabled: driver's Hot-Plug Interrupt Enable setting, updated only +* by commands that explicitly change HPIE, not by hardware readback * @ctrl_lock: serializes writes to the Slot Control register * @cmd_started: jiffies when the Slot Control register was last written; * the next write is allowed 1 second later, absent a Command Completed
@@ -96,6 +98,7 @@ struct controller { unsigned int inband_presence_disabled:1;u16 slot_ctrl;/* control register access */ +bool hpie_enabled;struct mutex ctrl_lock;unsigned long cmd_started; unsigned int cmd_busy:1;
drivers/pci/hotplug/pciehp_hpc.c +14 -4
@@ -89,6 +89,10 @@ static int pcie_poll_cmd(struct controller *ctrl, int timeout)u16 slot_status; do { + /* The IRQ handler may have consumed the completion event. */ + if (!ctrl->cmd_busy) + return 1; + pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status); if (PCI_POSSIBLE_ERROR(slot_status)) { ctrl_info(ctrl, "%s: no response from device\n",
@@ -106,7 +110,7 @@ static int pcie_poll_cmd(struct controller *ctrl, int timeout)msleep (10 ); timeout -=10 ; }while (timeout >=0 ); -return 0 ;/* timeout */ +return !ctrl->cmd_busy; }static void pcie_wait_cmd (struct controller *ctrl)
@@ -137,7 +141,8 @@ static void pcie_wait_cmd(struct controller *ctrl)else timeout = cmd_timeout - now; -if (ctrl->slot_ctrl &PCI_EXP_SLTCTL_HPIE && +if (READ_ONCE (ctrl->hpie_enabled) && + ctrl->slot_ctrl &PCI_EXP_SLTCTL_HPIE && ctrl->slot_ctrl &PCI_EXP_SLTCTL_CCIE ) rc =wait_event_timeout (ctrl->queue, !ctrl->cmd_busy, timeout);else
@@ -179,6 +184,9 @@ static void pcie_do_write_cmd(struct controller *ctrl, u16 cmd, ctrl->cmd_busy =1 ;smp_mb (); ctrl->slot_ctrl = slot_ctrl; +/* Do not let unrelated read-modify-writes change the IRQ setting. */ +if (mask &PCI_EXP_SLTCTL_HPIE ) +WRITE_ONCE (ctrl->hpie_enabled, !!(cmd &PCI_EXP_SLTCTL_HPIE ));pcie_capability_write_word (pdev,PCI_EXP_SLTCTL , slot_ctrl); ctrl->cmd_started = jiffies;
@@ -629,10 +637,12 @@ static irqreturn_t pciehp_isr(int irq, void *dev_id)/* * Interrupts only occur in D3hot or shallower and only if enabled -* in the Slot Control register (PCIe r4.0, sec 6.7.3.4). +* in the Slot Control register (PCIe r4.0, sec 6.7.3.4). Use the +* driver's HPIE setting, not a cached hardware value which may be +* affected by firmware changes to Slot Control. */ if (pdev->current_state == PCI_D3cold || - (!(ctrl->slot_ctrl &PCI_EXP_SLTCTL_HPIE ) && !pciehp_poll_mode)) + (!READ_ONCE (ctrl->hpie_enabled) && !pciehp_poll_mode))return IRQ_NONE ;/*
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2] PCI: pciehp: Avoid dropping hotplug interrupts due to stale HPIE
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 20 lines
pciehp_isr()uses the Hot-Plug Interrupt Enable (HPIE) bit inctrl->slot_ctrlto reject interrupts when hotplug interrupts are disabled. This check is needed because hotplug can share an interrupt with other sources, including native PME.However,
ctrl->slot_ctrlis updated from hardware on every Slot Control Register read-modify-write. A firmware change to HPIE can race with an unrelated driver operation, such as an LED indicator update:1. The driver enables hotplug interrupts, setting both hardware HPIE and the cached HPIE bit to 1.
2. Firmware temporarily clears hardware HPIE; the cached bit remains 1.
3.pcie_do_write_cmd()reads Slot Control for an indicator update and observes HPIE=0. Since the command's mask excludes HPIE, it preserves the value read from hardware.
[ ... ]
This prevents stale-cache rejection of delivered interrupts, but does not restore hardware interrupt delivery. If firmware restores HPIE before the driver's write, that write may clear it again.
Quoted 1 line
Signed-off-by Zhu Qiyu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2] PCI: pciehp: Avoid dropping hotplug interrupts due to stale HPIE
In reply to Zhu QiyuQuoted 8 lines · Zhu Qiyu
pciehp_isr()uses the Hot-Plug Interrupt Enable (HPIE) bit inctrl->slot_ctrlto reject interrupts when hotplug interrupts are disabled. This check is needed because hotplug can share an interrupt with other sources, including native PME.However,
ctrl->slot_ctrlis updated from hardware on every Slot Control Register read-modify-write. A firmware change to HPIE can race with an unrelated driver operation, such as an LED indicator update:
Firmware is not allowed to change the Slot Control Register once it has granted control of PCI Express Native Hot Plug Control to the operating system.
From PCIe r3.3 table 4-6 bit 0:
"Additionally, after control is transferred to the operating system,
firmware must not update the state of hot plug slots, including the state of the indicators and power controller."
Are you seeing this on a real-world system or is it an LLM-generated hallucination?
If the former, please consider fixing this through a firmware update.
Thanks,
Lukas
Re: [PATCH v2] PCI: pciehp: Avoid dropping hotplug interrupts due to stale HPIE
Quoted 1 line · Lukas Wunner
From PCIe r3.3 table 4-6 bit 0:
Sorry, this should have been PCIe *Firmware* r3.3.
Re: [PATCH v2] PCI: pciehp: Avoid dropping hotplug interrupts due to stale HPIE
In reply to Lukas WunnerHi Lukas,
Quoted 2 lines
Are you seeing this on a real-world system or is it an LLM-generated hallucination?
Yes, this was observed on a real system during NVMe hot-add:
System: AMD EPYC 9554
Port: 0000:c0:03.4, slot 71
Our PMFW temporarily clears HPIE and sets it again later, with the intent of suppressing and then re-enabling DLSC interrupt notifications. The previously mentioned race condition prevents the DLSC event from being processed correctly, resulting in a failure to clear certain status flags, which in turn affects the subsequent hot-plug state machine.
Only PDC event, no DLSC event:
[Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: pciehp: pending interrupts 0x0008 from Slot Status [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: pciehp: pciehp_check_link_active: lnk_status = 5041 [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: pciehp: Slot(71): Card present [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: pciehp: pciehp_set_indicators: SLOTCTRL 70 write cmd 200 [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: pciehp: pciehp_check_link_status: lnk_status = f045 [Thu May 14 11:30:46 2026] pci 0000:c7:00.0: [144d:a900] type 00 class 0x010802 [Thu May 14 11:30:46 2026] pci 0000:c7:00.0: reg 0x10: [mem 0x00000000-0x00007fff 64bit] [Thu May 14 11:30:46 2026] pci 0000:c7:00.0: Max Payload Size set to 512 (was 128, max 512) [Thu May 14 11:30:46 2026] pci 0000:c7:00.0: Adding to iommu group 54 [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: bridge window [io 0x1000-0x0fff] to [bus c7-c8] add_size 1000 [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: BAR 13: no space for [io size 0x1000] [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: BAR 13: failed to assign [io size 0x1000] [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: BAR 13: no space for [io size 0x1000] [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: BAR 13: failed to assign [io size 0x1000] [Thu May 14 11:30:46 2026] pci 0000:c7:00.0: BAR 0: assigned [mem 0xba300000-0xba307fff 64bit] [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: PCI bridge to [bus c7-c8] [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: bridge window [mem 0xba300000-0xba3fffff] [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: bridge window [mem 0x600e1600000-0x600e17fffff 64bit pref] [Thu May 14 11:30:46 2026] nvme nvme3: pci function 0000:c7:00.0 [Thu May 14 11:30:46 2026] nvme 0000:c7:00.0: enabling device (0000 -> 0002) [Thu May 14 11:30:46 2026] pcieport 0000:c0:03.4: pciehp: pciehp_set_indicators: SLOTCTRL 70 write cmd 1c0 [Thu May 14 11:30:48 2026] nvme nvme3: Shutdown timeout set to 8 seconds [Thu May 14 11:30:48 2026] nvme nvme3: 256/0/0 default/read/poll queues [Thu May 14 11:30:48 2026] nvme3n1: p1 p2
after hot-plug, run lspci -vvv and find that the Linkstate+
DevCap: MaxPayload 512 bytes, PhantFunc 0
ExtTag+ RBE+
DevCtl: CorrErr+ NonFatalErr+ FatalErr+ UnsupReq-
RlxdOrd+ ExtTag+ PhantFunc- AuxPwr- NoSnoop+
MaxPayload 512 bytes, MaxReadReq 512 bytes
DevSta: CorrErr- NonFatalErr- FatalErr- UnsupReq- AuxPwr- TransPend- LnkCap: Port #0, Speed 32GT/s, Width x4, ASPM L1, Exit Latency L1 <64us
ClockPM- Surprise+ LLActRep+ BwNot+ ASPMOptComp+
LnkCtl: ASPM Disabled; RCB 64 bytes, Disabled- CommClk+
ExtSynch- ClockPM- AutWidDis- BWInt- AutBWInt-
LnkSta: Speed 32GT/s (ok), Width x4 (ok)
TrErr- Train- SlotClk+ DLActive+ BWMgmt+ ABWMgmt+
SltCap: AttnBtn- PwrCtrl- MRL- AttnInd+ PwrInd+ HotPlug+ Surprise-
Slot #71, PowerLimit 75.000W; Interlock- NoCompl+
SltCtl: Enable: AttnBtn- PwrFlt- MRL- PresDet+ CmdCplt- HPIrq+ LinkChg+ Control: AttnInd Off, PwrInd On, Power- Interlock- SltSta: Status: AttnBtn- PowerFlt- MRL- CmdCplt- PresDet+ Interlock- Changed: MRL- PresDet- LinkState+
Quoted 3 lines
Firmware is not allowed to change the Slot Control Register once it has granted control of PCI Express Native Hot Plug Control to the operating system.
Thanks for pointing this out. I checked the boot log: the host bridge covering the affected port does grant native hotplug control to the OS. The relevant lines, with timestamps omitted, are:
ACPI: PCI Root Bridge [S1D1] (domain 0000 [bus c0-df])
acpi PNP0A08:05: _OSC: platform does not support [AER LTR DPC]
acpi PNP0A08:05: _OSC: OS now controls [PCIeHotplug SHPCHotplug PME PCIeCapability]
There is no pcie_ports=native override in the boot command line. The same boot also reports:
GHES: APEI firmware first mode is enabled by APEI bit.
That refers to APEI/GHES error handling, not hotplug ownership. Firmware-first error handling does not justify changing Slot Control after the native hotplug handoff.
The runtime log shows PDC on insertion, followed by successful NVMe enumeration and queue setup. A separate instrumented capture shows the ISR returning early with cached HPIE clear. These captures do not trace the complete firmware/driver write sequence, so I should have made that evidence boundary clearer in the commit message.
I will follow up with the firmware team on why the HPIE masking logic runs with native hotplug enabled, and pursue a firmware-side correction. Please hold off on this patch while we investigate that first.
Thanks,
Qiyu