* [PATCH v3] vfio/pci: expose ACPI _ROM through a device region
@ 2026-12-03 14:35 Michael S. Tsirkin
2026-12-03 14:56 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Michael S. Tsirkin @ 2026-12-03 14:35 UTC (permalink / raw)
To: linux-kernel; +Cc: Alex Williamson, 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.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
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 | 115 +++++++++++++++++++++++++++++++
include/uapi/linux/vfio.h | 9 +++
4 files changed, 134 insertions(+)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index fcbb2da860e2a..49ec5a3a29ae5 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -829,6 +829,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 4e7162234a2eb..1af780d7f1f37 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 df88480876815..71ddf8f500508 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,118 @@ 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
+/* Bound the firmware probe and cache to a reasonable option ROM size. */
+#define VFIO_PCI_ACPI_ROM_MAX SZ_16M
+
+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];
+ loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
+
+ if (iswrite)
+ return -EINVAL;
+
+ if (!count || pos >= region->size)
+ return 0;
+
+ count = min_t(size_t, count, region->size - pos);
+ count -= copy_to_user(buf, region->data + pos, count);
+ 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)
+{
+ kvfree(region->data);
+}
+
+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);
+ union acpi_object args[] = {
+ { .integer = { .type = ACPI_TYPE_INTEGER } },
+ { .integer = { .type = ACPI_TYPE_INTEGER, .value = SZ_4K } },
+ };
+ struct acpi_object_list params = { ARRAY_SIZE(args), args };
+ size_t size = 0, capacity = 0;
+ void *rom = NULL;
+ acpi_handle method;
+ int ret = -EIO;
+
+ if (!handle || ACPI_FAILURE(acpi_get_handle(handle, "_ROM", &method)))
+ return;
+
+ /*
+ * Some firmware requires 4 KiB-aligned requests. Snapshot the image
+ * at open so reads never invoke AML and the size and contents agree.
+ */
+ for (;;) {
+ struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
+ union acpi_object *obj;
+ size_t len;
+
+ args[0].integer.value = size;
+ if (ACPI_FAILURE(acpi_evaluate_object_typed(method, NULL, ¶ms,
+ &result, ACPI_TYPE_BUFFER)))
+ break;
+
+ obj = result.pointer;
+ len = obj->buffer.length;
+ if (len > SZ_4K || len > VFIO_PCI_ACPI_ROM_MAX - size) {
+ ret = -EFBIG;
+ kfree(obj);
+ break;
+ }
+ if (size + len > capacity) {
+ void *new_rom;
+
+ capacity = max_t(size_t, SZ_4K, capacity * 2);
+ new_rom = kvrealloc(rom, capacity, GFP_KERNEL);
+ if (!new_rom) {
+ ret = -ENOMEM;
+ kfree(obj);
+ break;
+ }
+ rom = new_rom;
+ }
+ if (len)
+ memcpy(rom + size, obj->buffer.pointer, len);
+ kfree(obj);
+ size += len;
+
+ if (len < SZ_4K) {
+ if (!size)
+ return;
+
+ ret = vfio_pci_core_register_dev_region(vdev,
+ VFIO_REGION_TYPE_ACPI,
+ VFIO_REGION_SUBTYPE_ACPI_ROM,
+ &vfio_pci_acpi_rom_ops, size,
+ VFIO_REGION_INFO_FLAG_READ, rom);
+ if (!ret)
+ return;
+ break;
+ }
+ }
+
+ pci_dbg(vdev->pdev, "ACPI _ROM region unavailable (%d)\n", ret);
+ kvfree(rom);
+}
+#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 e41437fa17ad0..0d77f860020e9 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -345,6 +345,15 @@ 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_ACPI */
+/*
+ * The device firmware's ACPI _ROM image, when available. The region size is
+ * the exact image length in bytes. This region supports read() and pread()
+ * only. It is independent of the PCI expansion ROM region.
+ */
+#define VFIO_REGION_SUBTYPE_ACPI_ROM (1)
/* sub-types for VFIO_REGION_TYPE_PCI_* */
--
MST
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v3] vfio/pci: expose ACPI _ROM through a device region
2026-12-03 14:35 [PATCH v3] vfio/pci: expose ACPI _ROM through a device region Michael S. Tsirkin
@ 2026-12-03 14:56 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 14:56 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: vfio_pci_register_acpi_rom() assumes short-read EOF
on ACPI _ROM
- [Medium] vfio/pci: vfio_pci_register_acpi_rom() omits
GFP_KERNEL_ACCOUNT and trim
--
commit adf1c581f5c9628496bf91c04bf27018695d4a66
Author: Michael S. Tsirkin <mst@redhat.com>
vfio/pci: expose ACPI _ROM through a device region
This commit adds a new VFIO_REGION_TYPE_ACPI region type with a
VFIO_REGION_SUBTYPE_ACPI_ROM subtype, snapshots the ACPI _ROM image at
device open, and exposes it as a read-only VFIO device region.
> diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
> index df88480876815..71ddf8f500508 100644
> --- a/drivers/vfio/pci/vfio_pci_rdwr.c
> +++ b/drivers/vfio/pci/vfio_pci_rdwr.c
> @@ -198,6 +201,118 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
[ ... ]
> +void vfio_pci_register_acpi_rom(struct vfio_pci_core_device *vdev)
> +{
[ ... ]
> + for (;;) {
> + struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
> + union acpi_object *obj;
> + size_t len;
> +
> + args[0].integer.value = size;
> + if (ACPI_FAILURE(acpi_evaluate_object_typed(method, NULL, ¶ms,
> + &result, ACPI_TYPE_BUFFER)))
> + break;
> +
> + obj = result.pointer;
> + len = obj->buffer.length;
> + if (len > SZ_4K || len > VFIO_PCI_ACPI_ROM_MAX - size) {
> + ret = -EFBIG;
> + kfree(obj);
> + break;
> + }
> + if (size + len > capacity) {
> + void *new_rom;
> +
> + capacity = max_t(size_t, SZ_4K, capacity * 2);
> + new_rom = kvrealloc(rom, capacity, GFP_KERNEL);
> + if (!new_rom) {
> + ret = -ENOMEM;
> + kfree(obj);
> + break;
> + }
> + rom = new_rom;
> + }
[Severity: Medium]
Should this allocation use GFP_KERNEL_ACCOUNT and be trimmed to size once
probing completes?
The cached ROM buffer can reach 16 MiB and stays pinned for the lifetime
of the open device. Using GFP_KERNEL leaves it outside the opening
process's memcg accounting, unlike other per-open allocations such as
vfio_pci_core_register_dev_region() and vfio_pci_ioeventfd().
Because capacity doubles and is never shrunk back to size, an image just
over a power-of-two boundary also keeps nearly twice as much kernel
memory allocated while the device remains open.
> + if (len)
> + memcpy(rom + size, obj->buffer.pointer, len);
> + kfree(obj);
> + size += len;
> +
> + if (len < SZ_4K) {
> + if (!size)
> + return;
> +
> + ret = vfio_pci_core_register_dev_region(vdev,
> + VFIO_REGION_TYPE_ACPI,
> + VFIO_REGION_SUBTYPE_ACPI_ROM,
> + &vfio_pci_acpi_rom_ops, size,
> + VFIO_REGION_INFO_FLAG_READ, rom);
> + if (!ret)
> + return;
> + break;
> + }
> + }
> +
> + pci_dbg(vdev->pdev, "ACPI _ROM region unavailable (%d)\n", ret);
> + kvfree(rom);
[Severity: High]
Can this loop discard valid 4 KiB-aligned ROMs by relying on a short read
to detect EOF?
GPU option ROM images are routinely exact multiples of 4 KiB, so every
valid chunk returns SZ_4K and the loop probes _ROM at offset == rom_size.
ACPI _ROM does not guarantee a short or zero-length buffer past EOF.
On fixed-size DSDT buffers or OperationRegions, that out-of-bounds probe
can fail with an AML error such as AE_AML_BUFFER_LIMIT or return a
non-Buffer, breaking straight to kvfree(rom) and discarding the ROM.
Conversely, firmware that returns 4 KiB zero-padded or clamped buffers
past EOF never hits len < SZ_4K, runs 4096 AML evaluations up to 16 MiB,
and fails with -EFBIG; if it returns trailing non-ROM bytes before a
short read, the region size also exceeds the actual image length.
Aborting with -EFBIG when len > SZ_4K also differs from existing in-tree
ACPI _ROM readers in nouveau and amdgpu, which clamp the length to SZ_4K.
Parsing the PCI Expansion ROM headers to determine the exact image
length, similar to pci_get_rom_size() or nouveau's shadow_image(), would
avoid probing blindly past EOF.
--
Sashiko AI review · https://sashiko.dev/#/patchset/147be6bed8795824d27429a8c717e2d1d08ad89f.1796307888.git.mst@redhat.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 14:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 14:35 [PATCH v3] vfio/pci: expose ACPI _ROM through a device region Michael S. Tsirkin
2026-12-03 14:56 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox