git: a82c04293ecc - main - libvmmapi: Size the guard reservation from existing memory
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 24 Nov 2026 08:01:47 UTC
The branch main has been updated by hayzam:
URL: https://cgit.FreeBSD.org/src/commit/?id=a82c04293ecc72ea87f68c9506e160944ec26047
commit a82c04293ecc72ea87f68c9506e160944ec26047
Author: Hayzam Sherif <hayzam@FreeBSD.org>
AuthorDate: 2026-11-24 07:24:23 +0000
Commit: Hayzam Sherif <hayzam@FreeBSD.org>
CommitDate: 2026-11-24 07:59:16 +0000
libvmmapi: Size the guard reservation from existing memory
When the VM's memory already exists, as it does after bhyveload(8),
vm_setup_memory_domains() maps the existing segment but sizes its
address space reservation from the requested size. If the existing
segment is larger, the mapping runs past the reservation and replaces
the guard region after it, and anything else mapped there.
Look for existing memory before sizing the reservation.
Reviewed by: bnovkov, markj
Approved by: markj
Fixes: 08c7dd2fbe4f (libvmmapi: Add support for setting up and configuring guest NUMA domains)
MFC after: 1 week
Sponsored by: Alchemilla Ventures Private Limited
Differential Revision: https://reviews.freebsd.org/D61917
---
lib/libvmmapi/vmmapi.c | 48 ++++++++++++++++++++++++++++--------------------
1 file changed, 28 insertions(+), 20 deletions(-)
diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c
index 99d0a1ec7e39..c9630e357940 100644
--- a/lib/libvmmapi/vmmapi.c
+++ b/lib/libvmmapi/vmmapi.c
@@ -498,6 +498,7 @@ vm_setup_memory_domains(struct vmctx *ctx, enum vm_mmap_style vms,
char *baseaddr, *ptr;
int error, i, segid;
vm_paddr_t gpa;
+ bool exists;
/* Sanity checks. */
assert(vms == VM_MMAP_ALL);
@@ -506,6 +507,32 @@ vm_setup_memory_domains(struct vmctx *ctx, enum vm_mmap_style vms,
return (-1);
}
+ /*
+ * Check if the memory segments already exist.
+ * If 'ndoms' is greater than one, refuse to proceed if a
+ * memseg already exists. If only one domain was requested, use
+ * the existing segment to preserve the behaviour of the previous
+ * implementation. This has to be done before the address space
+ * reservation below is sized.
+ *
+ * Splitting existing memory segments is tedious and
+ * error-prone, which is why we don't support NUMA
+ * domains for bhyveload(8)-loaded VMs.
+ */
+ exists = false;
+ for (i = 0; i < ndoms; i++) {
+ error = vm_get_memseg(ctx, VM_SYSMEM + i, &len, memseg.name,
+ sizeof(memseg.name));
+ if (error == 0 && len != 0) {
+ if (ndoms != 1) {
+ errno = EEXIST;
+ return (-1);
+ }
+ doms[0].size = len;
+ exists = true;
+ }
+ }
+
/* Calculate total memory size. */
totalsize = 0;
for (i = 0; i < ndoms; i++)
@@ -534,26 +561,7 @@ vm_setup_memory_domains(struct vmctx *ctx, enum vm_mmap_style vms,
segid = VM_SYSMEM + i;
dom = &doms[i];
- /*
- * Check if the memory segment already exists.
- * If 'ndoms' is greater than one, refuse to proceed if the
- * memseg already exists. If only one domain was requested, use
- * the existing segment to preserve the behaviour of the previous
- * implementation.
- *
- * Splitting existing memory segments is tedious and
- * error-prone, which is why we don't support NUMA
- * domains for bhyveload(8)-loaded VMs.
- */
- error = vm_get_memseg(ctx, segid, &len, memseg.name,
- sizeof(memseg.name));
- if (error == 0 && len != 0) {
- if (ndoms != 1) {
- errno = EEXIST;
- return (-1);
- } else
- doms[0].size = len;
- } else {
+ if (!exists) {
error = vm_alloc_memseg(ctx, segid, dom->size, NULL,
dom->ds_policy, dom->ds_mask, dom->ds_size);
if (error)