git: f9da54713966 - main - libvmmapi: Compare mapping lengths in vm_mmap_memseg()

From: Hayzam Sherif <hayzam_at_FreeBSD.org>
Date: Fri, 27 Nov 2026 07:29:31 UTC
The branch main has been updated by hayzam:

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

commit f9da54713966a99953a71a96d87e5c709b132617
Author:     Hayzam Sherif <hayzam@FreeBSD.org>
AuthorDate: 2026-11-27 07:28:10 +0000
Commit:     Hayzam Sherif <hayzam@FreeBSD.org>
CommitDate: 2026-11-27 07:28:10 +0000

    libvmmapi: Compare mapping lengths in vm_mmap_memseg()
    
    vm_mmap_memseg() returns success without mapping anything when a
    mapping with the same segment, offset, protection and flags already
    starts at the requested address, but it does not compare the length.
    A longer request then reports success for pages it did not map, and a
    shorter one for a mapping that vm_munmap_memseg() cannot remove with
    the same range.  No caller in the tree makes such a request.
    
    Fail with EEXIST when the length differs, as for the other attributes,
    and document that behavior.  Read the existing mapping into its own
    structure rather than over the arguments.
    
    Reviewed by:    markj
    Approved by:    markj
    Fixes:          9b1aa8d622e8 (Restructure memory allocation in bhyve to support "devmem".)
    Sponsored by:   Alchemilla Ventures Private Limited
    Differential Revision:  https://reviews.freebsd.org/D61530
---
 lib/libvmmapi/vmmapi.c | 16 ++++++++++------
 lib/libvmmapi/vmmapi.h |  4 +++-
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c
index 7d2532e2fc14..6ca0d43796dd 100644
--- a/lib/libvmmapi/vmmapi.c
+++ b/lib/libvmmapi/vmmapi.c
@@ -290,8 +290,8 @@ int
 vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, vm_ooffset_t off,
     size_t len, int prot)
 {
-	struct vm_memmap memmap;
-	int error, flags;
+	struct vm_memmap cur, memmap;
+	int error;
 
 	memmap.gpa = gpa;
 	memmap.segid = segid;
@@ -306,11 +306,15 @@ vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, vm_ooffset_t off,
 	/*
 	 * If this mapping already exists then don't create it again. This
 	 * is the common case for SYSMEM mappings created by bhyveload(8).
+	 * Any other mapping starting at the same address is an error.
 	 */
-	error = vm_mmap_getnext(ctx, &gpa, &segid, &off, &len, &prot, &flags);
-	if (error == 0 && gpa == memmap.gpa) {
-		if (segid != memmap.segid || off != memmap.segoff ||
-		    prot != memmap.prot || flags != memmap.flags) {
+	cur.gpa = gpa;
+	error = vm_mmap_getnext(ctx, &cur.gpa, &cur.segid, &cur.segoff,
+	    &cur.len, &cur.prot, &cur.flags);
+	if (error == 0 && cur.gpa == memmap.gpa) {
+		if (cur.segid != memmap.segid || cur.segoff != memmap.segoff ||
+		    cur.len != memmap.len || cur.prot != memmap.prot ||
+		    cur.flags != memmap.flags) {
 			errno = EEXIST;
 			return (-1);
 		} else {
diff --git a/lib/libvmmapi/vmmapi.h b/lib/libvmmapi/vmmapi.h
index bec7ba85a617..ed51a767ef26 100644
--- a/lib/libvmmapi/vmmapi.h
+++ b/lib/libvmmapi/vmmapi.h
@@ -104,7 +104,9 @@ void	*vm_create_devmem(struct vmctx *ctx, int segid, const char *name,
 
 /*
  * Map the memory segment identified by 'segid' into the guest address space
- * at [gpa,gpa+len) with protection 'prot'.
+ * at [gpa,gpa+len) with protection 'prot'.  If an identical mapping already
+ * exists at 'gpa', return 0 without creating another.  If a different mapping
+ * starts at 'gpa', fail with EEXIST.
  */
 int	vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid,
 	    vm_ooffset_t segoff, size_t len, int prot);