Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH v6] vfio/pci: expose ACPI _ROM through a device region
@ 2026-12-04  9:55 Michael S. Tsirkin
  2026-12-04 10:19 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Michael S. Tsirkin @ 2026-12-04  9:55 UTC (permalink / raw)
  To: Alex Williamson; +Cc: kvm

Some GPUs provide their option ROM through the ACPI _ROM method.
According to the spec, _ROM, if present, must supercede the
PCI ROM BAR, but VFIO currently provides no means to access its
contents.

Let's report it through a new VFIO_REGION_TYPE_ACPI subtype:
VFIO_REGION_SUBTYPE_ACPI_ROM.

Note that _ROM size is not provided by the firmware and has to be parsed
from the image. Let's leave this to userspace.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---

Changes from v3:
	Drop rom cache and size heuristics, userspace can handle them.
	Document source of 4k alignment information.
Changes from v2:
        Leave ROM BAR alone to avoid a risk of regressions.


 drivers/vfio/pci/vfio_pci_core.c |  1 +
 drivers/vfio/pci/vfio_pci_priv.h |  9 ++++
 drivers/vfio/pci/vfio_pci_rdwr.c | 81 ++++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h        |  9 ++++
 4 files changed, 100 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 511dcb49d2f6..8b43b5148f18 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -831,6 +831,7 @@ EXPORT_SYMBOL_GPL(vfio_pci_core_close_device);
 
 void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev)
 {
+	vfio_pci_register_acpi_rom(vdev);
 	vfio_pci_probe_mmaps(vdev);
 #if IS_ENABLED(CONFIG_EEH)
 	eeh_dev_open(vdev->pdev);
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index 4e7162234a2e..1af780d7f1f3 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -44,6 +44,15 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,
 ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite);
 
+#ifdef CONFIG_ACPI
+void vfio_pci_register_acpi_rom(struct vfio_pci_core_device *vdev);
+#else
+static inline void
+vfio_pci_register_acpi_rom(struct vfio_pci_core_device *vdev)
+{
+}
+#endif
+
 #ifdef CONFIG_VFIO_PCI_VGA
 ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite);
diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
index df8848087681..b1489c5178c4 100644
--- a/drivers/vfio/pci/vfio_pci_rdwr.c
+++ b/drivers/vfio/pci/vfio_pci_rdwr.c
@@ -10,8 +10,11 @@
  * Author: Tom Lyon, pugs@cisco.com
  */
 
+#include <linux/acpi.h>
 #include <linux/fs.h>
 #include <linux/pci.h>
+#include <linux/sizes.h>
+#include <linux/slab.h>
 #include <linux/uaccess.h>
 #include <linux/io.h>
 #include <linux/vfio.h>
@@ -198,6 +201,85 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw);
 
