From: Jan Beulich <jbeulich@suse.com>
To: "xen-devel@lists.xenproject.org" <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
"Roger Pau Monné" <roger@xenproject.org>,
"Teddy Astie" <teddy.astie@vates.tech>,
"Marek Marczykowski" <marmarek@invisiblethingslab.com>,
"Daniel Smith" <dpsmith@apertussolutions.com>
Subject: [PATCH v4 2/3] x86/EFI: replace ebmalloc()
Date: Tue, 3 Nov 2026 13:50:37 +0100 [thread overview]
Message-ID: <2d45d102-e26f-4e93-a213-b02fb685b6ef@suse.com> (raw)
In-Reply-To: <640c0ea5-fbbf-436d-b150-4537cbe98fdf@suse.com>
Use the new brk_alloc() instead, with ebmalloc() merely being a thin
wrapper.
Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
---
I'm not quite certain whether we ought to permit non-page-granular
reservations. The in-memory image being somewhat larger due to possibly
excessive padding isn't really a big problem, I think.
As of now, is_xen_fixed_mfn()'s use of brk_end happens to okay with that
variable's declaration living in asm/setup.h. Hence I'd prefer to keep it
that way, yet eventually the decl may need to move to asm/setup.h.
---
v4: Also adjust is_xen_fixed_mfn().
v3: Re-base, in particular over moving of brk.c to common/.
v2: Re-base.
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -263,7 +263,7 @@ quiet_cmd_obj_init_o = INIT_O $@
define cmd_obj_init_o
$(OBJDUMP) -h $< | while read idx name sz rest; do \
case "$$name" in \
- .*.local|.data.rel.ro|.data.rel.ro.*) ;; \
+ .*.local|.data.rel.ro|.data.rel.ro.*|.bss..brk*) ;; \
.text|.text.*|.data|.data.*|.bss|.bss.*) \
test $$(echo $$sz | sed 's,00*,0,') != 0 || continue; \
echo "Error: size of $<:$$name is 0x$$sz" >&2; \
--- a/xen/arch/x86/efi/efi-boot.h
+++ b/xen/arch/x86/efi/efi-boot.h
@@ -7,6 +7,7 @@
#ifndef X86_EFI_EFI_BOOT_H
#define X86_EFI_EFI_BOOT_H
+#include <xen/brk.h>
#include <xen/vga.h>
#include <asm/boot-helpers.h>
@@ -119,6 +120,18 @@ static void __init relocate_trampoline(u
reloc_trampoline64();
}
+DEFINE_BRK(efi, MB(1));
+
+static void *__init ebmalloc(size_t size)
+{
+ void *ptr = brk_alloc(size);
+
+ if ( !ptr )
+ blexit(L"Out of BRK memory\r\n");
+
+ return ptr;
+}
+
static void __init place_string(u32 *addr, const char *s)
{
char *alloc = NULL;
--- a/xen/arch/x86/efi/stub.c
+++ b/xen/arch/x86/efi/stub.c
@@ -41,12 +41,4 @@ void __init noreturn efi_multiboot2(EFI_
void __init efi_init_memory(void) { }
-bool efi_boot_mem_unused(unsigned long *start, unsigned long *end)
-{
- /* FIXME: Simplify once the call here with two NULLs goes away. */
- if ( start || end )
- *start = *end = (unsigned long)_end;
- return false;
-}
-
void efi_update_l4_pgtable(unsigned int l4idx, l4_pgentry_t l4e) { }
--- a/xen/arch/x86/include/asm/mm.h
+++ b/xen/arch/x86/include/asm/mm.h
@@ -313,7 +313,7 @@ struct page_info
(mfn_valid(mfn) && is_xen_heap_page(mfn_to_page(mfn)))
#define is_xen_fixed_mfn(mfn) \
(((mfn_to_maddr(mfn)) >= virt_to_maddr((unsigned long)_stext)) && \
- ((mfn_to_maddr(mfn)) <= virt_to_maddr((unsigned long)__2M_rwdata_end - 1)))
+ (mfn_to_maddr(mfn) <= virt_to_maddr(brk_end - 1)))
#define PRtype_info "016lx"/* should only be used for printk's */
--- a/xen/arch/x86/include/asm/setup.h
+++ b/xen/arch/x86/include/asm/setup.h
@@ -9,6 +9,8 @@ extern const char __2M_rodata_start[], _
extern char __2M_init_start[], __2M_init_end[];
extern char __2M_rwdata_start[], __2M_rwdata_end[];
+extern unsigned long brk_end;
+
extern unsigned long xenheap_initial_phys_start;
extern uint64_t boot_tsc_stamp;
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -1,6 +1,7 @@
#include <xen/acpi.h>
#include <xen/alternative-call.h>
#include <xen/bitops.h>
+#include <xen/brk.h>
#include <xen/console.h>
#include <xen/cpu.h>
#include <xen/cpuidle.h>
@@ -164,6 +165,8 @@ cpumask_t __read_mostly cpu_present_map;
unsigned long __read_mostly xen_phys_start;
+unsigned long __ro_after_init brk_end;
+
/* Only used in asm code and within this source file */
char asmlinkage __section(".init.bss.stack_aligned") __aligned(STACK_SIZE)
cpu0_stack[STACK_SIZE];
@@ -1157,7 +1160,6 @@ void asmlinkage __init noreturn __start_
struct boot_info *bi;
unsigned long raw_max_page;
int i, j, bytes = 0;
- unsigned long eb_start, eb_end;
bool acpi_boot_table_init_done = false, relocated = false;
bool vm_init_done = false;
int ret;
@@ -1523,7 +1525,7 @@ void asmlinkage __init noreturn __start_
/*
* This needs to remain in sync with remove_xen_ranges() and the
* respective reserve_e820_ram() invocation below. No need to
- * query efi_boot_mem_unused() here, though.
+ * query brk_cease() here, though.
*/
xen->start = virt_to_maddr(_stext);
xen->size = __2M_rwdata_end - _stext;
@@ -1667,20 +1669,12 @@ void asmlinkage __init noreturn __start_
if ( !xen_phys_start )
panic("Not enough memory to relocate Xen\n");
- /* FIXME: Putting a hole in .bss would shatter the large page mapping. */
- if ( using_2M_mapping() )
- efi_boot_mem_unused(NULL, NULL);
-
/* This needs to remain in sync with remove_xen_ranges(). */
- if ( efi_boot_mem_unused(&eb_start, &eb_end) )
- {
- if ( !reserve_e820_ram(&boot_e820, __pa(_stext), __pa(eb_start)) ||
- !reserve_e820_ram(&boot_e820, __pa(eb_end), __pa(__2M_rwdata_end)) )
- panic("Failed to reserve Xen in E820\n");
- }
- else
- if ( !reserve_e820_ram(&boot_e820, __pa(_stext), __pa(__2M_rwdata_end)) )
- panic("Failed to reserve Xen in E820\n");
+ brk_end = brk_cease();
+ if ( using_2M_mapping() )
+ brk_end = PAGE_ALIGN_2M(brk_end);
+ if ( !reserve_e820_ram(&boot_e820, __pa(_stext), __pa(brk_end)) )
+ panic("Failed to reserve Xen in E820\n");
/* Late kexec reservation (dynamic start address). */
kexec_reserve_area();
@@ -2274,7 +2268,6 @@ __initcall(init_xen_cap_info);
int __hwdom_init remove_xen_ranges(struct rangeset *r)
{
- paddr_t start, end;
int rc;
/* S3 resume code (and other real mode trampoline code) */
@@ -2296,26 +2289,10 @@ int __hwdom_init remove_xen_ranges(struc
return rc;
/* hypervisor .data + .bss */
- if ( efi_boot_mem_unused(&start, &end) )
- {
- ASSERT(__pa(start) >= __pa(&__2M_rwdata_start));
- rc = rangeset_remove_range(r, PFN_DOWN(__pa(&__2M_rwdata_start)),
- PFN_DOWN(__pa(start) - 1));
- if ( rc )
- return rc;
- ASSERT(__pa(end) <= __pa(&__2M_rwdata_end));
- rc = rangeset_remove_range(r, PFN_DOWN(__pa(end)),
- PFN_DOWN(__pa(&__2M_rwdata_end) - 1));
- if ( rc )
- return rc;
- }
- else
- {
- rc = rangeset_remove_range(r, PFN_DOWN(__pa(&__2M_rwdata_start)),
- PFN_DOWN(__pa(&__2M_rwdata_end) - 1));
- if ( rc )
- return rc;
- }
+ rc = rangeset_remove_range(r, PFN_DOWN(__pa(&__2M_rwdata_start)),
+ PFN_DOWN(__pa(brk_end) - 1));
+ if ( rc )
+ return rc;
return 0;
}
--- a/xen/arch/x86/tboot.c
+++ b/xen/arch/x86/tboot.c
@@ -321,8 +321,6 @@ void tboot_shutdown(uint32_t shutdown_ty
/* if this is S3 then set regions to MAC */
if ( shutdown_type == TB_SHUTDOWN_S3 )
{
- unsigned long s, e;
-
/*
* Xen regions for tboot to MAC. This needs to remain in sync with
* remove_xen_ranges().
@@ -336,16 +334,8 @@ void tboot_shutdown(uint32_t shutdown_ty
g_tboot_shared->mac_regions[1].size = __2M_rodata_end - _stext;
/* hypervisor .data + .bss */
g_tboot_shared->mac_regions[2].start = (uint64_t)__pa(&__2M_rwdata_start);
- g_tboot_shared->mac_regions[2].size = __2M_rwdata_end - __2M_rwdata_start;
- if ( efi_boot_mem_unused(&s, &e) )
- {
- g_tboot_shared->mac_regions[2].size =
- s - (unsigned long)__2M_rwdata_start;
- g_tboot_shared->mac_regions[3].start = __pa(e);
- g_tboot_shared->mac_regions[3].size =
- (unsigned long)__2M_rwdata_end - e;
- g_tboot_shared->num_mac_regions = 4;
- }
+ g_tboot_shared->mac_regions[2].size =
+ brk_end - (unsigned long)__2M_rwdata_start;
/*
* MAC domains and other Xen memory
--- a/xen/common/efi/boot.c
+++ b/xen/common/efi/boot.c
@@ -1830,8 +1830,6 @@ void __init efi_init_memory(void)
pte_attr_t prot;
} *extra, *extra_head = NULL;
- free_ebmalloc_unused_mem();
-
if ( !efi_enabled(EFI_BOOT) )
return;
--- a/xen/common/efi/ebmalloc.c
+++ /dev/null
@@ -1,74 +0,0 @@
-#include "efi.h"
-#include <xen/init.h>
-#include <xen/mm.h>
-
-#ifdef CONFIG_ARM
-/*
- * TODO: Enable EFI boot allocator on ARM.
- * This code can be common for x86 and ARM.
- * Things TODO on ARM before enabling ebmalloc:
- * - estimate required EBMALLOC_SIZE value,
- * - where (in which section) ebmalloc_mem[] should live; if in
- * .bss.page_aligned, as it is right now, then whole BSS zeroing
- * have to be disabled in xen/arch/arm/arm64/head.S; though BSS
- * should be initialized somehow before use of variables living there,
- * - use ebmalloc() in ARM/common EFI boot code,
- * - call free_ebmalloc_unused_mem() somewhere in init code.
- */
-#define EBMALLOC_SIZE MB(0)
-#else
-#define EBMALLOC_SIZE MB(1)
-#endif
-
-static char __section(".bss.page_aligned") __aligned(PAGE_SIZE)
- ebmalloc_mem[EBMALLOC_SIZE];
-static unsigned long __read_mostly ebmalloc_allocated;
-
-/* EFI boot allocator. */
-void __init *ebmalloc(size_t size)
-{
- void *ptr = ebmalloc_mem + ebmalloc_allocated;
-
- ebmalloc_allocated += ROUNDUP(size, sizeof(void *));
-
- if ( ebmalloc_allocated > sizeof(ebmalloc_mem) )
- blexit(L"Out of static memory\r\n");
-
- return ptr;
-}
-
-bool efi_boot_mem_unused(unsigned long *start, unsigned long *end)
-{
- /* FIXME: Drop once the call here with two NULLs goes away. */
- if ( !start && !end )
- {
- ebmalloc_allocated = sizeof(ebmalloc_mem);
- return false;
- }
-
- *start = (unsigned long)ebmalloc_mem + PAGE_ALIGN(ebmalloc_allocated);
- *end = (unsigned long)ebmalloc_mem + sizeof(ebmalloc_mem);
-
- return *start < *end;
-}
-
-void __init free_ebmalloc_unused_mem(void)
-{
- unsigned long start, end;
-
- if ( !efi_boot_mem_unused(&start, &end) )
- return;
-
- destroy_xen_mappings(start, end);
-
-#ifdef CONFIG_X86
- /*
- * By reserving the space early in the E820 map, it gets freed way before
- * we make it here. Don't free the range a 2nd time.
- */
-#else
- init_xenheap_pages(__pa(start), __pa(end));
-#endif
-
- printk(XENLOG_INFO "Freed %lukB unused BSS memory\n", (end - start) >> 10);
-}
--- a/xen/common/efi/efi-common.mk
+++ b/xen/common/efi/efi-common.mk
@@ -1,4 +1,4 @@
-EFIOBJ-y := boot.init.o pe.init.o ebmalloc.o runtime.o
+EFIOBJ-y := boot.init.o pe.init.o runtime.o
EFIOBJ-$(CONFIG_COMPAT) += compat.o
CFLAGS-y += -fshort-wchar
--- a/xen/common/efi/efi.h
+++ b/xen/common/efi/efi.h
@@ -48,10 +48,6 @@ void noreturn blexit(const CHAR16 *str);
const CHAR16 *wmemchr(const CHAR16 *s, CHAR16 c, UINTN n);
-/* EFI boot allocator. */
-void *ebmalloc(size_t size);
-void free_ebmalloc_unused_mem(void);
-
const void *pe_find_section(const void *image, const UINTN image_size,
const CHAR16 *section_name, UINTN *size_out);
--- a/xen/include/xen/efi.h
+++ b/xen/include/xen/efi.h
@@ -39,7 +39,6 @@ static inline bool efi_enabled(unsigned
extern bool efi_secure_boot;
void efi_init_memory(void);
-bool efi_boot_mem_unused(unsigned long *start, unsigned long *end);
unsigned long efi_get_time(void);
void efi_reset_system(bool warm);
#ifndef COMPAT
next prev parent reply other threads:[~2026-11-03 12:50 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 12:47 [PATCH v4 0/3] "brk" allocator Jan Beulich
2026-11-03 12:49 ` [PATCH v4 1/3] common: introduce " Jan Beulich
2026-11-16 17:27 ` Andrew Cooper
2026-11-03 12:50 ` Jan Beulich [this message]
2026-11-16 18:13 ` [PATCH v4 2/3] x86/EFI: replace ebmalloc() Andrew Cooper
2026-11-17 7:37 ` Jan Beulich
2026-11-18 10:00 ` Jan Beulich
2026-11-03 12:51 ` [PATCH v4 3/3] xhci-dbc: use brk_alloc() Jan Beulich
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=2d45d102-e26f-4e93-a213-b02fb685b6ef@suse.com \
--to=jbeulich@suse.com \
--cc=andrew.cooper3@citrix.com \
--cc=dpsmith@apertussolutions.com \
--cc=marmarek@invisiblethingslab.com \
--cc=roger@xenproject.org \
--cc=teddy.astie@vates.tech \
--cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.