Skip to content

Commit 421cfe6

Browse files
rosatomjawilliam
authored andcommitted
vfio: remove VFIO_GROUP_NOTIFY_SET_KVM
Rather than relying on a notifier for associating the KVM with the group, let's assume that the association has already been made prior to device_open. The first time a device is opened associate the group KVM with the device. This fixes a user-triggerable oops in GVT. Reviewed-by: Tony Krowiak <akrowiak@linux.ibm.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Acked-by: Zhi Wang <zhi.a.wang@intel.com> Link: https://lore.kernel.org/r/20220519183311.582380-2-mjrosato@linux.ibm.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent c490513 commit 421cfe6

File tree

7 files changed

+57
-159
lines changed

7 files changed

+57
-159
lines changed

drivers/gpu/drm/i915/gvt/gtt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static int preallocated_oos_pages = 8192;
5151

5252
static bool intel_gvt_is_valid_gfn(struct intel_vgpu *vgpu, unsigned long gfn)
5353
{
54-
struct kvm *kvm = vgpu->kvm;
54+
struct kvm *kvm = vgpu->vfio_device.kvm;
5555
int idx;
5656
bool ret;
5757

@@ -1185,7 +1185,7 @@ static int is_2MB_gtt_possible(struct intel_vgpu *vgpu,
11851185

11861186
if (!vgpu->attached)
11871187
return -EINVAL;
1188-
pfn = gfn_to_pfn(vgpu->kvm, ops->get_pfn(entry));
1188+
pfn = gfn_to_pfn(vgpu->vfio_device.kvm, ops->get_pfn(entry));
11891189
if (is_error_noslot_pfn(pfn))
11901190
return -EINVAL;
11911191
return PageTransHuge(pfn_to_page(pfn));

drivers/gpu/drm/i915/gvt/gvt.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ struct intel_vgpu {
227227
struct mutex cache_lock;
228228

229229
struct notifier_block iommu_notifier;
230-
struct notifier_block group_notifier;
231-
struct kvm *kvm;
232-
struct work_struct release_work;
233230
atomic_t released;
234231

235232
struct kvm_page_track_notifier_node track_node;

drivers/gpu/drm/i915/gvt/kvmgt.c

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,6 @@ static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
228228
}
229229
}
230230

231-
static void intel_vgpu_release_work(struct work_struct *work);
232-
233231
static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
234232
unsigned long size)
235233
{
@@ -761,23 +759,6 @@ static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
761759
return NOTIFY_OK;
762760
}
763761

764-
static int intel_vgpu_group_notifier(struct notifier_block *nb,
765-
unsigned long action, void *data)
766-
{
767-
struct intel_vgpu *vgpu =
768-
container_of(nb, struct intel_vgpu, group_notifier);
769-
770-
/* the only action we care about */
771-
if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
772-
vgpu->kvm = data;
773-
774-
if (!data)
775-
schedule_work(&vgpu->release_work);
776-
}
777-
778-
return NOTIFY_OK;
779-
}
780-
781762
static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu)
782763
{
783764
struct intel_vgpu *itr;
@@ -789,7 +770,7 @@ static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu)
789770
if (!itr->attached)
790771
continue;
791772

792-
if (vgpu->kvm == itr->kvm) {
773+
if (vgpu->vfio_device.kvm == itr->vfio_device.kvm) {
793774
ret = true;
794775
goto out;
795776
}
@@ -806,7 +787,6 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
806787
int ret;
807788

808789
vgpu->iommu_notifier.notifier_call = intel_vgpu_iommu_notifier;
809-
vgpu->group_notifier.notifier_call = intel_vgpu_group_notifier;
810790

811791
events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
812792
ret = vfio_register_notifier(vfio_dev, VFIO_IOMMU_NOTIFY, &events,
@@ -817,38 +797,32 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
817797
goto out;
818798
}
819799

820-
events = VFIO_GROUP_NOTIFY_SET_KVM;
821-
ret = vfio_register_notifier(vfio_dev, VFIO_GROUP_NOTIFY, &events,
822-
&vgpu->group_notifier);
823-
if (ret != 0) {
824-
gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
825-
ret);
826-
goto undo_iommu;
827-
}
828-
829800
ret = -EEXIST;
830801
if (vgpu->attached)
831-
goto undo_register;
802+
goto undo_iommu;
832803

833804
ret = -ESRCH;
834-
if (!vgpu->kvm || vgpu->kvm->mm != current->mm) {
805+
if (!vgpu->vfio_device.kvm ||
806+
vgpu->vfio_device.kvm->mm != current->mm) {
835807
gvt_vgpu_err("KVM is required to use Intel vGPU\n");
836-
goto undo_register;
808+
goto undo_iommu;
837809
}
838810

811+
kvm_get_kvm(vgpu->vfio_device.kvm);
812+
839813
ret = -EEXIST;
840814
if (__kvmgt_vgpu_exist(vgpu))
841-
goto undo_register;
815+
goto undo_iommu;
842816

843817
vgpu->attached = true;
844-
kvm_get_kvm(vgpu->kvm);
845818

846819
kvmgt_protect_table_init(vgpu);
847820
gvt_cache_init(vgpu);
848821

849822
vgpu->track_node.track_write = kvmgt_page_track_write;
850823
vgpu->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
851-
kvm_page_track_register_notifier(vgpu->kvm, &vgpu->track_node);
824+
kvm_page_track_register_notifier(vgpu->vfio_device.kvm,
825+
&vgpu->track_node);
852826

853827
debugfs_create_ulong(KVMGT_DEBUGFS_FILENAME, 0444, vgpu->debugfs,
854828
&vgpu->nr_cache_entries);
@@ -858,10 +832,6 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
858832
atomic_set(&vgpu->released, 0);
859833
return 0;
860834

861-
undo_register:
862-
vfio_unregister_notifier(vfio_dev, VFIO_GROUP_NOTIFY,
863-
&vgpu->group_notifier);
864-
865835
undo_iommu:
866836
vfio_unregister_notifier(vfio_dev, VFIO_IOMMU_NOTIFY,
867837
&vgpu->iommu_notifier);
@@ -880,8 +850,9 @@ static void intel_vgpu_release_msi_eventfd_ctx(struct intel_vgpu *vgpu)
880850
}
881851
}
882852

883-
static void __intel_vgpu_release(struct intel_vgpu *vgpu)
853+
static void intel_vgpu_close_device(struct vfio_device *vfio_dev)
884854
{
855+
struct intel_vgpu *vgpu = vfio_dev_to_vgpu(vfio_dev);
885856
struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
886857
int ret;
887858

@@ -898,35 +869,19 @@ static void __intel_vgpu_release(struct intel_vgpu *vgpu)
898869
drm_WARN(&i915->drm, ret,
899870
"vfio_unregister_notifier for iommu failed: %d\n", ret);
900871

901-
ret = vfio_unregister_notifier(&vgpu->vfio_device, VFIO_GROUP_NOTIFY,
902-
&vgpu->group_notifier);
903-
drm_WARN(&i915->drm, ret,
904-
"vfio_unregister_notifier for group failed: %d\n", ret);
905-
906872
debugfs_remove(debugfs_lookup(KVMGT_DEBUGFS_FILENAME, vgpu->debugfs));
907873

908-
kvm_page_track_unregister_notifier(vgpu->kvm, &vgpu->track_node);
909-
kvm_put_kvm(vgpu->kvm);
874+
kvm_page_track_unregister_notifier(vgpu->vfio_device.kvm,
875+
&vgpu->track_node);
910876
kvmgt_protect_table_destroy(vgpu);
911877
gvt_cache_destroy(vgpu);
912878

913879
intel_vgpu_release_msi_eventfd_ctx(vgpu);
914880

915-
vgpu->kvm = NULL;
916881
vgpu->attached = false;
917-
}
918-
919-
static void intel_vgpu_close_device(struct vfio_device *vfio_dev)
920-
{
921-
__intel_vgpu_release(vfio_dev_to_vgpu(vfio_dev));
922-
}
923-
924-
static void intel_vgpu_release_work(struct work_struct *work)
925-
{
926-
struct intel_vgpu *vgpu =
927-
container_of(work, struct intel_vgpu, release_work);
928882

929-
__intel_vgpu_release(vgpu);
883+
if (vgpu->vfio_device.kvm)
884+
kvm_put_kvm(vgpu->vfio_device.kvm);
930885
}
931886

932887
static u64 intel_vgpu_get_bar_addr(struct intel_vgpu *vgpu, int bar)
@@ -1675,7 +1630,6 @@ static int intel_vgpu_probe(struct mdev_device *mdev)
16751630
return PTR_ERR(vgpu);
16761631
}
16771632

1678-
INIT_WORK(&vgpu->release_work, intel_vgpu_release_work);
16791633
vfio_init_group_dev(&vgpu->vfio_device, &mdev->dev,
16801634
&intel_vgpu_dev_ops);
16811635

@@ -1713,7 +1667,7 @@ static struct mdev_driver intel_vgpu_mdev_driver = {
17131667

17141668
int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn)
17151669
{
1716-
struct kvm *kvm = info->kvm;
1670+
struct kvm *kvm = info->vfio_device.kvm;
17171671
struct kvm_memory_slot *slot;
17181672
int idx;
17191673

@@ -1743,7 +1697,7 @@ int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn)
17431697

17441698
int intel_gvt_page_track_remove(struct intel_vgpu *info, u64 gfn)
17451699
{
1746-
struct kvm *kvm = info->kvm;
1700+
struct kvm *kvm = info->vfio_device.kvm;
17471701
struct kvm_memory_slot *slot;
17481702
int idx;
17491703

drivers/s390/crypto/vfio_ap_ops.c

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,25 +1284,6 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
12841284
}
12851285
}
12861286

1287-
static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
1288-
unsigned long action, void *data)
1289-
{
1290-
int notify_rc = NOTIFY_OK;
1291-
struct ap_matrix_mdev *matrix_mdev;
1292-
1293-
if (action != VFIO_GROUP_NOTIFY_SET_KVM)
1294-
return NOTIFY_OK;
1295-
1296-
matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
1297-
1298-
if (!data)
1299-
vfio_ap_mdev_unset_kvm(matrix_mdev);
1300-
else if (vfio_ap_mdev_set_kvm(matrix_mdev, data))
1301-
notify_rc = NOTIFY_DONE;
1302-
1303-
return notify_rc;
1304-
}
1305-
13061287
static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
13071288
{
13081289
struct device *dev;
@@ -1402,11 +1383,10 @@ static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
14021383
unsigned long events;
14031384
int ret;
14041385

1405-
matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
1406-
events = VFIO_GROUP_NOTIFY_SET_KVM;
1386+
if (!vdev->kvm)
1387+
return -EINVAL;
14071388

1408-
ret = vfio_register_notifier(vdev, VFIO_GROUP_NOTIFY, &events,
1409-
&matrix_mdev->group_notifier);
1389+
ret = vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
14101390
if (ret)
14111391
return ret;
14121392

@@ -1415,12 +1395,11 @@ static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
14151395
ret = vfio_register_notifier(vdev, VFIO_IOMMU_NOTIFY, &events,
14161396
&matrix_mdev->iommu_notifier);
14171397
if (ret)
1418-
goto out_unregister_group;
1398+
goto err_kvm;
14191399
return 0;
14201400

1421-
out_unregister_group:
1422-
vfio_unregister_notifier(vdev, VFIO_GROUP_NOTIFY,
1423-
&matrix_mdev->group_notifier);
1401+
err_kvm:
1402+
vfio_ap_mdev_unset_kvm(matrix_mdev);
14241403
return ret;
14251404
}
14261405

@@ -1431,8 +1410,6 @@ static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
14311410

14321411
vfio_unregister_notifier(vdev, VFIO_IOMMU_NOTIFY,
14331412
&matrix_mdev->iommu_notifier);
1434-
vfio_unregister_notifier(vdev, VFIO_GROUP_NOTIFY,
1435-
&matrix_mdev->group_notifier);
14361413
vfio_ap_mdev_unset_kvm(matrix_mdev);
14371414
}
14381415

drivers/s390/crypto/vfio_ap_private.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ struct ap_matrix {
8181
* @node: allows the ap_matrix_mdev struct to be added to a list
8282
* @matrix: the adapters, usage domains and control domains assigned to the
8383
* mediated matrix device.
84-
* @group_notifier: notifier block used for specifying callback function for
85-
* handling the VFIO_GROUP_NOTIFY_SET_KVM event
8684
* @iommu_notifier: notifier block used for specifying callback function for
8785
* handling the VFIO_IOMMU_NOTIFY_DMA_UNMAP even
8886
* @kvm: the struct holding guest's state
@@ -94,7 +92,6 @@ struct ap_matrix_mdev {
9492
struct vfio_device vdev;
9593
struct list_head node;
9694
struct ap_matrix matrix;
97-
struct notifier_block group_notifier;
9895
struct notifier_block iommu_notifier;
9996
struct kvm *kvm;
10097
crypto_hook pqap_hook;

0 commit comments

Comments
 (0)