From: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
To: linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org, virtualization@lists.linux.dev,
alex@shazbot.org, den@openvz.org, khorenko@virtuozzo.com,
andrey.drobyshev@virtuozzo.com
Subject: [PATCH 1/1] vfio: Take the KVM reference only for drivers that read device->kvm
Date: Fri, 20 Nov 2026 17:47:30 +0200 [thread overview]
Message-ID: <20261120154730.882393-2-andrey.drobyshev@virtuozzo.com> (raw)
In-Reply-To: <20261120154730.882393-1-andrey.drobyshev@virtuozzo.com>
A device takes a reference to the KVM file of its group or device file
at the first open and drops it at the last close, so that device->kvm
stays valid for the driver in between.
QEMU's cpr-exec and cpr-transfer keep the device open while the VM is
re-created. The first VM then stays referenced for as long as the
device is open: its file is not released, and its vcpus, mm and
in-kernel PIT thread stay with it.
To see it, start QEMU with a vfio-pci device, run cpr-exec and trace
kvm_create_vm() and kvm_destroy_vm(). The old VM is not destroyed at
the exec, and two VMs stay alive until QEMU exits. Without the device
the old VM is destroyed at the exec. The new VM also logs
kvm: debugfs: duplicate directory <pid>-<fd>
because it gets the pid and fd number of the old one.
Only three drivers read device->kvm: KVMGT, vfio_ap and vfio-pci's
zdev support. vfio_ap reads it only in open_device(), zdev also at
close, KVMGT also at close and while the guest runs. Let them say so
with needs_kvm_ref. Without it device->kvm stays NULL and no reference
is taken, so the device can't keep a VM alive.
Fixes: 2b48f52f2bff ("vfio: fix deadlock between group lock and kvm lock")
Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
---
drivers/gpu/drm/i915/gvt/kvmgt.c | 1 +
drivers/s390/crypto/vfio_ap_ops.c | 1 +
drivers/vfio/device_cdev.c | 3 ++-
drivers/vfio/group.c | 3 ++-
drivers/vfio/pci/vfio_pci_core.c | 1 +
drivers/vfio/vfio_main.c | 6 ++++--
include/linux/vfio.h | 2 ++
7 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index ec62db5cc367..3e05ad946c5d 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1420,6 +1420,7 @@ static int intel_vgpu_init_dev(struct vfio_device *vfio_dev)
int ret;
vgpu->gvt = kdev_to_i915(mdev->type->parent->dev)->gvt;
+ vfio_dev->needs_kvm_ref = true;
ret = intel_gvt_create_vgpu(vgpu, type->conf);
if (ret)
return ret;
diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index 373ce574c431..5ec858389ba3 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -786,6 +786,7 @@ static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
container_of(vdev, struct ap_matrix_mdev, vdev);
matrix_mdev->mdev = to_mdev_device(vdev->dev);
+ vdev->needs_kvm_ref = true;
vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
matrix_mdev->pqap_hook = handle_pqap;
vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c
index 92627295182e..d661bdeb1834 100644
--- a/drivers/vfio/device_cdev.c
+++ b/drivers/vfio/device_cdev.c
@@ -157,7 +157,8 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
* Before the device open, get the KVM pointer currently
* associated with the device file (if there is) and obtain
* a reference. This reference is held until device closed.
- * Save the pointer in the device for use by drivers.
+ * Save the pointer in the device for use by drivers. Both only
+ * if the driver set needs_kvm_ref.
*/
vfio_df_get_kvm_safe(df);
diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c
index 185f359f75d3..1b7b4f907895 100644
--- a/drivers/vfio/group.c
+++ b/drivers/vfio/group.c
@@ -184,7 +184,8 @@ static int vfio_df_group_open(struct vfio_device_file *df)
* Before the first device open, get the KVM pointer currently
* associated with the group (if there is one) and obtain a reference
* now that will be held until the open_count reaches 0 again. Save
- * the pointer in the device for use by drivers.
+ * the pointer in the device for use by drivers. Both only if the
+ * driver set needs_kvm_ref.
*/
if (device->open_count == 0)
vfio_device_group_get_kvm_safe(device);
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index fcbb2da860e2..b1689f9325e4 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -2173,6 +2173,7 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)
vdev->pdev = to_pci_dev(core_vdev->dev);
vdev->irq_type = VFIO_PCI_NUM_IRQS;
+ core_vdev->needs_kvm_ref = IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM);
mutex_init(&vdev->igate);
spin_lock_init(&vdev->irqlock);
mutex_init(&vdev->ioeventfds_lock);
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 4e5d738ccf4b..aa26c4e4c6e0 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -476,7 +476,8 @@ void vfio_device_get_kvm(struct vfio_device *device, struct file *kvm)
{
lockdep_assert_held(&device->dev_set->lock);
- if (!kvm)
+ /* Only a driver that reads device->kvm needs the KVM referenced */
+ if (!kvm || !device->needs_kvm_ref)
return;
device->kvm = get_file(kvm);
@@ -1536,7 +1537,8 @@ static void vfio_device_file_set_kvm(struct file *file, struct file *kvm)
* @kvm: KVM file to link
*
* When a VFIO device is first opened the KVM will be available in
- * device->kvm if one was associated with the file.
+ * device->kvm if one was associated with the file and the driver set
+ * needs_kvm_ref.
*/
void vfio_file_set_kvm(struct file *vfio_file, struct file *kvm)
{
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 0cc91c6f96d2..debf75e83cae 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -54,6 +54,8 @@ struct vfio_device {
struct list_head dev_set_list;
unsigned int migration_flags;
u8 precopy_info_v2;
+ /* Drivers that use kvm must set this before registering the device */
+ bool needs_kvm_ref;
struct file *kvm;
/* Members below here are private, not for driver use */
--
2.47.1
next prev parent reply other threads:[~2026-11-20 15:47 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 15:47 [PATCH 0/1] vfio: fix KVM reference leak on QEMU CPR Andrey Drobyshev
2026-11-20 15:47 ` Andrey Drobyshev [this message]
2026-11-20 15:55 ` [PATCH 1/1] vfio: Take the KVM reference only for drivers that read device->kvm sashiko-bot
2026-11-20 16:52 ` [PATCH 0/1] vfio: fix KVM reference leak on QEMU CPR Paolo Bonzini
2026-11-20 18:54 ` Jason Gunthorpe
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=20261120154730.882393-2-andrey.drobyshev@virtuozzo.com \
--to=andrey.drobyshev@virtuozzo.com \
--cc=alex@shazbot.org \
--cc=den@openvz.org \
--cc=khorenko@virtuozzo.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=virtualization@lists.linux.dev \
/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