QEMU and crosvm export virtio-gpu blob resources from guest memory as
udmabufs, which the host renderer and display mmap(). The guest memory is
often backed by 2 MiB huge pages, yet udmabuf maps it 4 KiB at a time:
mapping and touching a 4 GiB buffer inserts a million PTEs and takes over
200 ms.
Patch 1 has mmap() of a dma-buf align large mappings to PMD boundaries,
which any exporter mapping contiguous memory with PMDs needs, such as the
proposed VFIO dma-buf mmap() [1]. Patch 2 maps the huge page backed
ranges of a udmabuf with PMDs, which cuts the time to map and touch it
by 87-90% for hugetlb and shmem THP backed buffers and leaves 4 KiB
backed ones as they are. Patch 3 adds selftests.
[1] https://lore.kernel.org/r/20260527102319.100128-1-mattev@meta.com
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
V1 -> V2:
- 1/3: Add Reviewed-by (Christian König).
- 2/3: Pre-fault only the faulting PMD range when the VMA can't have
PMDs, so that disabling THP after a PMD was mapped no longer has
vmf_insert_pfn() walk into that PMD (Sashiko review).
- 3/3: Give each huge page its own data pattern, skip the alignment
check for 32-bit tasks and read the inherited mapping in the forked
child (Sashiko review).
A fault racing with prctl(PR_SET_THP_DISABLE) can still see a PMD
installed by a concurrent fault. Lorenzo's series in mm/core fixes this
in the core mm for all PFN maps with ->huge_fault() [2].
[2] https://lore.kernel.org/r/20261117-huge-pfnmap-fault-races-v2-0-276007e73bcb@kernel.org
v1: https://lore.kernel.org/r/20261122-feat-udmabuf-huge-pfnmap-v1-0-dcba888940c2@linux.dev
---
Qiliang Yuan (3):
dma-buf: align large mappings to PMD boundaries
udmabuf: map huge page backed ranges with PMDs
selftests/udmabuf: test mapping a huge page backed udmabuf
drivers/dma-buf/dma-buf.c | 1 +
drivers/dma-buf/udmabuf.c | 85 +++++++++++++++++++-
tools/testing/selftests/drivers/dma-buf/udmabuf.c | 97 ++++++++++++++++++++++-
3 files changed, 178 insertions(+), 5 deletions(-)
---
base-commit: c408466a4dfaee4b12587e926c5b85e68c8b3c40
change-id: 20261121-feat-udmabuf-huge-pfnmap-3258272d077e
Best regards,
--
Qiliang Yuan <qiliang.yuan@linux.dev>