Re: [PATCH v4] selftests/mm: khugepaged: initialize file contents via mmap

From: Zi Yan

Date: Tue Apr 28 2026 - 15:58:58 EST


On 28 Apr 2026, at 9:31, Vineet Agarwal wrote:

> file_setup_area() currently allocates anonymous memory, fills it,
> and writes it into the backing file used for collapse testing.
>
> Instead of copying data through write(), resize the file with
> ftruncate(), map it directly with MAP_SHARED, and initialize the
> mapped area in place.
>
> This simplifies the setup path and avoids the need for explicit
> partial write handling.
>
> Signed-off-by: Vineet Agarwal <agarwal.vineet2006@xxxxxxxxx>
>
> v3 -> v4:
> - Restore unrelated blank line removal
> - Restore original close()/munmap() ordering
> ---
> tools/testing/selftests/mm/khugepaged.c | 14 ++++++++++++--
> 1 file changed, 12 insertions(+), 2 deletions(-)

This patch breaks the khugepaged test for READ_ONLY_THP_FOR_FS.

When I ran sudo ./khugepaged all:file ~/ on ext4,

collapse_max_ptes_none (khugepaged:file) fails.

Run test: collapse_max_ptes_none (khugepaged:file)
Creating /mnt/test/collapse_test_file for collapse... OK
Opening /mnt/test/collapse_test_file read only for collapse... OK
Maybe collapse with max_ptes_none exceeded.... Fail
Unexpected huge page
Restore THP and khugepaged settings... OK

Reverting the change fixes the issue.

It is also causing issues for my READ_ONLY_THP_FOR_FS removal patchset.

>
> diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
> index 3fe7ef04ac62..6e929a484a1b 100644
> --- a/tools/testing/selftests/mm/khugepaged.c
> +++ b/tools/testing/selftests/mm/khugepaged.c
> @@ -381,9 +381,19 @@ static void *file_setup_area(int nr_hpages)
> }
>
> size = nr_hpages * hpage_pmd_size;
> - p = alloc_mapping(nr_hpages);
> + if (ftruncate(fd, size)) {
> + perror("ftruncate()");
> + close(fd);
> + exit(EXIT_FAILURE);
> + }
> + p = mmap(BASE_ADDR, size, PROT_READ | PROT_WRITE,
> + MAP_SHARED, fd, 0);
> + if (p == MAP_FAILED || p != BASE_ADDR) {
> + perror("mmap()");
> + close(fd);
> + exit(EXIT_FAILURE);
> + }
> fill_memory(p, 0, size);
> - write(fd, p, size);
> close(fd);
> munmap(p, size);
> success("OK");
> --
> 2.54.0

The code looks OK, but I have not had time to look into it yet.

Best Regards,
Yan, Zi