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

Vineet Agarwal
2026-04-28T09:52:41Z

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

ftruncate() is needed before mmap(), since the file is created with
O_TRUNC and initially has size 0. Without resizing it first, mapping
and accessing the full range can go beyond EOF.

You are right about msync() though — since all pages are written
through the shared mapping and the test later drops page cache
explicitly, an explicit msync() is unnecessary here.

I'll remove msync() and send a v3.

Thanks,
Vineet

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

> On 4/28/26 11:43, 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(), map the file directly with
> > MAP_SHARED after resizing it with ftruncate(), initialize the mapped
> > area in place, and sync it with msync().
> >
> > This simplifies the setup path and avoids the need for explicit
> > partial write handling.
> >
> > Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>
> > ---
> > tools/testing/selftests/mm/khugepaged.c | 25 +++++++++++++++++++++----
> > 1 file changed, 21 insertions(+), 4 deletions(-)
> >
> > diff --git a/tools/testing/selftests/mm/khugepaged.c
> b/tools/testing/selftests/mm/khugepaged.c
> > index 3fe7ef04ac62..f6e171e1da4f 100644
> > --- a/tools/testing/selftests/mm/khugepaged.c
> > +++ b/tools/testing/selftests/mm/khugepaged.c
> > @@ -369,7 +369,6 @@ static void *file_setup_area(int nr_hpages)
> > int fd;
> > void *p;
> > unsigned long size;
> > -
> > unlink(finfo.path); /* Cleanup from previous failed tests */
> > printf("Creating %s for collapse%s...", finfo.path,
> > finfo.type == VMA_SHMEM ? " (tmpfs)" : "");
> > @@ -381,11 +380,29 @@ 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);
> > + }
>
> Why is that required? We write all pages.
>
> > + 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);
> > +
> > + if (msync(p, size, MS_SYNC)) {
> > + perror("msync()");
> > + munmap(p, size);
> > + close(fd);
> > + exit(EXIT_FAILURE);
>
> Why is that required?
>
> --
> Cheers,
>
> David
>

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