Re: [PATCH] vdpa_sim: use iova module to allocate IOVA addresses

2020-12-22 Thread Stefano Garzarella

On Wed, Dec 23, 2020 at 11:43:40AM +0800, Jason Wang wrote:


On 2020/12/23 上午1:45, Stefano Garzarella wrote:

The identical mapping used until now created issues when mapping
different virtual pages with the same physical address.
To solve this issue, we can use the iova module, to handle the IOVA
allocation.
For semplicity we use an IOVA allocator with byte granularity.



Should be simplicity, so did one comment below.


Right, I'll fix here and in the comment below.






We add two new functions, vdpasim_map_range() and vdpasim_unmap_range(),
to handle the IOVA allocation and the registration into the IOMMU/IOTLB.
These functions are used by dma_map_ops callbacks.

Signed-off-by: Stefano Garzarella 



Few nits, but:

Acked-by: Jason Wang 


Thanks!





---
 drivers/vdpa/vdpa_sim/vdpa_sim.h |   2 +
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 108 +++
 drivers/vdpa/Kconfig |   1 +
 3 files changed, 69 insertions(+), 42 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
index b02142293d5b..6efe205e583e 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
@@ -6,6 +6,7 @@
 #ifndef _VDPA_SIM_H
 #define _VDPA_SIM_H
+#include 
 #include 
 #include 
 #include 
@@ -55,6 +56,7 @@ struct vdpasim {
/* virtio config according to device type */
void *config;
struct vhost_iotlb *iommu;
+   struct iova_domain iova;
void *buffer;
u32 status;
u32 generation;
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index b3fcc67bfdf0..341b9daf2ea4 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -17,6 +17,7 @@
 #include 
 #include 
 #include 
+#include 
 #include "vdpa_sim.h"
@@ -128,30 +129,57 @@ static int dir_to_perm(enum dma_data_direction dir)
return perm;
 }
+static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr,
+   size_t size, unsigned int perm)
+{
+   struct iova *iova;
+   dma_addr_t dma_addr;
+   int ret;
+
+   /* We set the limit_pfn to the maximum (~0UL - 1) */
+   iova = alloc_iova(>iova, size, ~0UL - 1, true);



Let's use ULONG_MAX?


Definitely, much better!





+   if (!iova)
+   return DMA_MAPPING_ERROR;
+
+   dma_addr = iova_dma_addr(>iova, iova);
+
+   spin_lock(>iommu_lock);
+   ret = vhost_iotlb_add_range(vdpasim->iommu, (u64)dma_addr,
+   (u64)dma_addr + size - 1, (u64)paddr, perm);
+   spin_unlock(>iommu_lock);
+
+   if (ret) {
+   __free_iova(>iova, iova);
+   return DMA_MAPPING_ERROR;
+   }
+
+   return dma_addr;
+}
+
+static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr,
+   size_t size)
+{
+   spin_lock(>iommu_lock);
+   vhost_iotlb_del_range(vdpasim->iommu, (u64)dma_addr,
+ (u64)dma_addr + size - 1);
+   spin_unlock(>iommu_lock);
+
+   free_iova(>iova, iova_pfn(>iova, dma_addr));
+}
+
 static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
   unsigned long offset, size_t size,
   enum dma_data_direction dir,
   unsigned long attrs)
 {
struct vdpasim *vdpasim = dev_to_sim(dev);
-   struct vhost_iotlb *iommu = vdpasim->iommu;
-   u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
-   int ret, perm = dir_to_perm(dir);
+   phys_addr_t paddr = page_to_phys(page) + offset;
+   int perm = dir_to_perm(dir);
if (perm < 0)
return DMA_MAPPING_ERROR;
-   /* For simplicity, use identical mapping to avoid e.g iova
-* allocator.
-*/
-   spin_lock(>iommu_lock);
-   ret = vhost_iotlb_add_range(iommu, pa, pa + size - 1,
-   pa, dir_to_perm(dir));
-   spin_unlock(>iommu_lock);
-   if (ret)
-   return DMA_MAPPING_ERROR;
-
-   return (dma_addr_t)(pa);
+   return vdpasim_map_range(vdpasim, paddr, size, perm);
 }
 static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
@@ -159,12 +187,8 @@ static void vdpasim_unmap_page(struct device *dev, 
dma_addr_t dma_addr,
   unsigned long attrs)
 {
struct vdpasim *vdpasim = dev_to_sim(dev);
-   struct vhost_iotlb *iommu = vdpasim->iommu;
-   spin_lock(>iommu_lock);
-   vhost_iotlb_del_range(iommu, (u64)dma_addr,
- (u64)dma_addr + size - 1);
-   spin_unlock(>iommu_lock);
+   vdpasim_unmap_range(vdpasim, dma_addr, size);
 }
 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
