Skip to contentSkip to conversation

PCI: Expose resource resizing through sysfs

PATCH In review Alex Williamson 2 participants 7 messages Aug 16, 2022 – Aug 18, 2022 PCI

lore
Share & tools
lore
#0 lore raw

cover PCI: Expose resource resizing through sysfs

We have a couple graphics drivers making use of PCIe Resizable BARs now, but I've been trying to figure out how we can make use of such features for devices assigned to a VM. This is a proposal for a rather basic interface in sysfs such that we have the ability to pre-enable larger BARs before we bind devices to vfio-pci and attach them to a VM.

Along the way I found a double-free in the error path of creating resource attributes, that can certainly be pulled separately (1/).

I'm using an RTX6000 for testing, which unexpectedly only supports REBAR with smaller than default sizes, which led me to question why we have such heavy requirements for shrinking resources (2/).

The final patch proposes the sysfs interface and I'll leave the discussion there for whether this is a good approach. Thanks,

Alex
---

Alex Williamson (3):
PCI: Fix double-free in resource attribute error path
PCI: Skip reassigning bridge resources if reducing BAR size
PCI: Expose PCIe Resizable BAR support via sysfs

 Documentation/ABI/testing/sysfs-bus-pci |  27 +++++
 drivers/pci/pci-sysfs.c                 | 126 +++++++++++++++++++++++-
 drivers/pci/setup-res.c                 |   2 +-
 include/linux/pci.h                     |   1 +
 4 files changed, 154 insertions(+), 2 deletions(-)
#1 lore raw

PATCH 1/3 PCI: Fix double-free in resource attribute error path

If pci_create_attr() fails the remaining resource bin attributes are removed and freed based on the pointer entries which are not cleared in the case that creation of the bin attribute fails.

---
 drivers/pci/pci-sysfs.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/pci/pci-sysfs.c +7 -1
index fc804e08e3cb..9ac92e6a2397 100644
@@ -1196,8 +1196,14 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
 	res_attr->size = pci_resource_len(pdev, num);
 	res_attr->private = (void *)(unsigned long)num;
 	retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
-	if (retval)
+	if (retval) {
+		if (write_combine)
+			pdev->res_attr_wc[num] = NULL;
+		else
+			pdev->res_attr[num] = NULL;
+
 		kfree(res_attr);
+	}
 
 	return retval;
 }
#2 lore raw

RFC PATCH 2/3 PCI: Skip reassigning bridge resources if reducing BAR size

In reply to Alex Williamson

More work is probably necessary here, pci_reassign_bridge_resources() has heavy weight requirements that the aperture windows are unused, without even testing if the requested resize is possible in the existing aperture. One case we can clearly skip is when reducing the size of a BAR.

Cc Christian König <[email protected]>
---
 drivers/pci/setup-res.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/setup-res.c +1 -1
index 439ac5f5907a..7a8e1e4f4d33 100644
@@ -450,7 +450,7 @@ int pci_resize_resource(struct pci_dev *dev, int resno, int size)
 	res->end = res->start + pci_rebar_size_to_bytes(size) - 1;
 
 	/* Check if the new config works by trying to assign everything. */
-	if (dev->bus->self) {
+	if (size > old && dev->bus->self) {
 		ret = pci_reassign_bridge_resources(dev->bus->self, res->flags);
 		if (ret)
 			goto error_resize;
#3 lore raw

RFC PATCH 3/3 PCI: Expose PCIe Resizable BAR support via sysfs

In reply to Alex Williamson

This proposes a simple sysfs interface to Resizable BAR support, largely for the purposes of assigning such devices to a VM through VFIO. Resizable BARs present a difficult feature to expose to a VM through emulation, as resizing a BAR is done on the host. It can fail, and often does, but we have no means via emulation of a PCIe REBAR capability to handle the error cases.

A vfio-pci specific ioctl interface is also cumbersome as there are often multiple devices within the same bridge aperture and handling them is a challenge. In the interface proposed here, expanding a BAR potentially requires such devices to be soft-removed during the resize operation and rescanned after, in order for all the necessary resources to be released. A pci-sysfs interface is also more universal than a vfio specific interface.

Please see the ABI documentation update for usage.

Cc Christian König <[email protected]>

---

NB, I realize the read value of the syfs attribute provides two values, the bitmap of possible sizes and the current size. There are a number of ways to determine the current size, including stat(1) on the resourceN file, but I found this output to be useful while developing the interface and provides consistency with the store value to the attribute. Suggestions welcome for better semantics.

 Documentation/ABI/testing/sysfs-bus-pci |   27 +++++++
 drivers/pci/pci-sysfs.c                 |  118 +++++++++++++++++++++++++++++++
 include/linux/pci.h                     |    1 
 3 files changed, 146 insertions(+)
Documentation/ABI/testing/sysfs-bus-pci +27 -0
index 6fc2c2efe8ab..5eea5d89c9f2 100644
@@ -457,3 +457,30 @@ Description:
 
 		The file is writable if the PF is bound to a driver that
 		implements ->sriov_set_msix_vec_count().
+
+What:		/sys/bus/pci/devices/.../resourceN_resize
+Date:		August 2022
+Contact:	Alex Williamson <[email protected]>
+Description:
+		These files provide an interface to PCIe Resizable BAR support.
+		A file is created for each BAR resource (N) supported by the
+		PCIe Resizable BAR extended capability of the device.  Reading
+		each file exposes the capability and current setting for the
+		device, ex.
+
+		# cat resource1_resize
+		00000000000001c0:6
+
+		The first field provides the supported sizes bitmap, where
+		bit0 = 1MB, bit1 = 2MB, bit2 = 4MB, etc.  In the above example
+		the devices supports 64MB, 128MB, and 256MB BAR sizes.  The
+		second field provides the current setting, the value 6
+		indicates bit6 is set, which corresponds to 64MB.
+
+		When writing the file, only the latter is used, ex.
+
+		# echo 7 > resource1_resize
+
+		This indicates to set the size value corresponding to bit 7,
+		which is 128MB.  The resulting size is 2 ^ (bit# + 20).  This
+		definition matches the PCIe specification of this capability.
drivers/pci/pci-sysfs.c +118 -0

142 lines not on this page · raw message

include/linux/pci.h +1 -0
index 060af91bafcd..9c4db0c5f215 100644
@@ -470,6 +470,7 @@ struct pci_dev {
 	int		rom_attr_enabled;	/* Display of ROM attribute enabled? */
 	struct bin_attribute *res_attr[DEVICE_COUNT_RESOURCE]; /* sysfs file for resources */
 	struct bin_attribute *res_attr_wc[DEVICE_COUNT_RESOURCE]; /* sysfs file for WC mapping of resources */
+	struct dev_ext_attribute *res_attr_resize[DEVICE_COUNT_RESOURCE]; /* sysfs file for resizing BAR resources */
 
 #ifdef CONFIG_HOTPLUG_PCI_PCIE
 	unsigned int	broken_cmd_compl:1;	/* No compl for some cmds */
#4 lore raw
In reply to Alex Williamson

Am 16.08.22 um 21:39 schrieb Alex Williamson:

Quoted 6 lines

We have a couple graphics drivers making use of PCIe Resizable BARs now, but I've been trying to figure out how we can make use of such features for devices assigned to a VM. This is a proposal for a rather basic interface in sysfs such that we have the ability to pre-enable larger BARs before we bind devices to vfio-pci and attach them to a VM.

Ah, yes please.

I was considering doing this myself just for testing while adding the rebar support for the GFX drivers, but then just implementing it on the GFX side was simpler.

I would just add a warning that resizing BARs can easily crash the system even when no driver directly claimed the resource or PCIe device.

It literally took me weeks to figure out that I need to kick out the EFI framebuffer driver before trying to resize the BAR or otherwise I just get a hung system.

Quoted 6 lines

Along the way I found a double-free in the error path of creating resource attributes, that can certainly be pulled separately (1/).

I'm using an RTX6000 for testing, which unexpectedly only supports REBAR with smaller than default sizes, which led me to question why we have such heavy requirements for shrinking resources (2/).

Oh, that's easy. You got tons of ARM boards with less than 512MiB of address space per root PCIe complex.

If you want to get a GPU working on those you need to decrease the BAR size or otherwise you won't be able to fit 256MiB VRAM BAR + register BAR into the same hole for the PCIe root complex.

An alternative explanation is that at least AMD produced some boards with a messed up resize configuration word. But on those you only got 256MiB, 512MiB and 1GiB potential BAR sizes IIRC.

Anyway, with an appropriate warning added to the sysfs documentation the patch #2 and #3 are Acked-by: Christian König <[email protected]>

Regards,
Christian.

Quoted 19 lines

The final patch proposes the sysfs interface and I'll leave the discussion there for whether this is a good approach. Thanks,

Alex
---

Alex Williamson (3):
PCI: Fix double-free in resource attribute error path
PCI: Skip reassigning bridge resources if reducing BAR size
PCI: Expose PCIe Resizable BAR support via sysfs

  Documentation/ABI/testing/sysfs-bus-pci |  27 +++++
  drivers/pci/pci-sysfs.c                 | 126 +++++++++++++++++++++++-
  drivers/pci/setup-res.c                 |   2 +-
  include/linux/pci.h                     |   1 +
  4 files changed, 154 insertions(+), 2 deletions(-)
#5 lore raw
Quoted 14 lines · On Wed, 17 Aug 2022 12:10:44 +0200 Christian König <[email protected]>…

Am 16.08.22 um 21:39 schrieb Alex Williamson:

Quoted 6 lines

We have a couple graphics drivers making use of PCIe Resizable BARs now, but I've been trying to figure out how we can make use of such features for devices assigned to a VM. This is a proposal for a rather basic interface in sysfs such that we have the ability to pre-enable larger BARs before we bind devices to vfio-pci and attach them to a VM.

Ah, yes please.

I was considering doing this myself just for testing while adding the rebar support for the GFX drivers, but then just implementing it on the GFX side was simpler.

Quoted 6 lines

I would just add a warning that resizing BARs can easily crash the system even when no driver directly claimed the resource or PCIe device.

It literally took me weeks to figure out that I need to kick out the EFI framebuffer driver before trying to resize the BAR or otherwise I just get a hung system.

Good point, I think maybe we can avoid crashing the system though if we use the new aperture support to remove conflicting drivers from all VGA class devices, similar to d17378062079 ("vfio/pci: Remove console drivers"). A note in the ABI documentation about removing console drivers from the device when resizing resources would definitely be in order.

Quoted 10 lines
Quoted 6 lines

Along the way I found a double-free in the error path of creating resource attributes, that can certainly be pulled separately (1/).

I'm using an RTX6000 for testing, which unexpectedly only supports REBAR with smaller than default sizes, which led me to question why we have such heavy requirements for shrinking resources (2/).

Oh, that's easy. You got tons of ARM boards with less than 512MiB of address space per root PCIe complex.

Quoted 7 lines

If you want to get a GPU working on those you need to decrease the BAR size or otherwise you won't be able to fit 256MiB VRAM BAR + register BAR into the same hole for the PCIe root complex.

An alternative explanation is that at least AMD produced some boards with a messed up resize configuration word. But on those you only got 256MiB, 512MiB and 1GiB potential BAR sizes IIRC.

An aspect of shrinking BARs that maybe I'm not giving enough consideration to is that we might be shrinking a BAR on one device in order to release MMIO space from a bridge aperture, that we might then use to expand a BAR elsewhere. The RTX6000 case only frees a rather modest amount of MMIO space, but I could imagine more substantial configurations. Maybe this justifies resizing the bridge aperture even in the shrinking case?

Quoted 3 lines

Anyway, with an appropriate warning added to the sysfs documentation the patch #2 and #3 are Acked-by: Christian König <[email protected]>

Thanks!

Alex

#6 lore raw

Am 17.08.22 um 16:02 schrieb Alex Williamson:

Quoted 45 lines
Quoted 13 lines · On Wed, 17 Aug 2022 12:10:44 +0200 Christian König <[email protected]>…

Am 16.08.22 um 21:39 schrieb Alex Williamson:

Quoted 6 lines

We have a couple graphics drivers making use of PCIe Resizable BARs now, but I've been trying to figure out how we can make use of such features for devices assigned to a VM. This is a proposal for a rather basic interface in sysfs such that we have the ability to pre-enable larger BARs before we bind devices to vfio-pci and attach them to a VM.

Ah, yes please.

I was considering doing this myself just for testing while adding the rebar support for the GFX drivers, but then just implementing it on the GFX side was simpler.

Quoted 6 lines

I would just add a warning that resizing BARs can easily crash the system even when no driver directly claimed the resource or PCIe device.

It literally took me weeks to figure out that I need to kick out the EFI framebuffer driver before trying to resize the BAR or otherwise I just get a hung system.

Good point, I think maybe we can avoid crashing the system though if we use the new aperture support to remove conflicting drivers from all VGA class devices, similar to d17378062079 ("vfio/pci: Remove console drivers"). A note in the ABI documentation about removing console drivers from the device when resizing resources would definitely be in order.

Quoted 9 lines
Quoted 6 lines

Along the way I found a double-free in the error path of creating resource attributes, that can certainly be pulled separately (1/).

I'm using an RTX6000 for testing, which unexpectedly only supports REBAR with smaller than default sizes, which led me to question why we have such heavy requirements for shrinking resources (2/).

Oh, that's easy. You got tons of ARM boards with less than 512MiB of address space per root PCIe complex.

Quoted 7 lines

If you want to get a GPU working on those you need to decrease the BAR size or otherwise you won't be able to fit 256MiB VRAM BAR + register BAR into the same hole for the PCIe root complex.

An alternative explanation is that at least AMD produced some boards with a messed up resize configuration word. But on those you only got 256MiB, 512MiB and 1GiB potential BAR sizes IIRC.

Quoted 7 lines

An aspect of shrinking BARs that maybe I'm not giving enough consideration to is that we might be shrinking a BAR on one device in order to release MMIO space from a bridge aperture, that we might then use to expand a BAR elsewhere. The RTX6000 case only frees a rather modest amount of MMIO space, but I could imagine more substantial configurations. Maybe this justifies resizing the bridge aperture even in the shrinking case?

That was the original idea why I've kept that in there, yes.

But I never really seen a case where that really mattered.

So far making BARs smaller was something only the BIOS does.

Christian.

Quoted 8 lines
Quoted 4 lines

Anyway, with an appropriate warning added to the sysfs documentation the patch #2 and #3 are Acked-by: Christian König <[email protected]>

Thanks!

Alex