From nobody Thu Jun 18 09:18:25 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA2393AA507 for ; Tue, 7 Apr 2026 18:01:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775584886; cv=none; b=aPNeE8lIgeMMadk52+0wLcUUuwYyNnK5SmvKzxVgQkoN1kvOHN7ibA3qqgpO/d2Ul7XGbSo69d50fmfEGQ8t6gmtFsK+i+AYIXJrqhOo+tKiEvHqM2eR8va6kCtHx91QVsWgsGtOxI6+7T/uGYR7KDL2sH4jl7dErdvrqwg5g38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775584886; c=relaxed/simple; bh=fCKRONX8yM7lQOG/vgBN8rFi8saRuPtzgJkTstXrnz8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Jq8V/StCAN8XqdjShpxuj22Jz1eK6g1vka/mFJcba68Q+vzITNDg52HouRkLfevXBTjxgvxHhv+lvoK3rFQIqJulPPqvF9CJsfLSSGMiWAIxvVoHoGHzFndhQH+JNTOwCwe2XzW36bIURlUySOt7UbpoCv8XU2U8+/aOTVIQuGA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=B4CFFyie; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=CKmbOM6y; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="B4CFFyie"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="CKmbOM6y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1775584884; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F52AVfFEhfnHhPUaSAFo1kWAX0sZksEa7u1ua5dSj1E=; b=B4CFFyieDpd8lFvLy8MQPaYc38TGW24TrsvCxxVCUiYsgm5ASjJRrNHT0FaOdRTiM2X9Xb Ha0C+eouiFJRnkkBs0mVPuYALfrXnMUk7B32h00u4nR3Tu5Pjdmv85dcI/94eHX+eOcgx9 siCWYPxNc7FqysobGMsGTwnJ7ncEX7s= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-634-VGQnGBTiPDW8PdaaTiUUCA-1; Tue, 07 Apr 2026 14:01:22 -0400 X-MC-Unique: VGQnGBTiPDW8PdaaTiUUCA-1 X-Mimecast-MFC-AGG-ID: VGQnGBTiPDW8PdaaTiUUCA_1775584880 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-483786a09b1so69422005e9.3 for ; Tue, 07 Apr 2026 11:01:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1775584879; x=1776189679; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=F52AVfFEhfnHhPUaSAFo1kWAX0sZksEa7u1ua5dSj1E=; b=CKmbOM6ykDoI+NT1tKqGDU5eqqauVRAHVg8azJCmwqQDoRUgNo+pciHY38ILMLQJwm qj5Was841CulBxHDKdib60YsVHH76tKP3jHprlPkLkxkUIkHW95pbpz3UoLX/Y+8MVGh JBrjJIHo9Eom4bIiPbWbnlN/UVbgddJBmbe1hJfP/SWVOvrsibF4k0vGKoQOo5n53IX0 wkItlxg0DL7Rq4E9Jl+TPhKq+5RMZkw3YNqatq6PcX9Ikr53JvMUpZFtiaRZIBIoG6KN D+J/tnU76EHPo3iUokP2u4qzbx4+VDyAKicXcN1aLvXTBesZ2xNn+t61Ztm/1uw/bnwi 5Xng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775584879; x=1776189679; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=F52AVfFEhfnHhPUaSAFo1kWAX0sZksEa7u1ua5dSj1E=; b=rKZOx+nQc7q7B7XLrarE2r8cl+ZEPZhyg8N00OXxMzNhAOS8OWR13VOCArB/A04zUO Lo0mhilrzvBcCUPVU8b2aJeM1M4nj8wh0Vy7dHNhz4IbxZl6jvT6OMYbLJcsQ/TWL84U BZ79zk57Ggw4mCineGF50oNLq74sxe17ynd5q+vdz3bsgzH3Q9QT06BUw3+LOlULpqfO V3vx7uiz66jvC9ty+gGAEK3kHDsVcgyfXmGFeVzMsxuA+NRQNmGmc0cZF5sEFRwmiLOC RwddcioV0OlsAK7MnZRT2AjgvNPBoFe65HF7dooG/t4NSY3uNekCApAr+u+IrT3rNDS+ Q/XA== X-Gm-Message-State: AOJu0YxgqphlgUKfbR7tcKEgqSGN68bJ5VUz3slqAlHli/sxhV8YvWVS F5vnZumgXNmSnpbKF6Ym8jYNs4WgYDDHfwKatVFr/m9f+eAU2368UsNNMV4sWp0E75+/4IKBGZF 1Ehqr4uLMsSglsB7yl8LVvCu/f8f795VDJgqjsVmmn/XvREL/RKVi2G7Wzm+ANXFOlQd7oTUJ+X NFe48/47RWBt9DelG6AwNj+NI0bejZ/7/MVCBpRAAXxhApc7lUIQ== X-Gm-Gg: AeBDieuaMDxXrfTWxsW8ZIMAtnlzDhEzZB+TABCZAtWfoPATn8IJkzVU61rJ7j9Pguy GP4xUfziwcoQsf612L94jctSspL7BRVQ0JjJ9bLZf5yCAxnXn5eq8hJI6JrlR/OGq9iB45G4xui Uu65fMnMaeffeKCBUgdI2N9VCy6gM46xRcMQGx2FVD800RMJ7U3SQSvrdEBK/dSPy1Qpmc37lSo gL2Cer36xCOSD6AU1zr4VUGHvkrlz/bNJaj2/WX43RRPRp0AYvzetSlnkrI9yhEqFHtOkidxXVK 1Xztho9jvhrtMkPo5iaZ3hvw2T336RRfRt/Q886zzQlSJ0hlwIiVUfsH6UutGF/ee5WrEHTSCik lg7OrOKDePrXhS/CQS/wYYKLE8oA9pkTGMIEFlvz5m5iHmJYB1W/GcRf7R8NLLeX6dfryuI8PzK dMpa6b/+MEkie0Qjh1fS5GdsvN X-Received: by 2002:a05:600c:628e:b0:480:20f1:7aa6 with SMTP id 5b1f17b1804b1-488997dd822mr232166705e9.21.1775584879276; Tue, 07 Apr 2026 11:01:19 -0700 (PDT) X-Received: by 2002:a05:600c:628e:b0:480:20f1:7aa6 with SMTP id 5b1f17b1804b1-488997dd822mr232166265e9.21.1775584878698; Tue, 07 Apr 2026 11:01:18 -0700 (PDT) Received: from [192.168.10.48] ([151.49.85.67]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-488c531ab02sm2465625e9.1.2026.04.07.11.01.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2026 11:01:16 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Steffen Eiden , Alex Williamson Subject: [PATCH 1/3] VFIO: take reference to the KVM module Date: Tue, 7 Apr 2026 20:01:05 +0200 Message-ID: <20260407180107.1603697-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260407180107.1603697-1-pbonzini@redhat.com> References: <20260407180107.1603697-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" VFIO is implicitly taking a reference to the KVM module between vfio_device_get_kvm_safe and vfio_device_put_kvm, thanks to symbol_get and symbol_put. In preparation for removing symbol_get and symbol_put themselves from VFIO, actually store a pointer to the KVM module and use module_get()/module_put() to keep KVM alive. Signed-off-by: Paolo Bonzini Acked-by: Alex Williamson Reviewed-by: Steffen Eiden --- drivers/vfio/device_cdev.c | 2 +- drivers/vfio/group.c | 5 +++-- drivers/vfio/vfio.h | 15 ++++++++++----- drivers/vfio/vfio_main.c | 39 +++++++++++++++++++++++--------------- include/linux/vfio.h | 3 ++- virt/kvm/vfio.c | 4 ++-- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index 8ceca24ac136..a67d7215c239 100644 --- a/drivers/vfio/device_cdev.c +++ b/drivers/vfio/device_cdev.c @@ -56,7 +56,7 @@ int vfio_device_fops_cdev_open(struct inode *inode, struc= t file *filep) static void vfio_df_get_kvm_safe(struct vfio_device_file *df) { spin_lock(&df->kvm_ref_lock); - vfio_device_get_kvm_safe(df->device, df->kvm); + vfio_device_get_kvm_safe(df->device, df->kvm, df->kvm_module); spin_unlock(&df->kvm_ref_lock); } =20 diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index 4f15016d2a5f..7d28f45fefaa 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -158,7 +158,7 @@ static int vfio_group_ioctl_set_container(struct vfio_g= roup *group, static void vfio_device_group_get_kvm_safe(struct vfio_device *device) { spin_lock(&device->group->kvm_ref_lock); - vfio_device_get_kvm_safe(device, device->group->kvm); + vfio_device_get_kvm_safe(device, device->group->kvm, device->group->kvm_m= odule); spin_unlock(&device->group->kvm_ref_lock); } =20 @@ -858,10 +858,11 @@ bool vfio_group_enforced_coherent(struct vfio_group *= group) return ret; } =20 -void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) +void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm, struct = module *kvm_module) { spin_lock(&group->kvm_ref_lock); group->kvm =3D kvm; + group->kvm_module =3D kvm_module; spin_unlock(&group->kvm_ref_lock); } =20 diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 50128da18bca..a0c38f89b30a 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -22,8 +22,9 @@ struct vfio_device_file { =20 u8 access_granted; u32 devid; /* only valid when iommufd is valid */ - spinlock_t kvm_ref_lock; /* protect kvm field */ + spinlock_t kvm_ref_lock; /* protect kvm and kvm_module fields */ struct kvm *kvm; + struct module *kvm_module; struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock= */ }; =20 @@ -89,6 +90,7 @@ struct vfio_group { enum vfio_group_type type; struct mutex group_lock; struct kvm *kvm; + struct module *kvm_module; struct file *opened_file; struct blocking_notifier_head notifier; struct iommufd_ctx *iommufd; @@ -108,7 +110,7 @@ void vfio_device_group_unuse_iommu(struct vfio_device *= device); void vfio_df_group_close(struct vfio_device_file *df); struct vfio_group *vfio_group_from_file(struct file *file); bool vfio_group_enforced_coherent(struct vfio_group *group); -void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); +void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm, struct = module *kvm_module); bool vfio_device_has_container(struct vfio_device *device); int __init vfio_group_init(void); void vfio_group_cleanup(void); @@ -171,7 +173,8 @@ static inline bool vfio_group_enforced_coherent(struct = vfio_group *group) return true; } =20 -static inline void vfio_group_set_kvm(struct vfio_group *group, struct kvm= *kvm) +static inline void vfio_group_set_kvm(struct vfio_group *group, struct kvm= *kvm, + struct module *kvm_module) { } =20 @@ -435,11 +438,13 @@ static inline void vfio_virqfd_exit(void) #endif =20 #if IS_ENABLED(CONFIG_KVM) -void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm); +void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm, + struct module *kvm_module); void vfio_device_put_kvm(struct vfio_device *device); #else static inline void vfio_device_get_kvm_safe(struct vfio_device *device, - struct kvm *kvm) + struct kvm *kvm, + struct module *kvm_module) { } =20 diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 742477546b15..d1bbc42d484a 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -433,7 +433,7 @@ void vfio_unregister_group_dev(struct vfio_device *devi= ce) EXPORT_SYMBOL_GPL(vfio_unregister_group_dev); =20 #if IS_ENABLED(CONFIG_KVM) -void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm) +void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm,= struct module *kvm_module) { void (*pfn)(struct kvm *kvm); bool (*fn)(struct kvm *kvm); @@ -444,25 +444,31 @@ void vfio_device_get_kvm_safe(struct vfio_device *dev= ice, struct kvm *kvm) if (!kvm) return; =20 - pfn =3D symbol_get(kvm_put_kvm); - if (WARN_ON(!pfn)) + if (!try_module_get(kvm_module)) return; =20 + pfn =3D symbol_get(kvm_put_kvm); + if (WARN_ON(!pfn)) + goto out_put_mod; + fn =3D symbol_get(kvm_get_kvm_safe); - if (WARN_ON(!fn)) { - symbol_put(kvm_put_kvm); - return; - } + if (WARN_ON(!fn)) + goto out_put_sym; =20 ret =3D fn(kvm); symbol_put(kvm_get_kvm_safe); - if (!ret) { - symbol_put(kvm_put_kvm); - return; - } + if (!ret) + goto out_put_sym; =20 device->put_kvm =3D pfn; device->kvm =3D kvm; + device->kvm_module =3D kvm_module; + return; + +out_put_sym: + symbol_put(kvm_put_kvm); +out_put_mod: + module_put(kvm_module); } =20 void vfio_device_put_kvm(struct vfio_device *device) @@ -481,6 +487,8 @@ void vfio_device_put_kvm(struct vfio_device *device) =20 clear: device->kvm =3D NULL; + module_put(device->kvm_module); + device->kvm_module =3D NULL; } #endif =20 @@ -1483,7 +1491,7 @@ bool vfio_file_enforced_coherent(struct file *file) } EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent); =20 -static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm) +static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm, s= truct module *kvm_module) { struct vfio_device_file *df =3D file->private_data; =20 @@ -1494,6 +1502,7 @@ static void vfio_device_file_set_kvm(struct file *fil= e, struct kvm *kvm) */ spin_lock(&df->kvm_ref_lock); df->kvm =3D kvm; + df->kvm_module =3D kvm_module; spin_unlock(&df->kvm_ref_lock); } =20 @@ -1505,16 +1514,16 @@ static void vfio_device_file_set_kvm(struct file *f= ile, struct kvm *kvm) * When a VFIO device is first opened the KVM will be available in * device->kvm if one was associated with the file. */ -void vfio_file_set_kvm(struct file *file, struct kvm *kvm) +void vfio_file_set_kvm(struct file *file, struct kvm *kvm, struct module *= kvm_module) { struct vfio_group *group; =20 group =3D vfio_group_from_file(file); if (group) - vfio_group_set_kvm(group, kvm); + vfio_group_set_kvm(group, kvm, kvm_module); =20 if (vfio_device_from_file(file)) - vfio_device_file_set_kvm(file, kvm); + vfio_device_file_set_kvm(file, kvm, kvm_module); } EXPORT_SYMBOL_GPL(vfio_file_set_kvm); =20 diff --git a/include/linux/vfio.h b/include/linux/vfio.h index e90859956514..69a8d527b0e8 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -53,6 +53,7 @@ struct vfio_device { struct list_head dev_set_list; unsigned int migration_flags; struct kvm *kvm; + struct module *kvm_module; =20 /* Members below here are private, not for driver use */ unsigned int index; @@ -339,7 +340,7 @@ static inline bool vfio_file_has_dev(struct file *file,= struct vfio_device *devi #endif bool vfio_file_is_valid(struct file *file); bool vfio_file_enforced_coherent(struct file *file); -void vfio_file_set_kvm(struct file *file, struct kvm *kvm); +void vfio_file_set_kvm(struct file *file, struct kvm *kvm, struct module *= kvm_module); =20 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) =20 diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c index 9f9acb66cc1e..515ed445d8e1 100644 --- a/virt/kvm/vfio.c +++ b/virt/kvm/vfio.c @@ -37,13 +37,13 @@ struct kvm_vfio { =20 static void kvm_vfio_file_set_kvm(struct file *file, struct kvm *kvm) { - void (*fn)(struct file *file, struct kvm *kvm); + void (*fn)(struct file *file, struct kvm *kvm, struct module *kvm_module); =20 fn =3D symbol_get(vfio_file_set_kvm); if (!fn) return; =20 - fn(file, kvm); + fn(file, kvm, kvm ? THIS_MODULE : NULL); =20 symbol_put(vfio_file_set_kvm); } --=20 2.53.0 From nobody Thu Jun 18 09:18:25 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A706346E40 for ; Tue, 7 Apr 2026 18:01:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775584890; cv=none; b=gYMU7xLJF9w8j5CFC4BfNVXgOlG+0l336RhZK3Oo5McEGk8CssbWDYy5jC5ttHd9I9nDFm5HxqCTr8+P5rUW7381nxUVzM59pvOHc2GyhA09oXLgjh9DSHzajizvnMrc7nREtch048xgZjKctNtdDDDxopQgU8DaqodNWs8g9bk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775584890; c=relaxed/simple; bh=fsIDXAbIYZu+a2glp0ukFJmWMSTlU6ubDK7M83jkY+g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=amKOjVzQ4NAW9lK5NuoDhIWTsGVwc2FAPugIjufJtz+yJiocHyT/7/eALm9T6jgrTYAvSN72XzKgvkZ0rU+SlZkAybhVUPGu6UK+O+n0lj1EqHhX+1cS23QBcT12wqOTqm38isZY+PrinQntdvAksMH9GnuKe4MWZJC8qdgcS2U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=hS6sh2oU; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=VZIpfWkl; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="hS6sh2oU"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="VZIpfWkl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1775584888; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JMYuhA38uZpLgzPpLIwAk2pCXc1DGham/nuBdQUx0Xw=; b=hS6sh2oUNHjegFiTsRZayK1Y7MM8oz2IxTqDCl/BS2WBd+il5zZoXm1AqTSitOUAe9rupE pTgicGTFgBQlGwprYkkaaO1TJ/Oeo9ipi8pj73MZkq+aYgYmku/jTQyRVWHFIqzouGLJkX dqq1wr/4WThly/jSmcNQB6ZmDX+PDMo= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-225-M9pDsvLKMeeI_WleHYrpVw-1; Tue, 07 Apr 2026 14:01:26 -0400 X-MC-Unique: M9pDsvLKMeeI_WleHYrpVw-1 X-Mimecast-MFC-AGG-ID: M9pDsvLKMeeI_WleHYrpVw_1775584885 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-43d177fb157so5219624f8f.0 for ; Tue, 07 Apr 2026 11:01:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1775584884; x=1776189684; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JMYuhA38uZpLgzPpLIwAk2pCXc1DGham/nuBdQUx0Xw=; b=VZIpfWklRun0WiY5FNV3Cr3qrZBPlQXL68suAmy3GA22rPdeOm5+62z0C+tS5Js62d pHGVTObacC2W1I2bzFU1gF/PCcdZhxpqerGJMwwhaFlsoX7IGXhENSN/AbKsSjbn3dDF PeUhamUQ0P8krPp7RTBn4RR5xlkAJ8+UpW+VrpdSG0q0401qcp/2PXV5mnr4RwYa1JlR 67/grM2kj7UdYpMck7tBoHq3Ooqtf5kmfjtnnmhn/WfpvhX4/EGuEqzhnHNW5KoYX/jK SCBjQvfw7GzoOerNB4iUFNxh1IH6HNw62ZwbHm2VAs7TWt7SRKpWFeNsXOyFPAvxaKzg Nw1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775584884; x=1776189684; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=JMYuhA38uZpLgzPpLIwAk2pCXc1DGham/nuBdQUx0Xw=; b=Wz8KmCGWyGBq04wOYV3gC/f0gs0sbB1cjZ+ohNR752qSx/8jGRSDIZ9e5HRQYiH5lF FEOnilfaWBeeiQKlNYpd0Ufp5f0YSthHfIepf4fgcRY5KCo8D7UHkTOnC9okwXjbm16v RUkWz7oao9gX6X2uBTAa7Kpd6V6CBfSn+tS0wuwYRdAB4TjIe4NouiAHA2w5dFtXqPfw e/MaPWyMRlhVpU/2sQzC+XMD7Z5b6UZAKtDnP1wx0MtTHy8ObavMU2O3vlRhewCfZyq4 NjB3jIkIFMlOGl9LdRrLstp56nT5uMPWcwdaqtrndplVG60wtWisFgBF254zbUKVnZvt /DtA== X-Gm-Message-State: AOJu0YxNmB0e8fy6COqarUP+4kRK7M7v23oSTQtjRHTrx9aeE9oUt2zb vNZi8JqiXgi69hASjKevSGrV/QArebyRNpsUITdUwD8wIy4SP6/PeLBvNKFG4cgHQxdxNKe1yVp ZbyJidZ0V3PD1C0RrrT0cCoDRR40qBe7tSkslh1Bu0l5GeLEqARCQh/TV6/y3H+KDnP08P/mQjW 0+2XoWVJDB3qtGaMBqnB3Ks9r/JcyOC6QzlClRTWxeUw3eOc3Yag== X-Gm-Gg: AeBDies3vlE402ZGeRHkb19NfeICFhQ1CRz+PVIZ9IGXUbEdLKtR902Wq3jBgrMC42E tPvY+3Nvtz0I2WBxV1+bhs9+pR+fYdvSzzdgDBsx/s6BVFlvRBKnnbe5aZfwiSJkaXhEk8BhRb6 RlqOWp8TtX25k+1hR5Ro/IGleDCn/T3C2FOtRUC71qJExxqGDDaYCg8fnqUAL3FZtAzau7cvWLA s/zRgmmdY6ZRLClHJ655cTuq8RKpBTd7pDSd1bOcXteEKmSnvDuxTYg8qRKu1veDZmEoZDro0Wf 7mk0yJ+2aPidr537pZa4cAFwzfuhYdYm0MMaovGielEvPZKdeO3p+mpGWDkyIgrXgkKnia81ycC EyOmjsgt1Mj/ImPOW9DwqqOF+2n+znWVcV76O7wA15KVFvaoKsA6sC+lKVAfplffkIH6ep4GlJM J5T62DGBAXsoD0/nWuc+wgXI3x X-Received: by 2002:a5d:5f87:0:b0:43c:ffcc:b682 with SMTP id ffacd0b85a97d-43d292ff296mr26465116f8f.33.1775584883687; Tue, 07 Apr 2026 11:01:23 -0700 (PDT) X-Received: by 2002:a5d:5f87:0:b0:43c:ffcc:b682 with SMTP id ffacd0b85a97d-43d292ff296mr26465033f8f.33.1775584883005; Tue, 07 Apr 2026 11:01:23 -0700 (PDT) Received: from [192.168.10.48] ([151.49.85.67]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43d1e4e56fesm49857921f8f.27.2026.04.07.11.01.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2026 11:01:19 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Steffen Eiden , Alex Williamson Subject: [PATCH 2/3] KVM, vfio: remove symbol_get(kvm_get_kvm_safe) from vfio Date: Tue, 7 Apr 2026 20:01:06 +0200 Message-ID: <20260407180107.1603697-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260407180107.1603697-1-pbonzini@redhat.com> References: <20260407180107.1603697-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Right now, KVM and VFIO are using symbol_get to access each other's symbols because of a circular reference between the modules, as well as to avoid loading them unnecessarily. However, usage of symbol_get is mostly deprecated and there are just a handful of users left. In the case of VFIO, in particular, the functions it calls can be made inline. Start with kvm_get_kvm_safe, for which it is trivial to do so. While at it, move the function from kvm_host.h to kvm_types.h. Unlike e.g. drivers/s390/crypto/vfio_ap_ops.c, there's no need for VFIO to know any implementation details of KVM, and struct kvm can be treated as an opaque type. Signed-off-by: Paolo Bonzini Acked-by: Alex Williamson Reviewed-by: Steffen Eiden --- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- arch/x86/kvm/vmx/nested.h | 4 ++-- drivers/vfio/vfio_main.c | 8 +------- include/linux/kvm_host.h | 8 ++++---- include/linux/kvm_types.h | 22 ++++++++++++++++++++++ virt/kvm/kvm_main.c | 27 ++++++--------------------- 6 files changed, 36 insertions(+), 35 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 9c26038f6b77..a88686b5db24 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -1136,7 +1136,7 @@ void kvm_tdp_mmu_invalidate_roots(struct kvm *kvm, * being destroyed in an error path of KVM_CREATE_VM. */ if (IS_ENABLED(CONFIG_PROVE_LOCKING) && - refcount_read(&kvm->users_count) && kvm->created_vcpus) + refcount_read(&kvm->rc.users_count) && kvm->created_vcpus) lockdep_assert_held_write(&kvm->mmu_lock); =20 /* diff --git a/arch/x86/kvm/vmx/nested.h b/arch/x86/kvm/vmx/nested.h index 213a448104af..2c83fc905698 100644 --- a/arch/x86/kvm/vmx/nested.h +++ b/arch/x86/kvm/vmx/nested.h @@ -58,7 +58,7 @@ bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, u= nsigned int port, static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) { lockdep_assert_once(lockdep_is_held(&vcpu->mutex) || - !refcount_read(&vcpu->kvm->users_count)); + !refcount_read(&vcpu->kvm->rc.users_count)); =20 return to_vmx(vcpu)->nested.cached_vmcs12; } @@ -66,7 +66,7 @@ static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *= vcpu) static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu) { lockdep_assert_once(lockdep_is_held(&vcpu->mutex) || - !refcount_read(&vcpu->kvm->users_count)); + !refcount_read(&vcpu->kvm->rc.users_count)); =20 return to_vmx(vcpu)->nested.cached_shadow_vmcs12; } diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index d1bbc42d484a..cb6eaabd64ce 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -436,7 +436,6 @@ EXPORT_SYMBOL_GPL(vfio_unregister_group_dev); void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm,= struct module *kvm_module) { void (*pfn)(struct kvm *kvm); - bool (*fn)(struct kvm *kvm); bool ret; =20 lockdep_assert_held(&device->dev_set->lock); @@ -451,12 +450,7 @@ void vfio_device_get_kvm_safe(struct vfio_device *devi= ce, struct kvm *kvm, struc if (WARN_ON(!pfn)) goto out_put_mod; =20 - fn =3D symbol_get(kvm_get_kvm_safe); - if (WARN_ON(!fn)) - goto out_put_sym; - - ret =3D fn(kvm); - symbol_put(kvm_get_kvm_safe); + ret =3D kvm_get_kvm_safe(kvm); if (!ret) goto out_put_sym; =20 diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 6b76e7a6f4c2..5163b541c82d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -767,6 +767,9 @@ struct kvm_memslots { }; =20 struct kvm { + /* Must be the first field, see function definitions in kvm_types.h. */ + struct kvm_refcount rc; + #ifdef KVM_HAVE_MMU_RWLOCK rwlock_t mmu_lock; #else @@ -830,7 +833,6 @@ struct kvm { struct list_head ioeventfds; struct kvm_vm_stat stat; struct kvm_arch arch; - refcount_t users_count; #ifdef CONFIG_KVM_MMIO struct kvm_coalesced_mmio_ring *coalesced_mmio_ring; spinlock_t ring_lock; @@ -1062,8 +1064,6 @@ static inline void kvm_irqfd_exit(void) int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *modul= e); void kvm_exit(void); =20 -void kvm_get_kvm(struct kvm *kvm); -bool kvm_get_kvm_safe(struct kvm *kvm); void kvm_put_kvm(struct kvm *kvm); bool file_is_kvm(struct file *file); void kvm_put_kvm_no_destroy(struct kvm *kvm); @@ -1073,7 +1073,7 @@ static inline struct kvm_memslots *__kvm_memslots(str= uct kvm *kvm, int as_id) as_id =3D array_index_nospec(as_id, KVM_MAX_NR_ADDRESS_SPACES); return srcu_dereference_check(kvm->memslots[as_id], &kvm->srcu, lockdep_is_held(&kvm->slots_lock) || - !refcount_read(&kvm->users_count)); + !refcount_read(&kvm->rc.users_count)); } =20 static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm) diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h index a568d8e6f4e8..4cb68c71a13c 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -33,6 +33,7 @@ =20 #include #include +#include =20 struct kvm; struct kvm_async_pf; @@ -140,6 +141,27 @@ struct kvm_vcpu_stat_generic { }; =20 #define KVM_STATS_NAME_SIZE 48 + +struct kvm_refcount { + refcount_t users_count; +}; + +static inline void kvm_get_kvm(struct kvm *kvm) +{ + struct kvm_refcount *rc =3D (struct kvm_refcount *)kvm; + refcount_inc(&rc->users_count); +} + +/* + * A safe version of kvm_get_kvm(), making sure the vm is not being destro= yed. + * Return true if kvm referenced successfully, false otherwise. + */ +static inline bool kvm_get_kvm_safe(struct kvm *kvm) +{ + struct kvm_refcount *rc =3D (struct kvm_refcount *)kvm; + return refcount_inc_not_zero(&rc->users_count); +} + #endif /* !__ASSEMBLER__ */ =20 #endif /* __KVM_TYPES_H__ */ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 9093251beb39..6e3796814da7 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1099,7 +1099,7 @@ static inline struct kvm_io_bus *kvm_get_bus_for_dest= ruction(struct kvm *kvm, enum kvm_bus idx) { return rcu_dereference_protected(kvm->buses[idx], - !refcount_read(&kvm->users_count)); + !refcount_read(&kvm->rc.users_count)); } =20 static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) @@ -1153,7 +1153,8 @@ static struct kvm *kvm_create_vm(unsigned long type, = const char *fdname) if (r) goto out_err_no_irq_routing; =20 - refcount_set(&kvm->users_count, 1); + BUILD_BUG_ON(offsetof(struct kvm, rc) !=3D 0); + refcount_set(&kvm->rc.users_count, 1); =20 for (i =3D 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { for (j =3D 0; j < 2; j++) { @@ -1223,7 +1224,7 @@ static struct kvm *kvm_create_vm(unsigned long type, = const char *fdname) out_err_no_disable: kvm_arch_destroy_vm(kvm); out_err_no_arch_destroy_vm: - WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count)); + WARN_ON_ONCE(!refcount_dec_and_test(&kvm->rc.users_count)); for (i =3D 0; i < KVM_NR_BUSES; i++) kfree(kvm_get_bus_for_destruction(kvm, i)); kvm_free_irq_routing(kvm); @@ -1316,25 +1317,9 @@ static void kvm_destroy_vm(struct kvm *kvm) mmdrop(mm); } =20 -void kvm_get_kvm(struct kvm *kvm) -{ - refcount_inc(&kvm->users_count); -} -EXPORT_SYMBOL_GPL(kvm_get_kvm); - -/* - * Make sure the vm is not during destruction, which is a safe version of - * kvm_get_kvm(). Return true if kvm referenced successfully, false other= wise. - */ -bool kvm_get_kvm_safe(struct kvm *kvm) -{ - return refcount_inc_not_zero(&kvm->users_count); -} -EXPORT_SYMBOL_GPL(kvm_get_kvm_safe); - void kvm_put_kvm(struct kvm *kvm) { - if (refcount_dec_and_test(&kvm->users_count)) + if (refcount_dec_and_test(&kvm->rc.users_count)) kvm_destroy_vm(kvm); } EXPORT_SYMBOL_GPL(kvm_put_kvm); @@ -1348,7 +1333,7 @@ EXPORT_SYMBOL_GPL(kvm_put_kvm); */ void kvm_put_kvm_no_destroy(struct kvm *kvm) { - WARN_ON(refcount_dec_and_test(&kvm->users_count)); + WARN_ON(refcount_dec_and_test(&kvm->rc.users_count)); } EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_put_kvm_no_destroy); =20 --=20 2.53.0 From nobody Thu Jun 18 09:18:26 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E0320347537 for ; Tue, 7 Apr 2026 18:01:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775584903; cv=none; b=SCBrS0v/2wPVwUDPIGgMOmzRZey5dI0yqz8R2WRIWQzuAiyWkeu7eSZalPtIVeESJnV8Nq1x6bHiGmxhrZ6MyKZHarBCHG0HNDlyvP8pU+4SsDEL2usTNQ1Dv2CaEyM5Mo80b1gHzSvusrCvVAM7fUpJyHfFhAcP4rVc+trOAwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775584903; c=relaxed/simple; bh=bgGqLOGrvDnNuaZb+/+HyfYImZod2w+FVuoRIUn/uPk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KD1PbUAaI66BekXneAacYMkAxUe9EZDp96Xfa1q86/raNsvI0mjm7SyhRHGTL9qdYcdPnq8zy5L3I0wYyJXP/bZSBPOs1qW+4g2IFQd+nA3mcwuLpIwXCs80V8kSwlx71yNyG4G3HsztvV3xc2cG/u6zxKf0p3Y3r/5/SSWMuds= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=M3aSdxtp; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=bYNwF0sn; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="M3aSdxtp"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="bYNwF0sn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1775584899; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5ptoacRRqyfrvyJsuaTHTOJB1Wd2nT72S8Ib9SDoAls=; b=M3aSdxtpNODs2kq9B49QHlg0u3l3roVagoHf7Eb9IdWKZsItLwKdH/so+Qm0IBVyorQ9W/ +uF/YKFjvJWnASDF8aqKMGF1Ni+nwjcN7MdyZrrrwOmSgBiFYtPesN44S1dPMeoiTyFik6 4zPL12A2xrkRgXc5RoywPCe2m/tDS9E= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-694-B3faYq4CN5GQuDA8sAyYRQ-1; Tue, 07 Apr 2026 14:01:31 -0400 X-MC-Unique: B3faYq4CN5GQuDA8sAyYRQ-1 X-Mimecast-MFC-AGG-ID: B3faYq4CN5GQuDA8sAyYRQ_1775584888 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-488be33b7a6so6138455e9.0 for ; Tue, 07 Apr 2026 11:01:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1775584887; x=1776189687; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5ptoacRRqyfrvyJsuaTHTOJB1Wd2nT72S8Ib9SDoAls=; b=bYNwF0snhzSaAEmTcxC3PpAR8V9J94jiAVrMVwCTLWmrj35ZrsPkfBd/xO3dbYa7fg t/J1M8Xkn+YpHdnG58F/EhbX4P+wpiAtdQknnpFMpKOgbWuttq09gS0EVQYNA8A/fpuC W8XW46v8bNspzphvILPPC0zV/lxU8dywXv/RzfhaN4Gc2GatWHMzMw9n6FpBPn+8ckll 2GHehlzRdw4JVbN08AFldxMmhm/8/zySoVGqw3q+TNevktmNq0NDYdvTHrUmhVKqhANk ckcoyzG81DhJ8g/7szgckwZEFJWwdT3X7xWd59Vv1QJ5KYDHkfu+jqlU5usRHjrd/fSp TS8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775584887; x=1776189687; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5ptoacRRqyfrvyJsuaTHTOJB1Wd2nT72S8Ib9SDoAls=; b=JsoWOeF88SLpz1bc8u7tXZ903mwrkrtLv/6O4ky6NuLcFZqayGhbGsPvLO0yQ3MIgL +4YF5SjcP4gmAqYhjjMfSLP30Vz2U8QJRja2RYpgQakHPcXqT9FwjF1ILWqtw1yo0BiQ BHVGSox9px3WSaFFu9a899/Uiz58haX8T1VzCpYT7NXlVBfS0fysRSvg37hJQcRNTr3g Wap17hzmiVHxqgL8nPwwkc2IQ1E9OOLDyCLWCXAF152vwu4E2rQ+P0lWX7HTEBFGr+fB O6SVbjIF6/UWZ5vmrXCF2h1/k14fQgJjkmAuCxtcrDQm3h6JVLchPq1jN9+/p3uxvV2K zX6w== X-Gm-Message-State: AOJu0Yya/GIhmeFWh/7ocE9Zarb7wthwrAgrLeNSeekqfmRg8GQ4Ms+H zEyH7uVrYon2Tdu+d9VWK80nLJuLwPffVrx0bR2d7z4N/PQTcj+2aOSKlzQ2ocLKHVqKVkwUS5o TDq5PfPuyFVbYwygtMbu4SO4MwHAg0bMurzosnZdAcRrhAF+A6CxIfx+7c7FeBPOlZ6oaykcshM EuezpaQTlZ4W87uRwLdD4gzyhWDMBu86jNXDY1fmdnVbuAzPdmAA== X-Gm-Gg: AeBDieu5VQv0vytXw1Entfu+DMDDIpMoiXrmY0ekqpEAyZl2eGD65NUhpxc3dbRdSjr m2ukPJfr5Gx2RgNt2BEwsK6HLzmsvF7xYEYCIKIX5WfJz8sHkm5znWKQGvWchs1MbHcSDvFcNeG GKzdU3iDaU+eUSauregl86OH91eyMmtZE0ZLvr8EdhTTA3zdKWtcRJ/ae8mD7YePNOc3cd/CcjY UZ3tiM5oNw77yEnhD4oeops17ZY5nMJWVD67awHHabTZ3MocSFMZ44e4rb9wV94GSX5LkQW3XZ2 flhl1eq/GF94cii07LYmrnkQBkuXo7vkMJLT0qO42TNy0Ffh2GNvLZzFc1uF4EmmdEQrebmUhCi bGLAWYpoFxXps7SQHgOtHFftyiB6MdPETKT1Hb5gYlq5HmF4sU+4PZtN5PGyU1EBUDnSq0WjJ1t gb8i1fQ3LwQQgaxodhYiOoZoxu X-Received: by 2002:a05:600c:4743:b0:487:219e:42d with SMTP id 5b1f17b1804b1-4889970642emr252137605e9.11.1775584886598; Tue, 07 Apr 2026 11:01:26 -0700 (PDT) X-Received: by 2002:a05:600c:4743:b0:487:219e:42d with SMTP id 5b1f17b1804b1-4889970642emr252137205e9.11.1775584886036; Tue, 07 Apr 2026 11:01:26 -0700 (PDT) Received: from [192.168.10.48] ([151.49.85.67]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-488a8ddfde7sm443698845e9.5.2026.04.07.11.01.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2026 11:01:24 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Steffen Eiden , Alex Williamson Subject: [PATCH 3/3] KVM, vfio: remove symbol_get(kvm_put_kvm) from vfio Date: Tue, 7 Apr 2026 20:01:07 +0200 Message-ID: <20260407180107.1603697-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260407180107.1603697-1-pbonzini@redhat.com> References: <20260407180107.1603697-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Right now, KVM and VFIO are using symbol_get to access each other's symbols because of a circular reference between the modules, as well as to avoid loading them unnecessarily. The remaining use in VFIO is for kvm_put_kvm, which is not inline because it needs to call kvm_destroy_vm. However, storing the address of kvm_destroy_vm in the "struct kvm" is enough to remove the dependency from VFIO. This also makes it possible to direct kvm_put_kvm to either the arm64 or the s390 implementation of kvm_destroy_vm. Signed-off-by: Paolo Bonzini Acked-by: Alex Williamson Reviewed-by: Steffen Eiden --- drivers/vfio/vfio_main.c | 24 +++--------------------- include/linux/kvm_host.h | 1 - include/linux/kvm_types.h | 8 ++++++++ include/linux/vfio.h | 1 - virt/kvm/kvm_main.c | 9 ++------- 5 files changed, 13 insertions(+), 30 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index cb6eaabd64ce..f27a1d00c21e 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -17,7 +17,7 @@ #include #include #if IS_ENABLED(CONFIG_KVM) -#include +#include #endif #include #include @@ -435,9 +435,6 @@ EXPORT_SYMBOL_GPL(vfio_unregister_group_dev); #if IS_ENABLED(CONFIG_KVM) void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm,= struct module *kvm_module) { - void (*pfn)(struct kvm *kvm); - bool ret; - lockdep_assert_held(&device->dev_set->lock); =20 if (!kvm) @@ -446,21 +443,13 @@ void vfio_device_get_kvm_safe(struct vfio_device *dev= ice, struct kvm *kvm, struc if (!try_module_get(kvm_module)) return; =20 - pfn =3D symbol_get(kvm_put_kvm); - if (WARN_ON(!pfn)) + if (!kvm_get_kvm_safe(kvm)) goto out_put_mod; =20 - ret =3D kvm_get_kvm_safe(kvm); - if (!ret) - goto out_put_sym; - - device->put_kvm =3D pfn; device->kvm =3D kvm; device->kvm_module =3D kvm_module; return; =20 -out_put_sym: - symbol_put(kvm_put_kvm); out_put_mod: module_put(kvm_module); } @@ -472,14 +461,7 @@ void vfio_device_put_kvm(struct vfio_device *device) if (!device->kvm) return; =20 - if (WARN_ON(!device->put_kvm)) - goto clear; - - device->put_kvm(device->kvm); - device->put_kvm =3D NULL; - symbol_put(kvm_put_kvm); - -clear: + kvm_put_kvm(device->kvm); device->kvm =3D NULL; module_put(device->kvm_module); device->kvm_module =3D NULL; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 5163b541c82d..ca06f9bda028 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1064,7 +1064,6 @@ static inline void kvm_irqfd_exit(void) int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *modul= e); void kvm_exit(void); =20 -void kvm_put_kvm(struct kvm *kvm); bool file_is_kvm(struct file *file); void kvm_put_kvm_no_destroy(struct kvm *kvm); =20 diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h index 4cb68c71a13c..8a9be07cb8dd 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -144,6 +144,7 @@ struct kvm_vcpu_stat_generic { =20 struct kvm_refcount { refcount_t users_count; + void (*destroy)(struct kvm *kvm); }; =20 static inline void kvm_get_kvm(struct kvm *kvm) @@ -162,6 +163,13 @@ static inline bool kvm_get_kvm_safe(struct kvm *kvm) return refcount_inc_not_zero(&rc->users_count); } =20 +static inline void kvm_put_kvm(struct kvm *kvm) +{ + struct kvm_refcount *rc =3D (struct kvm_refcount *)kvm; + if (refcount_dec_and_test(&rc->users_count)) + rc->destroy(kvm); +} + #endif /* !__ASSEMBLER__ */ =20 #endif /* __KVM_TYPES_H__ */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 69a8d527b0e8..5c69532d6127 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -65,7 +65,6 @@ struct vfio_device { unsigned int open_count; struct completion comp; struct iommufd_access *iommufd_access; - void (*put_kvm)(struct kvm *kvm); struct inode *inode; #if IS_ENABLED(CONFIG_IOMMUFD) struct iommufd_device *iommufd_device; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6e3796814da7..27a718a0bc01 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -120,6 +120,7 @@ static struct dentry *kvm_debugfs_dir; =20 static const struct file_operations stat_fops_per_vm; =20 +static void kvm_destroy_vm(struct kvm *kvm); static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, unsigned long arg); #ifdef CONFIG_KVM_COMPAT @@ -1155,6 +1156,7 @@ static struct kvm *kvm_create_vm(unsigned long type, = const char *fdname) =20 BUILD_BUG_ON(offsetof(struct kvm, rc) !=3D 0); refcount_set(&kvm->rc.users_count, 1); + kvm->rc.destroy =3D kvm_destroy_vm; =20 for (i =3D 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { for (j =3D 0; j < 2; j++) { @@ -1317,13 +1319,6 @@ static void kvm_destroy_vm(struct kvm *kvm) mmdrop(mm); } =20 -void kvm_put_kvm(struct kvm *kvm) -{ - if (refcount_dec_and_test(&kvm->rc.users_count)) - kvm_destroy_vm(kvm); -} -EXPORT_SYMBOL_GPL(kvm_put_kvm); - /* * Used to put a reference that was taken on behalf of an object associated * with a user-visible file descriptor, e.g. a vcpu or device, if installa= tion --=20 2.53.0