[PATCH 11/13] PCI/P2PDMA: add helper pci_p2pdma_max_pagemap_align()

Hou Tao houtao at huaweicloud.com
Fri Dec 19 20:04:44 PST 2025


From: Hou Tao <houtao1 at huawei.com>

Add helper pci_p2pdma_max_pagemap_align() to find the max possible
alignment for p2p dma memory mapping in both userspace and kernel space.

When huge transparent page is supported, and the physical address, the
size of the BAR and the offset is {PUD|PMM}_SIZE aligned, it returns
{PUD|PMD_SIZE} accordingly. Otherwise, it returns PAGE_SIZE.

Signed-off-by: Hou Tao <houtao1 at huawei.com>
---
 include/linux/pci-p2pdma.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/include/linux/pci-p2pdma.h b/include/linux/pci-p2pdma.h
index 2fa671274c45..5d940b9e5338 100644
--- a/include/linux/pci-p2pdma.h
+++ b/include/linux/pci-p2pdma.h
@@ -210,4 +210,30 @@ pci_p2pdma_bus_addr_map(struct p2pdma_provider *provider, phys_addr_t paddr)
 	return paddr + provider->bus_offset;
 }
 
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+static inline size_t pci_p2pdma_max_pagemap_align(struct pci_dev *pdev, int bar,
+						  u64 size, u64 offset)
+{
+	resource_size_t start = pci_resource_start(pdev, bar);
+
+	if (has_transparent_pud_hugepage() &&
+	    IS_ALIGNED(start, PUD_SIZE) && IS_ALIGNED(size, PUD_SIZE) &&
+	    IS_ALIGNED(offset, PUD_SIZE))
+		return PUD_SIZE;
+
+	if (has_transparent_hugepage() &&
+	    IS_ALIGNED(start, PMD_SIZE) && IS_ALIGNED(size, PMD_SIZE) &&
+	    IS_ALIGNED(offset, PMD_SIZE))
+		return PMD_SIZE;
+
+	return PAGE_SIZE;
+}
+#else
+static inline size_t pci_p2pdma_max_pagemap_align(resource_size_t start,
+						  u64 size, u64 offset)
+{
+	return PAGE_SIZE;
+}
+#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
+
 #endif /* _LINUX_PCI_P2P_H */
-- 
2.29.2




More information about the Linux-nvme mailing list