Skip to contentSkip to conversation

dma,x86,xen: fix dma_get_required_mask() when dma != phys address

PATCH Superseded David Vrabel 2 participants 7 messages May 12, 2014

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover dma,x86,xen: fix dma_get_required_mask() when dma != phys address

x86 uses the generic dma_get_required_mask() which assumes that DMA address == physical address. For Xen PV guests this is not the case and certain drivers will cause excessive use of the SWIOTLB.

Patch #1 is a straight forward refactoring of a common function.

Patch #2 addes the required arch-specific implementation of dma_get_required_mask() to x86.

David

#1 lore raw

PATCH 1/2 dma: add dma_get_required_mask_from_max_pfn()

ia64 provides a duplicate of the generic dma_get_required_mask() because it has ARCH_HAS_GET_REQUIRED_MASK. Provide a common dma_get_require_mask_max_pfn() instead.

---
 arch/ia64/include/asm/machvec.h      |    2 +-
 arch/ia64/include/asm/machvec_init.h |    1 -
 arch/ia64/pci/pci.c                  |   20 --------------------
 drivers/base/platform.c              |   10 ++++++++--
 include/linux/dma-mapping.h          |    1 +
 5 files changed, 10 insertions(+), 24 deletions(-)
arch/ia64/include/asm/machvec.h +1 -1
index 9c39bdf..beaa47d 100644
@@ -287,7 +287,7 @@ extern struct dma_map_ops *dma_get_ops(struct device *);
 # define platform_dma_get_ops		dma_get_ops
 #endif
 #ifndef platform_dma_get_required_mask
-# define  platform_dma_get_required_mask	ia64_dma_get_required_mask
+# define  platform_dma_get_required_mask	dma_get_required_mask_from_max_pfn
 #endif
 #ifndef platform_irq_to_vector
 # define platform_irq_to_vector		__ia64_irq_to_vector
arch/ia64/include/asm/machvec_init.h +0 -1
index 37a4698..ef964b2 100644
@@ -3,7 +3,6 @@
 
 extern ia64_mv_send_ipi_t ia64_send_ipi;
 extern ia64_mv_global_tlb_purge_t ia64_global_tlb_purge;
-extern ia64_mv_dma_get_required_mask ia64_dma_get_required_mask;
 extern ia64_mv_irq_to_vector __ia64_irq_to_vector;
 extern ia64_mv_local_vector_to_irq __ia64_local_vector_to_irq;
 extern ia64_mv_pci_get_legacy_mem_t ia64_pci_get_legacy_mem;
arch/ia64/pci/pci.c +0 -20
index 291a582..79da21b 100644
@@ -791,26 +791,6 @@ static void __init set_pci_dfl_cacheline_size(void)
 	pci_dfl_cache_line_size = (1 << cci.pcci_line_size) / 4;
 }
 
-u64 ia64_dma_get_required_mask(struct device *dev)
-{
-	u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
-	u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
-	u64 mask;
-
-	if (!high_totalram) {
-		/* convert to mask just covering totalram */
-		low_totalram = (1 << (fls(low_totalram) - 1));
-		low_totalram += low_totalram - 1;
-		mask = low_totalram;
-	} else {
-		high_totalram = (1 << (fls(high_totalram) - 1));
-		high_totalram += high_totalram - 1;
-		mask = (((u64)high_totalram) << 32) + 0xffffffff;
-	}
-	return mask;
-}
-EXPORT_SYMBOL_GPL(ia64_dma_get_required_mask);
-
 u64 dma_get_required_mask(struct device *dev)
 {
 	return platform_dma_get_required_mask(dev);
drivers/base/platform.c +8 -2
index 5b47210..a53ef9d 100644
@@ -942,8 +942,7 @@ int __init platform_bus_init(void)
 	return error;
 }
 
-#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
-u64 dma_get_required_mask(struct device *dev)
+u64 dma_get_required_mask_from_max_pfn(struct device *dev)
 {
 	u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
 	u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
@@ -961,6 +960,13 @@ u64 dma_get_required_mask(struct device *dev)
 	}
 	return mask;
 }
+EXPORT_SYMBOL_GPL(dma_get_required_mask_from_max_pfn);
+
+#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
+u64 dma_get_required_mask(struct device *dev)
+{
+	return dma_get_required_mask_from_max_pfn(dev);
+}
 EXPORT_SYMBOL_GPL(dma_get_required_mask);
 #endif
 
include/linux/dma-mapping.h +1 -0
index fd4aee2..bc6e2b6 100644
@@ -121,6 +121,7 @@ static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
 	return dma_set_mask_and_coherent(dev, mask);
 }
 
+extern u64 dma_get_required_mask_from_max_pfn(struct device *dev);
 extern u64 dma_get_required_mask(struct device *dev);
 
 static inline unsigned int dma_get_max_seg_size(struct device *dev)
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 1/2 dma: add dma_get_required_mask_from_max_pfn()

In reply to David Vrabel

ia64 provides a duplicate of the generic dma_get_required_mask() because it has ARCH_HAS_GET_REQUIRED_MASK. Provide a common dma_get_require_mask_max_pfn() instead.

---
 arch/ia64/include/asm/machvec.h      |    2 +-
 arch/ia64/include/asm/machvec_init.h |    1 -
 arch/ia64/pci/pci.c                  |   20 --------------------
 drivers/base/platform.c              |   10 ++++++++--
 include/linux/dma-mapping.h          |    1 +
 5 files changed, 10 insertions(+), 24 deletions(-)
arch/ia64/include/asm/machvec.h +1 -1
index 9c39bdf..beaa47d 100644
@@ -287,7 +287,7 @@ extern struct dma_map_ops *dma_get_ops(struct device *);
 # define platform_dma_get_ops		dma_get_ops
 #endif
 #ifndef platform_dma_get_required_mask
-# define  platform_dma_get_required_mask	ia64_dma_get_required_mask
+# define  platform_dma_get_required_mask	dma_get_required_mask_from_max_pfn
 #endif
 #ifndef platform_irq_to_vector
 # define platform_irq_to_vector		__ia64_irq_to_vector
arch/ia64/include/asm/machvec_init.h +0 -1
index 37a4698..ef964b2 100644
@@ -3,7 +3,6 @@
 
 extern ia64_mv_send_ipi_t ia64_send_ipi;
 extern ia64_mv_global_tlb_purge_t ia64_global_tlb_purge;
-extern ia64_mv_dma_get_required_mask ia64_dma_get_required_mask;
 extern ia64_mv_irq_to_vector __ia64_irq_to_vector;
 extern ia64_mv_local_vector_to_irq __ia64_local_vector_to_irq;
 extern ia64_mv_pci_get_legacy_mem_t ia64_pci_get_legacy_mem;
arch/ia64/pci/pci.c +0 -20
index 291a582..79da21b 100644
@@ -791,26 +791,6 @@ static void __init set_pci_dfl_cacheline_size(void)
 	pci_dfl_cache_line_size = (1 << cci.pcci_line_size) / 4;
 }
 
-u64 ia64_dma_get_required_mask(struct device *dev)
-{
-	u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
-	u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
-	u64 mask;
-
-	if (!high_totalram) {
-		/* convert to mask just covering totalram */
-		low_totalram = (1 << (fls(low_totalram) - 1));
-		low_totalram += low_totalram - 1;
-		mask = low_totalram;
-	} else {
-		high_totalram = (1 << (fls(high_totalram) - 1));
-		high_totalram += high_totalram - 1;
-		mask = (((u64)high_totalram) << 32) + 0xffffffff;
-	}
-	return mask;
-}
-EXPORT_SYMBOL_GPL(ia64_dma_get_required_mask);
-
 u64 dma_get_required_mask(struct device *dev)
 {
 	return platform_dma_get_required_mask(dev);
drivers/base/platform.c +8 -2
index 5b47210..a53ef9d 100644
@@ -942,8 +942,7 @@ int __init platform_bus_init(void)
 	return error;
 }
 
-#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
-u64 dma_get_required_mask(struct device *dev)
+u64 dma_get_required_mask_from_max_pfn(struct device *dev)
 {
 	u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
 	u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
@@ -961,6 +960,13 @@ u64 dma_get_required_mask(struct device *dev)
 	}
 	return mask;
 }
+EXPORT_SYMBOL_GPL(dma_get_required_mask_from_max_pfn);
+
+#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
+u64 dma_get_required_mask(struct device *dev)
+{
+	return dma_get_required_mask_from_max_pfn(dev);
+}
 EXPORT_SYMBOL_GPL(dma_get_required_mask);
 #endif
 
include/linux/dma-mapping.h +1 -0
index fd4aee2..bc6e2b6 100644
@@ -121,6 +121,7 @@ static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
 	return dma_set_mask_and_coherent(dev, mask);
 }
 
+extern u64 dma_get_required_mask_from_max_pfn(struct device *dev);
 extern u64 dma_get_required_mask(struct device *dev);
 
 static inline unsigned int dma_get_max_seg_size(struct device *dev)
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 2/2 x86, xen: correct dma_get_required_mask() for Xen PV guests

In reply to David Vrabel

On systems where DMA addresses and physical addresses are not 1:1 (such as Xen PV guests), the generic dma_get_required_mask() will not return the correct mask (since it uses max_pfn).

Some device drivers (such as mptsas, mpt2sas) use dma_get_required_mask() to set device DMA masks to allow them to use only 32-bit DMA addresses in hardware structures. This results in unnecessary use of the SWIOTLB if DMA addresses are more than 32-bits, impacting performance significantly.

Provide an arch-specific dma_get_required_mask() that defaults to the generic dma_get_required_mask_from_pfn().

Under Xen, the required DMA mask can then be set to always 64-bits.

---
 arch/x86/include/asm/device.h  |    2 ++
 arch/x86/kernel/pci-dma.c      |    7 +++++++
 arch/x86/xen/pci-swiotlb-xen.c |    1 +
 drivers/xen/swiotlb-xen.c      |    7 +++++++
 include/xen/swiotlb-xen.h      |    4 ++++
 5 files changed, 21 insertions(+)