@@ -172,27 +196,22 @@ static void *vdpasim_alloc_coherent(struct device *dev, 
size_t size,
unsigned 

Re: [PATCH] vdpa_sim: use iova module to allocate IOVA addresses

2020-12-22 Thread Jason Wang



On 2020/12/23 上午1:45, Stefano Garzarella wrote:

The identical mapping used until now created issues when mapping
different virtual pages with the same physical address.
To solve this issue, we can use the iova module, to handle the IOVA
allocation.
For semplicity we use an IOVA allocator with byte granularity.



Should be simplicity, so did one comment below.




We add two new functions, vdpasim_map_range() and vdpasim_unmap_range(),
to handle the IOVA allocation and the registration into the IOMMU/IOTLB.
These functions are used by dma_map_ops callbacks.

Signed-off-by: Stefano Garzarella 



Few nits, but:

Acked-by: Jason Wang 



---
  drivers/vdpa/vdpa_sim/vdpa_sim.h |   2 +
  drivers/vdpa/vdpa_sim/vdpa_sim.c | 108 +++
  drivers/vdpa/Kconfig |   1 +
  3 files changed, 69 insertions(+), 42 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
index b02142293d5b..6efe205e583e 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
@@ -6,6 +6,7 @@
  #ifndef _VDPA_SIM_H
  #define _VDPA_SIM_H
  
+#include 

  #include 
  #include 
  #include 
@@ -55,6 +56,7 @@ struct vdpasim {
/* virtio config according to device type */
void *config;
struct vhost_iotlb *iommu;
+   struct iova_domain iova;
void *buffer;
u32 status;
u32 generation;
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index b3fcc67bfdf0..341b9daf2ea4 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -17,6 +17,7 @@
  #include 
  #include 
  #include 
+#include 
  
  #include "vdpa_sim.h"
  
@@ -128,30 +129,57 @@ static int dir_to_perm(enum dma_data_direction dir)

return perm;
  }
  
+static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr,

+   size_t size, unsigned int perm)
+{
+   struct iova *iova;
+   dma_addr_t dma_addr;
+   int ret;
+
+   /* We set the limit_pfn to the maximum (~0UL - 1) */
+   iova = alloc_iova(>iova, size, ~0UL - 1, true);



Let's use ULONG_MAX?



+   if (!iova)
+   return DMA_MAPPING_ERROR;
+
+   dma_addr = iova_dma_addr(>iova, iova);
+
+   spin_lock(>iommu_lock);
+   ret = vhost_iotlb_add_range(vdpasim->iommu, (u64)dma_addr,
+   (u64)dma_addr + size - 1, (u64)paddr, perm);
+   spin_unlock(>iommu_lock);
+
+   if (ret) {
+   __free_iova(>iova, iova);
+   return DMA_MAPPING_ERROR;
+   }
+
+   return dma_addr;
+}
+
+static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr,
+   size_t size)
+{
+   spin_lock(>iommu_lock);
+   vhost_iotlb_del_range(vdpasim->iommu, (u64)dma_addr,
+ (u64)dma_addr + size - 1);
+   spin_unlock(>iommu_lock);
+
+   free_iova(>iova, iova_pfn(>iova, dma_addr));
+}
+
  static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
   unsigned long offset, size_t size,
   enum dma_data_direction dir,
   unsigned long attrs)
  {
struct vdpasim *vdpasim = dev_to_sim(dev);
-   struct vhost_iotlb *iommu = vdpasim->iommu;
-   u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
-   int ret, perm = dir_to_perm(dir);
+   phys_addr_t paddr = page_to_phys(page) + offset;
+   int perm = dir_to_perm(dir);
  
  	if (perm < 0)

return DMA_MAPPING_ERROR;
  
-	/* For simplicity, use identical mapping to avoid e.g iova

-* allocator.
-*/
-   spin_lock(>iommu_lock);
-   ret = vhost_iotlb_add_range(iommu, pa, pa + size - 1,
-   pa, dir_to_perm(dir));
-   spin_unlock(>iommu_lock);
-   if (ret)
-   return DMA_MAPPING_ERROR;
-
-   return (dma_addr_t)(pa);
+   return vdpasim_map_range(vdpasim, paddr, size, perm);
  }
  
  static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,

@@ -159,12 +187,8 @@ static void vdpasim_unmap_page(struct device *dev, 
dma_addr_t dma_addr,
   unsigned long attrs)
  {
struct vdpasim *vdpasim = dev_to_sim(dev);
-   struct vhost_iotlb *iommu = vdpasim->iommu;
  
-	spin_lock(>iommu_lock);

-   vhost_iotlb_del_range(iommu, (u64)dma_addr,
- (u64)dma_addr + size - 1);
-   spin_unlock(>iommu_lock);
+   vdpasim_unmap_range(vdpasim, dma_addr, size);
  }
  
  static void *vdpasim_alloc_coherent(struct device *dev, size_t size,

@@ -172,27 +196,22 @@ static void *vdpasim_alloc_coherent(struct device *dev, 
size_t size,
unsigned long attrs)
  {
struct vdpasim *vdpasim = dev_to_sim(dev);
-   struct vhost_iotlb *iommu =