Skip to contentSkip to conversation

x86: "brk" allocator

PATCH Superseded Jan Beulich 3 participants 17 messages Nov 13, 2025 – Nov 14, 2025

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover x86: "brk" allocator

Derive for our purposes something similar to what Linux has been having for a long while.

1: x86: introduce "brk" allocator
2: x86/EFI: replace ebmalloc()
3: xhci-dbc: use brk_alloc()

To reduce padding holes, .bss.page_aligned and per-CPU data may want moving immediately ahead of __brk_start[]. Albeit then the tail of per-CPU data will all be padding space; sadly the TSS wants/needs page-aligning for XPTI purposes.

Jan

#1 lore raw

PATCH 1/3 x86: introduce "brk" allocator

... to replace ebmalloc(), and then to find further use(s) to allow recovering memory which is needed very early (and hence needs setting up statically), but may not fully be used (or not used at all).

Note that unlike free_ebmalloc_unused_mem(), brk_free_unused() (once other code is converted) will be able to free part of the BRK space even in the xen.efi case. That would happen if BRK space extends across a 2Mb boundary, and actual use stops before that boundary.

---
Changing setup.c's reserve_e820_ram() uses would be cumbersome when done right here. That'll be done when ebmalloc() is replaced, and hence what's there can also simply be replaced.

The xen.efi detection may want separating out into a helper.

When linking xen.efi, ld produces a base relocation for the reference to __subsystem__, which is wrong (that's an absolute symbol, after all). While that will need fixing there, it does no harm for our purposes.

xen/arch/x86/boot/Makefile +1 -0
@@ -1,4 +1,5 @@
 obj-bin-y += head.o
+obj-bin-y += brk.init.o
 obj-bin-y += built-in-32.o
 obj-bin-y += $(obj64)
 
xen/arch/x86/boot/brk.c new file +72 -0

72 lines not on this page · raw message

xen/arch/x86/include/asm/brk.h new file +7 -0
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <xen/types.h>
+
+void *brk_alloc(size_t size);
+unsigned long brk_get_unused_start(void);
+void brk_free_unused(void);
xen/arch/x86/xen.lds.S +5 -1
@@ -331,7 +331,11 @@ SECTIONS
        __bss_start = .;
        *(.bss.page_aligned*)
        PERCPU_BSS
-       *(.bss .bss.*)
+       *(.bss .bss.[a-zA-Z0-9_]*)
+       . = ALIGN(PAGE_SIZE);
+       __brk_start = .;
+       *(.bss..brk.page_aligned*)
+       *(.bss..brk*)
        . = ALIGN(POINTER_ALIGN);
        __bss_end = .;
   } PHDR(text)
xen/arch/x86/mm.c +3 -0
@@ -112,6 +112,7 @@
 #include <xen/vmap.h>
 #include <xen/xmalloc.h>
 
+#include <asm/brk.h>
 #include <asm/e820.h>
 #include <asm/fixmap.h>
 #include <asm/flushtlb.h>
@@ -337,6 +338,8 @@ void __init arch_init_memory(void)
 
     efi_init_memory();
 
+    brk_free_unused();
+
 #ifndef NDEBUG
     if ( highmem_start )
     {
#4 lore raw

PATCH 2/3 x86/EFI: replace ebmalloc()

In reply to Jan Beulich

Use the new brk_alloc() instead, with ebmalloc() merely being a thin wrapper.

---
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.

xen/Rules.mk +1 -1
@@ -262,7 +262,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) ;; \
+        .*.local|.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; \
xen/arch/x86/efi/efi-boot.h +13 -0
@@ -10,6 +10,7 @@
 #include <xen/vga.h>
 
 #include <asm/boot-helpers.h>
+#include <asm/brk.h>
 #include <asm/e820.h>
 #include <asm/edd.h>
 #include <asm/microcode.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;
xen/arch/x86/efi/stub.c +0 -8
@@ -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) { }
xen/arch/x86/include/asm/brk.h +4 -0
@@ -2,6 +2,10 @@
 
 #include <xen/types.h>
 
+#define DEFINE_BRK(var, size) \
+    static char __section(".bss..brk.page_aligned") __aligned(PAGE_SIZE) \
+        __used var ## _brk_[size]
+
 void *brk_alloc(size_t size);
 unsigned long brk_get_unused_start(void);
 void brk_free_unused(void);
xen/arch/x86/include/asm/setup.h +2 -0
@@ -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;
 
xen/arch/x86/setup.c +12 -34

89 lines not on this page · raw message

xen/arch/x86/tboot.c +2 -12
@@ -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
xen/common/efi/boot.c +0 -2
@@ -1788,8 +1788,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;
 
xen/common/efi/ebmalloc.c deleted +0 -74

74 lines not on this page · raw message

xen/common/efi/efi-common.mk +1 -1
@@ -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
xen/common/efi/efi.h +0 -4
@@ -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);
 
xen/include/xen/efi.h +0 -1
@@ -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);
 bool efi_rs_using_pgtables(void);
 unsigned long efi_get_time(void);
 void efi_halt_system(void);
#12 lore raw

PATCH 3/3 xhci-dbc: use brk_alloc()

In reply to Jan Beulich

This way the relatively large chunk of DMA buffers can be freed when the driver isn't in use.

xen/drivers/char/xhci-dbc.c +23 -13
@@ -27,6 +27,8 @@
 #include <xen/serial.h>
 #include <xen/timer.h>
 #include <xen/types.h>
+
+#include <asm/brk.h>
 #include <asm/fixmap.h>
 #include <asm/io.h>
 #include <asm/string.h>
@@ -1321,7 +1323,7 @@ static struct uart_driver dbc_uart_drive
 };
 
 /* Those are accessed via DMA. */
-struct dbc_dma_bufs {
+struct __aligned(PAGE_SIZE) dbc_dma_bufs {
     struct xhci_trb evt_trb[DBC_TRB_RING_CAP];
     struct xhci_trb out_trb[DBC_TRB_RING_CAP];
     struct xhci_trb in_trb[DBC_TRB_RING_CAP];
@@ -1335,8 +1337,7 @@ struct dbc_dma_bufs {
      * DMA-reachable by the USB controller.
      */
 };
-static struct dbc_dma_bufs __section(".bss.page_aligned") __aligned(PAGE_SIZE)
-    dbc_dma_bufs;
+DEFINE_BRK(xhci, sizeof(struct dbc_dma_bufs));
 
 static int __init cf_check xhci_parse_dbgp(const char *opt_dbgp)
 {
@@ -1413,24 +1414,33 @@ void __init xhci_dbc_uart_init(void)
 {
     struct dbc_uart *uart = &dbc_uart;
     struct dbc *dbc = &uart->dbc;
+    struct dbc_dma_bufs *dma_bufs;
 
     if ( !dbc->enable )
         return;
 
-    dbc->dbc_ctx = &dbc_dma_bufs.ctx;
-    dbc->dbc_erst = &dbc_dma_bufs.erst;
-    dbc->dbc_ering.trb = dbc_dma_bufs.evt_trb;
-    dbc->dbc_oring.trb = dbc_dma_bufs.out_trb;
-    dbc->dbc_iring.trb = dbc_dma_bufs.in_trb;
-    dbc->dbc_owork.buf = dbc_dma_bufs.out_wrk_buf;
-    dbc->dbc_iwork.buf = dbc_dma_bufs.in_wrk_buf;
-    dbc->dbc_str = dbc_dma_bufs.str_buf;
+    dma_bufs = brk_alloc(sizeof(*dma_bufs));
+    if ( !dma_bufs )
+    {
+        dbc->enable = false;
+        printk(XENLOG_ERR "XHCI: not enough BRK space available\n");
+        return;
+    }
+
+    dbc->dbc_ctx = &dma_bufs->ctx;
+    dbc->dbc_erst = &dma_bufs->erst;
+    dbc->dbc_ering.trb = dma_bufs->evt_trb;
+    dbc->dbc_oring.trb = dma_bufs->out_trb;
+    dbc->dbc_iring.trb = dma_bufs->in_trb;
+    dbc->dbc_owork.buf = dma_bufs->out_wrk_buf;
+    dbc->dbc_iwork.buf = dma_bufs->in_wrk_buf;
+    dbc->dbc_str = dma_bufs->str_buf;
 
     if ( dbc_open(dbc) )
     {
         iommu_add_extra_reserved_device_memory(
-                PFN_DOWN(virt_to_maddr(&dbc_dma_bufs)),
-                PFN_UP(sizeof(dbc_dma_bufs)),
+                PFN_DOWN(virt_to_maddr(dma_bufs)),
+                PFN_DOWN(sizeof(*dma_bufs)),
                 uart->dbc.sbdf,
                 "XHCI console");
         serial_register_uart(SERHND_XHCI, &dbc_uart_driver, &dbc_uart);
#13 lore raw

Re: [PATCH 3/3] xhci-dbc: use brk_alloc()

Quoted 26 lines · Jan Beulich

This way the relatively large chunk of DMA buffers can be freed when the driver isn't in use.

xen/drivers/char/xhci-dbc.c +3 -1
@@ -27,6 +27,8 @@
 #include <xen/serial.h>
 #include <xen/timer.h>
 #include <xen/types.h>
+
+#include <asm/brk.h>
 #include <asm/fixmap.h>
 #include <asm/io.h>
 #include <asm/string.h>
@@ -1321,7 +1323,7 @@ static struct uart_driver dbc_uart_drive
 };
 
 /* Those are accessed via DMA. */
-struct dbc_dma_bufs {
+struct __aligned(PAGE_SIZE) dbc_dma_bufs {
     struct xhci_trb evt_trb[DBC_TRB_RING_CAP];
     struct xhci_trb out_trb[DBC_TRB_RING_CAP];
     struct xhci_trb in_trb[DBC_TRB_RING_CAP];

xen/drivers/char/xhci-dbc.c struct dbc_dma_bufs { · line 1340

Quoted 6 lines
xen/drivers/char/xhci-dbc.c +1 -2
@@ -1335,8 +1337,7 @@ struct dbc_dma_bufs {
      * DMA-reachable by the USB controller.
      */
 };
-static struct dbc_dma_bufs __section(".bss.page_aligned") __aligned(PAGE_SIZE)
-    dbc_dma_bufs;
+DEFINE_BRK(xhci, sizeof(struct dbc_dma_bufs));

I think with this change (or rather with using brk_alloc() below), the structure wants to be padded up to the page size, to avoid putting anything else on the same page (see comment just outside of context above). Previously .bss.page_aligned contained (hopefully) only page-aligned objects, but now brk_alloc() can combine them.

Quoted 34 lines
xen/drivers/char/xhci-dbc.c +15 -8
 static int __init cf_check xhci_parse_dbgp(const char *opt_dbgp)
 {
@@ -1413,24 +1414,33 @@ void __init xhci_dbc_uart_init(void)
 {
     struct dbc_uart *uart = &dbc_uart;
     struct dbc *dbc = &uart->dbc;
+    struct dbc_dma_bufs *dma_bufs;
 
     if ( !dbc->enable )
         return;
 
-    dbc->dbc_ctx = &dbc_dma_bufs.ctx;
-    dbc->dbc_erst = &dbc_dma_bufs.erst;
-    dbc->dbc_ering.trb = dbc_dma_bufs.evt_trb;
-    dbc->dbc_oring.trb = dbc_dma_bufs.out_trb;
-    dbc->dbc_iring.trb = dbc_dma_bufs.in_trb;
-    dbc->dbc_owork.buf = dbc_dma_bufs.out_wrk_buf;
-    dbc->dbc_iwork.buf = dbc_dma_bufs.in_wrk_buf;
-    dbc->dbc_str = dbc_dma_bufs.str_buf;
+    dma_bufs = brk_alloc(sizeof(*dma_bufs));
+    if ( !dma_bufs )
+    {
+        dbc->enable = false;
+        printk(XENLOG_ERR "XHCI: not enough BRK space available\n");
+        return;
+    }
+
+    dbc->dbc_ctx = &dma_bufs->ctx;
+    dbc->dbc_erst = &dma_bufs->erst;
+    dbc->dbc_ering.trb = dma_bufs->evt_trb;
+    dbc->dbc_oring.trb = dma_bufs->out_trb;
+    dbc->dbc_iring.trb = dma_bufs->in_trb;
+    dbc->dbc_owork.buf = dma_bufs->out_wrk_buf;

xen/drivers/char/xhci-dbc.c void __init xhci_dbc_uart_init(void) · line 1443

Quoted 10 lines
xen/drivers/char/xhci-dbc.c +4 -2
+    dbc->dbc_iwork.buf = dma_bufs->in_wrk_buf;
+    dbc->dbc_str = dma_bufs->str_buf;
 
     if ( dbc_open(dbc) )
     {
         iommu_add_extra_reserved_device_memory(
-                PFN_DOWN(virt_to_maddr(&dbc_dma_bufs)),
-                PFN_UP(sizeof(dbc_dma_bufs)),
+                PFN_DOWN(virt_to_maddr(dma_bufs)),
+                PFN_DOWN(sizeof(*dma_bufs)),

Is that really correct? But with padding (see earlier comment) it shouldn't really matter.

Quoted 4 lines
xen/drivers/char/xhci-dbc.c +0 -0
                 uart->dbc.sbdf,
                 "XHCI console");
         serial_register_uart(SERHND_XHCI, &dbc_uart_driver, &dbc_uart);
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#5 lore raw

Re: [PATCH 2/3] x86/EFI: replace ebmalloc()

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich

Use the new brk_alloc() instead, with ebmalloc() merely being a thin wrapper.

---
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.

My grep says ebmalloc is used in just two places:
1. For efi_memmap (via efi_arch_allocate_mmap_buffer())
2. For various cmdline options and module names (via aplace_string())

The second one is probably undesirable to allocate full page for each one. On the other hand, the current approach (putting small allocations at the same page as an earlier page-aligned one) also has its issues - see comments on 3/3 patch.

Quoted 58 lines
xen/Rules.mk +1 -1
@@ -262,7 +262,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) ;; \
+        .*.local|.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; \
xen/arch/x86/efi/efi-boot.h +13 -0
@@ -10,6 +10,7 @@
 #include <xen/vga.h>
 
 #include <asm/boot-helpers.h>
+#include <asm/brk.h>
 #include <asm/e820.h>
 #include <asm/edd.h>
 #include <asm/microcode.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;
xen/arch/x86/efi/stub.c +0 -8
@@ -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) { }

xen/arch/x86/include/asm/brk.h line 7

Quoted 6 lines
xen/arch/x86/include/asm/brk.h +3 -0
@@ -2,6 +2,10 @@
 
 #include <xen/types.h>
 
+#define DEFINE_BRK(var, size) \
+    static char __section(".bss..brk.page_aligned") __aligned(PAGE_SIZE) \
+        __used var ## _brk_[size]

This chunk belongs to the previous patch I think.

Quoted 4 lines
xen/arch/x86/include/asm/brk.h +1 -0
+
 void *brk_alloc(size_t size);
 unsigned long brk_get_unused_start(void);
 void brk_free_unused(void);
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#6 lore raw

Re: [PATCH 2/3] x86/EFI: replace ebmalloc()

Quoted 5 lines · Marek Marczykowski
Quoted 4 lines · Jan Beulich
xen/arch/x86/include/asm/brk.h +0 -0
@@ -2,6 +2,10 @@
 
Quoted 7 lines

xen/arch/x86/include/asm/brk.h line 7

Quoted 5 lines
xen/arch/x86/include/asm/brk.h +3 -0
 #include <xen/types.h>
 
+#define DEFINE_BRK(var, size) \
+    static char __section(".bss..brk.page_aligned") __aligned(PAGE_SIZE) \
+        __used var ## _brk_[size]

This chunk belongs to the previous patch I think.

It could, but it's not used there yet (i.e. would count as dead code).

Jan

#14 lore raw

Re: [PATCH 3/3] xhci-dbc: use brk_alloc()

In reply to Marek Marczykowski
Quoted 25 lines · Marek Marczykowski
Quoted 24 lines · Jan Beulich
xen/drivers/char/xhci-dbc.c +3 -1
@@ -27,6 +27,8 @@
 #include <xen/serial.h>
 #include <xen/timer.h>
 #include <xen/types.h>
+
+#include <asm/brk.h>
 #include <asm/fixmap.h>
 #include <asm/io.h>
 #include <asm/string.h>
@@ -1321,7 +1323,7 @@ static struct uart_driver dbc_uart_drive
 };
 
 /* Those are accessed via DMA. */
-struct dbc_dma_bufs {
+struct __aligned(PAGE_SIZE) dbc_dma_bufs {
     struct xhci_trb evt_trb[DBC_TRB_RING_CAP];
     struct xhci_trb out_trb[DBC_TRB_RING_CAP];
     struct xhci_trb in_trb[DBC_TRB_RING_CAP];
@@ -1335,8 +1337,7 @@ struct dbc_dma_bufs {
      * DMA-reachable by the USB controller.
      */
 };
Quoted 8 lines

xen/drivers/char/xhci-dbc.c struct dbc_dma_bufs { · line 1340

Quoted 3 lines
xen/drivers/char/xhci-dbc.c +1 -2
-static struct dbc_dma_bufs __section(".bss.page_aligned") __aligned(PAGE_SIZE)
-    dbc_dma_bufs;
+DEFINE_BRK(xhci, sizeof(struct dbc_dma_bufs));

I think with this change (or rather with using brk_alloc() below), the structure wants to be padded up to the page size, to avoid putting anything else on the same page (see comment just outside of context above).

Are you sure? My understanding is that sizeof(xyz) is always evenly divisible by alignof(xyz). Hence such padding doesn't need making explicit. (And yes, I did see that comment while making the change.)

Quoted 34 lines
Quoted 34 lines
xen/drivers/char/xhci-dbc.c +17 -8
@@ -1413,24 +1414,33 @@ void __init xhci_dbc_uart_init(void)
 {
     struct dbc_uart *uart = &dbc_uart;
     struct dbc *dbc = &uart->dbc;
+    struct dbc_dma_bufs *dma_bufs;
 
     if ( !dbc->enable )
         return;
 
-    dbc->dbc_ctx = &dbc_dma_bufs.ctx;
-    dbc->dbc_erst = &dbc_dma_bufs.erst;
-    dbc->dbc_ering.trb = dbc_dma_bufs.evt_trb;
-    dbc->dbc_oring.trb = dbc_dma_bufs.out_trb;
-    dbc->dbc_iring.trb = dbc_dma_bufs.in_trb;
-    dbc->dbc_owork.buf = dbc_dma_bufs.out_wrk_buf;
-    dbc->dbc_iwork.buf = dbc_dma_bufs.in_wrk_buf;
-    dbc->dbc_str = dbc_dma_bufs.str_buf;
+    dma_bufs = brk_alloc(sizeof(*dma_bufs));
+    if ( !dma_bufs )
+    {
+        dbc->enable = false;
+        printk(XENLOG_ERR "XHCI: not enough BRK space available\n");
+        return;
+    }
+
+    dbc->dbc_ctx = &dma_bufs->ctx;
+    dbc->dbc_erst = &dma_bufs->erst;
+    dbc->dbc_ering.trb = dma_bufs->evt_trb;
+    dbc->dbc_oring.trb = dma_bufs->out_trb;
+    dbc->dbc_iring.trb = dma_bufs->in_trb;
+    dbc->dbc_owork.buf = dma_bufs->out_wrk_buf;
+    dbc->dbc_iwork.buf = dma_bufs->in_wrk_buf;
+    dbc->dbc_str = dma_bufs->str_buf;
 
Quoted 10 lines

xen/drivers/char/xhci-dbc.c void __init xhci_dbc_uart_init(void) · line 1443

Quoted 7 lines
xen/drivers/char/xhci-dbc.c +2 -2
     if ( dbc_open(dbc) )
     {
         iommu_add_extra_reserved_device_memory(
-                PFN_DOWN(virt_to_maddr(&dbc_dma_bufs)),
-                PFN_UP(sizeof(dbc_dma_bufs)),
+                PFN_DOWN(virt_to_maddr(dma_bufs)),
+                PFN_DOWN(sizeof(*dma_bufs)),

Is that really correct? But with padding (see earlier comment) it shouldn't really matter.

I think this is addressed by the reply further up as well.

Jan

#10 lore raw

Re: [PATCH 2/3] x86/EFI: replace ebmalloc()

In reply to Marek Marczykowski
Quoted 10 lines · Marek Marczykowski
Quoted 8 lines · Jan Beulich

Use the new brk_alloc() instead, with ebmalloc() merely being a thin wrapper.

---
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.

Quoted 8 lines

My grep says ebmalloc is used in just two places:
1. For efi_memmap (via efi_arch_allocate_mmap_buffer())
2. For various cmdline options and module names (via aplace_string())

The second one is probably undesirable to allocate full page for each one. On the other hand, the current approach (putting small allocations at the same page as an earlier page-aligned one) also has its issues - see comments on 3/3 patch.

Imo if such sharing of a page is unwanted, then it's the side caring about the non-sharing which ought to request an exact multiple of pages. Wasting space due to doing this in the BRK implementation is undesirable.

Jan

#7 lore raw

Re: [PATCH 2/3] x86/EFI: replace ebmalloc()

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Marek Marczykowski
Quoted 6 lines · Jan Beulich
xen/arch/x86/include/asm/brk.h +0 -0
@@ -2,6 +2,10 @@
 
 #include <xen/types.h>
 
Quoted 7 lines
Quoted 5 lines

xen/arch/x86/include/asm/brk.h line 7

Quoted 3 lines
xen/arch/x86/include/asm/brk.h +3 -0
+#define DEFINE_BRK(var, size) \
+    static char __section(".bss..brk.page_aligned") __aligned(PAGE_SIZE) \
+        __used var ## _brk_[size]

This chunk belongs to the previous patch I think.

It could, but it's not used there yet (i.e. would count as dead code).

But in the current shape the linker script change in the first patch is unused. IOW, I think adding .bss..brk.page_aligned to the linker script should go together with DEFINE_BRK.

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
Reviewed-by
#15 lore raw

Re: [PATCH 3/3] xhci-dbc: use brk_alloc()

In reply to Jan Beulich
Quoted 35 lines · Jan Beulich
Quoted 25 lines · Marek Marczykowski
Quoted 24 lines · Jan Beulich
xen/drivers/char/xhci-dbc.c +3 -1
@@ -27,6 +27,8 @@
 #include <xen/serial.h>
 #include <xen/timer.h>
 #include <xen/types.h>
+
+#include <asm/brk.h>
 #include <asm/fixmap.h>
 #include <asm/io.h>
 #include <asm/string.h>
@@ -1321,7 +1323,7 @@ static struct uart_driver dbc_uart_drive
 };
 
 /* Those are accessed via DMA. */
-struct dbc_dma_bufs {
+struct __aligned(PAGE_SIZE) dbc_dma_bufs {
     struct xhci_trb evt_trb[DBC_TRB_RING_CAP];
     struct xhci_trb out_trb[DBC_TRB_RING_CAP];
     struct xhci_trb in_trb[DBC_TRB_RING_CAP];
@@ -1335,8 +1337,7 @@ struct dbc_dma_bufs {
      * DMA-reachable by the USB controller.
      */
 };
Quoted 8 lines

xen/drivers/char/xhci-dbc.c struct dbc_dma_bufs { · line 1340

Quoted 3 lines
xen/drivers/char/xhci-dbc.c +1 -2
-static struct dbc_dma_bufs __section(".bss.page_aligned") __aligned(PAGE_SIZE)
-    dbc_dma_bufs;
+DEFINE_BRK(xhci, sizeof(struct dbc_dma_bufs));

I think with this change (or rather with using brk_alloc() below), the structure wants to be padded up to the page size, to avoid putting anything else on the same page (see comment just outside of context above).

Quoted 3 lines

Are you sure? My understanding is that sizeof(xyz) is always evenly divisible by alignof(xyz). Hence such padding doesn't need making explicit. (And yes, I did see that comment while making the change.)

Ok, then indeed the added (or rather moved) __aligned(PAGE_SIZE) is enough.

Quoted 48 lines
Quoted 34 lines
Quoted 34 lines
xen/drivers/char/xhci-dbc.c +17 -8
@@ -1413,24 +1414,33 @@ void __init xhci_dbc_uart_init(void)
 {
     struct dbc_uart *uart = &dbc_uart;
     struct dbc *dbc = &uart->dbc;
+    struct dbc_dma_bufs *dma_bufs;
 
     if ( !dbc->enable )
         return;
 
-    dbc->dbc_ctx = &dbc_dma_bufs.ctx;
-    dbc->dbc_erst = &dbc_dma_bufs.erst;
-    dbc->dbc_ering.trb = dbc_dma_bufs.evt_trb;
-    dbc->dbc_oring.trb = dbc_dma_bufs.out_trb;
-    dbc->dbc_iring.trb = dbc_dma_bufs.in_trb;
-    dbc->dbc_owork.buf = dbc_dma_bufs.out_wrk_buf;
-    dbc->dbc_iwork.buf = dbc_dma_bufs.in_wrk_buf;
-    dbc->dbc_str = dbc_dma_bufs.str_buf;
+    dma_bufs = brk_alloc(sizeof(*dma_bufs));
+    if ( !dma_bufs )
+    {
+        dbc->enable = false;
+        printk(XENLOG_ERR "XHCI: not enough BRK space available\n");
+        return;
+    }
+
+    dbc->dbc_ctx = &dma_bufs->ctx;
+    dbc->dbc_erst = &dma_bufs->erst;
+    dbc->dbc_ering.trb = dma_bufs->evt_trb;
+    dbc->dbc_oring.trb = dma_bufs->out_trb;
+    dbc->dbc_iring.trb = dma_bufs->in_trb;
+    dbc->dbc_owork.buf = dma_bufs->out_wrk_buf;
+    dbc->dbc_iwork.buf = dma_bufs->in_wrk_buf;
+    dbc->dbc_str = dma_bufs->str_buf;
 
Quoted 10 lines

xen/drivers/char/xhci-dbc.c void __init xhci_dbc_uart_init(void) · line 1443

Quoted 7 lines
xen/drivers/char/xhci-dbc.c +2 -2
     if ( dbc_open(dbc) )
     {
         iommu_add_extra_reserved_device_memory(
-                PFN_DOWN(virt_to_maddr(&dbc_dma_bufs)),
-                PFN_UP(sizeof(dbc_dma_bufs)),
+                PFN_DOWN(virt_to_maddr(dma_bufs)),
+                PFN_DOWN(sizeof(*dma_bufs)),

Is that really correct? But with padding (see earlier comment) it shouldn't really matter.

I think this is addressed by the reply further up as well.

Jan

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#11 lore raw

Re: [PATCH 2/3] x86/EFI: replace ebmalloc()

In reply to Jan Beulich
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Marek Marczykowski
Quoted 8 lines · Jan Beulich

Use the new brk_alloc() instead, with ebmalloc() merely being a thin wrapper.

---
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.

My grep says ebmalloc is used in just two places:
1. For efi_memmap (via efi_arch_allocate_mmap_buffer())
2. For various cmdline options and module names (via aplace_string())

Quoted 8 lines
Quoted 4 lines

The second one is probably undesirable to allocate full page for each one. On the other hand, the current approach (putting small allocations at the same page as an earlier page-aligned one) also has its issues - see comments on 3/3 patch.

Imo if such sharing of a page is unwanted, then it's the side caring about the non-sharing which ought to request an exact multiple of pages. Wasting space due to doing this in the BRK implementation is undesirable.

Makes sense.

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#8 lore raw

Re: [PATCH 2/3] x86/EFI: replace ebmalloc()

In reply to Marek Marczykowski
Quoted 15 lines · Marek Marczykowski
Quoted 14 lines · Jan Beulich
Quoted 12 lines · Marek Marczykowski

xen/arch/x86/include/asm/brk.h line 7

Quoted 9 lines · Jan Beulich
xen/arch/x86/include/asm/brk.h +3 -0
@@ -2,6 +2,10 @@
 
 #include <xen/types.h>
 
+#define DEFINE_BRK(var, size) \
+    static char __section(".bss..brk.page_aligned") __aligned(PAGE_SIZE) \
+        __used var ## _brk_[size]

This chunk belongs to the previous patch I think.

Quoted 5 lines
Quoted 1 line

It could, but it's not used there yet (i.e. would count as dead code).

But in the current shape the linker script change in the first patch is unused. IOW, I think adding .bss..brk.page_aligned to the linker script should go together with DEFINE_BRK.

Again - could be done that way, but there's no concept of "dead code" in the linker script (afaik at least).

Jan

#16 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich

Derive for our purposes something similar to what Linux has been having for a long while.

1: x86: introduce "brk" allocator
2: x86/EFI: replace ebmalloc()
3: xhci-dbc: use brk_alloc()

To reduce padding holes, .bss.page_aligned and per-CPU data may want moving immediately ahead of __brk_start[]. Albeit then the tail of per-CPU data will all be padding space; sadly the TSS wants/needs page-aligning for XPTI purposes.

I've not done this yet, but I was intending to make the TSSes be dynamically allocated, so they (along with the IDTs) can be omitted entirely under FRED.

The VT-x tr limit bug/misfeature complicates this somewhat, hence why I've left this out of the FRED basic support, but it can be addressed by pointing HOST_TR_BASE at a singleton TSS.

~Andrew

Reviewed-by
#9 lore raw

Re: [PATCH 2/3] x86/EFI: replace ebmalloc()

In reply to Jan Beulich
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Marek Marczykowski
Quoted 14 lines · Jan Beulich
Quoted 12 lines · Marek Marczykowski

xen/arch/x86/include/asm/brk.h line 7

Quoted 9 lines · Jan Beulich
xen/arch/x86/include/asm/brk.h +3 -0
@@ -2,6 +2,10 @@
 
 #include <xen/types.h>
 
+#define DEFINE_BRK(var, size) \
+    static char __section(".bss..brk.page_aligned") __aligned(PAGE_SIZE) \
+        __used var ## _brk_[size]

This chunk belongs to the previous patch I think.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

It could, but it's not used there yet (i.e. would count as dead code).

But in the current shape the linker script change in the first patch is unused. IOW, I think adding .bss..brk.page_aligned to the linker script should go together with DEFINE_BRK.

Again - could be done that way, but there's no concept of "dead code" in the linker script (afaik at least).

Anyway, I voiced my opinion, but it isn't important enough to block the patch on it. Either way:

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#2 lore raw

Re: [PATCH 1/3] x86: introduce "brk" allocator

In reply to Jan Beulich
Quoted 57 lines · Jan Beulich

... to replace ebmalloc(), and then to find further use(s) to allow recovering memory which is needed very early (and hence needs setting up statically), but may not fully be used (or not used at all).

Note that unlike free_ebmalloc_unused_mem(), brk_free_unused() (once other code is converted) will be able to free part of the BRK space even in the xen.efi case. That would happen if BRK space extends across a 2Mb boundary, and actual use stops before that boundary.

---
Changing setup.c's reserve_e820_ram() uses would be cumbersome when done right here. That'll be done when ebmalloc() is replaced, and hence what's there can also simply be replaced.

The xen.efi detection may want separating out into a helper.

When linking xen.efi, ld produces a base relocation for the reference to __subsystem__, which is wrong (that's an absolute symbol, after all). While that will need fixing there, it does no harm for our purposes.

xen/arch/x86/boot/Makefile +1 -0
@@ -1,4 +1,5 @@
 obj-bin-y += head.o
+obj-bin-y += brk.init.o
 obj-bin-y += built-in-32.o
 obj-bin-y += $(obj64)
 
xen/arch/x86/boot/brk.c new file +25 -0
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <xen/efi.h>
+#include <xen/lib.h>
+#include <xen/mm.h>
+#include <xen/page-defs.h>
+
+#include <asm/brk.h>
+
+extern char __brk_start[];
+extern const char __bss_end[];
+
+static unsigned long __initdata allocated;
+static bool __initdata finished;
+
+void *__init brk_alloc(size_t size)
+{
+    void *ptr = __brk_start + allocated;
+
+    if ( finished )
+        return NULL;
+
+    /* Allocations PAGE_SIZE and up will be page-aligned. */
+    if ( size >= PAGE_SIZE )
+        allocated = ROUNDUP(allocated, PAGE_SIZE);

xen/arch/x86/boot/brk.c line 35

Quoted 10 lines
xen/arch/x86/boot/brk.c new file +10 -0
+
+    allocated += ROUNDUP(size, sizeof(void *));
+
+    if ( allocated > __bss_end - __brk_start )
+        return NULL;
+
+    return ptr;
+}
+
+unsigned long __init brk_get_unused_start(void)

It's a bit unintuitive for brk_get_* to have this significant side effect. Maybe name it brk_finalize_get_unused_start() ?

Quoted 10 lines
xen/arch/x86/boot/brk.c new file +10 -0
+{
+    finished = true;
+
+    allocated = ROUNDUP(allocated, PAGE_SIZE);
+
+    return (unsigned long)__brk_start + allocated;
+}
+
+void __init brk_free_unused(void)
+{

xen/arch/x86/boot/brk.c line 55

Quoted 10 lines
xen/arch/x86/boot/brk.c new file +10 -0
+    unsigned long start = brk_get_unused_start(),
+                  end = (unsigned long)__bss_end;
+    unsigned int subsys;
+
+    /*
+     * Only xen.efi will have the symbol __subsystem__ available, and it'll
+     * be non-zero (10) there.  In ELF the symbol will be undefined, and
+     * hence zero will be loaded into the register.
+     */
+    asm ( ".weak __subsystem__; mov $__subsystem__, %0" : "=r" (subsys) );

Is this really the best way to detect xen.efi?

Quoted 63 lines
xen/arch/x86/boot/brk.c new file +17 -0
+
+    /* using_2M_mapping() isn't available here. */
+    if ( IS_ENABLED(CONFIG_XEN_ALIGN_2M) || subsys )
+        start = PAGE_ALIGN_2M(start);
+
+    if ( start >= end )
+        return;
+
+    destroy_xen_mappings(start, PAGE_ALIGN_2M(end));
+
+    /*
+     * By reserving needed space early in the E820 map, excess space gets freed
+     * way before we make it here. Don't free the range a 2nd time.
+     */
+
+    printk(XENLOG_INFO "Freed %lukB unused BRK memory\n", (end - start) >> 10);
+}
xen/arch/x86/include/asm/brk.h new file +7 -0
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <xen/types.h>
+
+void *brk_alloc(size_t size);
+unsigned long brk_get_unused_start(void);
+void brk_free_unused(void);
xen/arch/x86/xen.lds.S +5 -1
@@ -331,7 +331,11 @@ SECTIONS
        __bss_start = .;
        *(.bss.page_aligned*)
        PERCPU_BSS
-       *(.bss .bss.*)
+       *(.bss .bss.[a-zA-Z0-9_]*)
+       . = ALIGN(PAGE_SIZE);
+       __brk_start = .;
+       *(.bss..brk.page_aligned*)
+       *(.bss..brk*)
        . = ALIGN(POINTER_ALIGN);
        __bss_end = .;
   } PHDR(text)
xen/arch/x86/mm.c +3 -0
@@ -112,6 +112,7 @@
 #include <xen/vmap.h>
 #include <xen/xmalloc.h>
 
+#include <asm/brk.h>
 #include <asm/e820.h>
 #include <asm/fixmap.h>
 #include <asm/flushtlb.h>
@@ -337,6 +338,8 @@ void __init arch_init_memory(void)
 
     efi_init_memory();
 
+    brk_free_unused();
+
 #ifndef NDEBUG
     if ( highmem_start )
     {
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#3 lore raw

Re: [PATCH 1/3] x86: introduce "brk" allocator

Quoted 34 lines · Marek Marczykowski-Górecki
Quoted 33 lines · Jan Beulich
xen/arch/x86/boot/brk.c new file +30 -0
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <xen/efi.h>
+#include <xen/lib.h>
+#include <xen/mm.h>
+#include <xen/page-defs.h>
+
+#include <asm/brk.h>
+
+extern char __brk_start[];
+extern const char __bss_end[];
+
+static unsigned long __initdata allocated;
+static bool __initdata finished;
+
+void *__init brk_alloc(size_t size)
+{
+    void *ptr = __brk_start + allocated;
+
+    if ( finished )
+        return NULL;
+
+    /* Allocations PAGE_SIZE and up will be page-aligned. */
+    if ( size >= PAGE_SIZE )
+        allocated = ROUNDUP(allocated, PAGE_SIZE);
+
+    allocated += ROUNDUP(size, sizeof(void *));
+
+    if ( allocated > __bss_end - __brk_start )
+        return NULL;
Quoted 8 lines

xen/arch/x86/boot/brk.c line 35

Quoted 5 lines
xen/arch/x86/boot/brk.c new file +5 -0
+
+    return ptr;
+}
+
+unsigned long __init brk_get_unused_start(void)

It's a bit unintuitive for brk_get_* to have this significant side effect. Maybe name it brk_finalize_get_unused_start() ?

Getting too long for my taste, and a caller obtaining this value kind of needs to understand that either what it gets back is stale the moment it uses the result, or (for it to not be stale) no further changes (i.e. allocations) are permitted afterwards.

Quoted 14 lines
Quoted 14 lines
xen/arch/x86/boot/brk.c new file +14 -0
+{
+    finished = true;
+
+    allocated = ROUNDUP(allocated, PAGE_SIZE);
+
+    return (unsigned long)__brk_start + allocated;
+}
+
+void __init brk_free_unused(void)
+{
+    unsigned long start = brk_get_unused_start(),
+                  end = (unsigned long)__bss_end;
+    unsigned int subsys;
+
Quoted 8 lines

xen/arch/x86/boot/brk.c line 55

Quoted 6 lines
xen/arch/x86/boot/brk.c new file +6 -0
+    /*
+     * Only xen.efi will have the symbol __subsystem__ available, and it'll
+     * be non-zero (10) there.  In ELF the symbol will be undefined, and
+     * hence zero will be loaded into the register.
+     */
+    asm ( ".weak __subsystem__; mov $__subsystem__, %0" : "=r" (subsys) );

Is this really the best way to detect xen.efi?

Well, it took me a while to figure _some_ reasonably reliable way. I'm all ears towards better approaches.

Jan