From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taylor Stark <tstark@linux.microsoft.com>
Date: Thu, 15 Jul 2021 15:36:38 -0700
Subject: virtio-pmem: Set DRIVER_OK status prior to creating pmem region

Update virtio-pmem to call virtio_device_ready prior to creating the pmem
region. Otherwise, the guest may try to access the pmem region prior to
the DRIVER_OK status being set.

In the case of Hyper-V, the backing pmem file isn't mapped to the guest
until the DRIVER_OK status is set. Therefore, attempts to access the pmem
region can cause the guest to crash. Hyper-V could map the file earlier,
for example at VM creation, but we didn't want to pay the mapping cost if
the device is never used. Additionally, it felt weird to allow the guest
to access the region prior to the device fully coming online.

Signed-off-by: Taylor Stark <tstark@microsoft.com>
Reviewed-by: Pankaj Gupta <pankaj.gupta@ionos.com>

Link: https://lore.kernel.org/r/20210715223638.GA29649@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net
Signed-off-by: Tyler Hicks <tyhicks@linux.microsoft.com>
---
 drivers/nvdimm/virtio_pmem.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/nvdimm/virtio_pmem.c b/drivers/nvdimm/virtio_pmem.c
index ec20d642f030..48dfc5d1c3a4 100644
--- a/drivers/nvdimm/virtio_pmem.c
+++ b/drivers/nvdimm/virtio_pmem.c
@@ -90,6 +90,11 @@ static int virtio_pmem_probe(struct virtio_device *vdev)
 
 	dev_set_drvdata(&vdev->dev, vpmem->nvdimm_bus);
 
+	/* Online the device prior to creating a pmem region, to ensure that
+	 * the region is never touched while the device is offline.
+	 */
+	virtio_device_ready(vdev);
+
 	ndr_desc.res = &res;
 
 	ndr_desc.numa_node = memory_add_physaddr_to_nid(res.start);
@@ -118,6 +123,7 @@ static int virtio_pmem_probe(struct virtio_device *vdev)
 	}
 	return 0;
 out_nd:
+	vdev->config->reset(vdev);
 	virtio_reset_device(vdev);
 	nvdimm_bus_unregister(vpmem->nvdimm_bus);
 out_vq:
-- 
Armbian

