Skip to contentSkip to conversation

PCI/CXL: Guard device accesses during reset recovery

PATCH In review Richard Cheng 3 participants 9 messages Nov 16 – Nov 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover PCI/CXL: Guard device 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 the device in that state can cause additional errors, including DPC.

This series adds checks before two such access paths: MSI/MSI-X state restoration and CXL HDM decoder checking.

Patch 1 checks pci_dev_config_accessible() at the start of __pci_restore_msi_state() and __pci_restore_msix_state(). If configuration space is inaccessible, warn and return before touching the device. The DPC containment was observed on the MSI-X table writes, which are MMIO. MSI is programmed through configuration space only, but restoring it on an inaccessible device is equally pointless, so both are covered.

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.

version 2's lore link is at
https://lore.kernel.org/all/[email protected]/

Best regards,
Richard Cheng.

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

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

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/2 PCI/MSI: Skip MSI/MSI-X restoration when config space is inaccessible

PCI state restoration runs even when a reset fails. If the device's config space remains inaccessible, MSI-X restoration still writes the MSI-X table. The table lives in device memory space, and these MMIO accesses can trigger additional errors, including DPC.

MSI is programmed entirely through config space, so it doesn't issue such MMIO accesses. Restoring it is still pointless when config space is
inaccessible: reads return all ones and the restore path writes back values derived from them.

Check whether config space of pci device is accessible or not at the start of both MSI and MSI-X path, after msi_enabled/msix_enabled test and before any config access, and skip the restoration if the device is inaccessible.
Make the helper non-static and declare it in pci.h so the MSI code can use it, too.

---
Changelog:

v2 -> v3:
- Cover MSI restoration path as well (Farhan)
- Use pci_dev_config_accessible() instead of open-coding the check, and make it non-static (Farhan)
- Move the check before any config space access in the restore functions (Farhan)
- The return value of the PCI Command read is no longer checked, since the helper only tests for an all-ones value.
- Update the subject and commit message accordingly

---
 drivers/pci/msi/msi.c | 6 ++++++
 drivers/pci/pci.c     | 2 +-
 drivers/pci/pci.h     | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/pci/msi/msi.c +6 -0
index 8a0133a6a286..2865f73f5ead 100644
@@ -524,6 +524,9 @@ void __pci_restore_msi_state(struct pci_dev *dev)
 	if (!dev->msi_enabled)
 		return;
 
+	if (!pci_dev_config_accessible(dev, "restore MSI state"))
+		return;
+
 	entry = irq_get_msi_desc(dev->irq);
 
 	pci_intx_for_msi(dev, 0);
@@ -877,6 +880,9 @@ void __pci_restore_msix_state(struct pci_dev *dev)
 	if (!dev->msix_enabled)
 		return;
 
+	if (!pci_dev_config_accessible(dev, "restore MSI-X state"))
+		return;
+
 	/* route the table */
 	pci_intx_for_msi(dev, 0);
 	pci_msix_clear_and_set_ctrl(dev, 0,
drivers/pci/pci.c +1 -1
index 7c6f2aba8144..671ffe4da1c8 100644
@@ -714,7 +714,7 @@ u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec)
 }
 EXPORT_SYMBOL_GPL(pci_find_dvsec_capability);
 
-static bool pci_dev_config_accessible(struct pci_dev *dev, char *msg)
+bool pci_dev_config_accessible(struct pci_dev *dev, char *msg)
 {
 	u32 val;
 
drivers/pci/pci.h +1 -0
index 77efa4c5c960..98dd3dbd5edb 100644
@@ -240,6 +240,7 @@ enum pci_mmap_api {
 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vmai,
 		  enum pci_mmap_api mmap_api);
 
+bool pci_dev_config_accessible(struct pci_dev *dev, char *msg);
 bool pci_reset_supported(struct pci_dev *dev);
 void pci_init_reset_methods(struct pci_dev *dev);
 int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 1/2] PCI/MSI: Skip MSI/MSI-X restoration when config space 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 config space remains inaccessible, MSI-X restoration still writes the MSI-X table. The table lives in device memory space, and these MMIO accesses can trigger additional errors, including DPC.

MSI is programmed entirely through config space, so it doesn't issue such MMIO accesses. Restoring it is still pointless when config space is
inaccessible: reads return all ones and the restore path writes back values derived from them.

Quoted 8 lines

Check whether config space of pci device is accessible or not at the start of both MSI and MSI-X path, after msi_enabled/msix_enabled test and before any config access, and skip the restoration if the device is inaccessible.
Make the helper non-static and declare it in pci.h so the MSI code can use it, too.

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 v3 1/2] PCI/MSI: Skip MSI/MSI-X restoration when config space 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 config space remains inaccessible, MSI-X restoration still writes the MSI-X table. The table lives in device memory space, and these MMIO accesses can trigger additional errors, including DPC.

MSI is programmed entirely through config space, so it doesn't issue such MMIO accesses. Restoring it is still pointless when config space is
inaccessible: reads return all ones and the restore path writes back values derived from them.

Quoted 4 lines

Check whether config space of pci device is accessible or not at the start of both MSI and MSI-X path, after msi_enabled/msix_enabled test and before any config access, and skip the restoration if the device is inaccessible.

If these are two paragraphs, they need a blank line between. If a single paragraph, rewrap to show that.

Quoted 27 lines

Make the helper non-static and declare it in pci.h so the MSI code can use it, too.

---
Changelog:

v2 -> v3:
- Cover MSI restoration path as well (Farhan)
- Use pci_dev_config_accessible() instead of open-coding the check, and make it non-static (Farhan)
- Move the check before any config space access in the restore functions (Farhan)
- The return value of the PCI Command read is no longer checked, since the helper only tests for an all-ones value.
- Update the subject and commit message accordingly

---
 drivers/pci/msi/msi.c | 6 ++++++
 drivers/pci/pci.c     | 2 +-
 drivers/pci/pci.h     | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/pci/msi/msi.c +0 -0
index 8a0133a6a286..2865f73f5ead 100644

drivers/pci/msi/msi.c void __pci_restore_msi_state(struct pci_dev *dev) · line 528

Quoted 5 lines
drivers/pci/msi/msi.c +2 -0
@@ -524,6 +524,9 @@ void __pci_restore_msi_state(struct pci_dev *dev)
 	if (!dev->msi_enabled)
 		return;
 
+	if (!pci_dev_config_accessible(dev, "restore MSI state"))
+		return;

Sigh. I hate the fact that pci_dev_config_accessible() is proliferating even though it's obviously racy. Maybe it's the best we can do, but I just feel like we're digging this hole deeper.

Also, this is called in the pci_restore_state() path, which is full of other things that also write to config space and will also fail in this situation.

Quoted 41 lines
drivers/pci/msi/msi.c +3 -0
 	entry = irq_get_msi_desc(dev->irq);
 
 	pci_intx_for_msi(dev, 0);
@@ -877,6 +880,9 @@ void __pci_restore_msix_state(struct pci_dev *dev)
 	if (!dev->msix_enabled)
 		return;
 
+	if (!pci_dev_config_accessible(dev, "restore MSI-X state"))
+		return;
+
 	/* route the table */
 	pci_intx_for_msi(dev, 0);
 	pci_msix_clear_and_set_ctrl(dev, 0,
drivers/pci/pci.c +1 -1
index 7c6f2aba8144..671ffe4da1c8 100644
@@ -714,7 +714,7 @@ u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec)
 }
 EXPORT_SYMBOL_GPL(pci_find_dvsec_capability);
 
-static bool pci_dev_config_accessible(struct pci_dev *dev, char *msg)
+bool pci_dev_config_accessible(struct pci_dev *dev, char *msg)
 {
 	u32 val;
 
drivers/pci/pci.h +1 -0
index 77efa4c5c960..98dd3dbd5edb 100644
@@ -240,6 +240,7 @@ enum pci_mmap_api {
 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vmai,
 		  enum pci_mmap_api mmap_api);
 
+bool pci_dev_config_accessible(struct pci_dev *dev, char *msg);
 bool pci_reset_supported(struct pci_dev *dev);
 void pci_init_reset_methods(struct pci_dev *dev);
 int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v3 1/2] PCI/MSI: Skip MSI/MSI-X restoration when config space is inaccessible

Quoted 11 lines · Bjorn Helgaas
Quoted 10 lines · Richard Cheng

PCI state restoration runs even when a reset fails. If the device's config space remains inaccessible, MSI-X restoration still writes the MSI-X table. The table lives in device memory space, and these MMIO accesses can trigger additional errors, including DPC.

MSI is programmed entirely through config space, so it doesn't issue such MMIO accesses. Restoring it is still pointless when config space is
inaccessible: reads return all ones and the restore path writes back values derived from them.

Quoted 8 lines
Quoted 4 lines

