Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] selftests/udmabuf: test_huge_mapping() does not inherit
  mapping across fork()
- [Low] selftests/udmabuf: periodic data pattern in test_huge_mapping()
  repeats per PMD
- [Low] selftests/udmabuf: false failure in test_huge_mapping() on
  32-bit tasks
--

commit c9830236dc0b56e59510003d91e0bc32e9d198ca
Author: Qiliang Yuan <[email protected]>

selftests/udmabuf: test mapping a huge page backed udmabuf

This commit adds a selftest that maps a 64 MiB udmabuf backed by 2 MiB huge
pages and verifies reading it back across whole-buffer, unaligned-offset,
and forked-child cases.

> 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());

[Severity: Low]
Does casting (i / getpagesize()) to char make every 2 MiB huge page contain
identical data?  Because the 8-bit pattern repeats every 256 base pages (1
MiB on 4 KiB page systems), all 32 huge pages have the same contents, so
check_huge_range() cannot detect if udmabuf maps the wrong huge page or
ignores the HPAGE_SIZE offset.

> +
> +     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));

[Severity: Low]
Can this alignment check fail when the test is built as a 32-bit binary?

Even when /sys/kernel/mm/transparent_hugepage exists,
__thp_get_unmapped_area() skips PMD alignment for 32-bit and compat tasks:

mm/huge_memory.c:__thp_get_unmapped_area() {
        if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall())
                return 0;
    ...
}

Because mmap() falls back to page alignment in that case, test-8 will
falsely fail whenever the returned address is not 2 MiB aligned.

> +     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);

[Severity: Medium]
Does the forked child ever read an inherited udmabuf mapping across fork()?

Both the initial alignment check and check_huge_range() call munmap() before
returning, so the parent has no active udmabuf VMA when fork() runs.  Calling
check_huge_range() in the child creates a fresh mmap() after fork() rather
than exercising fork() page-table duplication (such as copy_huge_pmd()) on
an existing PMD-mapped udmabuf VMA.

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/[email protected]?part=3

Reply via email to