selftests/mm: khugepaged: initialize file contents via mmap
Subsystems (4)
PATCH v5 1/1 selftests/mm: khugepaged: initialize file contents via mmap
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.
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(-)
tools/testing/selftests/mm/khugepaged.c +15 -3
@@ -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" );
Signature · 1 line
2.54.0
Quoted 12 lines · 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 withftruncate(), map it directly withMAP_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 <[email protected]>
Quoted 8 lines
v4 -> v5:
- Restoremsync()to keep folios clean forREAD_ONLY_THP_FOR_FS
- RemoveO_DSYNCsincemsync()handles synchronization
- Simplifymmap()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.
Best Regards,
Yan, Zi
Quoted 11 lines · 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 withftruncate(), map it directly withMAP_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 <[email protected]>
Thanks! :)
Signature · 3 lines
Cheers, David