arch/x86/include/asm/device.h +2 -0
index 03dd729..10bc628 100644
@@ -13,4 +13,6 @@ struct dev_archdata {
 struct pdev_archdata {
 };
 
+#define ARCH_HAS_DMA_GET_REQUIRED_MASK
+
 #endif /* _ASM_X86_DEVICE_H */
arch/x86/kernel/pci-dma.c +7 -0
index f7d0672..ad2c3e2 100644
@@ -135,6 +135,13 @@ void dma_generic_free_coherent(struct device *dev, size_t size, void *vaddr,
 		free_pages((unsigned long)vaddr, get_order(size));
 }
 
+u64 dma_get_required_mask(struct device *dev)
+{
+	if (dma_ops->get_required_mask)
+		return dma_ops->get_required_mask(dev);
+	return dma_get_required_mask_from_max_pfn(dev);
+}
+
 /*
  * See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
  * parameter documentation.
arch/x86/xen/pci-swiotlb-xen.c +1 -0
index 0e98e5d..a5d180a 100644
@@ -31,6 +31,7 @@ static struct dma_map_ops xen_swiotlb_dma_ops = {
 	.map_page = xen_swiotlb_map_page,
 	.unmap_page = xen_swiotlb_unmap_page,
 	.dma_supported = xen_swiotlb_dma_supported,
+	.get_required_mask = xen_swiotlb_get_required_mask,
 };
 
 /*
drivers/xen/swiotlb-xen.c +7 -0
index ebd8f21..798f62b 100644
@@ -683,3 +683,10 @@ xen_swiotlb_set_dma_mask(struct device *dev, u64 dma_mask)
 	return 0;
 }
 EXPORT_SYMBOL_GPL(xen_swiotlb_set_dma_mask);
+
+u64
+xen_swiotlb_get_required_mask(struct device *dev)
+{
+	return DMA_BIT_MASK(64);
+}
+EXPORT_SYMBOL_GPL(xen_swiotlb_get_required_mask);
include/xen/swiotlb-xen.h +4 -0
index 8b2eb93..6408888 100644
@@ -58,4 +58,8 @@ xen_swiotlb_dma_supported(struct device *hwdev, u64 mask);
 
 extern int
 xen_swiotlb_set_dma_mask(struct device *dev, u64 dma_mask);
+
+extern u64
+xen_swiotlb_get_required_mask(struct device *dev);
+
 #endif /* __LINUX_SWIOTLB_XEN_H */
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH 2/2 x86,xen: correct dma_get_required_mask() for Xen PV guests

In reply to David Vrabel

On systems where DMA addresses and physical addresses are not 1:1 (such as Xen PV guests), the generic dma_get_required_mask() will not return the correct mask (since it uses max_pfn).

Some device drivers (such as mptsas, mpt2sas) use dma_get_required_mask() to set device DMA masks to allow them to use only 32-bit DMA addresses in hardware structures. This results in unnecessary use of the SWIOTLB if DMA addresses are more than 32-bits, impacting performance significantly.

Provide an arch-specific dma_get_required_mask() that defaults to the generic dma_get_required_mask_from_pfn().

Under Xen, the required DMA mask can then be set to always 64-bits.

---
 arch/x86/include/asm/device.h  |    2 ++
 arch/x86/kernel/pci-dma.c      |    7 +++++++
 arch/x86/xen/pci-swiotlb-xen.c |    1 +
 drivers/xen/swiotlb-xen.c      |    7 +++++++
 include/xen/swiotlb-xen.h      |    4 ++++
 5 files changed, 21 insertions(+)
arch/x86/include/asm/device.h +2 -0
index 03dd729..10bc628 100644
@@ -13,4 +13,6 @@ struct dev_archdata {
 struct pdev_archdata {
 };
 
+#define ARCH_HAS_DMA_GET_REQUIRED_MASK
+
 #endif /* _ASM_X86_DEVICE_H */
arch/x86/kernel/pci-dma.c +7 -0
index f7d0672..ad2c3e2 100644
@@ -135,6 +135,13 @@ void dma_generic_free_coherent(struct device *dev, size_t size, void *vaddr,
 		free_pages((unsigned long)vaddr, get_order(size));
 }
 
+u64 dma_get_required_mask(struct device *dev)
+{
+	if (dma_ops->get_required_mask)
+		return dma_ops->get_required_mask(dev);
+	return dma_get_required_mask_from_max_pfn(dev);
+}
+
 /*
  * See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
  * parameter documentation.
arch/x86/xen/pci-swiotlb-xen.c +1 -0
index 0e98e5d..a5d180a 100644
@@ -31,6 +31,7 @@ static struct dma_map_ops xen_swiotlb_dma_ops = {
 	.map_page = xen_swiotlb_map_page,
 	.unmap_page = xen_swiotlb_unmap_page,
 	.dma_supported = xen_swiotlb_dma_supported,
+	.get_required_mask = xen_swiotlb_get_required_mask,
 };
 
 /*
drivers/xen/swiotlb-xen.c +7 -0
index ebd8f21..798f62b 100644
@@ -683,3 +683,10 @@ xen_swiotlb_set_dma_mask(struct device *dev, u64 dma_mask)
 	return 0;
 }
 EXPORT_SYMBOL_GPL(xen_swiotlb_set_dma_mask);
+
+u64
+xen_swiotlb_get_required_mask(struct device *dev)
+{
+	return DMA_BIT_MASK(64);
+}
+EXPORT_SYMBOL_GPL(xen_swiotlb_get_required_mask);
include/xen/swiotlb-xen.h +4 -0
index 8b2eb93..6408888 100644
@@ -58,4 +58,8 @@ xen_swiotlb_dma_supported(struct device *hwdev, u64 mask);
 
 extern int
 xen_swiotlb_set_dma_mask(struct device *dev, u64 dma_mask);
+
+extern u64
+xen_swiotlb_get_required_mask(struct device *dev);
+
 #endif /* __LINUX_SWIOTLB_XEN_H */
Signature · 1 line
1.7.10.4
#5 lore raw

Re: [PATCH 0/2] dma, x86, xen: fix dma_get_required_mask() when dma != phys address

In reply to David Vrabel
Quoted 8 lines · David Vrabel

x86 uses the generic dma_get_required_mask() which assumes that DMA address == physical address. For Xen PV guests this is not the case and certain drivers will cause excessive use of the SWIOTLB.

Patch #1 is a straight forward refactoring of a common function.

Patch #2 addes the required arch-specific implementation of dma_get_required_mask() to x86.

David,

Thank you for taking a look at that. If you would like, you can tack on Reviewed-by: Konrad Rzeszutek Wilk <[email protected]>

And with the *SWIOTLB maintainer hat on - you can add that tag as well.

Thanks!

Quoted 3 lines

David

#6 lore raw
In reply to David Vrabel
Quoted 8 lines · David Vrabel

x86 uses the generic dma_get_required_mask() which assumes that DMA address == physical address. For Xen PV guests this is not the case and certain drivers will cause excessive use of the SWIOTLB.

Patch #1 is a straight forward refactoring of a common function.

Patch #2 addes the required arch-specific implementation of dma_get_required_mask() to x86.

David,

Thank you for taking a look at that. If you would like, you can tack on Reviewed-by: Konrad Rzeszutek Wilk <[email protected]>

And with the *SWIOTLB maintainer hat on - you can add that tag as well.

Thanks!

Quoted 3 lines

David