git: 99aeb28b2f7e - main - bhyve: don't panic if e820 finds no available memory

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Fri, 12 May 2023 07:47:45 UTC
The branch main has been updated by corvink:

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

commit 99aeb28b2f7e28c516dd6434db63a9bc1c1f3218
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2023-05-09 12:32:33 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-05-12 07:29:39 +0000

    bhyve: don't panic if e820 finds no available memory
    
    The GVT-d emulation tries to allocate some specific memory. It could
    happen that this address doesn't exist. In that case, GVT-d will fall
    back to allocate any address. Nevertheless, this only works if the e820
    fails with an error instead of exiting on an assertion.
    
    Reviewed by:            markj
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D40034
---
 usr.sbin/bhyve/e820.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/usr.sbin/bhyve/e820.c b/usr.sbin/bhyve/e820.c
index 6c43e6eda3a5..99a66645f70f 100644
--- a/usr.sbin/bhyve/e820.c
+++ b/usr.sbin/bhyve/e820.c
@@ -197,11 +197,17 @@ e820_add_entry(const uint64_t base, const uint64_t end,
 		return (0);
 	}
 
-	assert(element != NULL);
-	/* Non system memory should be allocated inside system memory. */
-	assert(element->type == E820_TYPE_MEMORY);
-	/* New element should fit into existing system memory element. */
-	assert(base >= element->base && end <= element->end);
+	/*
+	 * If some one tries to allocate a specific address, it could happen, that
+	 * this address is not allocatable. Therefore, do some checks. If the
+	 * address is not allocatable, don't panic. The user may have a fallback and
+	 * tries to allocate another address. This is true for the GVT-d emulation
+	 * which tries to reuse the host address of the graphics stolen memory and
+	 * falls back to allocating the highest address below 4 GB.
+	 */
+	if (element == NULL || element->type != E820_TYPE_MEMORY ||
+	    (base < element->base || end > element->end))
+		return (ENOMEM);
 
 	if (base == element->base) {
 		/*