Skip to contentSkip to conversation

PCI: Fix BAR resizing when VF BARs are assigned

PATCH Superseded Ilpo Järvinen 4 participants 8 messages Mar 7, 2025 – Mar 18, 2025 PCI

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 PCI: Fix BAR resizing when VF BARs are assigned

__resource_resize_store() attempts to release all resources of the device before attempting the resize. The loop, however, only covers standard BARs (< PCI_STD_NUM_BARS). If a device has VF BARs that are assigned, pci_reassign_bridge_resources() finds the bridge window still has some assigned child resources and returns -NOENT which makes pci_resize_resource() to detect an error and abort the resize.

Change the release loop to cover all resources up to VF BARs which allows the resize operation to release the bridge windows and attempt to assigned them again with the different size.

As __resource_resize_store() checks first that no driver is bound to the PCI device before resizing is allowed, SR-IOV cannot be enabled during resize so it is safe to release also the IOV resources.

Fixes 91fa127794ac ("PCI: Expose PCIe Resizable BAR support via sysfs")

---

v2:
- Removed language about expansion ROMs

 drivers/pci/pci-sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/pci-sysfs.c +1 -1
index b46ce1a2c554..0c16751bab40 100644
@@ -1578,7 +1578,7 @@ static ssize_t __resource_resize_store(struct device *dev, int n,
 
 	pci_remove_resource_files(pdev);
 
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
+	for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
 		if (pci_resource_len(pdev, i) &&
 		    pci_resource_flags(pdev, i) == flags)
 			pci_release_resource(pdev, i);

base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b

Signature · 1 line
2.39.5
#1 lore raw
Quoted 7 lines · On Fri, 7 Mar 2025 16:03:49 +0200 Ilpo Järvinen…

__resource_resize_store() attempts to release all resources of the device before attempting the resize. The loop, however, only covers standard BARs (< PCI_STD_NUM_BARS). If a device has VF BARs that are assigned, pci_reassign_bridge_resources() finds the bridge window still has some assigned child resources and returns -NOENT which makes pci_resize_resource() to detect an error and abort the resize.

Quoted 7 lines

Change the release loop to cover all resources up to VF BARs which allows the resize operation to release the bridge windows and attempt to assigned them again with the different size.

As __resource_resize_store() checks first that no driver is bound to the PCI device before resizing is allowed, SR-IOV cannot be enabled during resize so it is safe to release also the IOV resources.

Is this true? pci-pf-stub doesn't teardown SR-IOV on release, which I understand is done intentionally. Thanks,

Alex

Quoted 26 lines
Fixes 91fa127794ac ("PCI: Expose PCIe Resizable BAR support via sysfs")

---

v2:
- Removed language about expansion ROMs

 drivers/pci/pci-sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/pci-sysfs.c +1 -1
index b46ce1a2c554..0c16751bab40 100644
@@ -1578,7 +1578,7 @@ static ssize_t __resource_resize_store(struct device *dev, int n,
 
 	pci_remove_resource_files(pdev);
 
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
+	for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
 		if (pci_resource_len(pdev, i) &&
 		    pci_resource_flags(pdev, i) == flags)
 			pci_release_resource(pdev, i);

base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b

#2 lore raw
Quoted 14 lines · Alex Williamson
Quoted 11 lines · On Fri, 7 Mar 2025 16:03:49 +0200 Ilpo Järvinen…

__resource_resize_store() attempts to release all resources of the device before attempting the resize. The loop, however, only covers standard BARs (< PCI_STD_NUM_BARS). If a device has VF BARs that are assigned, pci_reassign_bridge_resources() finds the bridge window still has some assigned child resources and returns -NOENT which makes pci_resize_resource() to detect an error and abort the resize.

Change the release loop to cover all resources up to VF BARs which allows the resize operation to release the bridge windows and attempt to assigned them again with the different size.

Quoted 6 lines
Quoted 3 lines

As __resource_resize_store() checks first that no driver is bound to the PCI device before resizing is allowed, SR-IOV cannot be enabled during resize so it is safe to release also the IOV resources.

Is this true? pci-pf-stub doesn't teardown SR-IOV on release, which I understand is done intentionally. Thanks,

Is that really intentional?
PCI warns when that scenario occurs:
https://elixir.bootlin.com/linux/v6.13.7/source/drivers/pci/iov.c#L936

I thought that the usecase is binding pci-pf-stub, creating VFs, and letting the driver be.
But unbinding after creating VFs? What's the goal of that? Perhaps we're just missing .remove() in pci-pf-stub?

Thanks,
-Michał

Quoted 30 lines

Alex

Quoted 26 lines
Fixes 91fa127794ac ("PCI: Expose PCIe Resizable BAR support via sysfs")

---

v2:
- Removed language about expansion ROMs

 drivers/pci/pci-sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/pci-sysfs.c +1 -1
index b46ce1a2c554..0c16751bab40 100644
@@ -1578,7 +1578,7 @@ static ssize_t __resource_resize_store(struct device *dev, int n,
 
 	pci_remove_resource_files(pdev);
 
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
+	for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
 		if (pci_resource_len(pdev, i) &&
 		    pci_resource_flags(pdev, i) == flags)
 			pci_release_resource(pdev, i);

base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b

#3 lore raw
Quoted 19 lines · On Mon, 17 Mar 2025 19:18:03 +0100 Michał Winiarski…
Quoted 18 lines · Alex Williamson
Quoted 14 lines · On Fri, 7 Mar 2025 16:03:49 +0200 Ilpo Järvinen…

__resource_resize_store() attempts to release all resources of the device before attempting the resize. The loop, however, only covers standard BARs (< PCI_STD_NUM_BARS). If a device has VF BARs that are assigned, pci_reassign_bridge_resources() finds the bridge window still has some assigned child resources and returns -NOENT which makes pci_resize_resource() to detect an error and abort the resize.

Change the release loop to cover all resources up to VF BARs which allows the resize operation to release the bridge windows and attempt to assigned them again with the different size.

As __resource_resize_store() checks first that no driver is bound to the PCI device before resizing is allowed, SR-IOV cannot be enabled during resize so it is safe to release also the IOV resources.

Quoted 6 lines
Quoted 2 lines

Is this true? pci-pf-stub doesn't teardown SR-IOV on release, which I understand is done intentionally. Thanks,

Is that really intentional?
PCI warns when that scenario occurs:
https://elixir.bootlin.com/linux/v6.13.7/source/drivers/pci/iov.c#L936

Yep, it warns. It doesn't prevent it from happening though.

Quoted 4 lines

I thought that the usecase is binding pci-pf-stub, creating VFs, and letting the driver be.
But unbinding after creating VFs? What's the goal of that? Perhaps we're just missing .remove() in pci-pf-stub?

I guess I don't actually know that leaving SR-IOV enabled was intentional, maybe it was an oversight. The original commit only mentions the case of a device that requires nothing but this shim as the PF driver. A pci_warn() isn't much disincentive, the system might already have taints. If it's something that we really want to show as broken, it'd probably need to be a WARN_ON. Thanks,

Alex

#4 lore raw

+ Jakub
+ Alexander

Quoted 17 lines · Alex Williamson
Quoted 15 lines · On Mon, 17 Mar 2025 19:18:03 +0100 Michał Winiarski…
Quoted 14 lines · Alex Williamson
Quoted 11 lines · On Fri, 7 Mar 2025 16:03:49 +0200 Ilpo Järvinen…

__resource_resize_store() attempts to release all resources of the device before attempting the resize. The loop, however, only covers standard BARs (< PCI_STD_NUM_BARS). If a device has VF BARs that are assigned, pci_reassign_bridge_resources() finds the bridge window still has some assigned child resources and returns -NOENT which makes pci_resize_resource() to detect an error and abort the resize.

Change the release loop to cover all resources up to VF BARs which allows the resize operation to release the bridge windows and attempt to assigned them again with the different size.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

As __resource_resize_store() checks first that no driver is bound to the PCI device before resizing is allowed, SR-IOV cannot be enabled during resize so it is safe to release also the IOV resources.

Is this true? pci-pf-stub doesn't teardown SR-IOV on release, which I understand is done intentionally. Thanks,

Thanks for reviewing. I'm sorry I just took Michał's word on this for granted so I didn't check it myself.

I could amend __resource_resize_store() to return -EBUSY if SR-IOV is there despite no driver being present, but lets hear if Alexander or Jakub has some input on this.

Quoted 11 lines
Quoted 3 lines

Is that really intentional?
PCI warns when that scenario occurs:
https://elixir.bootlin.com/linux/v6.13.7/source/drivers/pci/iov.c#L936

Yep, it warns. It doesn't prevent it from happening though.

Quoted 4 lines

I thought that the usecase is binding pci-pf-stub, creating VFs, and letting the driver be.
But unbinding after creating VFs? What's the goal of that? Perhaps we're just missing .remove() in pci-pf-stub?

Quoted 6 lines

I guess I don't actually know that leaving SR-IOV enabled was intentional, maybe it was an oversight. The original commit only mentions the case of a device that requires nothing but this shim as the PF driver. A pci_warn() isn't much disincentive, the system might already have taints. If it's something that we really want to show as broken, it'd probably need to be a WARN_ON.

Added Alexander and Jakub, perhaps they remember something or if there are caveats going either way.

Signature · 1 line
 i.
#5 lore raw
Quoted 25 lines · On Tue, 18 Mar 2025 13:42:57 +0200 (EET) Ilpo Järvinen…

+ Jakub
+ Alexander

Quoted 21 lines · Alex Williamson
Quoted 19 lines · On Mon, 17 Mar 2025 19:18:03 +0100 Michał Winiarski…
Quoted 18 lines · Alex Williamson
Quoted 14 lines · On Fri, 7 Mar 2025 16:03:49 +0200 Ilpo Järvinen…

__resource_resize_store() attempts to release all resources of the device before attempting the resize. The loop, however, only covers standard BARs (< PCI_STD_NUM_BARS). If a device has VF BARs that are assigned, pci_reassign_bridge_resources() finds the bridge window still has some assigned child resources and returns -NOENT which makes pci_resize_resource() to detect an error and abort the resize.

Change the release loop to cover all resources up to VF BARs which allows the resize operation to release the bridge windows and attempt to assigned them again with the different size.

As __resource_resize_store() checks first that no driver is bound to the PCI device before resizing is allowed, SR-IOV cannot be enabled during resize so it is safe to release also the IOV resources.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Is this true? pci-pf-stub doesn't teardown SR-IOV on release, which I understand is done intentionally. Thanks,

Thanks for reviewing. I'm sorry I just took Michał's word on this for granted so I didn't check it myself.

I could amend __resource_resize_store() to return -EBUSY if SR-IOV is there despite no driver being present

I probably never really considered resizing BARs for an SR-IOV capable device when adding this support originally, but it seems valid to me that if we extend releasing resources to the SR-IOV BARs that we simply need to assert that SR-IOV is disabled and fail otherwise. Thanks,

Alex

#6 lore raw
Quoted 34 lines · On Tue, 18 Mar 2025 09:01:57 -0600 Alex Williamson…
Quoted 31 lines · On Tue, 18 Mar 2025 13:42:57 +0200 (EET) Ilpo Järvinen…

+ Jakub
+ Alexander

Quoted 17 lines · Alex Williamson
Quoted 15 lines · On Mon, 17 Mar 2025 19:18:03 +0100 Michał Winiarski…
Quoted 14 lines · Alex Williamson
Quoted 11 lines · On Fri, 7 Mar 2025 16:03:49 +0200 Ilpo Järvinen…

__resource_resize_store() attempts to release all resources of the device before attempting the resize. The loop, however, only covers standard BARs (< PCI_STD_NUM_BARS). If a device has VF BARs that are assigned, pci_reassign_bridge_resources() finds the bridge window still has some assigned child resources and returns -NOENT which makes pci_resize_resource() to detect an error and abort the resize.

Change the release loop to cover all resources up to VF BARs which allows the resize operation to release the bridge windows and attempt to assigned them again with the different size.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

As __resource_resize_store() checks first that no driver is bound to the PCI device before resizing is allowed, SR-IOV cannot be enabled during resize so it is safe to release also the IOV resources.

Is this true? pci-pf-stub doesn't teardown SR-IOV on release, which I understand is done intentionally. Thanks,

Thanks for reviewing. I'm sorry I just took Michał's word on this for granted so I didn't check it myself.

Quoted 7 lines
Quoted 2 lines

I could amend __resource_resize_store() to return -EBUSY if SR-IOV is there despite no driver being present

I probably never really considered resizing BARs for an SR-IOV capable device when adding this support originally, but it seems valid to me that if we extend releasing resources to the SR-IOV BARs that we simply need to assert that SR-IOV is disabled and fail otherwise. Thanks,

Also, I've never seen it, but I'm under the impression that it's possible for pre-boot to hand-off a device with SR-IOV already enabled. Therefore I think this would be a worthwhile addition, regardless of the behavior of any given in-kernel driver. Thanks,

Alex

#7 lore raw
In reply to Ilpo Järvinen
Quoted 37 lines · Ilpo Järvinen

__resource_resize_store() attempts to release all resources of the device before attempting the resize. The loop, however, only covers standard BARs (< PCI_STD_NUM_BARS). If a device has VF BARs that are assigned, pci_reassign_bridge_resources() finds the bridge window still
has some assigned child resources and returns -NOENT which makes pci_resize_resource() to detect an error and abort the resize.

Change the release loop to cover all resources up to VF BARs which allows the resize operation to release the bridge windows and attempt to assigned them again with the different size.

As __resource_resize_store() checks first that no driver is bound to the PCI device before resizing is allowed, SR-IOV cannot be enabled during resize so it is safe to release also the IOV resources.

Fixes 91fa127794ac ("PCI: Expose PCIe Resizable BAR support via

sysfs")

---

v2:
- Removed language about expansion ROMs

 drivers/pci/pci-sysfs.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/pci-sysfs.c +0 -0
index b46ce1a2c554..0c16751bab40 100644
@@ -1578,7 +1578,7 @@ static ssize_t __resource_resize_store(struct
 device *dev, int n,


  pci_remove_resource_files(pdev);

drivers/pci/pci-sysfs.c static ssize_t __resource_resize_store(struct device *dev, int n, · line 1581

Quoted 2 lines
drivers/pci/pci-sysfs.c +1 -1
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
+	for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {

I, just recently, fixed a similar bug (assuming it is one in this particular case here) [1].

I think it would be great if someone who has the knowledge could improve the documentation in linux/pci.h where those constants are defined.

PCI_STD_NUM_BARS is even defined in a different header, hinting at a different background.

P.

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

Quoted 5 lines

  if (pci_resource_len(pdev, i) && pci_resource_flags(pdev, i) == flags) pci_release_resource(pdev, i);

base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b