All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alex Williamson <alex@shazbot.org>
To: John Levon <john.levon@nutanix.com>
Cc: "Cédric Le Goater" <clg@redhat.com>,
	qemu-devel@nongnu.org,
	"Alex Williamson" <alex.williamson@redhat.com>,
	"John Johnson" <john.g.johnson@oracle.com>,
	"Elena Ufimtseva" <elena.ufimtseva@oracle.com>,
	"Jagannathan Raman" <jag.raman@oracle.com>
Subject: Re: [PULL 25/28] vfio: add region info cache
Date: Tue, 14 Oct 2025 08:29:49 -0600	[thread overview]
Message-ID: <20251014082937.7de6f960@shazbot.org> (raw)
In-Reply-To: <aO5XeknOrUtvLJO9@lent>

On Tue, 14 Oct 2025 16:00:26 +0200
John Levon <john.levon@nutanix.com> wrote:

> On Tue, Oct 14, 2025 at 07:49:24AM -0600, Alex Williamson wrote:
> 
> > > My apologies - we hit the exact same issue internally, but with a much older
> > > codebase, so I did not realise this could be an upstream problem as well!
> > >
> > > We put this down to a bug in the nvidia driver - surely it shouldn't be
> > > reporting fewer regions than are actually in use. So we applied what we
> > > thought to be a gross hack of boundary checking, and not using the region
> > > cache in case it's beyond num_regions.
> > >
> > > To put it another way, the header file says:
> > >
> > >    217         __u32   num_regions;    /* Max region index + 1 */
> > >
> > > If it's not actually the max region index + 1, what are the expected
> > > semantics of this field, or of region indices more generally? We could not
> > > find any clear documentation on the topic other than this comment.  
> > 
> > '9' only defines the end of the fixed, pre-defined region indexes for
> > vfio-pci, ie. VFIO_PCI_NUM_REGIONS.  Beyond that, we support device specific
> > regions.  The GFX region is one such device specific region.  
> 
> To be clear, nowhere in the code are we hard-coding 9, we ask the underlying
> vfio instance for the num_regions value.
> 
> In this case, it appears that the underlying instance is returning 9, and then
> trying to use index 9.

That sounds like a bug, in vfio_pci_ioctl_get_info() we return
num_regions as (VFIO_PCI_NUM_REGIONS + vdev->num_regions) where
vdev->num_regions is incremented via
vfio_pci_core_register_dev_region().  Any read, write, or mmap access
done through vfio-pci-core would need to increment vdev->num_regions to
support that access.  I think we're dealing with NVIDIA vGPU here,
which afaik is the only driver that uses a region for the display, so
I'd suspect they have a bug and don't use the vfio-pci-core callbacks
here.

Does QEMU's caching model need to fall through to GET_REGION_INFO and
expand the cache for an out-of-bounds index to handle such a bug?

> > There is no fixed limit to the number of regions a device may expose  
> 
> Sure.
> 
> >, nor is vfio_device_info.num_regions necessarily a static value.  
> 
> Ah - is this documented somewhere? And do you have an example of where this
> varies over the lifetime of a device currently?

It's more the case that it's not specifically defined as static over
the lifetime of the device.  I don't think we want device spontaneously
creating new regions, we have no device-check notification mechanism to
userspace to reevaluate regions, but a user initiated action defined to
create a new region seems like a fair vector where num_regions could be
updated.  Also the current working idea would be that regions are only
added, not modified or deleted.

> IOW, are these devices actually changing the num_regions value they report
> sometime after initialization (namely, can it change after
> vfio_device_prepare()) ?

In theory, yes.

> Or would querying num_regions still return the original value, and in fact the
> semantics of the field are "it's not the max region index, but the max region
> index during initialization" or similar ?

Ultimately it's just a marker that *should* indicate there are no
regions to query above (num_regions - 1).  It should be valid at the
time it's queried.  I don't think it would be valid that it could be
dynamic in any other way than a monotonically increasing value.  Thanks,

