Skip to contentSkip to conversation

dma-mapping: reliably inform about DMA support for IOMMU

PATCH In review Leon Romanovsky 4 participants 5 messages Sep 11, 2024 – Sep 12, 2024 DMA mapping helpers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-mapping: reliably inform about DMA support for IOMMU

From: Leon Romanovsky <[email protected]>

If the DMA IOMMU path is going to be used, the appropriate check should return that DMA is supported.

---
 kernel/dma/mapping.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/dma/mapping.c +3 -0
index 1a7de37bd643..38d7b3239dbb 100644
@@ -824,6 +824,9 @@ static int dma_supported(struct device *dev, u64 mask)
 
 	if (WARN_ON(ops && use_dma_iommu(dev)))
 		return false;
+
+	if (use_dma_iommu(dev))
+		return true;
 	/*
 	 * ->dma_supported sets the bypass flag, so we must always call
 	 * into the method here unless the device is truly direct mapped.
Signature · 1 line
2.46.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Leon Romanovsky

From: Leon Romanovsky <[email protected]>

If the DMA IOMMU path is going to be used, the appropriate check should return that DMA is supported.

Oof, indeed the significance of what iommu_dma_ops *didn't* implement is a subtle one...

Quoted 22 lines
---
  kernel/dma/mapping.c | 3 +++
  1 file changed, 3 insertions(+)
kernel/dma/mapping.c +3 -0
index 1a7de37bd643..38d7b3239dbb 100644
@@ -824,6 +824,9 @@ static int dma_supported(struct device *dev, u64 mask)
 
  	if (WARN_ON(ops && use_dma_iommu(dev)))
  		return false;
+
+	if (use_dma_iommu(dev))
+		return true;
  	/*
  	 * ->dma_supported sets the bypass flag, so we must always call
  	 * into the method here unless the device is truly direct mapped.
Tested-by on v1 1/1
#2 lore raw
In reply to Leon Romanovsky
Quoted 9 lines · Leon Romanovsky

From: Leon Romanovsky <[email protected]>

If the DMA IOMMU path is going to be used, the appropriate check should return that DMA is supported.

Thank you for the quick patch. It fixes the reported issue.

Thanks,
Nícolas

#3 lore raw
In reply to Leon Romanovsky

Thanks, I've applied this. I do like the flow of dma_supported even less with the duplicate condition now. Can I get a quick review for this incremental cleanup, please?

---
From d311bfe189d12a836b4add55fdb946f37f8697fa Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Thu, 12 Sep 2024 09:21:18 +0200
Subject: dma-mapping: reflow dma_supported

dma_supported has become too much spaghetti for my taste. Reflow it to remove the duplicate use_dma_iommu condition and make the main path more obvious.

---
 kernel/dma/mapping.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
kernel/dma/mapping.c +14 -11
index 022d670f8cad29..cd2a97d362cd24 100644
@@ -841,20 +841,23 @@ static int dma_supported(struct device *dev, u64 mask)
 {
 	const struct dma_map_ops *ops = get_dma_ops(dev);
 
-	if (WARN_ON(ops && use_dma_iommu(dev)))
-		return false;
-
-	if (use_dma_iommu(dev))
+	if (use_dma_iommu(dev)) {
+		if (WARN_ON(ops))
+			return false;
 		return true;
+	}
+
 	/*
-	 * ->dma_supported sets the bypass flag, so we must always call
-	 * into the method here unless the device is truly direct mapped.
+	 * ->dma_supported sets and clears the bypass flag, so ignore it here
+	 * and always call into the method if there is one.
 	 */
-	if (!ops)
-		return dma_direct_supported(dev, mask);
-	if (!ops->dma_supported)
-		return 1;
-	return ops->dma_supported(dev, mask);
+	if (ops) {
+		if (!ops->dma_supported)
+			return 1;
+		return ops->dma_supported(dev, mask);
+	}
+
+	return dma_direct_supported(dev, mask);
 }
 
 bool dma_pci_p2pdma_supported(struct device *dev)
Signature · 1 line
2.45.2
author Reviewed-by
#4 lore raw
Quoted 10 lines · Christoph Hellwig

Thanks, I've applied this. I do like the flow of dma_supported even less with the duplicate condition now. Can I get a quick review for this incremental cleanup, please?

---
From d311bfe189d12a836b4add55fdb946f37f8697fa Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Thu, 12 Sep 2024 09:21:18 +0200
Subject: dma-mapping: reflow dma_supported

Quoted 8 lines

dma_supported has become too much spaghetti for my taste. Reflow it to remove the duplicate use_dma_iommu condition and make the main path more obvious.

---
 kernel/dma/mapping.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
Quoted 7 lines
kernel/dma/mapping.c +0 -0
index 022d670f8cad29..cd2a97d362cd24 100644
@@ -841,20 +841,23 @@ static int dma_supported(struct device *dev, u64 mask)
 {

kernel/dma/mapping.c static int dma_supported(struct device *dev, u64 mask) · line 847

Quoted 10 lines
kernel/dma/mapping.c +3 -4
 	const struct dma_map_ops *ops = get_dma_ops(dev);
 
-	if (WARN_ON(ops && use_dma_iommu(dev)))
-		return false;
-
-	if (use_dma_iommu(dev))
+	if (use_dma_iommu(dev)) {
+		if (WARN_ON(ops))
+			return false;
 		return true;

You can write it as "return !WARN_ON(ops);" if you want to be fancy, but your version is perfectly fine too, so ignore this comment :).

Quoted 6 lines
kernel/dma/mapping.c +3 -2
+	}
+
 	/*
-	 * ->dma_supported sets the bypass flag, so we must always call
-	 * into the method here unless the device is truly direct mapped.
+	 * ->dma_supported sets and clears the bypass flag, so ignore it here

kernel/dma/mapping.c static int dma_supported(struct device *dev, u64 mask) · line 856

Quoted 10 lines
kernel/dma/mapping.c +4 -5
+	 * and always call into the method if there is one.
 	 */
-	if (!ops)
-		return dma_direct_supported(dev, mask);
-	if (!ops->dma_supported)
-		return 1;
-	return ops->dma_supported(dev, mask);
+	if (ops) {
+		if (!ops->dma_supported)
+			return 1;

Worth to change it to be "return true;"

Quoted 10 lines
kernel/dma/mapping.c +4 -0
+		return ops->dma_supported(dev, mask);
+	}
+
+	return dma_direct_supported(dev, mask);
 }
 
 bool dma_pci_p2pdma_supported(struct device *dev)
Signature · 1 line
2.45.2