Skip to content

Commit 670e886

Browse files
Alex Williamsonawilliam
authored andcommitted
vfio/cdx: Serialize VFIO_DEVICE_SET_IRQS with a per-device mutex
vfio_cdx_set_msi_trigger() reads vdev->config_msi and operates on the vdev->cdx_irqs array based on its value, but provides no serialization against concurrent VFIO_DEVICE_SET_IRQS ioctls. Two callers can race such that one observes config_msi as set while another clears it and frees cdx_irqs via vfio_cdx_msi_disable(), resulting in a use-after-free of the cdx_irqs array. Add a cdx_irqs_lock mutex to struct vfio_cdx_device and acquire it in vfio_cdx_set_msi_trigger(), which is the single chokepoint through which all updates to config_msi, cdx_irqs, and msi_count flow, covering both the ioctl path and the close-device cleanup path. This keeps the test of config_msi atomic with the subsequent enable, disable, or trigger operations. Drop the pre-call !cdx_irqs test from vfio_cdx_irqs_cleanup() as part of this change: the optimization it provided is redundant with the !config_msi early-return inside vfio_cdx_msi_disable(), and leaving the test in place would be an unsynchronized read of state the new lock is meant to protect. Fixes: 848e447 ("vfio/cdx: add interrupt support") Cc: stable@vger.kernel.org Assisted-by: Claude:claude-opus-4-7 Signed-off-by: Alex Williamson <alex.williamson@nvidia.com> Acked-by: Nikhil Agarwal <nikhil.agarwal@amd.com> Link: https://lore.kernel.org/r/20260417202800.88287-3-alex.williamson@nvidia.com Signed-off-by: Alex Williamson <alex@shazbot.org>
1 parent 5ea5880 commit 670e886

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

drivers/vfio/cdx/intr.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ static int vfio_cdx_set_msi_trigger(struct vfio_cdx_device *vdev,
152152
if (start + count > cdx_dev->num_msi)
153153
return -EINVAL;
154154

155+
guard(mutex)(&vdev->cdx_irqs_lock);
156+
155157
if (!count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
156158
vfio_cdx_msi_disable(vdev);
157159
return 0;
@@ -210,12 +212,5 @@ int vfio_cdx_set_irqs_ioctl(struct vfio_cdx_device *vdev,
210212
/* Free All IRQs for the given device */
211213
void vfio_cdx_irqs_cleanup(struct vfio_cdx_device *vdev)
212214
{
213-
/*
214-
* Device does not support any interrupt or the interrupts
215-
* were not configured
216-
*/
217-
if (!vdev->cdx_irqs)
218-
return;
219-
220215
vfio_cdx_set_msi_trigger(vdev, 0, 0, 0, VFIO_IRQ_SET_DATA_NONE, NULL);
221216
}

drivers/vfio/cdx/main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@
88

99
#include "private.h"
1010

11+
static int vfio_cdx_init_dev(struct vfio_device *core_vdev)
12+
{
13+
struct vfio_cdx_device *vdev =
14+
container_of(core_vdev, struct vfio_cdx_device, vdev);
15+
16+
mutex_init(&vdev->cdx_irqs_lock);
17+
return 0;
18+
}
19+
20+
static void vfio_cdx_release_dev(struct vfio_device *core_vdev)
21+
{
22+
struct vfio_cdx_device *vdev =
23+
container_of(core_vdev, struct vfio_cdx_device, vdev);
24+
25+
mutex_destroy(&vdev->cdx_irqs_lock);
26+
}
27+
1128
static int vfio_cdx_open_device(struct vfio_device *core_vdev)
1229
{
1330
struct vfio_cdx_device *vdev =
@@ -273,6 +290,8 @@ static int vfio_cdx_mmap(struct vfio_device *core_vdev,
273290

274291
static const struct vfio_device_ops vfio_cdx_ops = {
275292
.name = "vfio-cdx",
293+
.init = vfio_cdx_init_dev,
294+
.release = vfio_cdx_release_dev,
276295
.open_device = vfio_cdx_open_device,
277296
.close_device = vfio_cdx_close_device,
278297
.ioctl = vfio_cdx_ioctl,

drivers/vfio/cdx/private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef VFIO_CDX_PRIVATE_H
77
#define VFIO_CDX_PRIVATE_H
88

9+
#include <linux/mutex.h>
10+
911
#define VFIO_CDX_OFFSET_SHIFT 40
1012

1113
static inline u64 vfio_cdx_index_to_offset(u32 index)
@@ -31,6 +33,7 @@ struct vfio_cdx_region {
3133
struct vfio_cdx_device {
3234
struct vfio_device vdev;
3335
struct vfio_cdx_region *regions;
36+
struct mutex cdx_irqs_lock;
3437
struct vfio_cdx_irq *cdx_irqs;
3538
u32 flags;
3639
#define BME_SUPPORT BIT(0)

0 commit comments

Comments
 (0)