Skip to contentSkip to conversation

iommu/dma: Fix IOVA reserve dma ranges

PATCH Superseded Srinath Mannam 2 participants 3 messages Sep 9, 2020 – Sep 10, 2020

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/dma: Fix IOVA reserve dma ranges

Fix IOVA reserve failure for memory regions listed in dma-ranges in the following cases.

- start address of memory region is 0x0.
- end address of a memory region is equal to start address of next memory region.

Fixes aadad097cd46f ("iommu/dma: Reserve IOVA for PCIe inaccessible DMA address")
---
 drivers/iommu/dma-iommu.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/iommu/dma-iommu.c +11 -4
index 5141d49a046b..0a3f67a4f9ae 100644
@@ -213,14 +213,21 @@ static int iova_reserve_pci_windows(struct pci_dev *dev,
 	resource_list_for_each_entry(window, &bridge->dma_ranges) {
 		end = window->res->start - window->offset;
 resv_iova:
+		if (end < start) {
+			/* dma_ranges list should be sorted */
+			dev_err(&dev->dev, "Failed to reserve IOVA\n");
+			return -EINVAL;
+		}
+		/*
+		 * Skip the cases when start address of first memory region is
+		 * 0x0 and end address of one memory region and start address
+		 * of next memory region are equal. Reserve IOVA for rest of
+		 * addresses fall in between given memory ranges.
+		 */
 		if (end > start) {
 			lo = iova_pfn(iovad, start);
 			hi = iova_pfn(iovad, end);
 			reserve_iova(iovad, lo, hi);
-		} else {
-			/* dma_ranges list should be sorted */
-			dev_err(&dev->dev, "Failed to reserve IOVA\n");
-			return -EINVAL;
 		}
 
 		start = window->res->end - window->offset + 1;
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 4 lines · Srinath Mannam

Fix IOVA reserve failure for memory regions listed in dma-ranges in the following cases.

- start address of memory region is 0x0.

That's fair enough, and in fact generalises to the case of zero-sized gaps between regions, which is indeed an oversight.

Quoted 2 lines

- end address of a memory region is equal to start address of next memory region.

This part doesn't make much sense, however - if the regions described in bridge->dma_ranges overlap, that's a bug in whoever created a malformed list to begin with. Possibly it's just poor wording, and you're using "memory regions" to refer to any or all of the dma_ranges, the reserved IOVA ranges, and what "start" and "end" in this function represent which isn't quite either of those.

Quoted 20 lines
Fixes aadad097cd46f ("iommu/dma: Reserve IOVA for PCIe inaccessible DMA address")
---
  drivers/iommu/dma-iommu.c | 15 +++++++++++----
  1 file changed, 11 insertions(+), 4 deletions(-)
drivers/iommu/dma-iommu.c +6 -0
index 5141d49a046b..0a3f67a4f9ae 100644
@@ -213,14 +213,21 @@ static int iova_reserve_pci_windows(struct pci_dev *dev,
  	resource_list_for_each_entry(window, &bridge->dma_ranges) {
  		end = window->res->start - window->offset;
  resv_iova:
+		if (end < start) {
+			/* dma_ranges list should be sorted */
+			dev_err(&dev->dev, "Failed to reserve IOVA\n");
+			return -EINVAL;
+		}
+		/*

drivers/iommu/dma-iommu.c static int iova_reserve_pci_windows(struct pci_dev *dev, · line 220

Quoted 10 lines
drivers/iommu/dma-iommu.c +5 -1
+		 * Skip the cases when start address of first memory region is
+		 * 0x0 and end address of one memory region and start address
+		 * of next memory region are equal. Reserve IOVA for rest of
+		 * addresses fall in between given memory ranges.
+		 */
  		if (end > start) {
  			lo = iova_pfn(iovad, start);
  			hi = iova_pfn(iovad, end);
  			reserve_iova(iovad, lo, hi);
-		} else {

Surely this only needs to be a one-liner?

- } else {
+ } else if (end < start) {

(or possibly "end != start"; I can't quite decide which expresses the semantic intent better)

The rest just looks like unnecessary churn - I don't think it needs commenting that a sorted list may simply not have gaps between entries, and as above I think the wording of that comment is actively misleading.

Robin.

drivers/iommu/dma-iommu.c static int iova_reserve_pci_windows(struct pci_dev *dev, · line 233

Quoted 7 lines
drivers/iommu/dma-iommu.c +0 -3
-			/* dma_ranges list should be sorted */
-			dev_err(&dev->dev, "Failed to reserve IOVA\n");
-			return -EINVAL;
  		}
 
  		start = window->res->end - window->offset + 1;

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#2 lore raw
Quoted 1 line · Robin Murphy

Hi Robin,
Thanks for review

Quoted 8 lines · Srinath Mannam
Quoted 4 lines

Fix IOVA reserve failure for memory regions listed in dma-ranges in the following cases.

- start address of memory region is 0x0.

That's fair enough, and in fact generalises to the case of zero-sized gaps between regions, which is indeed an oversight.

Yes this is the main reason for the requirement of this fix.

Quoted 10 lines
Quoted 3 lines

- end address of a memory region is equal to start address of next memory region.

This part doesn't make much sense, however - if the regions described in bridge->dma_ranges overlap, that's a bug in whoever created a malformed list to begin with. Possibly it's just poor wording, and you're using "memory regions" to refer to any or all of the dma_ranges, the reserved IOVA ranges, and what "start" and "end" in this function represent which isn't quite either of those.

You are right, this case is very unlikely that nobody lists regions with zero gap, in such a case they will combine both the regions. Reason for highlighting this point is, the same fix will handle this case also. Here I used memory regions to refer entries of dma-ranges(allowed IOVA addresses range) not reserved IOVA ranges. start and end variables in this function refers to start and end addresses of reserved IOVA ranges which are derived from dma ranges resources start and end values.

Quoted 25 lines
Quoted 25 lines
Fixes aadad097cd46f ("iommu/dma: Reserve IOVA for PCIe inaccessible DMA address")
---
  drivers/iommu/dma-iommu.c | 15 +++++++++++----
  1 file changed, 11 insertions(+), 4 deletions(-)
drivers/iommu/dma-iommu.c +10 -0
index 5141d49a046b..0a3f67a4f9ae 100644
@@ -213,14 +213,21 @@ static int iova_reserve_pci_windows(struct pci_dev *dev,
      resource_list_for_each_entry(window, &bridge->dma_ranges) {
              end = window->res->start - window->offset;
  resv_iova:
+             if (end < start) {
+                     /* dma_ranges list should be sorted */
+                     dev_err(&dev->dev, "Failed to reserve IOVA\n");
+                     return -EINVAL;
+             }
+             /*
+              * Skip the cases when start address of first memory region is
+              * 0x0 and end address of one memory region and start address
+              * of next memory region are equal. Reserve IOVA for rest of
+              * addresses fall in between given memory ranges.
Quoted 8 lines

drivers/iommu/dma-iommu.c static int iova_reserve_pci_windows(struct pci_dev *dev, · line 220

Quoted 6 lines
drivers/iommu/dma-iommu.c +1 -1
+              */
              if (end > start) {
                      lo = iova_pfn(iovad, start);
                      hi = iova_pfn(iovad, end);
                      reserve_iova(iovad, lo, hi);
-             } else {

Surely this only needs to be a one-liner?

Yes I agree with you this one line is sufficient.

Quoted 6 lines
(quoted diff) +1 -1
-               } else {
+               } else if (end < start) {

(or possibly "end != start"; I can't quite decide which expresses the semantic intent better)

I think "end < start" is better choice because it tells list is not sorted and "!=" contradicts previous condition "end > start".

Quoted 4 lines

The rest just looks like unnecessary churn - I don't think it needs commenting that a sorted list may simply not have gaps between entries, and as above I think the wording of that comment is actively misleading.

I agree with you, these lines were added to explain the issue and fix with more details.
I will send a new patch with a single line change as you said. " } else if (end < start) {"

Thanks & Regards,
Srinath.

Quoted 10 lines

Robin.

Quoted 7 lines
drivers/iommu/dma-iommu.c +0 -3
-                     /* dma_ranges list should be sorted */
-                     dev_err(&dev->dev, "Failed to reserve IOVA\n");
-                     return -EINVAL;
              }
 
              start = window->res->end - window->offset + 1;

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu