Re: [PATCH] selftests/mm: khugepaged: handle partial write in file_setup_area()

Vineet Agarwal
2026-04-28T09:15:22Z

[-- Attachment #1: Type: text/plain, Size: 3448 bytes --]

Thanks for pointing that out.

I’ll take a closer look at whether mapping the file descriptor directly and
initializing the mapped area would be a cleaner approach here instead of
relying on write().

If mmap-based initialization works for this test setup, that would indeed
avoid the partial write handling entirely. I’ll investigate and send a v2
accordingly.

Thanks,
Vineet

On Tue, 28 Apr 2026 at 13:34, David Hildenbrand (Arm) <david@kernel.org>
wrote:

> On 4/28/26 08:23, Vineet Agarwal wrote:
> > file_setup_area() writes initialized test data into the backing
> > file used for collapse testing, but it ignores the return value
> > from write().
> >
> > If write() fails or completes only partially, the test continues
> > with incomplete file contents, which can lead to incorrect test
> > results and make failures harder to diagnose.
> >
> > Handle partial writes by retrying until all data is written and
> > abort the test if write() fails.
> >
> > Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>
> > ---
> > tools/testing/selftests/mm/khugepaged.c | 18 ++++++++++++++++--
> > 1 file changed, 16 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/testing/selftests/mm/khugepaged.c
> b/tools/testing/selftests/mm/khugepaged.c
> > index 3fe7ef04ac62..c13f06758750 100644
> > --- a/tools/testing/selftests/mm/khugepaged.c
> > +++ b/tools/testing/selftests/mm/khugepaged.c
> > @@ -369,7 +369,9 @@ static void *file_setup_area(int nr_hpages)
> > int fd;
> > void *p;
> > unsigned long size;
> > -
> > + size_t remaining;
> > + ssize_t ret;
> > + char *buf;
> > unlink(finfo.path); /* Cleanup from previous failed tests */
> > printf("Creating %s for collapse%s...", finfo.path,
> > finfo.type == VMA_SHMEM ? " (tmpfs)" : "");
> > @@ -383,7 +385,19 @@ static void *file_setup_area(int nr_hpages)
> > size = nr_hpages * hpage_pmd_size;
> > p = alloc_mapping(nr_hpages);
> > fill_memory(p, 0, size);
> > - write(fd, p, size);
> > + remaining = size;
> > + buf = p;
> > +
> > + while (remaining > 0) {
> > + ret = write(fd, buf, remaining);
> > + if (ret <= 0) {
> > + close(fd);
> > + munmap(p, size);
> > + ksft_exit_fail_msg("write() failed while preparing
> test file\n");
> > + }
> > + buf += ret;
> > + remaining -= ret;
> > + }
>
> Why are we even using write() instead of just mapping the fd and writing
> to that
> area?
>
> diff --git a/tools/testing/selftests/mm/khugepaged.c
> b/tools/testing/selftests/mm/khugepaged.c
> index 3fe7ef04ac62..a77f753ccf38 100644
> --- a/tools/testing/selftests/mm/khugepaged.c
> +++ b/tools/testing/selftests/mm/khugepaged.c
> @@ -381,9 +381,10 @@ static void *file_setup_area(int nr_hpages)
> }
>
> size = nr_hpages * hpage_pmd_size;
> - p = alloc_mapping(nr_hpages);
> + p = mmap(BASE_ADDR, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
> 0);
> + if (p != BASE_ADDR)
> + ksft_exit_fail_msg("mmap() failed while preparing test
> file\n");
> fill_memory(p, 0, size);
> - write(fd, p, size);
> close(fd);
> munmap(p, size);
> success("OK");
>
>
> --
> Cheers,
>
> David
>

[-- Attachment #2: Type: text/html, Size: 4543 bytes --]