+#ifdef CONFIG_ACPI
+static ssize_t vfio_pci_acpi_rom_rw(struct vfio_pci_core_device *vdev,
+				    char __user *buf, size_t count,
+				    loff_t *ppos, bool iswrite)
+{
+	unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
+	struct vfio_pci_region *region = &vdev->region[index - VFIO_PCI_NUM_REGIONS];
+	u64 pos = *ppos & VFIO_PCI_OFFSET_MASK;
+	struct acpi_object_list params = { ARRAY_SIZE(args), args };
+	struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
+	union acpi_object *obj;
+	size_t available;
+	/*
+	 * ACPI limits requests to 4K, and some FW seems to only handle
+	 * full 4K aligned blocks. See
+	 * ad4a36263535 ("drm/nouveau/bios: split out shadow methods").
+	 * and
+	 * d1626a965141 ("drm/nouveau/bios: fetch full 4KiB block to determine ACPI ROM image size").
+	 */
+	size_t skip = pos % SZ_4K;
+	union acpi_object args[] = {
+		{ .integer = { .type = ACPI_TYPE_INTEGER, .value = pos - skip } },
+		{ .integer = { .type = ACPI_TYPE_INTEGER, .value = SZ_4K } },
+	};
+
+	if (iswrite)
+		return -EINVAL;
+
+	if (!count)
+		return 0;
+
+	if (ACPI_FAILURE(acpi_evaluate_object_typed(region->data, NULL, &params,
+						    &result, ACPI_TYPE_BUFFER)))
+		return -EIO;
+
+	obj = result.pointer;
+	available = min_t(size_t, obj->buffer.length, SZ_4K);
+	if (skip >= available) {
+		kfree(obj);
+		return 0;
+	}
+
+	count = min(count, available - skip);
+	count -= copy_to_user(buf, obj->buffer.pointer + skip, count);
+	kfree(obj);
+	if (!count)
+		return -EFAULT;
+
+	*ppos += count;
+	return count;
+}
+
+static void vfio_pci_acpi_rom_release(struct vfio_pci_core_device *vdev,
+				      struct vfio_pci_region *region)
+{
+}
+
+static const struct vfio_pci_regops vfio_pci_acpi_rom_ops = {
+	.rw = vfio_pci_acpi_rom_rw,
+	.release = vfio_pci_acpi_rom_release,
+};
+
+void vfio_pci_register_acpi_rom(struct vfio_pci_core_device *vdev)
+{
+	acpi_handle handle = ACPI_HANDLE(&vdev->pdev->dev), method;
+	int ret;
+
+	if (!handle || ACPI_FAILURE(acpi_get_handle(handle, "_ROM", &method)))
+		return;
+
+	ret = vfio_pci_core_register_dev_region(vdev, VFIO_REGION_TYPE_ACPI,
+						VFIO_REGION_SUBTYPE_ACPI_ROM,
+						&vfio_pci_acpi_rom_ops, 0,
+						VFIO_REGION_INFO_FLAG_READ, method);
+	if (ret)
+		pci_info(vdev->pdev, "ACPI _ROM region unavailable (%d)\n", ret);
+}
+#endif /* CONFIG_ACPI */
+
 ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite)
 {
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index e41437fa17ad..6e81844d47ad 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -345,6 +345,7 @@ struct vfio_region_info_cap_type {
 #define VFIO_REGION_TYPE_GFX                    (1)
 #define VFIO_REGION_TYPE_CCW			(2)
 #define VFIO_REGION_TYPE_MIGRATION_DEPRECATED   (3)
+#define VFIO_REGION_TYPE_ACPI			(4)
 
 /* sub-types for VFIO_REGION_TYPE_PCI_* */
 
@@ -428,6 +429,14 @@ struct vfio_region_gfx_edid {
 /* sub-types for VFIO_REGION_TYPE_MIGRATION */
 #define VFIO_REGION_SUBTYPE_MIGRATION_DEPRECATED (1)
 
+/* sub-types for VFIO_REGION_TYPE_ACPI */
+/*
+ * Read-only PCI expansion ROM from the device firmware's ACPI _ROM method.
+ * Size is always zero for this subtype. Userspace can parse the ROM image
+ * to determine its size.
+ */
+#define VFIO_REGION_SUBTYPE_ACPI_ROM		(1)
+
 struct vfio_device_migration_info {
 	__u32 device_state;         /* VFIO device state */
 #define VFIO_DEVICE_STATE_V1_STOP      (0)

base-commit: 0b75cdfe60c4db031d8a6dcdf5c7ceafcfe5fa02
-- 
MST

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v6] vfio/pci: expose ACPI _ROM through a device region
  2026-12-04  9:55 [PATCH v6] vfio/pci: expose ACPI _ROM through a device region Michael S. Tsirkin
@ 2026-12-04 10:19 ` sashiko-bot
  2026-12-04 10:32   ` Michael S. Tsirkin
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-12-04 10:19 UTC (permalink / raw)
  To: Michael S. Tsirkin; +Cc: kvm

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] vfio/pci: use of undeclared args in vfio_pci_acpi_rom_rw()
  breaks build
- [Low] vfio/pci: unbounded pos in vfio_pci_acpi_rom_rw() wraps on
  32-bit ACPI _ROM
--

commit 89f4506412531a6bddab13afe01a5db3c586ee11
Author: Michael S. Tsirkin <mst@redhat.com>

vfio/pci: expose ACPI _ROM through a device region

This commit exposes a PCI device's ACPI _ROM method through a new
read-only VFIO device region (VFIO_REGION_TYPE_ACPI /
VFIO_REGION_SUBTYPE_ACPI_ROM) so userspace can read the option ROM
contents.

> diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
> index df88480876815..f9c225b0bc72e 100644
> --- a/drivers/vfio/pci/vfio_pci_rdwr.c
> +++ b/drivers/vfio/pci/vfio_pci_rdwr.c

[ ... ]

> @@ -198,6 +201,85 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
>  }
>  EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw);
>  
> +#ifdef CONFIG_ACPI
> +static ssize_t vfio_pci_acpi_rom_rw(struct vfio_pci_core_device *vdev,
> +				    char __user *buf, size_t count,
> +				    loff_t *ppos, bool iswrite)
> +{
> +	unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
> +	struct vfio_pci_region *region = &vdev->region[index - VFIO_PCI_NUM_REGIONS];
> +	u64 pos = *ppos & VFIO_PCI_OFFSET_MASK;
> +	struct acpi_object_list params = { ARRAY_SIZE(args), args };
> +	struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
> +	union acpi_object *obj;
> +	size_t available;
> +	/*
> +	 * ACPI limits requests to 4K, and some FW seems to only handle
> +	 * full 4K aligned blocks. See
> +	 * ad4a36263535 ("drm/nouveau/bios: split out shadow methods").
> +	 * and
> +	 * d1626a965141 ("drm/nouveau/bios: fetch full 4KiB block to determine ACPI ROM image size").
> +	 */
> +	size_t skip = pos % SZ_4K;
> +	union acpi_object args[] = {
> +		{ .integer = { .type = ACPI_TYPE_INTEGER, .value = pos - skip } },
> +		{ .integer = { .type = ACPI_TYPE_INTEGER, .value = SZ_4K } },
> +	};

[Severity: High]
Does this compile with CONFIG_ACPI enabled?  params references args and
ARRAY_SIZE(args) before args[] is declared.  Moving params after args[]
would fix the build failure.

> +
> +	if (iswrite)
> +		return -EINVAL;
> +
> +	if (!count)
> +		return 0;
> +
> +	if (ACPI_FAILURE(acpi_evaluate_object_typed(region->data, NULL, &params,
> +						    &result, ACPI_TYPE_BUFFER)))
> +		return -EIO;
> +
> +	obj = result.pointer;
> +	available = min_t(size_t, obj->buffer.length, SZ_4K);
> +	if (skip >= available) {
> +		kfree(obj);
> +		return 0;
> +	}
> +
> +	count = min(count, available - skip);
> +	count -= copy_to_user(buf, obj->buffer.pointer + skip, count);
> +	kfree(obj);
> +	if (!count)
> +		return -EFAULT;
> +
> +	*ppos += count;
> +	return count;
> +}

