drm/i915/gvt: Get reference to KVM iff attachment to VM is successful
authorSean Christopherson <seanjc@google.com>
Fri, 11 Nov 2022 00:22:24 +0000 (00:22 +0000)
committerZhenyu Wang <zhenyuw@linux.intel.com>
Fri, 11 Nov 2022 05:13:50 +0000 (13:13 +0800)
Get a reference to KVM if and only if a vGPU is successfully attached to
the VM to avoid leaking a reference if there's no available vGPU.  On
open_device() failure, vfio_device_open() doesn't invoke close_device().

Fixes: 421cfe6596f6 ("vfio: remove VFIO_GROUP_NOTIFY_SET_KVM")
Cc: stable@vger.kernel.org
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Zhenyu Wang <zhenyuw@linux.intel.com>
Link: http://patchwork.freedesktop.org/patch/msgid/20221111002225.2418386-2-seanjc@google.com
drivers/gpu/drm/i915/gvt/kvmgt.c

index 7a45e53..e67d526 100644 (file)
@@ -664,8 +664,6 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
                return -ESRCH;
        }
 
-       kvm_get_kvm(vgpu->vfio_device.kvm);
-
        if (__kvmgt_vgpu_exist(vgpu))
                return -EEXIST;
 
@@ -676,6 +674,7 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
 
        vgpu->track_node.track_write = kvmgt_page_track_write;
        vgpu->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
+       kvm_get_kvm(vgpu->vfio_device.kvm);
        kvm_page_track_register_notifier(vgpu->vfio_device.kvm,
                                         &vgpu->track_node);