Skip to contentSkip to conversation

vdpasim: protect concurrent access to iommu iotlb

PATCH In review Jason Wang 3 participants 6 messages Jul 31, 2020 – Aug 14, 2020

Subsystems (2)
lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 vdpasim: protect concurrent access to iommu iotlb

From: Max Gurtovoy <[email protected]>

Iommu iotlb can be accessed by different cores for performing IO using multiple virt queues. Add a spinlock to synchronize iotlb accesses.

This could be easily reproduced when using more than 1 pktgen threads to inject traffic to vdpa simulator.

Fixes 2c53d0f64c06f("vdpasim: vDPA device simulator")
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
drivers/vdpa/vdpa_sim/vdpa_sim.c +27 -4
index a9bc5e0fb353..5b5725d951ce 100644
@@ -70,6 +70,8 @@ struct vdpasim {
 	u32 status;
 	u32 generation;
 	u64 features;
+	/* spinlock to synchronize iommu table */
+	spinlock_t iommu_lock;
 };
 
 static struct vdpasim *vdpasim_dev;
@@ -118,7 +120,9 @@ static void vdpasim_reset(struct vdpasim *vdpasim)
 	for (i = 0; i < VDPASIM_VQ_NUM; i++)
 		vdpasim_vq_reset(&vdpasim->vqs[i]);
 
+	spin_lock(&vdpasim->iommu_lock);
 	vhost_iotlb_reset(vdpasim->iommu);
+	spin_unlock(&vdpasim->iommu_lock);
 
 	vdpasim->features = 0;
 	vdpasim->status = 0;
@@ -236,8 +240,10 @@ static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
 	/* For simplicity, use identical mapping to avoid e.g iova
 	 * allocator.
 	 */
+	spin_lock(&vdpasim->iommu_lock);
 	ret = vhost_iotlb_add_range(iommu, pa, pa + size - 1,
 				    pa, dir_to_perm(dir));
+	spin_unlock(&vdpasim->iommu_lock);
 	if (ret)
 		return DMA_MAPPING_ERROR;
 
@@ -251,8 +257,10 @@ static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
 	struct vdpasim *vdpasim = dev_to_sim(dev);
 	struct vhost_iotlb *iommu = vdpasim->iommu;
 
+	spin_lock(&vdpasim->iommu_lock);
 	vhost_iotlb_del_range(iommu, (u64)dma_addr,
 			      (u64)dma_addr + size - 1);
+	spin_unlock(&vdpasim->iommu_lock);
 }
 
 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
@@ -264,9 +272,10 @@ static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
 	void *addr = kmalloc(size, flag);
 	int ret;
 
-	if (!addr)
+	spin_lock(&vdpasim->iommu_lock);
+	if (!addr) {
 		*dma_addr = DMA_MAPPING_ERROR;
-	else {
+	} else {
 		u64 pa = virt_to_phys(addr);
 
 		ret = vhost_iotlb_add_range(iommu, (u64)pa,
@@ -279,6 +288,7 @@ static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
 		} else
 			*dma_addr = (dma_addr_t)pa;
 	}
+	spin_unlock(&vdpasim->iommu_lock);
 
 	return addr;
 }
@@ -290,8 +300,11 @@ static void vdpasim_free_coherent(struct device *dev, size_t size,
 	struct vdpasim *vdpasim = dev_to_sim(dev);
 	struct vhost_iotlb *iommu = vdpasim->iommu;
 
+	spin_lock(&vdpasim->iommu_lock);
 	vhost_iotlb_del_range(iommu, (u64)dma_addr,
 			      (u64)dma_addr + size - 1);
+	spin_unlock(&vdpasim->iommu_lock);
+
 	kfree(phys_to_virt((uintptr_t)dma_addr));
 }
 
@@ -532,6 +545,7 @@ static int vdpasim_set_map(struct vdpa_device *vdpa,
 	u64 start = 0ULL, last = 0ULL - 1;
 	int ret;
 
+	spin_lock(&vdpasim->iommu_lock);
 	vhost_iotlb_reset(vdpasim->iommu);
 
 	for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
@@ -541,10 +555,12 @@ static int vdpasim_set_map(struct vdpa_device *vdpa,
 		if (ret)
 			goto err;
 	}
+	spin_unlock(&vdpasim->iommu_lock);
 	return 0;
 
 err:
 	vhost_iotlb_reset(vdpasim->iommu);
+	spin_unlock(&vdpasim->iommu_lock);
 	return ret;
 }
 
@@ -552,16 +568,23 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
 			   u64 pa, u32 perm)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
+	int ret;
 
-	return vhost_iotlb_add_range(vdpasim->iommu, iova,
-				     iova + size - 1, pa, perm);
+	spin_lock(&vdpasim->iommu_lock);
+	ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
+				    perm);
+	spin_unlock(&vdpasim->iommu_lock);
+
+	return ret;
 }
 
 static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 
+	spin_lock(&vdpasim->iommu_lock);
 	vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
+	spin_unlock(&vdpasim->iommu_lock);
 
 	return 0;
 }
Signature · 1 line
2.20.1
maintainer
#1 lore raw

Hi

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag fixing commit: .

The bot has tested the following trees: v5.7.11, v5.4.54, v4.19.135, v4.14.190, v4.9.231, v4.4.231.

v5.7.11: Build OK!
v5.4.54: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.19.135: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.14.190: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    7b95fec6d2ff ("virtio: make VIRTIO a menuconfig to ease disabling it all")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.9.231: Failed to apply! Possible dependencies:
0d7f4f0594fc ("MAINTAINERS: update rmk's entries")
2c53d0f64c06 ("vdpasim: vDPA device simulator")
384fe7a4d732 ("drivers: net: xgene-v2: Add DMA descriptor") 3b3f9a75d931 ("drivers: net: xgene-v2: Add base driver") 404a5c392dcc ("MAINTAINERS: fix virtio file pattern")
51c5d8447bd7 ("MMC: meson: initial support for GX platforms") 6bc37fac30cf ("arm64: dts: add Allwinner A64 SoC .dtsi") 70dbd9b258d5 ("MAINTAINERS: Add entry for APM X-Gene SoC Ethernet (v2) driver") 7683e9e52925 ("Properly alphabetize MAINTAINERS file") 81ccd0cab29b ("drivers: net: xgene-v2: Add mac configuration") 872d1ba47bdc ("MAINTAINERS: Add Actions Semi Owl section") 87c586a6a0e1 ("MAINTAINERS: Update the Allwinner sunXi entry") 961e9c84077f ("vDPA: introduce vDPA bus")
b105bcdaaa0e ("drivers: net: xgene-v2: Add transmit and receive") b26bff6e52d8 ("MAINTAINERS: Add device tree bindings to mv88e6xx section") c0a6a5ae6b5d ("MAINTAINERS: copy virtio on balloon_compaction.c") d5d4602e0405 ("Staging: iio: fix a MAINTAINERS entry") dbaf0624ffa5 ("crypto: add virtio-crypto driver")
fd33f3eca6bf ("MAINTAINERS: Add maintainers for the meson clock driver")

v4.4.231: Failed to apply! Possible dependencies:
02038fd6645a ("crypto: Added Chelsio Menu to the Kconfig file") 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko") 2c53d0f64c06 ("vdpasim: vDPA device simulator")
404a5c392dcc ("MAINTAINERS: fix virtio file pattern") 433cd2c617bf ("crypto: rockchip - add crypto driver for rk3288") 6f99612e2500 ("tpm: Proxy driver for supporting multiple emulated TPMs") 961e9c84077f ("vDPA: introduce vDPA bus")
c0a6a5ae6b5d ("MAINTAINERS: copy virtio on balloon_compaction.c") dbaf0624ffa5 ("crypto: add virtio-crypto driver")

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

Signature · 2 lines
Thanks
Sasha
maintainer
#2 lore raw
In reply to Jason Wang

Hi

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag fixing commit: .

The bot has tested the following trees: v5.7.11, v5.4.54, v4.19.135, v4.14.190, v4.9.231, v4.4.231.

v5.7.11: Build OK!
v5.4.54: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.19.135: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.14.190: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    7b95fec6d2ff ("virtio: make VIRTIO a menuconfig to ease disabling it all")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.9.231: Failed to apply! Possible dependencies:
0d7f4f0594fc ("MAINTAINERS: update rmk's entries")
2c53d0f64c06 ("vdpasim: vDPA device simulator")
384fe7a4d732 ("drivers: net: xgene-v2: Add DMA descriptor") 3b3f9a75d931 ("drivers: net: xgene-v2: Add base driver") 404a5c392dcc ("MAINTAINERS: fix virtio file pattern")
51c5d8447bd7 ("MMC: meson: initial support for GX platforms") 6bc37fac30cf ("arm64: dts: add Allwinner A64 SoC .dtsi") 70dbd9b258d5 ("MAINTAINERS: Add entry for APM X-Gene SoC Ethernet (v2) driver") 7683e9e52925 ("Properly alphabetize MAINTAINERS file") 81ccd0cab29b ("drivers: net: xgene-v2: Add mac configuration") 872d1ba47bdc ("MAINTAINERS: Add Actions Semi Owl section") 87c586a6a0e1 ("MAINTAINERS: Update the Allwinner sunXi entry") 961e9c84077f ("vDPA: introduce vDPA bus")
b105bcdaaa0e ("drivers: net: xgene-v2: Add transmit and receive") b26bff6e52d8 ("MAINTAINERS: Add device tree bindings to mv88e6xx section") c0a6a5ae6b5d ("MAINTAINERS: copy virtio on balloon_compaction.c") d5d4602e0405 ("Staging: iio: fix a MAINTAINERS entry") dbaf0624ffa5 ("crypto: add virtio-crypto driver")
fd33f3eca6bf ("MAINTAINERS: Add maintainers for the meson clock driver")

v4.4.231: Failed to apply! Possible dependencies:
02038fd6645a ("crypto: Added Chelsio Menu to the Kconfig file") 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko") 2c53d0f64c06 ("vdpasim: vDPA device simulator")
404a5c392dcc ("MAINTAINERS: fix virtio file pattern") 433cd2c617bf ("crypto: rockchip - add crypto driver for rk3288") 6f99612e2500 ("tpm: Proxy driver for supporting multiple emulated TPMs") 961e9c84077f ("vDPA: introduce vDPA bus")
c0a6a5ae6b5d ("MAINTAINERS: copy virtio on balloon_compaction.c") dbaf0624ffa5 ("crypto: add virtio-crypto driver")

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

Signature · 2 lines
Thanks
Sasha
author
#3 lore raw
Quoted 56 lines · Sasha Levin

Hi

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag fixing commit: .

The bot has tested the following trees: v5.7.11, v5.4.54, v4.19.135, v4.14.190, v4.9.231, v4.4.231.

v5.7.11: Build OK!
v5.4.54: Failed to apply! Possible dependencies:

     2c53d0f64c06 ("vdpasim: vDPA device simulator")
     961e9c84077f ("vDPA: introduce vDPA bus")

v4.19.135: Failed to apply! Possible dependencies:

     2c53d0f64c06 ("vdpasim: vDPA device simulator")
     961e9c84077f ("vDPA: introduce vDPA bus")

v4.14.190: Failed to apply! Possible dependencies:

     2c53d0f64c06 ("vdpasim: vDPA device simulator")
     7b95fec6d2ff ("virtio: make VIRTIO a menuconfig to ease disabling it all")
     961e9c84077f ("vDPA: introduce vDPA bus")

v4.9.231: Failed to apply! Possible dependencies:
0d7f4f0594fc ("MAINTAINERS: update rmk's entries")
2c53d0f64c06 ("vdpasim: vDPA device simulator")
384fe7a4d732 ("drivers: net: xgene-v2: Add DMA descriptor") 3b3f9a75d931 ("drivers: net: xgene-v2: Add base driver") 404a5c392dcc ("MAINTAINERS: fix virtio file pattern") 51c5d8447bd7 ("MMC: meson: initial support for GX platforms") 6bc37fac30cf ("arm64: dts: add Allwinner A64 SoC .dtsi") 70dbd9b258d5 ("MAINTAINERS: Add entry for APM X-Gene SoC Ethernet (v2) driver") 7683e9e52925 ("Properly alphabetize MAINTAINERS file") 81ccd0cab29b ("drivers: net: xgene-v2: Add mac configuration") 872d1ba47bdc ("MAINTAINERS: Add Actions Semi Owl section") 87c586a6a0e1 ("MAINTAINERS: Update the Allwinner sunXi entry") 961e9c84077f ("vDPA: introduce vDPA bus")
b105bcdaaa0e ("drivers: net: xgene-v2: Add transmit and receive") b26bff6e52d8 ("MAINTAINERS: Add device tree bindings to mv88e6xx section") c0a6a5ae6b5d ("MAINTAINERS: copy virtio on balloon_compaction.c") d5d4602e0405 ("Staging: iio: fix a MAINTAINERS entry") dbaf0624ffa5 ("crypto: add virtio-crypto driver")
fd33f3eca6bf ("MAINTAINERS: Add maintainers for the meson clock driver")

v4.4.231: Failed to apply! Possible dependencies:
02038fd6645a ("crypto: Added Chelsio Menu to the Kconfig file") 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko") 2c53d0f64c06 ("vdpasim: vDPA device simulator")
404a5c392dcc ("MAINTAINERS: fix virtio file pattern") 433cd2c617bf ("crypto: rockchip - add crypto driver for rk3288") 6f99612e2500 ("tpm: Proxy driver for supporting multiple emulated TPMs") 961e9c84077f ("vDPA: introduce vDPA bus")
c0a6a5ae6b5d ("MAINTAINERS: copy virtio on balloon_compaction.c") dbaf0624ffa5 ("crypto: add virtio-crypto driver")

Quoted 3 lines

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

The patch tries to fix a bug which is a commit introduced in 5.4.

So I think backporting it to 5.4 stable should be sufficient.

Thanks

Quoted 1 line
maintainer
#4 lore raw
In reply to Jason Wang

Hi

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag fixing commit: .

The bot has tested the following trees: v5.8, v5.7.14, v5.4.57, v4.19.138, v4.14.193, v4.9.232, v4.4.232.

v5.8: Build OK!
v5.7.14: Build OK!
v5.4.57: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.19.138: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.14.193: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    7b95fec6d2ff ("virtio: make VIRTIO a menuconfig to ease disabling it all")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.9.232: Failed to apply! Possible dependencies:
0d7f4f0594fc ("MAINTAINERS: update rmk's entries")
2c53d0f64c06 ("vdpasim: vDPA device simulator")
384fe7a4d732 ("drivers: net: xgene-v2: Add DMA descriptor") 3b3f9a75d931 ("drivers: net: xgene-v2: Add base driver") 404a5c392dcc ("MAINTAINERS: fix virtio file pattern")
51c5d8447bd7 ("MMC: meson: initial support for GX platforms") 6bc37fac30cf ("arm64: dts: add Allwinner A64 SoC .dtsi") 70dbd9b258d5 ("MAINTAINERS: Add entry for APM X-Gene SoC Ethernet (v2) driver") 7683e9e52925 ("Properly alphabetize MAINTAINERS file") 81ccd0cab29b ("drivers: net: xgene-v2: Add mac configuration") 872d1ba47bdc ("MAINTAINERS: Add Actions Semi Owl section") 87c586a6a0e1 ("MAINTAINERS: Update the Allwinner sunXi entry") 961e9c84077f ("vDPA: introduce vDPA bus")
b105bcdaaa0e ("drivers: net: xgene-v2: Add transmit and receive") b26bff6e52d8 ("MAINTAINERS: Add device tree bindings to mv88e6xx section") c0a6a5ae6b5d ("MAINTAINERS: copy virtio on balloon_compaction.c") d5d4602e0405 ("Staging: iio: fix a MAINTAINERS entry") dbaf0624ffa5 ("crypto: add virtio-crypto driver")
fd33f3eca6bf ("MAINTAINERS: Add maintainers for the meson clock driver")

v4.4.232: Failed to apply! Possible dependencies:
02038fd6645a ("crypto: Added Chelsio Menu to the Kconfig file") 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko") 2c53d0f64c06 ("vdpasim: vDPA device simulator")
404a5c392dcc ("MAINTAINERS: fix virtio file pattern") 433cd2c617bf ("crypto: rockchip - add crypto driver for rk3288") 6f99612e2500 ("tpm: Proxy driver for supporting multiple emulated TPMs") 961e9c84077f ("vDPA: introduce vDPA bus")
c0a6a5ae6b5d ("MAINTAINERS: copy virtio on balloon_compaction.c") dbaf0624ffa5 ("crypto: add virtio-crypto driver")

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

Signature · 2 lines
Thanks
Sasha
#5 lore raw
Quoted 57 lines · Sasha Levin

Hi

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag fixing commit: .

The bot has tested the following trees: v5.8, v5.7.14, v5.4.57, v4.19.138, v4.14.193, v4.9.232, v4.4.232.

v5.8: Build OK!
v5.7.14: Build OK!
v5.4.57: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.19.138: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.14.193: Failed to apply! Possible dependencies:

    2c53d0f64c06 ("vdpasim: vDPA device simulator")
    7b95fec6d2ff ("virtio: make VIRTIO a menuconfig to ease disabling it all")
    961e9c84077f ("vDPA: introduce vDPA bus")

v4.9.232: Failed to apply! Possible dependencies:
0d7f4f0594fc ("MAINTAINERS: update rmk's entries")
2c53d0f64c06 ("vdpasim: vDPA device simulator")
384fe7a4d732 ("drivers: net: xgene-v2: Add DMA descriptor") 3b3f9a75d931 ("drivers: net: xgene-v2: Add base driver") 404a5c392dcc ("MAINTAINERS: fix virtio file pattern")
51c5d8447bd7 ("MMC: meson: initial support for GX platforms") 6bc37fac30cf ("arm64: dts: add Allwinner A64 SoC .dtsi") 70dbd9b258d5 ("MAINTAINERS: Add entry for APM X-Gene SoC Ethernet (v2) driver") 7683e9e52925 ("Properly alphabetize MAINTAINERS file") 81ccd0cab29b ("drivers: net: xgene-v2: Add mac configuration") 872d1ba47bdc ("MAINTAINERS: Add Actions Semi Owl section") 87c586a6a0e1 ("MAINTAINERS: Update the Allwinner sunXi entry") 961e9c84077f ("vDPA: introduce vDPA bus")
b105bcdaaa0e ("drivers: net: xgene-v2: Add transmit and receive") b26bff6e52d8 ("MAINTAINERS: Add device tree bindings to mv88e6xx section") c0a6a5ae6b5d ("MAINTAINERS: copy virtio on balloon_compaction.c") d5d4602e0405 ("Staging: iio: fix a MAINTAINERS entry") dbaf0624ffa5 ("crypto: add virtio-crypto driver")
fd33f3eca6bf ("MAINTAINERS: Add maintainers for the meson clock driver")

v4.4.232: Failed to apply! Possible dependencies:
02038fd6645a ("crypto: Added Chelsio Menu to the Kconfig file") 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko") 2c53d0f64c06 ("vdpasim: vDPA device simulator")
404a5c392dcc ("MAINTAINERS: fix virtio file pattern") 433cd2c617bf ("crypto: rockchip - add crypto driver for rk3288") 6f99612e2500 ("tpm: Proxy driver for supporting multiple emulated TPMs") 961e9c84077f ("vDPA: introduce vDPA bus")
c0a6a5ae6b5d ("MAINTAINERS: copy virtio on balloon_compaction.c") dbaf0624ffa5 ("crypto: add virtio-crypto driver")

Quoted 3 lines

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

It's a simulator so I am not sure we need it at all. Note there's a fix for this patch also upstrean, if it is applied we need the fix too I think.

Quoted 3 lines
Signature · 2 lines
Thanks
Sasha