[Severity: Low]
Should pos be bounded before passing it to ACPI _ROM?  Because
VFIO_PCI_OFFSET_MASK allows 40-bit offsets up to 1 TiB, a read at an
offset >= 4 GiB passes a value above U32_MAX as Arg0.

ACPICA truncates values above U32_MAX to 32 bits on 32-bit DSDT tables
in acpi_ex_truncate_for32bit_table(), and casts buffer field offsets to
u32 in acpi_ds_init_buffer_field().  An out-of-range read can therefore
wrap modulo 4 GiB and alias offset 0 instead of returning 0 or -EINVAL.

In addition, adding count to *ppos in the final 4 KiB of the 40-bit
offset range can carry into VFIO_PCI_OFFSET_SHIFT, though this is
currently harmless because ACPI _ROM is registered last.  Rejecting
offsets above U32_MAX (or capping below 4 GiB) avoids both issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/fc3a67c19998f3fbfcfa3511a87a56f07697c9a1.1796376699.git.mst@redhat.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v6] vfio/pci: expose ACPI _ROM through a device region
  2026-12-04 10:19 ` sashiko-bot
@ 2026-12-04 10:32   ` Michael S. Tsirkin
  0 siblings, 0 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2026-12-04 10:32 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: kvm

On Fri, Dec 04, 2026 at 10:19:57AM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [High] vfio/pci: use of undeclared args in vfio_pci_acpi_rom_rw()
>   breaks build
> - [Low] vfio/pci: unbounded pos in vfio_pci_acpi_rom_rw() wraps on
>   32-bit ACPI _ROM
> --
> 
> commit 89f4506412531a6bddab13afe01a5db3c586ee11
> Author: Michael S. Tsirkin <mst@redhat.com>
> 
> vfio/pci: expose ACPI _ROM through a device region
> 
> This commit exposes a PCI device's ACPI _ROM method through a new
> read-only VFIO device region (VFIO_REGION_TYPE_ACPI /
> VFIO_REGION_SUBTYPE_ACPI_ROM) so userspace can read the option ROM
> contents.
> 
> > diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
> > index df88480876815..f9c225b0bc72e 100644
> > --- a/drivers/vfio/pci/vfio_pci_rdwr.c
> > +++ b/drivers/vfio/pci/vfio_pci_rdwr.c
> 
> [ ... ]
> 
> > @@ -198,6 +201,85 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
> >  }
> >  EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw);
> >  
> > +#ifdef CONFIG_ACPI
> > +static ssize_t vfio_pci_acpi_rom_rw(struct vfio_pci_core_device *vdev,
> > +				    char __user *buf, size_t count,
> > +				    loff_t *ppos, bool iswrite)
> > +{
> > +	unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
> > +	struct vfio_pci_region *region = &vdev->region[index - VFIO_PCI_NUM_REGIONS];
> > +	u64 pos = *ppos & VFIO_PCI_OFFSET_MASK;
> > +	struct acpi_object_list params = { ARRAY_SIZE(args), args };
> > +	struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
> > +	union acpi_object *obj;
> > +	size_t available;
> > +	/*
> > +	 * ACPI limits requests to 4K, and some FW seems to only handle
> > +	 * full 4K aligned blocks. See
> > +	 * ad4a36263535 ("drm/nouveau/bios: split out shadow methods").
> > +	 * and
> > +	 * d1626a965141 ("drm/nouveau/bios: fetch full 4KiB block to determine ACPI ROM image size").
> > +	 */
> > +	size_t skip = pos % SZ_4K;
> > +	union acpi_object args[] = {
> > +		{ .integer = { .type = ACPI_TYPE_INTEGER, .value = pos - skip } },
> > +		{ .integer = { .type = ACPI_TYPE_INTEGER, .value = SZ_4K } },
> > +	};
> 
> [Severity: High]
> Does this compile with CONFIG_ACPI enabled?  params references args and
> ARRAY_SIZE(args) before args[] is declared.  Moving params after args[]
> would fix the build failure.

Ugh. Should teach me not to try hand editing the patch right before
sending( 
Pls ignore will retest and resend.


> > +
> > +	if (iswrite)
> > +		return -EINVAL;
> > +
> > +	if (!count)
> > +		return 0;
> > +
> > +	if (ACPI_FAILURE(acpi_evaluate_object_typed(region->data, NULL, &params,
> > +						    &result, ACPI_TYPE_BUFFER)))
> > +		return -EIO;
> > +
> > +	obj = result.pointer;
> > +	available = min_t(size_t, obj->buffer.length, SZ_4K);
> > +	if (skip >= available) {
> > +		kfree(obj);
> > +		return 0;
> > +	}
> > +
> > +	count = min(count, available - skip);
> > +	count -= copy_to_user(buf, obj->buffer.pointer + skip, count);
> > +	kfree(obj);
> > +	if (!count)
> > +		return -EFAULT;
> > +
> > +	*ppos += count;
> > +	return count;
> > +}
> 
> [Severity: Low]
> Should pos be bounded before passing it to ACPI _ROM?  Because
> VFIO_PCI_OFFSET_MASK allows 40-bit offsets up to 1 TiB, a read at an
> offset >= 4 GiB passes a value above U32_MAX as Arg0.
> 
> ACPICA truncates values above U32_MAX to 32 bits on 32-bit DSDT tables
> in acpi_ex_truncate_for32bit_table(), and casts buffer field offsets to
> u32 in acpi_ds_init_buffer_field().  An out-of-range read can therefore
> wrap modulo 4 GiB and alias offset 0 instead of returning 0 or -EINVAL.
> 
> In addition, adding count to *ppos in the final 4 KiB of the 40-bit
> offset range can carry into VFIO_PCI_OFFSET_SHIFT, though this is
> currently harmless because ACPI _ROM is registered last.  Rejecting
> offsets above U32_MAX (or capping below 4 GiB) avoids both issues.
> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/fc3a67c19998f3fbfcfa3511a87a56f07697c9a1.1796376699.git.mst@redhat.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04 10:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  9:55 [PATCH v6] vfio/pci: expose ACPI _ROM through a device region Michael S. Tsirkin
2026-12-04 10:19 ` sashiko-bot
2026-12-04 10:32   ` Michael S. Tsirkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox