git: 10ebbeb4d78c - stable/15 - subr_physmem_test: add tests for two edge-cases

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Thu, 03 Sep 2026 16:36:43 UTC
The branch stable/15 has been updated by mhorne:

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

commit 10ebbeb4d78cebdf6398e224a94edf8373de5c88
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2026-07-13 19:49:32 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-09-03 16:32:44 +0000

    subr_physmem_test: add tests for two edge-cases
    
    Help validate my assertion that "physmem will never report empty
    ranges". Part of this is covered by the existing tests, which check the
    merging of adjacent/overlapping regions. The other part is to ensure
    that addition of zero-sized ranges is ignored.
    
    The physmem implementation also includes logic to ignore the first
    physical page of memory (physical addresses 0 to PAGE_SIZE-1). Add a
    second test case for this.
    
    Reviewed by:    markj
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D45914
    
    (cherry picked from commit 1b5ec2e466ee100161017ae2618f91829310f1d6)
---
 tests/sys/kern/subr_physmem_test.c | 43 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/tests/sys/kern/subr_physmem_test.c b/tests/sys/kern/subr_physmem_test.c
index b810a2016473..dad7882c93ec 100644
--- a/tests/sys/kern/subr_physmem_test.c
+++ b/tests/sys/kern/subr_physmem_test.c
@@ -127,11 +127,54 @@ ATF_TC_BODY(hwregion_unordered, tc)
 	ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE);
 }
 
+ATF_TC_WITHOUT_HEAD(hwregion_ignore_empty);
+ATF_TC_BODY(hwregion_ignore_empty, tc)
+{
+	vm_paddr_t avail[4];
+	size_t len;
+
+	/* Add a region. */
+	physmem_hardware_region(PAGE_SIZE, 2 * PAGE_SIZE);
+
+	/* Add full zero range (ignored) */
+	physmem_hardware_region(0, 0);
+
+	/* Add a zero-sized range (ignored) */
+	physmem_hardware_region(4 * PAGE_SIZE, 0);
+
+	len = physmem_avail(avail, nitems(avail));
+	ATF_CHECK_EQ(len, 2);
+	ATF_CHECK_EQ(avail[0], PAGE_SIZE);
+	ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE);
+}
+
+ATF_TC_WITHOUT_HEAD(hwregion_ignore_page0);
+ATF_TC_BODY(hwregion_ignore_page0, tc)
+{
+	vm_paddr_t avail[4];
+	size_t len;
+
+	/*
+	 * Physical addresses [0, PAGE_SIZE) are unusable in the VM layer.
+	 *
+	 * physmem will truncate this from the beginning of an otherwise valid
+	 * memory range; test that this is the case.
+	 */
+	physmem_hardware_region(0, 2 * PAGE_SIZE);
+
+	len = physmem_avail(avail, 4);
+	ATF_CHECK_EQ(len, 2);
+	ATF_CHECK_EQ(avail[0], PAGE_SIZE);
+	ATF_CHECK_EQ(avail[1], 2 * PAGE_SIZE);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 
 	ATF_TP_ADD_TC(tp, hwregion);
 	ATF_TP_ADD_TC(tp, hwregion_exclude);
 	ATF_TP_ADD_TC(tp, hwregion_unordered);
+	ATF_TP_ADD_TC(tp, hwregion_ignore_empty);
+	ATF_TP_ADD_TC(tp, hwregion_ignore_page0);
 	return (atf_no_error());
 }