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 <[email protected]> --- 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
