git: c5cff3df96c3 - main - tests: Improve vn_io_fault1 test

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Sat, 14 Nov 2026 00:23:27 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=c5cff3df96c35aa3613c083f151e853b532d88cb

commit c5cff3df96c35aa3613c083f151e853b532d88cb
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-14 00:22:40 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-14 00:23:24 +0000

    tests: Improve vn_io_fault1 test
    
    * Declare that the test needs 4 GB of memory so it can be automatically
      skipped on systems with low memory.
    
    * Switch back to mmap() as in my initial prototype.  Using malloc() is
      simpler, but on systems with malloc debugging enabled, malloc() will
      prefault the entire buffer, resulting in a false positive.  Using
      mmap() also guarantees that the buffer is page-aligned, which renders
      the test determinstic: we now only need to prefault exactly 2 GB, and
      the difference between ret and len will be exactly the amount that we
      prefaulted.
    
    * To prefault the buffer, instead of a simple loop that writes zeroes
      to it, which a compiler might recognize as dead stores and eliminate,
      read the first INT_MAX bytes from the file into it.
    
    PR:             298159
    Fixes:          ba02bbbe86ae ("tests: Add test for vn_io_fault1 bug")
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D61508
---
 tests/sys/kern/vn_io_fault1.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/tests/sys/kern/vn_io_fault1.c b/tests/sys/kern/vn_io_fault1.c
index d393d9d6b089..eabb35d2c9be 100644
--- a/tests/sys/kern/vn_io_fault1.c
+++ b/tests/sys/kern/vn_io_fault1.c
@@ -4,8 +4,11 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <sys/mman.h>
+
 #include <atf-c.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -15,6 +18,7 @@ ATF_TC_HEAD(big_unmapped_read, tc)
 {
 	atf_tc_set_md_var(tc, "descr", "Read a large amount of data "
 	    "into a partially unmapped buffer");
+	atf_tc_set_md_var(tc, "require.memory", "4G");
 }
 ATF_TC_BODY(big_unmapped_read, tc)
 {
@@ -24,20 +28,26 @@ ATF_TC_BODY(big_unmapped_read, tc)
 	int fd;
 
 	/* allocate a large buffer */
-	if ((buf = malloc(len)) == NULL)
+	buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+	if (buf == MAP_FAILED)
 		atf_tc_skip("failed to allocate buffer");
 
-	/* fault in just a tad more than the first 2 GB */
-	for (size_t i = 0; i <= 2LU << 30; i += 4096)
-		buf[i] = 0;
-
 	/* create a large file */
 	fd = open("big", O_RDWR | O_CREAT | O_TRUNC, 0600);
 	ATF_REQUIRE(fd >= 0);
 	ATF_REQUIRE_EQ_MSG(0, ftruncate(fd, len),
 	    "failed to extend file: %m");
 
-	/* read the file into the buffer */
+	/* read part of the file into the buffer */
+	ATF_REQUIRE_EQ(0, lseek(fd, 0, SEEK_SET));
+	ret = read(fd, buf, INT_MAX);
+	ATF_REQUIRE_MSG(ret >= 0,
+	    "failed to read from file: %m");
+	ATF_REQUIRE_EQ_MSG(INT_MAX, (size_t)ret,
+	    "requested %zu bytes, got %zd", (size_t)INT_MAX, ret);
+
+	/* read the entire file into the buffer */
+	ATF_REQUIRE_EQ(0, lseek(fd, 0, SEEK_SET));
 	ret = read(fd, buf, len);
 	ATF_REQUIRE_MSG(ret >= 0,
 	    "failed to read from file: %m");
@@ -46,7 +56,7 @@ ATF_TC_BODY(big_unmapped_read, tc)
 
 	/* done */
 	ATF_CHECK_EQ(0, close(fd));
-	free(buf);
+	ATF_CHECK_EQ(0, munmap(buf, len));
 }
 
 ATF_TP_ADD_TCS(tp)