Re: [PATCH v3 2/3] udmabuf: map huge page backed ranges with PMDs
From: T.J. Mercier
Date: Fri Dec 04 2026 - 12:38:05 EST
On Tue, Dec 1, 2026 at 9:16 AM Qiliang Yuan <qiliang.yuan@xxxxxxxxx> wrote:
>
> QEMU and crosvm hand guest memory to the host renderer and display as
> udmabufs for virtio-gpu blob resources. The guest memory is a memfd that
> is often backed by 2 MiB huge pages, and the host process mmap()s the
> dma-buf to access it with the CPU.
>
> udmabuf maps every page with its own PTE: the first fault inserts a PFN
> for each 4 KiB page of the whole VMA. Mapping and touching 4 GiB this way
> takes over 200 ms and a million PTEs, although the memory behind it is
> 2048 huge pages.
>
> Add a ->huge_fault() handler that maps a PMD-sized range with one PMD when
> it is backed by an aligned run of contiguous PFNs, and falls back to PTEs
> otherwise. Limit the pre-mapping on a fault to the PMD-sized range of the
> fault, so that it stays within one page table and never covers a range that
> is mapped with a PMD. Buffers backed by 4 KiB pages now take a fault for
> each PMD-sized range rather than one for the whole VMA, about 1.5 us each,
> or under 1 ms per GiB. The THP settings in sysfs don't apply to VM_PFNMAP
> mappings, so only MADV_NOHUGEPAGE and PR_SET_THP_DISABLE stop the PMD
> mappings.
>
> This depends on the series "mm: fix races between huge and non-huge
> faults in PFN mappings", which keeps a PTE fault that races with a PMD
> fault on the same range, after THP was disabled, from hitting a
> VM_BUG_ON or failing with VM_FAULT_OOM.
>
> Mapping a udmabuf over a memfd filled with data and reading a byte of
> each 4 KiB page through the mapping, base and patched kernels alternating
> over 6 rounds of 5 runs (7 runs for shmem 4K), median of the per-round
> medians and range in parentheses; nothp marks a task with
> PR_SET_THP_DISABLE set:
>
> backing size before (ms) after (ms) change
> hugetlb 256M 12.7 (12.6-13.9) 0.9 (0.9-0.9) -93%
> hugetlb 1G 50.5 (50.5-50.9) 3.7 (3.6-3.7) -93%
> hugetlb 4G 201 (201-202) 13.8 (13.5-13.9) -93%
> hugetlb 8G 402 (402-403) 26.8 (26.3-27.0) -93%
> shmem THP 1G 50.6 (50.6-50.8) 3.7 (3.6-3.7) -93%
> shmem THP 4G 201 (199-203) 13.7 (13.3-13.8) -93%
> shmem 4K 1G 50.5 (50.0-50.6) 51.4 (51.2-52.1) +2%
> shmem 4K nothp 1G 50.5 (50.2-50.7) 51.4 (51.1-51.5) +2%
> shmem 4K 4G 202 (201-202) 204 (204-205) +1%
>
> Signed-off-by: Qiliang Yuan <qiliang.yuan@xxxxxxxxx>
> ---
> drivers/dma-buf/udmabuf.c | 73 +++++++++++++++++++++++++++++++++++++++++++----
> 1 file changed, 68 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index 018937435356e..dd98d5a1b69fc 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
> @@ -44,12 +44,67 @@ struct udmabuf {
> struct miscdevice *device;
> };
>
> +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
I think you can switch both guards to CONFIG_ARCH_SUPPORTS_PMD_PFNMAP
and drop the if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_PMD_PFNMAP)) below.
(That's also what the DRM helper does in drm_gem_shmem_helper.c.)
> +/*
> + * Return whether the PMD-sized range of @vma at @addr is backed by one aligned
> + * run of contiguous PFNs, which can then be mapped with a single PMD.
> + */
> +static bool udmabuf_pmd_mappable(struct vm_area_struct *vma, unsigned long addr,
> + unsigned long *pfn)
> +{
> + struct udmabuf *ubuf = vma->vm_private_data;
> + const pgoff_t nr = PMD_SIZE >> PAGE_SHIFT;
> + unsigned long first;
> + pgoff_t pgoff, i;
> +
> + if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_PMD_PFNMAP))
> + return false;
> +
> + if (!IS_ALIGNED(addr, PMD_SIZE) || addr < vma->vm_start ||
> + vma->vm_end - addr < PMD_SIZE)
> + return false;
> +
> + pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
You can use linear_page_index(vma, addr) here and below in udmabuf_vm_fault().
> + if (pgoff >= ubuf->pagecount || ubuf->pagecount - pgoff < nr)
> + return false;
> +
> + first = page_to_pfn(ubuf->pages[pgoff]);
> + if (!IS_ALIGNED(first, nr))
> + return false;
> +
> + for (i = 1; i < nr; i++) {
> + if (page_to_pfn(ubuf->pages[pgoff + i]) != first + i)
> + return false;
> + }
> +
> + *pfn = first;
> + return true;
> +}
> +
> +/*
> + * The THP settings in /sys/kernel/mm/transparent_hugepage/ don't apply to
> + * VM_PFNMAP mappings, so this is called even when THP is set to "never".
> + * Only MADV_NOHUGEPAGE or PR_SET_THP_DISABLE keep it from being called.
> + */
> +static vm_fault_t udmabuf_vm_huge_fault(struct vm_fault *vmf,
> + unsigned int order)
> +{
> + unsigned long pfn;
> +
> + if (order != PMD_ORDER ||
> + !udmabuf_pmd_mappable(vmf->vma, vmf->address & PMD_MASK, &pfn))
> + return VM_FAULT_FALLBACK;
> +
> + return vmf_insert_pfn_pmd(vmf, pfn, vmf->flags & FAULT_FLAG_WRITE);
> +}
> +#endif
> +
> static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
> {
> struct vm_area_struct *vma = vmf->vma;
> struct udmabuf *ubuf = vma->vm_private_data;
> pgoff_t pgoff = vmf->pgoff;
> - unsigned long addr, pfn;
> + unsigned long addr, end, pfn;
> vm_fault_t ret;
>
> if (pgoff >= ubuf->pagecount)
> @@ -61,11 +116,16 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
> if (ret & VM_FAULT_ERROR)
> return ret;
>
> - /* pre fault */
> - pgoff = vma->vm_pgoff;
> - addr = vma->vm_start;
> + /*
> + * Pre-fault the rest of the PMD-sized range of the fault. Staying
> + * within its page table keeps the walk off the ranges that
> + * udmabuf_vm_huge_fault() maps with a PMD.
> + */
> + addr = max(vma->vm_start, vmf->address & PMD_MASK);
> + end = pmd_addr_end(vmf->address, vma->vm_end);
> + pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
I saw your reply to sashiko. Kind of too bad we can't use
pte_table_clamped_end(vmf->address, vma->vm_end) (commit 169169e26ab4)
here yet since drm-misc-next is based on 7.4-rc2 and doesn't have it
yet. I guess we have to wait on Lorenzo's series anyway.
> - for (; addr < vma->vm_end; pgoff++, addr += PAGE_SIZE) {
> + for (; addr < end; pgoff++, addr += PAGE_SIZE) {
> if (addr == vmf->address)
> continue;
>
> @@ -89,6 +149,9 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
>
> static const struct vm_operations_struct udmabuf_vm_ops = {
> .fault = udmabuf_vm_fault,
> +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
> + .huge_fault = udmabuf_vm_huge_fault,
> +#endif
> };
>
> static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
>
> --
> 2.43.0
>