Check whether config space of pci device is accessible or not at the start of both MSI and MSI-X path, after msi_enabled/msix_enabled test and before any config access, and skip the restoration if the device is inaccessible.

If these are two paragraphs, they need a blank line between. If a single paragraph, rewrap to show that.

Hi Bjorn,

Thanks for the review and feedback !

I'll add a blank linke before "Make the helper non-static" in v4.

Quoted 30 lines
Quoted 30 lines

Make the helper non-static and declare it in pci.h so the MSI code can use it, too.

---
Changelog:

v2 -> v3:
- Cover MSI restoration path as well (Farhan)
- Use pci_dev_config_accessible() instead of open-coding the check, and make it non-static (Farhan)
- Move the check before any config space access in the restore functions (Farhan)
- The return value of the PCI Command read is no longer checked, since the helper only tests for an all-ones value.
- Update the subject and commit message accordingly

---
 drivers/pci/msi/msi.c | 6 ++++++
 drivers/pci/pci.c     | 2 +-
 drivers/pci/pci.h     | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/pci/msi/msi.c +0 -0
index 8a0133a6a286..2865f73f5ead 100644
@@ -524,6 +524,9 @@ void __pci_restore_msi_state(struct pci_dev *dev)
 	if (!dev->msi_enabled)
 		return;
 
Quoted 7 lines

drivers/pci/msi/msi.c void __pci_restore_msi_state(struct pci_dev *dev) · line 528

Quoted 2 lines
drivers/pci/msi/msi.c +2 -0
+	if (!pci_dev_config_accessible(dev, "restore MSI state"))
+		return;

Sigh. I hate the fact that pci_dev_config_accessible() is proliferating even though it's obviously racy. Maybe it's the best we can do, but I just feel like we're digging this hole deeper.

Hmm I agree this is racy even though we're holding the device lock throughout the reset/restore process.

I assume you mean something like a separate sources DPC happened and make the device inaccessible , some scenario like that ?

Quoted 4 lines

Also, this is called in the pci_restore_state() path, which is full of other things that also write to config space and will also fail in this situation.

Actually I was trying to cover the whole pci_restore_state() in v1 [1], and Benjamin raise the concern about the drivers may depen on PCI state having been restored by the time reset_done() runs.

What my bug actually hit was MSI-X table write triggered DPC, I can't justify for all kinds of driver, so I limited the scope.

If you think a broader check in pci_dev_restore() is a better place to handle this, I'm happy to give a deeper research and revisit it.

Best regards,
Richard Cheng.

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

Quoted 42 lines
Quoted 41 lines
drivers/pci/msi/msi.c +3 -0
 	entry = irq_get_msi_desc(dev->irq);
 
 	pci_intx_for_msi(dev, 0);
@@ -877,6 +880,9 @@ void __pci_restore_msix_state(struct pci_dev *dev)
 	if (!dev->msix_enabled)
 		return;
 
+	if (!pci_dev_config_accessible(dev, "restore MSI-X state"))
+		return;
+
 	/* route the table */
 	pci_intx_for_msi(dev, 0);
 	pci_msix_clear_and_set_ctrl(dev, 0,
drivers/pci/pci.c +1 -1
index 7c6f2aba8144..671ffe4da1c8 100644
@@ -714,7 +714,7 @@ u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec)
 }
 EXPORT_SYMBOL_GPL(pci_find_dvsec_capability);
 
-static bool pci_dev_config_accessible(struct pci_dev *dev, char *msg)
+bool pci_dev_config_accessible(struct pci_dev *dev, char *msg)
 {
 	u32 val;
 
drivers/pci/pci.h +1 -0
index 77efa4c5c960..98dd3dbd5edb 100644
@@ -240,6 +240,7 @@ enum pci_mmap_api {
 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vmai,
 		  enum pci_mmap_api mmap_api);
 
+bool pci_dev_config_accessible(struct pci_dev *dev, char *msg);
 bool pci_reset_supported(struct pci_dev *dev);
 void pci_init_reset_methods(struct pci_dev *dev);
 int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v3 1/2] PCI/MSI: Skip MSI/MSI-X restoration when config space is inaccessible

[+cc Ben, Alex]

Quoted 53 lines · Richard Cheng
Quoted 45 lines · Bjorn Helgaas
Quoted 44 lines · Richard Cheng

PCI state restoration runs even when a reset fails. If the device's config space remains inaccessible, MSI-X restoration still writes the MSI-X table. The table lives in device memory space, and these MMIO accesses can trigger additional errors, including DPC.

MSI is programmed entirely through config space, so it doesn't issue such MMIO accesses. Restoring it is still pointless when config space is
inaccessible: reads return all ones and the restore path writes back values derived from them.

Check whether config space of pci device is accessible or not at the start of both MSI and MSI-X path, after msi_enabled/msix_enabled test and before any config access, and skip the restoration if the device is inaccessible.
Make the helper non-static and declare it in pci.h so the MSI code can use it, too.

---
Changelog:

v2 -> v3:
- Cover MSI restoration path as well (Farhan)
- Use pci_dev_config_accessible() instead of open-coding the check, and make it non-static (Farhan)
- Move the check before any config space access in the restore functions (Farhan)
- The return value of the PCI Command read is no longer checked, since the helper only tests for an all-ones value.
- Update the subject and commit message accordingly

---
 drivers/pci/msi/msi.c | 6 ++++++
 drivers/pci/pci.c     | 2 +-
 drivers/pci/pci.h     | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/pci/msi/msi.c +0 -0
index 8a0133a6a286..2865f73f5ead 100644
@@ -524,6 +524,9 @@ void __pci_restore_msi_state(struct pci_dev *dev)
 	if (!dev->msi_enabled)
 		return;
 
Quoted 7 lines

drivers/pci/msi/msi.c void __pci_restore_msi_state(struct pci_dev *dev) · line 528

Quoted 2 lines
drivers/pci/msi/msi.c +2 -0
+	if (!pci_dev_config_accessible(dev, "restore MSI state"))
+		return;

Sigh. I hate the fact that pci_dev_config_accessible() is proliferating even though it's obviously racy. Maybe it's the best we can do, but I just feel like we're digging this hole deeper.

Quoted 5 lines

Hmm I agree this is racy even though we're holding the device lock throughout the reset/restore process.

I assume you mean something like a separate sources DPC happened and make the device inaccessible , some scenario like that ?

Yes. A PCI device can stop responding at any time, e.g., it got physically removed from a slot, a cable was removed, its internal firmware crashed, power failed, etc.

Quoted 7 lines
Quoted 3 lines

Also, this is called in the pci_restore_state() path, which is full of other things that also write to config space and will also fail in this situation.

Actually I was trying to cover the whole pci_restore_state() in v1 [1], and Benjamin raise the concern about the drivers may depen on PCI state having been restored by the time reset_done() runs.

I think you're referring specifically to Ben's comment [2] that driver .reset_done() handlers expect that pci_restore_state() has already been completed, and your "[PATCH 3/4] PCI: Skip state restoration when configuration is inaccessible" patch calls .reset_done() without any restoration if the device was inaccessible:

  static void pci_dev_restore(struct pci_dev *dev)
  {
        const struct pci_error_handlers *err_handler =
                        dev->driver ? dev->driver->err_handler : NULL;
 -      pci_restore_state(dev);
 +      /* MSI-X restoration may access device MMIO. */
 +      if (pci_dev_config_accessible(dev, "restore state"))

+ pci_restore_state(dev);

        /*
         * dev->driver->err_handler->reset_done() is protected against
         * races with ->remove() by the device lock, which must be held by
         * the caller.
         */
        if (err_handler && err_handler->reset_done)
                err_handler->reset_done(dev);

I think Ben is absolutely right that .reset_done() handlers expect the device to be accessible and config state to have been restored, and I don't think it's reasonable to expect every driver's .reset_done() to handle that generic problem.

I think pci_restore_state() needs to return failure and callers need to figure out how to deal with that. There are a lot of callers, so it's messy, but we should recognize that it can fail.

Quoted 10 lines

What my bug actually hit was MSI-X table write triggered DPC, I can't justify for all kinds of driver, so I limited the scope.

If you think a broader check in pci_dev_restore() is a better place to handle this, I'm happy to give a deeper research and revisit it.

Best regards,
Richard Cheng.

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

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

Quoted 42 lines
Quoted 42 lines
Quoted 41 lines
drivers/pci/msi/msi.c +3 -0
 	entry = irq_get_msi_desc(dev->irq);
 
 	pci_intx_for_msi(dev, 0);
@@ -877,6 +880,9 @@ void __pci_restore_msix_state(struct pci_dev *dev)
 	if (!dev->msix_enabled)
 		return;
 
+	if (!pci_dev_config_accessible(dev, "restore MSI-X state"))
+		return;
+
 	/* route the table */
 	pci_intx_for_msi(dev, 0);
 	pci_msix_clear_and_set_ctrl(dev, 0,
drivers/pci/pci.c +1 -1
index 7c6f2aba8144..671ffe4da1c8 100644
@@ -714,7 +714,7 @@ u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec)
 }
 EXPORT_SYMBOL_GPL(pci_find_dvsec_capability);
 
