Thread (37 messages) 37 messages, 9 authors, 2025-08-05

Re: [PATCH 6/8] dma-mapping: fail early if physical address is mapped through platform callback

From: Robin Murphy <robin.murphy@arm.com>
Date: 2025-07-25 20:04:59
Also in: linux-doc, linux-iommu, linux-mm, linuxppc-dev, lkml, virtualization

On 2025-06-25 2:19 pm, Leon Romanovsky wrote:
From: Leon Romanovsky <leonro@nvidia.com>

All platforms which implement map_page interface don't support physical
addresses without real struct page. Add condition to check it.
As-is, the condition also needs to cover iommu-dma, because that also 
still doesn't support non-page-backed addresses. You can't just do a 
simple s/page/phys/ rename and hope it's OK because you happen to get 
away with it for coherent, 64-bit, trusted devices.

Thanks,
Robin.
quoted hunk ↗ jump to hunk
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
  kernel/dma/mapping.c | 15 ++++++++++++++-
  1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c
index 709405d46b2b..74efb6909103 100644
--- a/kernel/dma/mapping.c
+++ b/kernel/dma/mapping.c
@@ -158,6 +158,7 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
  {
  	const struct dma_map_ops *ops = get_dma_ops(dev);
  	phys_addr_t phys = page_to_phys(page) + offset;
+	bool is_pfn_valid = true;
  	dma_addr_t addr;
  
  	BUG_ON(!valid_dma_direction(dir));
@@ -170,8 +171,20 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
  		addr = dma_direct_map_phys(dev, phys, size, dir, attrs);
  	else if (use_dma_iommu(dev))
  		addr = iommu_dma_map_phys(dev, phys, size, dir, attrs);
-	else
+	else {
+		if (IS_ENABLED(CONFIG_DMA_API_DEBUG))
+			is_pfn_valid = pfn_valid(PHYS_PFN(phys));
+
+		if (unlikely(!is_pfn_valid))
+			return DMA_MAPPING_ERROR;
+
+		/*
+		 * All platforms which implement .map_page() don't support
+		 * non-struct page backed addresses.
+		 */
  		addr = ops->map_page(dev, page, offset, size, dir, attrs);
+	}
+
  	kmsan_handle_dma(phys, size, dir);
  	trace_dma_map_phys(dev, phys, addr, size, dir, attrs);
  	debug_dma_map_phys(dev, phys, size, dir, addr, attrs);
  
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help