On Mon, Nov 23, 2026 at 6:45 PM Qiliang Yuan qiliang.yuan@linux.dev wrote:
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 64 MiB udmabuf backed by 2 MiB huge 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, and from a forked child that inherits the mapping of the whole buffer.
Signed-off-by: Qiliang Yuan qiliang.yuan@linux.dev
tools/testing/selftests/drivers/dma-buf/udmabuf.c | 97 ++++++++++++++++++++++- 1 file changed, 96 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..6eaef1b3aee01 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,98 @@ static int compare_chunks(void *addr1, void *addr2, off64_t memfd_size) return ret; }
+#define HPAGE_SIZE (2UL << 20)
This isn't true on 16k or 64k PAGE_SIZE arm64. Can we read hpage_pmd_size for the PMD alignment and Hugepagesize: from /proc/meminfo for the MFD_HUGETLB backing size?
+#define HUGE_BUF_SIZE (32 * HPAGE_SIZE)
I think we only really need 8 of these for the sub-range test, and this gets huge for 16k or 64k base pages.
+/* 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;+}
+/*
- Map a udmabuf backed by 2MB huge pages, which can be mapped with PMDs, and
- read it back from a PMD-aligned address, a sub-range at an unaligned offset
- and a forked child that inherits the mapping.
- */
+static void test_huge_mapping(int devfd) +{
struct udmabuf_create create = {};int memfd, buf, status;bool thp, aligned, ok;char *mem, *addr;pid_t pid;size_t i;memfd = create_memfd_with_seals(HUGE_BUF_SIZE, true);mem = mmap_fd(memfd, HUGE_BUF_SIZE);/* Give each huge page its own pattern */for (i = 0; i < HUGE_BUF_SIZE; i += getpagesize())mem[i] = (char)(i / getpagesize() + i / HPAGE_SIZE);create.memfd = memfd;create.size = HUGE_BUF_SIZE;buf = ioctl(devfd, UDMABUF_CREATE, &create);if (buf < 0) {ksft_test_result_fail("%s: [FAIL,test-8]\n", TEST_PREFIX);ksft_test_result_skip("%s: [SKIP,test-9]\n", TEST_PREFIX);goto out;}/* The address is only aligned for 64-bit tasks with THP support */thp = sizeof(long) == 8 &&!access("/sys/kernel/mm/transparent_hugepage", F_OK);addr = mmap(NULL, HUGE_BUF_SIZE, PROT_READ, MAP_SHARED, buf, 0);aligned = addr != MAP_FAILED &&(!thp || !((unsigned long)addr % HPAGE_SIZE));ok = aligned && check_huge_mapping(addr, mem, 0, HUGE_BUF_SIZE);if (ok)ksft_test_result_pass("%s: [PASS,test-8]\n", TEST_PREFIX);elseksft_test_result_fail("%s: [FAIL,test-8]\n", TEST_PREFIX);ok = check_huge_range(buf, mem, HPAGE_SIZE + getpagesize(),5 * HPAGE_SIZE + 3 * getpagesize());
We should probably also have a test with create.offset = getpagesize() to exercise the misaligned PFN fallback.
Also a test that faults a PMD, then disables THP and then faults elsewhere to cover the v1 bug.