Skip to contentSkip to conversation

iommu/virtio: Implement viommu_{un,}map_pages

PATCH In review Erik Kurzinger 2 participants 2 messages Aug 2, 2022 – Aug 4, 2022

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

PATCH 1/1 iommu/virtio: Implement viommu_{un,}map_pages

Because virtio-iommu currently only implements the map and unmap iommu domain ops, when mapping a block of memory a separate mapping will be created for each page. Since the number of DMA mappings which may exist on the physical IOMMU is limited (either the user's locked memory limit for memory-backed mappings or else the 64K limit imposed by the type1 driver), this can become problematic for larger blocks of memory.

To avoid such issues, we provide implementations for the map_pages and unmap_pages iommu domain ops. This allow us to only use a single mapping, even for multiple-page blocks of memory. These new functions will simply defer to the existing map and unmap functions, with the size computed from the page size and page count.

---
 drivers/iommu/virtio-iommu.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/iommu/virtio-iommu.c +23 -0
index 25be4b822aa0..37f11a799f2a 100644
@@ -827,6 +827,18 @@ static int viommu_map(struct iommu_domain *domain, unsigned long iova,
 	return ret;
 }
 
+static int viommu_map_pages(struct iommu_domain *domain,
+			    unsigned long iova, phys_addr_t paddr,
+			    size_t pgsize, size_t pgcount,
+			    int prot, gfp_t gfp, size_t *mapped)
+{
+	u64 size = pgsize * pgcount;
+	int ret = viommu_map(domain, iova, paddr, size, prot, gfp);
+	if (!ret && mapped)
+		*mapped = size;
+	return ret;
+}
+
 static size_t viommu_unmap(struct iommu_domain *domain, unsigned long iova,
 			   size_t size, struct iommu_iotlb_gather *gather)
 {
@@ -854,6 +866,15 @@ static size_t viommu_unmap(struct iommu_domain *domain, unsigned long iova,
 	return ret ? 0 : unmapped;
 }
 
+static size_t viommu_unmap_pages(struct iommu_domain *domain,
+				 unsigned long iova,
+				 size_t pgsize, size_t pgcount,
+				 struct iommu_iotlb_gather *gather)
+{
+	u64 size = pgsize * pgcount;
+	return viommu_unmap(domain, iova, size, gather);
+}
+
 static phys_addr_t viommu_iova_to_phys(struct iommu_domain *domain,
 				       dma_addr_t iova)
 {
@@ -1019,7 +1040,9 @@ static struct iommu_ops viommu_ops = {
 	.default_domain_ops = &(const struct iommu_domain_ops) {
 		.attach_dev		= viommu_attach_dev,
 		.map			= viommu_map,
+		.map_pages		= viommu_map_pages,
 		.unmap			= viommu_unmap,
+		.unmap_pages		= viommu_unmap_pages,
 		.iova_to_phys		= viommu_iova_to_phys,
 		.iotlb_sync		= viommu_iotlb_sync,
 		.free			= viommu_domain_free,
Signature · 1 line
2.37.1
reviewer
#1 lore raw
Quoted 7 lines · Erik Kurzinger

Because virtio-iommu currently only implements the map and unmap iommu domain ops, when mapping a block of memory a separate mapping will be created for each page. Since the number of DMA mappings which may exist on the physical IOMMU is limited (either the user's locked memory limit for memory-backed mappings or else the 64K limit imposed by the type1 driver), this can become problematic for larger blocks of memory.

Quoted 5 lines

To avoid such issues, we provide implementations for the map_pages and unmap_pages iommu domain ops. This allow us to only use a single mapping, even for multiple-page blocks of memory. These new functions will simply defer to the existing map and unmap functions, with the size computed from the page size and page count.

FYI, an equivalent patch is already queued up for 5.20/6.0:

https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git/commit/?h=virtio

Robin.

Quoted 56 lines
---
  drivers/iommu/virtio-iommu.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
drivers/iommu/virtio-iommu.c +12 -0
index 25be4b822aa0..37f11a799f2a 100644
@@ -827,6 +827,18 @@ static int viommu_map(struct iommu_domain *domain, 
 unsigned long iova,
      return ret;
  }
 
+static int viommu_map_pages(struct iommu_domain *domain,
+                unsigned long iova, phys_addr_t paddr,
+                size_t pgsize, size_t pgcount,
+                int prot, gfp_t gfp, size_t *mapped)
+{
+    u64 size = pgsize * pgcount;
+    int ret = viommu_map(domain, iova, paddr, size, prot, gfp);
+    if (!ret && mapped)
+        *mapped = size;
+    return ret;
+}
+
  static size_t viommu_unmap(struct iommu_domain *domain, unsigned long 
 iova,

                size_t size, struct iommu_iotlb_gather *gather) {

drivers/iommu/virtio-iommu.c +9 -0
@@ -854,6 +866,15 @@ static size_t viommu_unmap(struct iommu_domain 
 *domain, unsigned long iova,
      return ret ? 0 : unmapped;
  }
 
+static size_t viommu_unmap_pages(struct iommu_domain *domain,
+                 unsigned long iova,
+                 size_t pgsize, size_t pgcount,
+                 struct iommu_iotlb_gather *gather)
+{
+    u64 size = pgsize * pgcount;
+    return viommu_unmap(domain, iova, size, gather);
+}
+
  static phys_addr_t viommu_iova_to_phys(struct iommu_domain *domain,
                         dma_addr_t iova)

 {

drivers/iommu/virtio-iommu.c +2 -0
@@ -1019,7 +1040,9 @@ static struct iommu_ops viommu_ops = {
      .default_domain_ops = &(const struct iommu_domain_ops) {
          .attach_dev        = viommu_attach_dev,
          .map            = viommu_map,
+        .map_pages        = viommu_map_pages,
          .unmap            = viommu_unmap,
+        .unmap_pages        = viommu_unmap_pages,
          .iova_to_phys        = viommu_iova_to_phys,
          .iotlb_sync        = viommu_iotlb_sync,
          .free            = viommu_domain_free,