git: 541696f2a1d0 - stable/14 - bhyve: remove empty E820 entries

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Tue, 12 Nov 2024 07:55:41 UTC
The branch stable/14 has been updated by corvink:

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

commit 541696f2a1d08eb7f808b3ac183dc64fa8fe3cf2
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2024-06-04 07:38:02 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2024-11-12 07:54:21 +0000

    bhyve: remove empty E820 entries
    
    When reserving a block with the same size of a RAM segement, we can end up with
    an empty RAM segmenet. Avoid that by removing this empty segment from the E820
    table.
    
    Reviewed by:            jhb, markj (older version)
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D45480
    
    (cherry picked from commit f325f81f4ab93646debed0c2291c4c7f31470cfb)
---
 usr.sbin/bhyve/amd64/e820.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/usr.sbin/bhyve/amd64/e820.c b/usr.sbin/bhyve/amd64/e820.c
index 9d95ec8ce688..148cae72ca6e 100644
--- a/usr.sbin/bhyve/amd64/e820.c
+++ b/usr.sbin/bhyve/amd64/e820.c
@@ -210,7 +210,19 @@ e820_add_entry(const uint64_t base, const uint64_t end,
 	    (base < element->base || end > element->end))
 		return (ENOMEM);
 
-	if (base == element->base) {
+	if (base == element->base && end == element->end) {
+		/*
+		 * The new entry replaces an existing one.
+		 *
+		 * Old table:
+		 * 	[ 0x1000, 0x4000] RAM		<-- element
+		 * New table:
+		 *	[ 0x1000, 0x4000] Reserved
+		 */
+		TAILQ_INSERT_BEFORE(element, new_element, chain);
+		TAILQ_REMOVE(&e820_table, element, chain);
+		free(element);
+	} else if (base == element->base) {
 		/*
 		 * New element at system memory base boundary. Add new
 		 * element before current and adjust the base of the old