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

Leon Romanovsky
2025-06-25T13:19:41Z

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.

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);
--
2.49.0