Re: [PATCH v2] vfio: Use file-based reference counting for KVM
From: Sean Christopherson
Date: Thu Sep 03 2026 - 13:42:11 EST
On Thu, Sep 03, 2026, Steffen Eiden wrote:
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 65eb26a0520d..34b4c43908b3 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -1314,7 +1314,7 @@ void kvm_get_kvm(struct kvm *kvm)
> {
> refcount_inc(&kvm->users_count);
> }
> -EXPORT_SYMBOL_GPL(kvm_get_kvm);
> +EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_get_kvm);
>
> /*
> * Make sure the vm is not during destruction, which is a safe version of
> @@ -1324,14 +1324,14 @@ bool kvm_get_kvm_safe(struct kvm *kvm)
> {
> return refcount_inc_not_zero(&kvm->users_count);
> }
> -EXPORT_SYMBOL_GPL(kvm_get_kvm_safe);
> +EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_get_kvm_safe);
>
> void kvm_put_kvm(struct kvm *kvm)
> {
> if (refcount_dec_and_test(&kvm->users_count))
> kvm_destroy_vm(kvm);
> }
> -EXPORT_SYMBOL_GPL(kvm_put_kvm);
> +EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_put_kvm);
Please isolate the export changes. They don't *need* to happen at the same time
as the VFIO changes, i.e. can be done on top.
> /*
> * Used to put a reference that was taken on behalf of an object associated
> @@ -1352,6 +1352,8 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
>
> kvm_irqfd_release(kvm);
>
> + WRITE_ONCE(kvm->file, NULL);
Please move tracking the file in "struct kvm" to its own patch as well.
> +
> kvm_put_kvm(kvm);
> return 0;
> }
> @@ -5496,6 +5498,15 @@ bool file_is_kvm(struct file *file)
> }
> EXPORT_SYMBOL_FOR_KVM_INTERNAL(file_is_kvm);
>
> +struct kvm *file_to_kvm(struct file *file)
> +{
> + if (!file_is_kvm(file))
file_is_kvm() should exist at the end of this series. The only reason to ever
use file_is_kvm() is in advance of getting at "struct kvm", i.e. this should be
open coded in file_to_kvm(). Though as I suggested in the other subtread, it
woudl be kvm_file_to_kvm_fn(), i.e. kvm_file_to_kvm_x86() or kvm_file_to_kvm_s390().
And those changes can and should be done as prep work, i.e. in separate patches,
e.g. to end up with something like:
1. Add kvm_file_to_kvm_fn() and use it on x86, i.e. replace the use of
file_is_kvm() in arch/x86/kvm/svm/sev.c.
2. Add kvm->file tracking.
3. Switch VFIO to tracking the file.
4. s/EXPORT_SYMBOL_GPL/EXPORT_SYMBOL_FOR_KVM_INTERNAL on the get/put APIs.
5. Enable kvm_file_to_kvm_fn() on s390 and use kvm_file_to_kvm_s390() in the
relevant code to prepare for s390+arm64.