[PATCH v3 3/3] selftests/udmabuf: test mapping a huge page backed udmabuf
From: Qiliang Yuan
Date: Tue Dec 01 2026 - 12:17:06 EST
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@xxxxxxxxx>
---
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