-static bool pci_dev_config_accessible(struct pci_dev *dev, char *msg)
+bool pci_dev_config_accessible(struct pci_dev *dev, char *msg)
 {
 	u32 val;
 
drivers/pci/pci.h +1 -0
index 77efa4c5c960..98dd3dbd5edb 100644
@@ -240,6 +240,7 @@ enum pci_mmap_api {
 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vmai,
 		  enum pci_mmap_api mmap_api);
 
+bool pci_dev_config_accessible(struct pci_dev *dev, char *msg);
 bool pci_reset_supported(struct pci_dev *dev);
 void pci_init_reset_methods(struct pci_dev *dev);
 int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v3 1/2] PCI/MSI: Skip MSI/MSI-X restoration when config space is inaccessible

Quoted 98 lines · Bjorn Helgaas

[+cc Ben, Alex]

Quoted 53 lines · Richard Cheng
Quoted 45 lines · Bjorn Helgaas
Quoted 44 lines · Richard Cheng

PCI state restoration runs even when a reset fails. If the device's config space remains inaccessible, MSI-X restoration still writes the MSI-X table. The table lives in device memory space, and these MMIO accesses can trigger additional errors, including DPC.

MSI is programmed entirely through config space, so it doesn't issue such MMIO accesses. Restoring it is still pointless when config space is
inaccessible: reads return all ones and the restore path writes back values derived from them.

Check whether config space of pci device is accessible or not at the start of both MSI and MSI-X path, after msi_enabled/msix_enabled test and before any config access, and skip the restoration if the device is inaccessible.
Make the helper non-static and declare it in pci.h so the MSI code can use it, too.

---
Changelog:

v2 -> v3:
- Cover MSI restoration path as well (Farhan)
- Use pci_dev_config_accessible() instead of open-coding the check, and make it non-static (Farhan)
- Move the check before any config space access in the restore functions (Farhan)
- The return value of the PCI Command read is no longer checked, since the helper only tests for an all-ones value.
- Update the subject and commit message accordingly

---
 drivers/pci/msi/msi.c | 6 ++++++
 drivers/pci/pci.c     | 2 +-
 drivers/pci/pci.h     | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/pci/msi/msi.c +0 -0
index 8a0133a6a286..2865f73f5ead 100644
@@ -524,6 +524,9 @@ void __pci_restore_msi_state(struct pci_dev *dev)
 	if (!dev->msi_enabled)
 		return;
 
Quoted 7 lines

drivers/pci/msi/msi.c void __pci_restore_msi_state(struct pci_dev *dev) · line 528

Quoted 2 lines
drivers/pci/msi/msi.c +2 -0
+	if (!pci_dev_config_accessible(dev, "restore MSI state"))
+		return;

Sigh. I hate the fact that pci_dev_config_accessible() is proliferating even though it's obviously racy. Maybe it's the best we can do, but I just feel like we're digging this hole deeper.

Quoted 5 lines

Hmm I agree this is racy even though we're holding the device lock throughout the reset/restore process.

I assume you mean something like a separate sources DPC happened and make the device inaccessible , some scenario like that ?

Yes. A PCI device can stop responding at any time, e.g., it got physically removed from a slot, a cable was removed, its internal firmware crashed, power failed, etc.

Quoted 7 lines
Quoted 3 lines

Also, this is called in the pci_restore_state() path, which is full of other things that also write to config space and will also fail in this situation.

Actually I was trying to cover the whole pci_restore_state() in v1 [1], and Benjamin raise the concern about the drivers may depen on PCI state having been restored by the time reset_done() runs.

I think you're referring specifically to Ben's comment [2] that driver .reset_done() handlers expect that pci_restore_state() has already been completed, and your "[PATCH 3/4] PCI: Skip state restoration when configuration is inaccessible" patch calls .reset_done() without any restoration if the device was inaccessible:

  static void pci_dev_restore(struct pci_dev *dev)
  {
        const struct pci_error_handlers *err_handler =
                        dev->driver ? dev->driver->err_handler : NULL;
 -      pci_restore_state(dev);
 +      /* MSI-X restoration may access device MMIO. */
 +      if (pci_dev_config_accessible(dev, "restore state"))

+ pci_restore_state(dev);

        /*
         * dev->driver->err_handler->reset_done() is protected against
         * races with ->remove() by the device lock, which must be held by
         * the caller.
         */
        if (err_handler && err_handler->reset_done)
                err_handler->reset_done(dev);
Quoted 9 lines

I think Ben is absolutely right that .reset_done() handlers expect the device to be accessible and config state to have been restored, and I don't think it's reasonable to expect every driver's .reset_done() to handle that generic problem.

I think pci_restore_state() needs to return failure and callers need to figure out how to deal with that. There are a lot of callers, so it's messy, but we should recognize that it can fail.

Hi Bjorn,

Thanks for the suggestion, that makes sense.
I'm thinking the following for v4 since I got a clearer picture now.

- Drop MSI/MSI-X specific check and make pci_restore_state() return errno, check whether the device is accessible at the top so we can cover all capabilities
- Keep calling .reset_done(), and return the restore failure from pci_reset_function(), etc. Driver that touch the device in .reset_done() need their own check

I am thinking about for .reset_done(), since the core can't just skip it, like you said the drivers part can be messy, maybe changing signature to

void (*reset_done)(struct pci_dev *dev, int status);

Passing the status from driver side to PCI core and let the core handle it generically will be more elegant ?

Once we decide the approach I can send v4.

Best regards,
Richard Cheng.

Quoted 55 lines
Quoted 10 lines

What my bug actually hit was MSI-X table write triggered DPC, I can't justify for all kinds of driver, so I limited the scope.

If you think a broader check in pci_dev_restore() is a better place to handle this, I'm happy to give a deeper research and revisit it.

Best regards,
Richard Cheng.

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

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

Quoted 42 lines
Quoted 42 lines
Quoted 41 lines
drivers/pci/msi/msi.c +3 -0
 	entry = irq_get_msi_desc(dev->irq);
 
 	pci_intx_for_msi(dev, 0);
@@ -877,6 +880,9 @@ void __pci_restore_msix_state(struct pci_dev *dev)
 	if (!dev->msix_enabled)
 		return;
 
+	if (!pci_dev_config_accessible(dev, "restore MSI-X state"))
+		return;
+
 	/* route the table */
 	pci_intx_for_msi(dev, 0);
 	pci_msix_clear_and_set_ctrl(dev, 0,
drivers/pci/pci.c +1 -1
index 7c6f2aba8144..671ffe4da1c8 100644
@@ -714,7 +714,7 @@ u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec)
 }
 EXPORT_SYMBOL_GPL(pci_find_dvsec_capability);
 
-static bool pci_dev_config_accessible(struct pci_dev *dev, char *msg)
+bool pci_dev_config_accessible(struct pci_dev *dev, char *msg)
 {
 	u32 val;
 
drivers/pci/pci.h +1 -0
index 77efa4c5c960..98dd3dbd5edb 100644
@@ -240,6 +240,7 @@ enum pci_mmap_api {
 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vmai,
 		  enum pci_mmap_api mmap_api);
 
+bool pci_dev_config_accessible(struct pci_dev *dev, char *msg);
 bool pci_reset_supported(struct pci_dev *dev);
 void pci_init_reset_methods(struct pci_dev *dev);
 int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
Signature · 1 line
2.43.0
#7 lore raw

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

In reply to Richard Cheng

The PCI core calls the driver's reset_done() callback even when a reset fails and the device has not recovered. cxl_reset_done() then reads the HDM decoder registers to check whether decoder state was lost. Reading device MMIO while the device is inaccessible can trigger another error.

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

Fixes 934edcd436dc ("cxl: Add post-reset warning if reset results in loss of previously committed HDM decoders")

---
Changelog:

v2 -> v3:
- Add Fixes tag (Sashiko)
- Read the PCI Command register with pci_read_config_word() instead of pci_read_config_dword(), since it's a 16-bit register.
- Reword the commit message and code comment

---
 drivers/cxl/pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/cxl/pci.c +6 -0
index 8ce58917c32d..dadca2db429d 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;
+	u16 command;
+
+	/* The device may still be inaccessible after a failed reset. */
+	if (pci_read_config_word(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
#8 lore raw

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

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

The PCI core calls the driver's reset_done() callback even when a reset fails and the device has not recovered. cxl_reset_done() then reads the HDM decoder registers to check whether decoder state was lost. Reading device MMIO while the device is inaccessible can trigger another error.

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

Fixes 934edcd436dc ("cxl: Add post-reset warning if reset results in loss of previously committed HDM decoders")

Sashiko has reviewed this patch and found no issues.

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