Re: [PATCH v3] vfio/pci: expose ACPI _ROM through a device region

From: Michael S. Tsirkin

Date: Fri Dec 04 2026 - 04:27:43 EST


On Thu, Dec 03, 2026 at 07:03:09PM -0700, Alex Williamson wrote:
> On Thu, 3 Dec 2026 18:06:34 -0500
> "Michael S. Tsirkin" <mst@xxxxxxxxxx> wrote:
>
> > On Thu, Dec 03, 2026 at 02:57:09PM -0700, Alex Williamson wrote:
> > > On Thu, 3 Dec 2026 09:35:01 -0500
> > > "Michael S. Tsirkin" <mst@xxxxxxxxxx> wrote:
> > > > +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.
> > > > + */
> > >
> > > Can you cite some firmware that has this requirement? I don't want to
> > > come back to this in the future without an example of firmware that
> > > imposes this requirement.
> >
> > Yes, it's in nouveau history:
> >
> > In 2012 Ben Skeggs changed the initial ACPI ROM header probe
> > from _ROM(0, 3) to _ROM(0, 4096) for firmware compatibility.
> >
> > https://github.com/torvalds/linux/commit/d1626a965141
>
> Let's cite the history in the comment then.
>
> > > That alone also doesn't feel like it justifies allocating up to 16MB of
> > > kernel memory and holding it for the whole user session.
> >
> > 16M of virtually contiguos memory a problem?
> >
> > Well I started with reading exactly what userspace requested. I then
> > got a comment that userspace reading byte by byte will invoke 4K ACPI
> > calls per page. So now I am caching it all. It is simple, at least.
>
> I think this was sashiko's comment on v1, where every call to
> vfio_pci_bar_rw() performed a brute force read of the full _ROM image
> to reevaluate the length. The example it provided was a user simply
> trying to read a 128KB ROM image with 4-byte accesses would trigger the
> sizing loop 32,768 times. Extrapolating, each loop would make 32 _ROM
> calls, plus another for the access itself, results in over a million
> _ROM calls.
>
> We can actually strategically size the ROM with just a few _ROM reads,
> cache that size, then perform one _ROM access for each of those 4-byte
> accesses. That's a one time sizing and 33x fewer _ROM calls to read
> the full image. That 33x scales linearly with the ROM size. This is
> what my v1 reply demonstrated.
>
> 16MB of kernel memory, that isn't even using GFP_KERNEL_ACCOUNT, that
> is rarely accessed, and doesn't even have a userspace that uses it yet,
> does seem excessive, IMO.
>
> > > We can
> > > perform reads with 4K alignment and return short reads to get the user
> > > onto the alignment we require.
> > >
> > > Sashiko notes a couple issues, particularly in this method of sizing
> > > the ROM. Why shouldn't we walk the ROM image chain read through _ROM
> > > to determine the size, save only that size in the init phase, then do
> > > aligned reads (if necessary), on demand as the user accesses?
> >
> > I don't mind, but didn't you worry about malformed firmware earlier?
> > Something about firmware burners?
>
> It's the same algorithm that pci_get_rom_size() uses on ROM BARs. On
> the PCI side the extent is bounded by the ROM BAR resource size, so
> we'd need some pragmatic limit to replace that. A short read or error
> walking the image chain should fail and generate a pci_info(). Probably
> also a good idea to test reading the last byte of the image chain.

I actually have a fairly simple idea. How about we just declare
that the size value is unavailable for this region? Set it to 0.
Let's have userspace walk the PCIR chain. QEMU already knows the format.
Drastically less kernel code. I'll post v4 with this idea.



> The burners issue was that some users update device firmware from the
> VM, so the previously proposed fall through (v2) could result in
> presenting an inconsistent view of the ROM BAR. Maybe the ROM BAR,
> maybe the _ROM. This isn't an issue with the separate device specific
> region, especially where the ROM image isn't provided by the device
> itself. It becomes a userspace problem not to repeat the fall-through
> trap.
>
> > > Aside from the alignment, and the device specific region proposed here,
> > > I basically proposed those mechanics in
> > > https://lore.kernel.org/all/20261015112719.20d08b4c@xxxxxxxxxxx/
> >
> > You actually complained about "letting the user so easily
> > monopolize the ACPI interpreter lock". If we invoke ACPI
> > in response to userspace system calls we do exactly that.
> > Confused.
>
> I believe that was my reiteration of the sashiko finding above, where I
> replied with the example code that does both the strategic and cached
> sizing, and on-demand _ROM calls for user access.
>
> I have waffled in my own thinking whether to use cached or on-demand
> access, but I think a few points skew in the direction of on-demand:
>
> - _ROM methods likely only exist for onboard devices
> - The known use cases are laptops
> - Laptops are often memory constrained
> - Laptops are rarely multi-tenant VM hosts
>
> Additionally, with the device specific region we can define our own
> access semantics and deprecate mechanisms that don't work. We could
> even define a read-once semantic if we needed to, versus being governed
> by the PCI spec for the ROM access semantics. Thanks,
>
> Alex