* [PATCH v3 2/3] udmabuf: map huge page backed ranges with PMDs
2026-12-01 17:16 [PATCH v3 0/3] udmabuf: speed up mmap() of huge page backed buffers Qiliang Yuan
2026-12-01 17:16 ` [PATCH v3 1/3] dma-buf: align large mappings to PMD boundaries Qiliang Yuan
@ 2026-12-01 17:16 ` Qiliang Yuan
2026-12-01 17:16 ` [PATCH v3 3/3] selftests/udmabuf: test mapping a huge page backed udmabuf Qiliang Yuan
2 siblings, 0 replies; 4+ messages in thread
From: Qiliang Yuan @ 2026-12-01 17:16 UTC (permalink / raw)
To: Sumit Semwal, Christian König, Philipp Stanner,
Gerd Hoffmann, Vivek Kasireddy, Shuah Khan
Cc: Thomas Hellström, T.J. Mercier, Philip Yang, linux-media,
dri-devel, linaro-mm-sig, linux-kernel, linux-kselftest,
Qiliang Yuan, Natalie Vock
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@linux.dev>
---
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
+/*
+ * 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);
+ 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);
- 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
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v3 3/3] selftests/udmabuf: test mapping a huge page backed udmabuf
2026-12-01 17:16 [PATCH v3 0/3] udmabuf: speed up mmap() of huge page backed buffers Qiliang Yuan
2026-12-01 17:16 ` [PATCH v3 1/3] dma-buf: align large mappings to PMD boundaries Qiliang Yuan
2026-12-01 17:16 ` [PATCH v3 2/3] udmabuf: map huge page backed ranges with PMDs Qiliang Yuan
@ 2026-12-01 17:16 ` Qiliang Yuan
2 siblings, 0 replies; 4+ messages in thread
From: Qiliang Yuan @ 2026-12-01 17:16 UTC (permalink / raw)
To: Sumit Semwal, Christian König, Philipp Stanner,
Gerd Hoffmann, Vivek Kasireddy, Shuah Khan
Cc: Thomas Hellström, T.J. Mercier, Philip Yang, linux-media,
dri-devel, linaro-mm-sig, linux-kernel, linux-kselftest,
Qiliang Yuan, Natalie Vock
udmabuf maps the ranges of a buffer that are backed by huge pages with
PMDs, provided the mapping covers whole PMDs at an aligned address.
The existing tests of huge page backed buffers only map a few pages, so
they never get a PMD mapping or check what is read through one.
Map a udmabuf of 8 huge pages, sized from hpage_pmd_size and the hugetlb
page size so that it also fits 16 KiB and 64 KiB base pages, and read
back what was written to the memfd, with a different pattern in each
huge page: through a mapping of the whole buffer, which must be PMD
aligned for a 64-bit task when THP support is built in, through a
mapping at an unaligned offset, from a forked child that inherits the
mapping of the whole buffer, through a buffer that starts one page into
the memfd so that only PTEs can map it, and next to a mapped PMD after
THP was disabled for the mapping with MADV_NOHUGEPAGE.
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
tools/testing/selftests/drivers/dma-buf/udmabuf.c | 171 +++++++++++++++++++++-
1 file changed, 170 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/drivers/dma-buf/udmabuf.c b/tools/testing/selftests/drivers/dma-buf/udmabuf.c
index d78aec662586b..1f4b7dd1f610e 100644
--- a/tools/testing/selftests/drivers/dma-buf/udmabuf.c
+++ b/tools/testing/selftests/drivers/dma-buf/udmabuf.c
@@ -14,6 +14,7 @@
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <sys/mman.h>
+#include <sys/wait.h>
#include <linux/memfd.h>
#include <linux/udmabuf.h>
#include "kselftest.h"
@@ -130,6 +131,172 @@ static int compare_chunks(void *addr1, void *addr2, off64_t memfd_size)
return ret;
}
+/* Read a size in bytes from @path, or from the "@key: <n> kB" line of @path */
+static size_t read_size(const char *path, const char *key)
+{
+ unsigned long val = 0;
+ size_t len = key ? strlen(key) : 0;
+ char line[128];
+ FILE *f;
+
+ f = fopen(path, "r");
+ if (!f)
+ return 0;
+ while (fgets(line, sizeof(line), f)) {
+ if (!key) {
+ val = strtoul(line, NULL, 10);
+ break;
+ }
+ if (!strncmp(line, key, len) && line[len] == ':') {
+ val = strtoul(line + len + 1, NULL, 10) << 10;
+ break;
+ }
+ }
+ fclose(f);
+ return val;
+}
+
+/* Fill each page of @mem with a byte that differs between huge pages */
+static void fill_huge(char *mem, size_t len, size_t hpage)
+{
+ size_t i;
+
+ for (i = 0; i < len; i += getpagesize())
+ mem[i] = (char)(i / getpagesize() + i / hpage);
+}
+
+/* Check that @len bytes mapped at @addr match @mem at @off */
+static bool check_huge_mapping(char *addr, char *mem, off_t off, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; i += getpagesize()) {
+ if (addr[i] != mem[off + i])
+ return false;
+ }
+ return true;
+}
+
+/* Check that @len bytes of @buf at @off read back what was written to @mem */
+static bool check_huge_range(int buf, char *mem, off_t off, size_t len)
+{
+ char *addr;
+ bool ok;
+
+ addr = mmap(NULL, len, PROT_READ, MAP_SHARED, buf, off);
+ if (addr == MAP_FAILED)
+ return false;
+
+ ok = check_huge_mapping(addr, mem, off, len);
+ munmap(addr, len);
+ return ok;
+}
+
+static void report(bool ok, int test)
+{
+ if (ok)
+ ksft_test_result_pass("%s: [PASS,test-%d]\n", TEST_PREFIX, test);
+ else
+ ksft_test_result_fail("%s: [FAIL,test-%d]\n", TEST_PREFIX, test);
+}
+
+/*
+ * Map a udmabuf backed by huge pages, which can be mapped with PMDs, and read
+ * it back: from a PMD-aligned address, a sub-range at an unaligned offset, a
+ * forked child that inherits the mapping, a buffer that starts one page into
+ * the memfd so that no PMD can map it, and a mapping that has THP disabled
+ * after a PMD was mapped.
+ */
+static void test_huge_mapping(int devfd)
+{
+ size_t pmd = read_size("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size",
+ NULL);
+ size_t hpage = read_size("/proc/meminfo", "Hugepagesize");
+ struct udmabuf_create create = {};
+ int memfd, buf, sub, status, test;
+ size_t unit, size;
+ char *mem, *addr;
+ pid_t pid;
+ bool ok;
+
+ if (!hpage) {
+ for (test = 8; test <= 11; test++)
+ ksft_test_result_skip("%s: [SKIP,test-%d]\n",
+ TEST_PREFIX, test);
+ return;
+ }
+ unit = hpage > pmd ? hpage : pmd;
+ size = 8 * unit;
+
+ memfd = create_memfd_with_seals(size, true);
+ mem = mmap_fd(memfd, size);
+ fill_huge(mem, size, hpage);
+
+ create.memfd = memfd;
+ create.size = size;
+ buf = ioctl(devfd, UDMABUF_CREATE, &create);
+ if (buf < 0) {
+ for (test = 8; test <= 11; test++)
+ ksft_test_result_fail("%s: [FAIL,test-%d]\n",
+ TEST_PREFIX, test);
+ goto out;
+ }
+
+ /* The address is only aligned for 64-bit tasks with THP support */
+ addr = mmap(NULL, size, PROT_READ, MAP_SHARED, buf, 0);
+ ok = addr != MAP_FAILED &&
+ (sizeof(long) != 8 || !pmd || !((unsigned long)addr % pmd)) &&
+ check_huge_mapping(addr, mem, 0, size);
+ report(ok, 8);
+
+ ok = check_huge_range(buf, mem, unit + getpagesize(),
+ 5 * unit + 3 * getpagesize());
+ pid = addr == MAP_FAILED ? -1 : fork();
+ if (!pid)
+ _exit(check_huge_mapping(addr, mem, 0, size) ? 0 : 1);
+ if (pid < 0 || waitpid(pid, &status, 0) != pid ||
+ !WIFEXITED(status) || WEXITSTATUS(status))
+ ok = false;
+ if (addr != MAP_FAILED)
+ munmap(addr, size);
+ report(ok, 9);
+
+ /* Only PTEs can map a buffer that starts one page into a huge page */
+ create.offset = getpagesize();
+ create.size = 4 * unit;
+ sub = ioctl(devfd, UDMABUF_CREATE, &create);
+ ok = sub >= 0;
+ if (ok) {
+ addr = mmap(NULL, create.size, PROT_READ, MAP_SHARED, sub, 0);
+ ok = addr != MAP_FAILED &&
+ check_huge_mapping(addr, mem, create.offset, create.size);
+ if (addr != MAP_FAILED)
+ munmap(addr, create.size);
+ close(sub);
+ }
+ report(ok, 10);
+
+ /* Map a PMD, disable THP for the mapping, then fault next to the PMD */
+ addr = mmap(NULL, size, PROT_READ, MAP_SHARED, buf, 0);
+ ok = addr != MAP_FAILED && addr[0] == mem[0];
+ if (ok && madvise(addr, size, MADV_NOHUGEPAGE)) {
+ munmap(addr, size);
+ ksft_test_result_skip("%s: [SKIP,test-11]\n", TEST_PREFIX);
+ } else {
+ ok = ok && check_huge_mapping(addr + 3 * unit, mem, 3 * unit,
+ unit) &&
+ check_huge_mapping(addr, mem, 0, size);
+ if (addr != MAP_FAILED)
+ munmap(addr, size);
+ report(ok, 11);
+ }
+
+ close(buf);
+out:
+ munmap(mem, size);
+ close(memfd);
+}
+
int main(int argc, char *argv[])
{
struct udmabuf_create create;
@@ -138,7 +305,7 @@ int main(int argc, char *argv[])
void *addr1, *addr2;
ksft_print_header();
- ksft_set_plan(7);
+ ksft_set_plan(11);
devfd = open("/dev/udmabuf", O_RDWR);
if (devfd < 0) {
@@ -268,6 +435,8 @@ int main(int argc, char *argv[])
close(buf);
close(memfd);
+
+ test_huge_mapping(devfd);
close(devfd);
ksft_print_msg("%s: ok\n", TEST_PREFIX);
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread