public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5] selftests/mm: khugepaged: initialize file contents via mmap
@ 2026-04-29 11:58 Vineet Agarwal
  2026-04-29 12:59 ` Zi Yan
  2026-04-29 13:14 ` David Hildenbrand (Arm)
  0 siblings, 2 replies; 3+ messages in thread
From: Vineet Agarwal @ 2026-04-29 11:58 UTC (permalink / raw)
  To: akpm, david, ljs, shuah
  Cc: ziy, linux-mm, linux-kselftest, linux-kernel, Vineet Agarwal

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>

v4 -> v5:
  - Restore msync() to keep folios clean for READ_ONLY_THP_FOR_FS
  - Remove O_DSYNC since msync() handles synchronization
  - Simplify mmap() check and drop unnecessary cleanup in error paths
---
 tools/testing/selftests/mm/khugepaged.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index 3fe7ef04ac62..c8393ca52cab 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -373,7 +373,7 @@ static void *file_setup_area(int nr_hpages)
 	unlink(finfo.path);  /* Cleanup from previous failed tests */
 	printf("Creating %s for collapse%s...", finfo.path,
 	       finfo.type == VMA_SHMEM ? " (tmpfs)" : "");
-	fd = open(finfo.path, O_DSYNC | O_CREAT | O_RDWR | O_TRUNC | O_EXCL,
+	fd = open(finfo.path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL,
 		  777);
 	if (fd < 0) {
 		perror("open()");
@@ -381,9 +381,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()");
+		exit(EXIT_FAILURE);
+	}
+	p = mmap(BASE_ADDR, size, PROT_READ | PROT_WRITE,
+		MAP_SHARED, fd, 0);
+	if (p != BASE_ADDR) {
+		perror("mmap()");
+		exit(EXIT_FAILURE);
+	}
 	fill_memory(p, 0, size);
-	write(fd, p, size);
+	if (msync(p, size, MS_SYNC)) {
+		perror("msync()");
+		exit(EXIT_FAILURE);
+	}
 	close(fd);
 	munmap(p, size);
 	success("OK");
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v5] selftests/mm: khugepaged: initialize file contents via mmap
  2026-04-29 11:58 [PATCH v5] selftests/mm: khugepaged: initialize file contents via mmap Vineet Agarwal
@ 2026-04-29 12:59 ` Zi Yan
  2026-04-29 13:14 ` David Hildenbrand (Arm)
  1 sibling, 0 replies; 3+ messages in thread
From: Zi Yan @ 2026-04-29 12:59 UTC (permalink / raw)
  To: Vineet Agarwal
  Cc: akpm, david, ljs, shuah, linux-mm, linux-kselftest, linux-kernel

On 29 Apr 2026, at 7:58, 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@gmail.com>
>
> v4 -> v5:
>   - Restore msync() to keep folios clean for READ_ONLY_THP_FOR_FS
>   - Remove O_DSYNC since msync() handles synchronization
>   - Simplify mmap() check and drop unnecessary cleanup in error paths
> ---
>  tools/testing/selftests/mm/khugepaged.c | 18 +++++++++++++++---
>  1 file changed, 15 insertions(+), 3 deletions(-)
>
LGTM. And it no longer regresses for READ_ONLY_THP_FOR_FS tests. Thanks.

Reviewed-by: Zi Yan <ziy@nvidia.com>
Tested-by: Zi Yan <ziy@nvidia.com>

Best Regards,
Yan, Zi

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v5] selftests/mm: khugepaged: initialize file contents via mmap
  2026-04-29 11:58 [PATCH v5] selftests/mm: khugepaged: initialize file contents via mmap Vineet Agarwal
  2026-04-29 12:59 ` Zi Yan
@ 2026-04-29 13:14 ` David Hildenbrand (Arm)
  1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-04-29 13:14 UTC (permalink / raw)
  To: Vineet Agarwal, akpm, ljs, shuah
  Cc: ziy, linux-mm, linux-kselftest, linux-kernel

On 4/29/26 13:58, 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@gmail.com>

Acked-by: David Hildenbrand (Arm) <david@kernel.org>

Thanks! :)

-- 
Cheers,

David

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-04-29 13:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-29 11:58 [PATCH v5] selftests/mm: khugepaged: initialize file contents via mmap Vineet Agarwal
2026-04-29 12:59 ` Zi Yan
2026-04-29 13:14 ` David Hildenbrand (Arm)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox