Stable Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] vdpasim: protect concurrent access to iommu iotlb
@ 2020-07-31  7:38 Jason Wang
  2020-08-01 23:18 ` Sasha Levin
  0 siblings, 1 reply; 2+ messages in thread
From: Jason Wang @ 2020-07-31  7:38 UTC (permalink / raw)
  To: mst, jasowang, virtualization, linux-kernel; +Cc: Max Gurtovoy, stable

From: Max Gurtovoy <maxg@mellanox.com>

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")
Cc: stable@vger.kernel.org
Signed-off-by: Max Gurtovoy <maxg@mellanox.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index a9bc5e0fb353..5b5725d951ce 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -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;
 }
-- 
2.20.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] vdpasim: protect concurrent access to iommu iotlb
  2020-07-31  7:38 [PATCH] vdpasim: protect concurrent access to iommu iotlb Jason Wang
@ 2020-08-01 23:18 ` Sasha Levin
  0 siblings, 0 replies; 2+ messages in thread
From: Sasha Levin @ 2020-08-01 23:18 UTC (permalink / raw)
  To: Sasha Levin, Jason Wang, Max Gurtovoy, mst, jasowang
  Cc: Max Gurtovoy, stable, stable, stable

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?

-- 
Thanks
Sasha

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-31  7:38 [PATCH] vdpasim: protect concurrent access to iommu iotlb Jason Wang
2020-08-01 23:18 ` Sasha Levin

Stable Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/stable/0 stable/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 stable stable/ https://lore.kernel.org/stable \
		stable@vger.kernel.org
	public-inbox-index stable

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.stable


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git