Re: [PATCH v3 4/5] KVM/vfio: Use file-based reference counting for KVM
From: Jason J. Herne
Date: Thu Sep 24 2026 - 08:52:06 EST
On 9/24/26 4:58 AM, Steffen Eiden wrote:
Replace manual module reference counting with file-based referenceReviewed-by: Jason J. Herne <jjherne@xxxxxxxxxxxxx>
counting for KVM integration. Previously, VFIO used symbol_get() to
obtain function pointers for kvm_get_kvm_safe() and kvm_put_kvm(),
then manually tracked module references through these symbols. This
approach required storing the put_kvm function pointer in each device
and carefully managing symbol references.
Pass struct file pointers instead of struct kvm pointers throughout the
VFIO-KVM interface, leveraging the kernel's existing file reference
counting via get_file()/get_file_active() and fput(). Convert the x86
page-track API and update s390 vfio to use file_to_kvm_<arch>(). This
simplifies the code and removes all remaining externally exported
symbols for KVM which would appear twice for when a second concurrent
KVM module is introduced.
Suggested-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
Co-developed-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Signed-off-by: Steffen Eiden <seiden@xxxxxxxxxxxxx>
---
arch/s390/include/asm/kvm_host_s390.h | 2 +-
arch/s390/kvm/s390/pci.c | 9 ++++--
arch/x86/include/asm/kvm_page_track.h | 8 ++---
arch/x86/kvm/mmu/page_track.c | 22 +++++++++-----
drivers/s390/crypto/vfio_ap_ops.c | 20 ++++++++----
drivers/vfio/group.c | 11 ++++++-
drivers/vfio/vfio.h | 12 ++++----
drivers/vfio/vfio_main.c | 57 +++++++++++------------------------
include/linux/vfio.h | 5 ++-
virt/kvm/vfio.c | 13 +++++---
10 files changed, 85 insertions(+), 74 deletions(-)
...
diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index 940c0ff668be..556e643244f2 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -1822,17 +1822,27 @@ static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
/**
* vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
- * to manage AP resources for the guest whose state is represented by @kvm
+ * to manage AP resources for the guest whose state is represented by
+ * @kvm_file
*
* @matrix_mdev: a mediated matrix device
- * @kvm: reference to KVM instance
+ * @kvm_file: the KVM VM file this vfio device is associated with
*
- * Return: 0 if no other mediated matrix device has a reference to @kvm;
+ * Return: 0 if no other mediated matrix device has a reference to the VM;
* otherwise, returns an -EPERM.
*/
static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
- struct kvm *kvm)
+ struct file *kvm_file)
{
+ struct kvm *kvm;
+
+ if (!kvm_file)
+ return -ENOENT;
+
+ kvm = file_to_kvm_s390(kvm_file);
+ if (!kvm)
+ return -ENOENT;
+
if (kvm->arch.crypto.crycbd) {
get_update_locks_for_kvm(kvm);
if (kvm->arch.crypto.pqap_hook) {
@@ -1841,7 +1851,6 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
}
kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
- kvm_get_kvm(kvm);
matrix_mdev->kvm = kvm;
vfio_ap_mdev_update_guest_apcb(matrix_mdev);
release_update_locks_for_kvm(kvm);
@@ -1894,7 +1903,6 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
matrix_mdev->kvm = NULL;
release_update_locks_for_kvm(kvm);
- kvm_put_kvm(kvm);
}
}