Skip to contentSkip to conversation

PCI/CXL: Guard MMIO accesses during reset recovery

PATCH Superseded Richard Cheng 3 participants 8 messages Nov 6 – Nov 16

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover PCI/CXL: Guard MMIO accesses during reset recovery

PCI reset recovery may proceed with state restoration and driver reset_done() callbacks even when a device has not recovered. Accessing device MMIO in that state can cause additional errors, including DPC containment.

This series adds checks before two MMIO access paths: MSI-X table resoration and CXL HDM decoder checking.

Patch 1 checks the existing PCI Command register read in __pci_restore_msix_state(). If the read fails or returns all ones, warn and return before accessing the MSI-X table.

Patch 2 guards CXL decoder accesses after reset.
The CXL reset_done() callback independently accesses HDM decoder MMIO to check whether decoder state was lost during reset.

Changes since v1 [1]:
- Dropped v1 patch 1, since commit e8df1d194c39
("PCI: Don't treat ~0 as ready in pci_dev_wait() with RRS SV") already addresses the issue.
- Dropped v1 patch 2, following Benjamin Cheatham's review. That some devices rely on the existing fallback behavior.
- Reworked v1 patch 3 into v2 patch 1. Instead of skipping all PCI state restoration, check config accessibility before MSI-X restoration. This reduces the impact on other restoration paths.
- Retained v1 patch 4 as v2 patch 2.

Richard Cheng (2):
PCI/MSI: Skip MSI-X restoration when configuration is inaccessible cxl/pci: Guard decoder accesses after reset

 drivers/cxl/pci.c     | 6 ++++++
 drivers/pci/msi/msi.c | 6 +++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

base-commit: f66469d9905b39d2c260a15d7d6db450f4db67c1

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 PCI/MSI: Skip MSI-X restoration when configuration is inaccessible

PCI state restoration runs even when a reset fails. If the device's configuration space remains inaccessible, MSI-X restoration can issue table MMIO accesses that trigger additional errors, including DPC containment.

Check the existing PCI Command register read and skip MSI-X restoration if it fails or returns all ones. Preserve the existing handling of a valid Command register with Memory Space Enable clear, which temporarily enables memory decoding while restoring the MSI-X table.

Limit the change to MSI-X restoration. Leave other PCI state restoration, driver reset callbacks, and reset return values unchanged.

---
 drivers/pci/msi/msi.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/pci/msi/msi.c +5 -1
index 8a0133a6a286..37de31f4cfb6 100644
@@ -887,7 +887,11 @@ void __pci_restore_msix_state(struct pci_dev *dev)
 	 * Since the MSI-X Table and PBA are in Memory Space, enable it
 	 * while restoring them.
 	 */
-	pci_read_config_word(dev, PCI_COMMAND, &cmd);
+	if (pci_read_config_word(dev, PCI_COMMAND, &cmd) ||
+	    PCI_POSSIBLE_ERROR(cmd)) {
+		pci_warn(dev, "Device config space inaccessible; unable to restore MSI-X state\n");
+		return;
+	}
 	pci_write_config_word(dev, PCI_COMMAND, cmd | PCI_COMMAND_MEMORY);
 
 	write_msg = arch_restore_msi_irqs(dev);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/2] PCI/MSI: Skip MSI-X restoration when configuration is inaccessible

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

PCI state restoration runs even when a reset fails. If the device's configuration space remains inaccessible, MSI-X restoration can issue table MMIO accesses that trigger additional errors, including DPC containment.

Check the existing PCI Command register read and skip MSI-X restoration if it fails or returns all ones. Preserve the existing handling of a valid Command register with Memory Space Enable clear, which temporarily enables memory decoding while restoring the MSI-X table.

Quoted 4 lines

Limit the change to MSI-X restoration. Leave other PCI state restoration, driver reset callbacks, and reset return values unchanged.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v2 1/2] PCI/MSI: Skip MSI-X restoration when configuration is inaccessible

In reply to Richard Cheng
Quoted 10 lines · Richard Cheng

PCI state restoration runs even when a reset fails. If the device's configuration space remains inaccessible, MSI-X restoration can issue table MMIO accesses that trigger additional errors, including DPC containment.

Check the existing PCI Command register read and skip MSI-X restoration if it fails or returns all ones. Preserve the existing handling of a valid Command register with Memory Space Enable clear, which temporarily enables memory decoding while restoring the MSI-X table.

Quoted 4 lines

Limit the change to MSI-X restoration. Leave other PCI state restoration, driver reset callbacks, and reset return values unchanged.

Just curious, why are we limiting this to only MSI-X and not MSI? I think MSI restoration will fail as well since the config space is not accessible.

Quoted 9 lines
---
  drivers/pci/msi/msi.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/pci/msi/msi.c +0 -0
index 8a0133a6a286..37de31f4cfb6 100644

drivers/pci/msi/msi.c void __pci_restore_msix_state(struct pci_dev *dev) · line 894

Quoted 9 lines
drivers/pci/msi/msi.c +5 -1
@@ -887,7 +887,11 @@ void __pci_restore_msix_state(struct pci_dev *dev)
  	 * Since the MSI-X Table and PBA are in Memory Space, enable it
  	 * while restoring them.
  	 */
-	pci_read_config_word(dev, PCI_COMMAND, &cmd);
+	if (pci_read_config_word(dev, PCI_COMMAND, &cmd) ||
+	    PCI_POSSIBLE_ERROR(cmd)) {
+		pci_warn(dev, "Device config space inaccessible; unable to restore MSI-X state\n");
+		return;
+	}

We have a helper function pci_dev_config_accessible() that does the same check, so might be appropriate to use (of course we would have to make the function global rather than static).

And maybe the check should be moved up before any config space access in this function?

Thanks

Farhan

Quoted 3 lines
drivers/pci/msi/msi.c +0 -0
  	pci_write_config_word(dev, PCI_COMMAND, cmd | PCI_COMMAND_MEMORY);
 
  	write_msg = arch_restore_msi_irqs(dev);
#4 lore raw

Re: [PATCH v2 1/2] PCI/MSI: Skip MSI-X restoration when configuration is inaccessible

Quoted 12 lines · Farhan Ali
Quoted 10 lines · Richard Cheng

PCI state restoration runs even when a reset fails. If the device's configuration space remains inaccessible, MSI-X restoration can issue table MMIO accesses that trigger additional errors, including DPC containment.

Check the existing PCI Command register read and skip MSI-X restoration if it fails or returns all ones. Preserve the existing handling of a valid Command register with Memory Space Enable clear, which temporarily enables memory decoding while restoring the MSI-X table.

Quoted 8 lines
Quoted 4 lines

Limit the change to MSI-X restoration. Leave other PCI state restoration, driver reset callbacks, and reset return values unchanged.

Just curious, why are we limiting this to only MSI-X and not MSI? I think MSI restoration will fail as well since the config space is not accessible.

Hi Farhan,

The orginal intention was to keep the change small, and the failure I hit was DPC triggered by the MSI-X table writes, which are MMIO, I didn't expect MSI to cause the same error since it's programmed entirely in config space, so I left it alone.

But I think you're right, the MSI restoration is equally pointless when config space is inaccessible, and it ends up writing back values derived from all-ones reads. I'll cover both MSI and MSI-X in v3.

Quoted 14 lines
Quoted 14 lines
---
  drivers/pci/msi/msi.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/pci/msi/msi.c +1 -1
index 8a0133a6a286..37de31f4cfb6 100644
@@ -887,7 +887,11 @@ void __pci_restore_msix_state(struct pci_dev *dev)
  	 * Since the MSI-X Table and PBA are in Memory Space, enable it
  	 * while restoring them.
  	 */
-	pci_read_config_word(dev, PCI_COMMAND, &cmd);
+	if (pci_read_config_word(dev, PCI_COMMAND, &cmd) ||
Quoted 9 lines

drivers/pci/msi/msi.c void __pci_restore_msix_state(struct pci_dev *dev) · line 894

Quoted 4 lines
drivers/pci/msi/msi.c +4 -0
+	    PCI_POSSIBLE_ERROR(cmd)) {
+		pci_warn(dev, "Device config space inaccessible; unable to restore MSI-X state\n");
+		return;
+	}

We have a helper function pci_dev_config_accessible() that does the same check, so might be appropriate to use (of course we would have to make the function global rather than static).

Agree, I'll make it non-static in v3.

Quoted 7 lines

And maybe the check should be moved up before any config space access in this function?

Thanks

Farhan

Yes, will do, this will be covered in v3.

Thanks for the review.

Best regards,
Richard Cheng.

Quoted 3 lines
Quoted 3 lines
drivers/pci/msi/msi.c +0 -0
  	pci_write_config_word(dev, PCI_COMMAND, cmd | PCI_COMMAND_MEMORY);
  	write_msg = arch_restore_msi_irqs(dev);
#5 lore raw

PATCH v2 2/2 cxl/pci: Guard decoder accesses after reset

In reply to Richard Cheng

cxl_reset_done() runs even when PCI state restoration is skipped. Reading HDM decoder registers while the device is inaccessible can trigger another error.

Skip decoder checks if the PCI Command read fails, returns all ones, or Memory Space Enable is clear.

---
 drivers/cxl/pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/cxl/pci.c +6 -0
index 3ed7c3e70042..ca9e2bb505c8 100644
@@ -1106,6 +1106,12 @@ static void cxl_reset_done(struct pci_dev *pdev)
 	struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
 	struct cxl_memdev *cxlmd = cxlds->cxlmd;
 	struct device *dev = &pdev->dev;
+	u32 command;
+
+	/* PCI state restoration may have been skipped after a failed reset. */
+	if (pci_read_config_dword(pdev, PCI_COMMAND, &command) ||
+	    PCI_POSSIBLE_ERROR(command) || !(command & PCI_COMMAND_MEMORY))
+		return;
 
 	/*
 	 * FLR does not expect to touch the HDM decoders and related
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v2 2/2] cxl/pci: Guard decoder accesses after reset

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] cxl/pci: missing Fixes trailer for cxl_reset_done() bug fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] cxl/pci: missing Fixes trailer for cxl_reset_done() bug fix --

commit d141a119957d4d69fa73f6cabf565bbf7fc50833
Author: Richard Cheng <[email protected]>

Quoted 11 lines

cxl/pci: Guard decoder accesses after reset

cxl_reset_done() runs even when PCI state restoration is skipped. Reading HDM decoder registers while the device is inaccessible can trigger another error.

Skip decoder checks if the PCI Command read fails, returns all ones, or Memory Space Enable is clear.

[Severity: Low]
This fixes an unconditional post-reset HDM decoder MMIO read in cxl_reset_done(). Should this include:

Fixes 934edcd436dc ("cxl: Add post-reset warning if reset results in loss of previously committed HDM decoders")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw
In reply to Richard Cheng
Quoted 16 lines · Richard Cheng

PCI reset recovery may proceed with state restoration and driver reset_done() callbacks even when a device has not recovered. Accessing device MMIO in that state can cause additional errors, including DPC containment.

This series adds checks before two MMIO access paths: MSI-X table resoration and CXL HDM decoder checking.

Patch 1 checks the existing PCI Command register read in __pci_restore_msix_state(). If the read fails or returns all ones, warn and return before accessing the MSI-X table.

Patch 2 guards CXL decoder accesses after reset.
The CXL reset_done() callback independently accesses HDM decoder MMIO to check whether decoder state was lost during reset.

Quoted 12 lines

Changes since v1 [1]:
- Dropped v1 patch 1, since commit e8df1d194c39
("PCI: Don't treat ~0 as ready in pci_dev_wait() with RRS SV") already addresses the issue.
- Dropped v1 patch 2, following Benjamin Cheatham's review. That some devices rely on the existing fallback behavior.
- Reworked v1 patch 3 into v2 patch 1. Instead of skipping all PCI state restoration, check config accessibility before MSI-X restoration. This reduces the impact on other restoration paths.
- Retained v1 patch 4 as v2 patch 2.

Sorry forgot to attach the link for v1:

https://lore.kernel.org/all/[email protected]/

Best regards,
Richard Cheng.

Quoted 13 lines

Richard Cheng (2):
PCI/MSI: Skip MSI-X restoration when configuration is inaccessible cxl/pci: Guard decoder accesses after reset

 drivers/cxl/pci.c     | 6 ++++++
 drivers/pci/msi/msi.c | 6 +++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

base-commit: f66469d9905b39d2c260a15d7d6db450f4db67c1

Signature · 1 line
2.43.0