qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Cédric Le Goater" <clg@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Alex Williamson" <alex.williamson@redhat.com>,
	"John Levon" <john.levon@nutanix.com>,
	"Cédric Le Goater" <clg@redhat.com>,
	"Alex Williamson" <alex@shazbot.org>
Subject: [PULL 12/12] vfio: only check region info cache for initial regions
Date: Wed, 22 Oct 2025 14:18:46 +0200	[thread overview]
Message-ID: <20251022121846.874152-13-clg@redhat.com> (raw)
In-Reply-To: <20251022121846.874152-1-clg@redhat.com>

From: John Levon <john.levon@nutanix.com>

It is semantically valid for a VFIO device to increase the number of
regions after initialization. In this case, we'd attempt to check for
cached region info past the size of the ->reginfo array. Check for the
region index and skip the cache in these cases.

This also works around some VGPU use cases which appear to be a bug,
where VFIO_DEVICE_QUERY_GFX_PLANE returns a region index beyond the
reported ->num_regions.

Fixes: 95cdb024 ("vfio: add region info cache")
Signed-off-by: John Levon <john.levon@nutanix.com>
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Reviewed-by: Alex Williamson <alex@shazbot.org>
Link: https://lore.kernel.org/qemu-devel/20251014151227.2298892-3-john.levon@nutanix.com
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
 hw/vfio/device.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 52079f4cf5bda7c1d9e6e3791a6c340a0e3f57ba..8b63e765acbacdccc5365d000e2aa9bd49ae9701 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -205,10 +205,19 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
     int fd = -1;
     int ret;
 
-    /* check cache */
-    if (vbasedev->reginfo[index] != NULL) {
-        *info = vbasedev->reginfo[index];
-        return 0;
+    /*
+     * We only set up the region info cache for the initial number of regions.
+     *
+     * Since a VFIO device may later increase the number of regions then use
+     * such regions with an index past ->num_initial_regions, don't attempt to
+     * use the info cache in those cases.
+     */
+    if (index < vbasedev->num_initial_regions) {
+        /* check cache */
+        if (vbasedev->reginfo[index] != NULL) {
+            *info = vbasedev->reginfo[index];
+            return 0;
+        }
     }
 
     *info = g_malloc0(argsz);
@@ -236,10 +245,12 @@ retry:
         goto retry;
     }
 
-    /* fill cache */
-    vbasedev->reginfo[index] = *info;
-    if (vbasedev->region_fds != NULL) {
-        vbasedev->region_fds[index] = fd;
+    if (index < vbasedev->num_initial_regions) {
+        /* fill cache */
+        vbasedev->reginfo[index] = *info;
+        if (vbasedev->region_fds != NULL) {
+            vbasedev->region_fds[index] = fd;
+        }
     }
 
     return 0;
-- 
2.51.0



  parent reply	other threads:[~2025-10-22 12:21 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-22 12:18 [PULL 00/12] vfio queue Cédric Le Goater
2025-10-22 12:18 ` [PULL 01/12] vfio/container: Remap only populated parts in a section Cédric Le Goater
2025-10-22 12:18 ` [PULL 02/12] vfio/cpr-legacy: drop an erroneous assert Cédric Le Goater
2025-10-22 12:18 ` [PULL 03/12] vfio/iommufd: Set cpr.ioas_id on source side for CPR transfer Cédric Le Goater
2025-10-22 12:18 ` [PULL 04/12] vfio/iommufd: Restore vbasedev's reference to hwpt after " Cédric Le Goater
2025-10-22 12:18 ` [PULL 05/12] accel/kvm: Fix an erroneous check on coalesced_mmio_ring Cédric Le Goater
2025-10-22 12:18 ` [PULL 06/12] vfio/container: Support unmap all in one ioctl() Cédric Le Goater
2025-10-22 12:18 ` [PULL 07/12] vfio/iommufd: " Cédric Le Goater
2025-10-22 12:18 ` [PULL 08/12] vfio/listener: Add an assertion for unmap_all Cédric Le Goater
2025-10-22 12:18 ` [PULL 09/12] docs/system/devices/vfio-user: fix formatting Cédric Le Goater
2025-10-22 12:18 ` [PULL 10/12] MAINTAINERS: Update Alex Williamson's email address Cédric Le Goater
2025-10-22 12:18 ` [PULL 11/12] vfio: rename field to "num_initial_regions" Cédric Le Goater
2025-10-22 12:18 ` Cédric Le Goater [this message]
2025-10-22 12:55 ` [PULL 00/12] vfio queue Cédric Le Goater
2025-10-23  9:53   ` Michael Tokarev
2025-10-23 12:16     ` Cédric Le Goater
2025-10-22 14:31 ` Richard Henderson

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=20251022121846.874152-13-clg@redhat.com \
    --to=clg@redhat.com \
    --cc=alex.williamson@redhat.com \
    --cc=alex@shazbot.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).