Alex


  reply	other threads:[~2025-10-14 14:31 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-09 13:12 [PULL 00/28] vfio queue Cédric Le Goater
2025-05-09 13:12 ` [PULL 01/28] vfio/container: ram discard disable helper Cédric Le Goater
2025-05-09 13:12 ` [PULL 02/28] vfio/container: reform vfio_container_connect cleanup Cédric Le Goater
2025-05-09 13:12 ` [PULL 03/28] vfio/container: vfio_container_group_add Cédric Le Goater
2025-05-09 13:12 ` [PULL 04/28] vfio/igd: Restrict legacy mode to Gen6-9 devices Cédric Le Goater
2025-05-09 13:12 ` [PULL 05/28] vfio/igd: Always emulate ASLS (OpRegion) register Cédric Le Goater
2025-05-09 13:12 ` [PULL 06/28] vfio/igd: Detect IGD device by OpRegion Cédric Le Goater
2025-05-09 13:12 ` [PULL 07/28] vfio/igd: Check vendor and device ID on GVT-g mdev Cédric Le Goater
2025-05-09 13:12 ` [PULL 08/28] vfio/igd: Check OpRegion support " Cédric Le Goater
2025-05-09 13:12 ` [PULL 09/28] vfio/igd: Enable OpRegion by default Cédric Le Goater
2025-05-09 13:12 ` [PULL 10/28] vfio/igd: Allow overriding GMS with 0xf0 to 0xfe on Gen9+ Cédric Le Goater
2025-05-09 13:13 ` [PULL 11/28] vfio/igd: Only emulate GGC register when x-igd-gms is set Cédric Le Goater
2025-05-09 13:13 ` [PULL 12/28] vfio/igd: Remove generation limitation for IGD passthrough Cédric Le Goater
2025-05-09 13:13 ` [PULL 13/28] linux-header: update-linux-header script changes Cédric Le Goater
2025-05-09 13:13 ` [PULL 14/28] linux-headers: Update to Linux v6.15-rc3 Cédric Le Goater
2025-05-09 13:13 ` [PULL 15/28] vfio: add vfio_device_prepare() Cédric Le Goater
2025-05-09 13:13 ` [PULL 16/28] vfio: add vfio_device_unprepare() Cédric Le Goater
2025-05-09 13:13 ` [PULL 17/28] vfio: add vfio_attach_device_by_iommu_type() Cédric Le Goater
2025-05-09 13:13 ` [PULL 18/28] vfio: add vfio_device_get_irq_info() helper Cédric Le Goater
2025-05-09 13:13 ` [PULL 19/28] vfio: consistently handle return value for helpers Cédric Le Goater
2025-05-09 13:13 ` [PULL 20/28] vfio: add strread/writeerror() Cédric Le Goater
2025-05-09 13:13 ` [PULL 21/28] vfio: add vfio_pci_config_space_read/write() Cédric Le Goater
2025-05-09 13:13 ` [PULL 22/28] vfio: add unmap_all flag to DMA unmap callback Cédric Le Goater
2025-05-09 13:13 ` [PULL 23/28] vfio: implement unmap all for DMA unmap callbacks Cédric Le Goater
2025-05-09 13:13 ` [PULL 24/28] vfio: add device IO ops vector Cédric Le Goater
2025-05-09 13:13 ` [PULL 25/28] vfio: add region info cache Cédric Le Goater
2025-10-14 13:16   ` Cédric Le Goater
2025-10-14 13:32     ` John Levon
2025-10-14 13:49       ` Alex Williamson
2025-10-14 13:58         ` Cédric Le Goater
2025-10-14 14:01           ` John Levon
2025-10-14 14:00         ` John Levon
2025-10-14 14:29           ` Alex Williamson [this message]
2025-10-14 14:36             ` John Levon
2025-05-09 13:13 ` [PULL 26/28] vfio: add read/write to device IO ops vector Cédric Le Goater
2025-05-09 13:13 ` [PULL 27/28] vfio: add vfio-pci-base class Cédric Le Goater
2025-05-09 13:13 ` [PULL 28/28] vfio/container: pass listener_begin/commit callbacks Cédric Le Goater
2025-05-10 18:36 ` [PULL 00/28] vfio queue Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251014082937.7de6f960@shazbot.org \
    --to=alex@shazbot.org \
    --cc=alex.williamson@redhat.com \
    --cc=clg@redhat.com \
    --cc=elena.ufimtseva@oracle.com \
    --cc=jag.raman@oracle.com \
    --cc=john.g.johnson@oracle.com \
    --cc=john.levon@nutanix.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.