KVM: Add file back-pointer to struct kvm

Add kvm->file as a no-reference back-pointer to the VM file in struct
kvm. The pointer is set at VM creation time and cleared under
WRITE_ONCE() in kvm_vm_release() before the file is freed.
Callers storing the file must take their own reference.

Co-developed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
Acked-by: Janosch Frank <frankja@linux.ibm.com>
Acked-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Christian Borntraeger <borntraeger@linux.ibm.com>
This commit is contained in:
Steffen Eiden 2026-09-28 15:07:03 +02:00 committed by Christian Borntraeger
parent deaf98ff52
commit 8b5cb9769c
2 changed files with 10 additions and 0 deletions

View File

@ -784,6 +784,12 @@ struct kvm {
* kvm_swap_active_memslots().
*/
struct mutex slots_arch_lock;
/*
* Back-reference to the VM file for subsystems (e.g., VFIO). Holds no
* reference to avoid pinning the VM. Callers storing the file must
* take their own reference.
*/
struct file *file;
struct mm_struct *mm; /* userspace tied to this vm */
unsigned long nr_memslot_pages;
/* The two memslot sets - active and inactive (per address space) */

View File

@ -1352,6 +1352,8 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
kvm_irqfd_release(kvm);
WRITE_ONCE(kvm->file, NULL);
kvm_put_kvm(kvm);
return 0;
}
@ -5532,6 +5534,8 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)
goto put_kvm;
}
kvm->file = file;
/*
* Don't call kvm_put_kvm anymore at this point; file->f_op is
* already set, with ->release() being kvm_vm_release(). In error