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: 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>
Subject: [PATCH 3/3] selftests/udmabuf: test mapping a huge page backed udmabuf
Date: Sun, 22 Nov 2026 02:37:38 -0500 [thread overview]
Message-ID: <20261122-feat-udmabuf-huge-pfnmap-v1-3-dcba888940c2@linux.dev> (raw)
In-Reply-To: <20261122-feat-udmabuf-huge-pfnmap-v1-0-dcba888940c2@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 64 MiB udmabuf backed by 2 MiB huge pages and read back what was
written to the memfd: through a mapping of the whole buffer, which must
be PMD aligned when THP support is built in, through a mapping at an
unaligned offset, and from a forked child.
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
tools/testing/selftests/drivers/dma-buf/udmabuf.c | 90 ++++++++++++++++++++++-
1 file changed, 89 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..e09330fece6fb 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,91 @@ static int compare_chunks(void *addr1, void *addr2, off64_t memfd_size)
return ret;
}
+#define HPAGE_SIZE (2UL << 20)
+#define HUGE_BUF_SIZE (32 * HPAGE_SIZE)
+
+/* 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;
+ size_t i;
+ bool ok = true;
+
+ addr = mmap(NULL, len, PROT_READ, MAP_SHARED, buf, off);
+ if (addr == MAP_FAILED)
+ return false;
+
+ for (i = 0; i < len; i += getpagesize()) {
+ if (addr[i] != mem[off + i]) {
+ ok = false;
+ break;
+ }
+ }
+
+ 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.
+ */
+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);
+ for (i = 0; i < HUGE_BUF_SIZE; i += getpagesize())
+ mem[i] = (char)(i / getpagesize());
+
+ 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 when THP support is built in */
+ thp = !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));
+ if (addr != MAP_FAILED)
+ munmap(addr, HUGE_BUF_SIZE);
+ ok = aligned && check_huge_range(buf, mem, 0, HUGE_BUF_SIZE);
+ if (ok)
+ ksft_test_result_pass("%s: [PASS,test-8]\n", TEST_PREFIX);
+ else
+ ksft_test_result_fail("%s: [FAIL,test-8]\n", TEST_PREFIX);
+
+ ok = check_huge_range(buf, mem, HPAGE_SIZE + getpagesize(),
+ 5 * HPAGE_SIZE + 3 * getpagesize());
+ pid = fork();
+ if (!pid)
+ _exit(check_huge_range(buf, mem, 0, HUGE_BUF_SIZE) ? 0 : 1);
+ if (pid < 0 || waitpid(pid, &status, 0) != pid ||
+ !WIFEXITED(status) || WEXITSTATUS(status))
+ ok = false;
+ if (ok)
+ ksft_test_result_pass("%s: [PASS,test-9]\n", TEST_PREFIX);
+ else
+ ksft_test_result_fail("%s: [FAIL,test-9]\n", TEST_PREFIX);
+
+ close(buf);
+out:
+ munmap(mem, HUGE_BUF_SIZE);
+ close(memfd);
+}
+
int main(int argc, char *argv[])
{
struct udmabuf_create create;
@@ -138,7 +224,7 @@ int main(int argc, char *argv[])
void *addr1, *addr2;
ksft_print_header();
- ksft_set_plan(7);
+ ksft_set_plan(9);
devfd = open("/dev/udmabuf", O_RDWR);
if (devfd < 0) {
@@ -268,6 +354,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-11-22 7:38 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 7:37 [PATCH 0/3] udmabuf: speed up mmap() of huge page backed buffers Qiliang Yuan
2026-11-22 7:37 ` [PATCH 1/3] dma-buf: align large mappings to PMD boundaries Qiliang Yuan
2026-11-22 7:37 ` [PATCH 2/3] udmabuf: map huge page backed ranges with PMDs Qiliang Yuan
2026-11-22 7:37 ` 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=20261122-feat-udmabuf-huge-pfnmap-v1-3-dcba888940c2@linux.dev \
--to=qiliang.yuan@linux.dev \
--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=phasta@kernel.org \
--cc=shuah@kernel.org \
--cc=sumit.semwal@linaro.org \
--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®