Skip to contentSkip to conversation

drm/hyperv: Fix device removal on Gen1 VMs

PATCH In review Mohammed Gamal 2 participants 2 messages Nov 19, 2021 – Nov 20, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/hyperv: Fix device removal on Gen1 VMs

The Hyper-V DRM driver tries to free MMIO region on removing the device regardless of VM type, while Gen1 VMs don't use MMIO and hence causing the kernel to crash on a NULL pointer dereference.

Fix this by making deallocating MMIO only on Gen2 machines and implement removal for Gen1

Fixes 76c56a5affeb ("drm/hyperv: Add DRM driver for hyperv synthetic video device")
---
 drivers/gpu/drm/hyperv/hyperv_drm_drv.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/hyperv/hyperv_drm_drv.c +18 -1
index cd818a629183..9f923beb7d8d 100644
@@ -225,12 +225,29 @@ static int hyperv_vmbus_remove(struct hv_device *hdev)
 {
 	struct drm_device *dev = hv_get_drvdata(hdev);
 	struct hyperv_drm_device *hv = to_hv(dev);
+	struct pci_dev *pdev;
 
 	drm_dev_unplug(dev);
 	drm_atomic_helper_shutdown(dev);
 	vmbus_close(hdev->channel);
 	hv_set_drvdata(hdev, NULL);
-	vmbus_free_mmio(hv->mem->start, hv->fb_size);
+
+	/*
+	 * Free allocated MMIO memory only on Gen2 VMs.
+	 * On Gen1 VMs, release the PCI device
+	 */
+	if (efi_enabled(EFI_BOOT)) {
+		vmbus_free_mmio(hv->mem->start, hv->fb_size);
+	} else {
+		pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
+				PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
+		if (!pdev) {
+			drm_err(dev, "Unable to find PCI Hyper-V video\n");
+			return -ENODEV;
+		}
+		pci_release_region(pdev, 0);
+		pci_dev_put(pdev);
+	}
 
 	return 0;
 }
Signature · 1 line
2.33.1
Reviewed-by on v1 1/1
#1 lore raw

Thanks for the patch.

I will push this to drm-fixes, let me know otherwise.

Deepak

Quoted 54 lines · Mohammed Gamal

The Hyper-V DRM driver tries to free MMIO region on removing the device regardless of VM type, while Gen1 VMs don't use MMIO and hence causing the kernel to crash on a NULL pointer dereference.

Fix this by making deallocating MMIO only on Gen2 machines and implement
removal for Gen1

Fixes 76c56a5affeb ("drm/hyperv: Add DRM driver for hyperv synthetic

video device")

---
 drivers/gpu/drm/hyperv/hyperv_drm_drv.c | 19 ++++++++++++++++++-

 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/hyperv/hyperv_drm_drv.c +1 -0
index cd818a629183..9f923beb7d8d 100644
@@ -225,12 +225,29 @@ static int hyperv_vmbus_remove(struct hv_device
 *hdev)
  {
         struct drm_device *dev = hv_get_drvdata(hdev);
         struct hyperv_drm_device *hv = to_hv(dev);
+       struct pci_dev *pdev;


        drm_dev_unplug(dev); drm_atomic_helper_shutdown(dev); vmbus_close(hdev->channel); hv_set_drvdata(hdev, NULL);
-       vmbus_free_mmio(hv->mem->start, hv->fb_size);
+
+       /*
+        * Free allocated MMIO memory only on Gen2 VMs. +        * On Gen1 VMs, release the PCI device
+        */
+       if (efi_enabled(EFI_BOOT)) {
+               vmbus_free_mmio(hv->mem->start, hv->fb_size); +       } else {
+               pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT, +                               PCI_DEVICE_ID_HYPERV_VIDEO, NULL); +               if (!pdev) {
+                       drm_err(dev, "Unable to find PCI Hyper-V video\n");
+                       return -ENODEV;
+               }
+               pci_release_region(pdev, 0);
+               pci_dev_put(pdev);
+       }

        return 0;
 }