From: Alex Williamson <alex.williamson@nvidia.com>
To: <alex@shazbot.org>
Cc: Alex Williamson <alex.williamson@nvidia.com>,
<ptsm@linux.microsoft.com>, <nipun.gupta@amd.com>,
<nikhil.agarwal@amd.com>, <kvm@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: [PATCH v2 3/3] vfio/cdx: Consolidate MSI configured state onto cdx_irqs
Date: Fri, 17 Apr 2026 14:27:58 -0600 [thread overview]
Message-ID: <20260417202800.88287-4-alex.williamson@nvidia.com> (raw)
In-Reply-To: <20260417202800.88287-1-alex.williamson@nvidia.com>
struct vfio_cdx_device carries three fields that track whether MSI has
been configured: vdev->cdx_irqs (the allocated vector array), vdev->
msi_count (the array length), and vdev->config_msi (a boolean flag).
The three are set together when vfio_cdx_msi_enable() succeeds and
cleared together by vfio_cdx_msi_disable(). However, the error paths
in vfio_cdx_msi_enable() free the cdx_irqs allocation on failure
without resetting the pointer, leaving it stale and skewed from the
other two fields until the next enable call overwrites it.
Clear vdev->cdx_irqs to NULL alongside the kfree() in both error paths
so the pointer consistently reflects the configured state. With that
invariant restored and access to the MSI state serialized by
cdx_irqs_lock, vdev->config_msi is fully redundant with
(vdev->cdx_irqs != NULL). Drop the config_msi field and switch all
readers to test cdx_irqs directly.
Assisted-by: Claude:claude-opus-4-7
Signed-off-by: Alex Williamson <alex.williamson@nvidia.com>
---
drivers/vfio/cdx/intr.c | 29 ++++++++++++++---------------
drivers/vfio/cdx/private.h | 1 -
2 files changed, 14 insertions(+), 16 deletions(-)
diff --git a/drivers/vfio/cdx/intr.c b/drivers/vfio/cdx/intr.c
index 6dfe0ced3bdd..4439481fe633 100644
--- a/drivers/vfio/cdx/intr.c
+++ b/drivers/vfio/cdx/intr.c
@@ -32,26 +32,27 @@ static int vfio_cdx_msi_enable(struct vfio_cdx_device *vdev, int nvec)
return -ENOMEM;
ret = cdx_enable_msi(cdx_dev);
- if (ret) {
- kfree(vdev->cdx_irqs);
- return ret;
- }
+ if (ret)
+ goto err_free;
/* Allocate cdx MSIs */
ret = msi_domain_alloc_irqs(dev, MSI_DEFAULT_DOMAIN, nvec);
- if (ret) {
- cdx_disable_msi(cdx_dev);
- kfree(vdev->cdx_irqs);
- return ret;
- }
+ if (ret)
+ goto err_disable;
for (msi_idx = 0; msi_idx < nvec; msi_idx++)
vdev->cdx_irqs[msi_idx].irq_no = msi_get_virq(dev, msi_idx);
vdev->msi_count = nvec;
- vdev->config_msi = 1;
return 0;
+
+err_disable:
+ cdx_disable_msi(cdx_dev);
+err_free:
+ kfree(vdev->cdx_irqs);
+ vdev->cdx_irqs = NULL;
+ return ret;
}
static int vfio_cdx_msi_set_vector_signal(struct vfio_cdx_device *vdev,
@@ -129,7 +130,7 @@ static void vfio_cdx_msi_disable(struct vfio_cdx_device *vdev)
vfio_cdx_msi_set_block(vdev, 0, vdev->msi_count, NULL);
- if (!vdev->config_msi)
+ if (!vdev->cdx_irqs)
return;
msi_domain_free_irqs_all(dev, MSI_DEFAULT_DOMAIN);
@@ -138,7 +139,6 @@ static void vfio_cdx_msi_disable(struct vfio_cdx_device *vdev)
vdev->cdx_irqs = NULL;
vdev->msi_count = 0;
- vdev->config_msi = 0;
}
static int vfio_cdx_set_msi_trigger(struct vfio_cdx_device *vdev,
@@ -163,7 +163,7 @@ static int vfio_cdx_set_msi_trigger(struct vfio_cdx_device *vdev,
s32 *fds = data;
int ret;
- if (vdev->config_msi)
+ if (vdev->cdx_irqs)
return vfio_cdx_msi_set_block(vdev, start, count,
fds);
ret = vfio_cdx_msi_enable(vdev, cdx_dev->num_msi);
@@ -177,8 +177,7 @@ static int vfio_cdx_set_msi_trigger(struct vfio_cdx_device *vdev,
return ret;
}
- /* Ensure MSI is configured before accessing cdx_irqs */
- if (!vdev->config_msi)
+ if (!vdev->cdx_irqs)
return -EINVAL;
for (i = start; i < start + count; i++) {
diff --git a/drivers/vfio/cdx/private.h b/drivers/vfio/cdx/private.h
index 94374b5fc989..4c00bf633356 100644
--- a/drivers/vfio/cdx/private.h
+++ b/drivers/vfio/cdx/private.h
@@ -38,7 +38,6 @@ struct vfio_cdx_device {
u32 flags;
#define BME_SUPPORT BIT(0)
u32 msi_count;
- u8 config_msi;
};
#ifdef CONFIG_GENERIC_MSI_IRQ
--
2.51.0
next prev parent reply other threads:[~2026-04-17 20:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-17 20:27 [PATCH v2 0/3] vfio/cdx: Fix interrupt trigger races and consolidate MSI state Alex Williamson
2026-04-17 20:27 ` [PATCH v2 1/3] vfio/cdx: Fix NULL pointer dereference in interrupt trigger path Alex Williamson
2026-04-17 20:27 ` [PATCH v2 2/3] vfio/cdx: Serialize VFIO_DEVICE_SET_IRQS with a per-device mutex Alex Williamson
2026-04-17 20:27 ` Alex Williamson [this message]
2026-04-21 11:27 ` [PATCH v2 0/3] vfio/cdx: Fix interrupt trigger races and consolidate MSI state Agarwal, Nikhil
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=20260417202800.88287-4-alex.williamson@nvidia.com \
--to=alex.williamson@nvidia.com \
--cc=alex@shazbot.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nikhil.agarwal@amd.com \
--cc=nipun.gupta@amd.com \
--cc=ptsm@linux.microsoft.com \
/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
all inboxes | Powered by JetHome®