From: Qiliang Yuan <qiliang.yuan@linux.dev>
To: "Sumit Semwal" <sumit.semwal@linaro.org>,
"Christian König" <christian.koenig@amd.com>,
"Philipp Stanner" <phasta@kernel.org>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Vivek Kasireddy" <vivek.kasireddy@intel.com>,
"Shuah Khan" <shuah@kernel.org>
Cc: "Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
"T.J. Mercier" <tjmercier@google.com>,
"Philip Yang" <Philip.Yang@amd.com>,
linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org,
linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org,
"Qiliang Yuan" <qiliang.yuan@linux.dev>,
"Natalie Vock" <nat@pixelcluster.dev>
Subject: [PATCH v3 3/3] selftests/udmabuf: test mapping a huge page backed udmabuf
Date: Tue, 01 Dec 2026 12:16:22 -0500 [thread overview]
Message-ID: <20261201-feat-udmabuf-huge-pfnmap-v3-3-144cc7a48f09@linux.dev> (raw)
In-Reply-To: <20261201-feat-udmabuf-huge-pfnmap-v3-0-144cc7a48f09@linux.dev>
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
prev parent reply other threads:[~2026-12-01 17:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201-feat-udmabuf-huge-pfnmap-v3-3-144cc7a48f09@linux.dev \
--to=qiliang.yuan@linux.dev \
--cc=Philip.Yang@amd.com \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=kraxel@redhat.com \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=nat@pixelcluster.dev \
--cc=phasta@kernel.org \
--cc=shuah@kernel.org \
--cc=sumit.semwal@linaro.org \
--cc=thomas.hellstrom@linux.intel.com \
--cc=tjmercier@google.com \
--cc=vivek.kasireddy@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®