tools/testing/selftests/mm/khugepaged.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
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@gmail.com>
---
tools/testing/selftests/mm/khugepaged.c | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index 3fe7ef04ac62..57ca4224bd5a 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,21 @@ 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);
+ close(fd);
success("OK");
printf("Opening %s read only for collapse...", finfo.path);
--
2.54.0
On 4/28/26 12:00, 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.
Please
a) Wait a bit longer before you resend.
b) Don't send as reply to earlier revisions.
>
> Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>
> ---
> tools/testing/selftests/mm/khugepaged.c | 17 +++++++++++++----
> 1 file changed, 13 insertions(+), 4 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
> index 3fe7ef04ac62..57ca4224bd5a 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;
> -
Unrelated change.
> unlink(finfo.path); /* Cleanup from previous failed tests */
> printf("Creating %s for collapse%s...", finfo.path,
> finfo.type == VMA_SHMEM ? " (tmpfs)" : "");
> @@ -381,11 +380,21 @@ 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);
> + close(fd);
That change is not strictly required. We can munmap() after close().
Apart from that LGTM.
--
Cheers,
David
© 2016 - 2026 Red Hat, Inc.