From: Giacomo Mazzola <gmazz@amazon.de>
To: <kvm@vger.kernel.org>
Cc: Giacomo Mazzola <gmazz@amazon.de>
Subject: [kvm-unit-tests PATCH 2/8] x86: fix EFI memory allocator to clamp regions to 4 GiB
Date: Tue, 9 Jun 2026 14:08:54 +0000 [thread overview]
Message-ID: <20260609140901.95727-3-gmazz@amazon.de> (raw)
In-Reply-To: <20260609140901.95727-1-gmazz@amazon.de>
setup_page_table() creates an identity map covering only the first
4 GiB (4 PDPT entries × 512 PDE entries × 2 MiB pages). However,
setup_memory_allocator() picks the largest EFI_CONVENTIONAL_MEMORY
region without regard to this limit.
On machines with more than 4 GiB of RAM, the largest conventional
memory region often starts below 4 GiB but extends well above it,
or resides entirely above 4 GiB. When the allocator selects such a
region, any access to memory beyond the mapped range triggers a page
fault, crashing the test before it can run.
Fix this by clamping every candidate region to the 4 GiB boundary:
skip regions that start at or above 4 GiB entirely, and truncate
regions that straddle the boundary so only the mapped portion is
considered. The allocator then picks the largest usable region that
is fully covered by the identity map.
Signed-off-by: Giacomo Mazzola <gmazz@amazon.de>
---
lib/x86/setup.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/lib/x86/setup.c b/lib/x86/setup.c
index c2f1c6d0..09241b33 100644
--- a/lib/x86/setup.c
+++ b/lib/x86/setup.c
@@ -215,6 +215,7 @@ static efi_status_t setup_memory_allocator(efi_bootinfo_t *efi_bootinfo)
struct efi_boot_memmap *map = &(efi_bootinfo->mem_map);
efi_memory_desc_t *buffer = *map->map;
efi_memory_desc_t *d = NULL;
+ unsigned long max_addr = 1UL << 32;
/*
* The 'buffer' contains multiple descriptors that describe memory
@@ -222,13 +223,26 @@ static efi_status_t setup_memory_allocator(efi_bootinfo_t *efi_bootinfo)
* EFI_CONVENTIONAL_MEMORY region which will be used to set up the
* memory allocator, so that the memory allocator can work in the
* largest free continuous memory region.
+ *
+ * Regions are clamped to the first 4 GiB because the EFI page
+ * tables set up by setup_page_table() only map that range.
*/
for (i = 0; i < *(map->map_size); i += *(map->desc_size)) {
d = (efi_memory_desc_t *)(&((u8 *)buffer)[i]);
if (d->type == EFI_CONVENTIONAL_MEMORY) {
- if (free_mem_pages < d->num_pages) {
- free_mem_pages = d->num_pages;
- free_mem_start = d->phys_addr;
+ unsigned long start = d->phys_addr;
+ unsigned long end = start + (d->num_pages << EFI_PAGE_SHIFT);
+ unsigned long pages;
+
+ if (start >= max_addr)
+ continue;
+ if (end > max_addr)
+ end = max_addr;
+ pages = (end - start) >> EFI_PAGE_SHIFT;
+
+ if (free_mem_pages < pages) {
+ free_mem_pages = pages;
+ free_mem_start = start;
}
}
}
--
2.47.3
Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
next prev parent reply other threads:[~2026-06-09 14:10 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-09 14:08 [kvm-unit-tests PATCH 0/8] x86: fixes for running KUT as EFI on non-QEMU KVM hosts Giacomo Mazzola
2026-06-09 14:08 ` [kvm-unit-tests PATCH 1/8] x86: efi: use PER_CPU_SIZE for per-CPU stack allocation Giacomo Mazzola
2026-06-09 14:08 ` Giacomo Mazzola [this message]
2026-06-09 14:08 ` [kvm-unit-tests PATCH 3/8] x86: skip PMU init when no PMU is advertised Giacomo Mazzola
2026-06-09 14:08 ` [kvm-unit-tests PATCH 4/8] x86: fix ISR thunk to use absolute indirect jump Giacomo Mazzola
2026-06-09 14:08 ` [kvm-unit-tests PATCH 5/8] x86: replace per-AP bringup prints with a single summary line Giacomo Mazzola
2026-06-09 14:08 ` [kvm-unit-tests PATCH 6/8] x86: add timeout-based SMP bringup when fw_cfg is unavailable Giacomo Mazzola
2026-06-09 14:08 ` [kvm-unit-tests PATCH 7/8] efi: fix load_options_size conversion to character count Giacomo Mazzola
2026-06-10 16:09 ` Andrew Jones
2026-06-09 14:09 ` [kvm-unit-tests PATCH 8/8] efi: parse KUT_ENV= from load options into environ Giacomo Mazzola
2026-06-10 18:18 ` Andrew Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260609140901.95727-3-gmazz@amazon.de \
--to=gmazz@amazon.de \
--cc=kvm@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox