Skip to contentSkip to conversation

"brk" allocator

PATCH Superseded Jan Beulich 3 participants 7 messages Oct 14 – Nov 2

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover "brk" allocator

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

v3 is mainly for moving the logic to common code, so other arch-es can make use of it right from the start (if so desired).

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

On x86, 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 v3 1/3 common: introduce "brk" allocator

... to replace (on x86) 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 be 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 on x86. 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.

When linking xen.efi, GNU ld prior to 2.46 produces a base relocation for the reference to __subsystem__, which is wrong (that's an absolute symbol, after all). That does no harm for our purposes, though.
---
v3: Move to common code and introduce BSS_WITH_BRK. Rename
brk_get_unused_start() to brk_cease(). Add header guard to brk.h. In brk_alloc() don't calculate result too early. Add printk_once_init(), for use in brk_alloc(). Move introduction of DEFINE_BRK() here; add comment.

xen/arch/x86/Kconfig +1 -0
@@ -15,6 +15,7 @@ config X86
 	select FUNCTION_ALIGNMENT_16B
 	select GENERIC_BUG_FRAME
 	select HAS_ALTERNATIVE
+	select HAS_BRK_ALLOCATOR
 	select HAS_COMPAT
 	select HAS_CPUFREQ
 	select HAS_DIT
xen/arch/x86/include/asm/brk.h new file +23 -0
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef X86_BRK_H
+#define X86_BRK_H
+
+static inline unsigned long brk_adjust_start(unsigned long start)
+{
+    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) );
+
+    /* using_2M_mapping() isn't available here. */
+    if ( IS_ENABLED(CONFIG_XEN_ALIGN_2M) || subsys )
+        start = PAGE_ALIGN_2M(start);
+
+    return start;
+}
+
+#endif /* X86_BRK_H */
xen/arch/x86/mm.c +3 -0
@@ -87,6 +87,7 @@
  * doing the final put_page(), and remove it from the iommu if so.
  */
 
+#include <xen/brk.h>
 #include <xen/domain.h>
 #include <xen/domain_page.h>
 #include <xen/efi.h>
@@ -337,6 +338,8 @@ void __init arch_init_memory(void)
 
     efi_init_memory();
 
+    brk_free_unused();
+
 #ifndef NDEBUG
     if ( highmem_start )
     {
xen/arch/x86/xen.lds.S +1 -6
@@ -309,12 +309,7 @@ SECTIONS
   LLVM_COV_RW_DATA
 
   DECL_SECTION(.bss) {
-       __bss_start = .;
-       *(.bss.page_aligned*)
-       PERCPU_BSS
-       *(.bss .bss.*)
-       . = ALIGN(POINTER_ALIGN);
-       __bss_end = .;
+       BSS_WITH_BRK
   } PHDR(text)
   _end = . ;
 
xen/common/Kconfig +3 -0
@@ -118,6 +118,9 @@ config GENERIC_BUG_FRAME
 config HAS_ALTERNATIVE
 	bool
 
+config HAS_BRK_ALLOCATOR
+	bool
+
 config HAS_COMPAT
 	bool
 
xen/common/Makefile +1 -0
@@ -1,6 +1,7 @@
 obj-$(CONFIG_ARGO) += argo.o
 obj-y += bitmap.o
 obj-bin-$(CONFIG_SELF_TESTS) += bitops.init.o
+obj-bin-$(CONFIG_HAS_BRK_ALLOCATOR) += brk.init.o
 obj-$(CONFIG_GENERIC_BUG_FRAME) += bug.o
 obj-$(CONFIG_HYPFS_CONFIG) += config_data.o
 obj-$(CONFIG_CORE_PARKING) += core_parking.o
xen/common/brk.c new file +74 -0

74 lines not on this page · raw message

xen/include/xen/brk.h new file +31 -0
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Early Boot memory allocator.
+ *
+ * Subsystems which conditionally need memory prior to the boot allocator
+ * (or the main heap) being set up may use DEFINE_BRK() to reserve BSS space
+ * in Xen.
+ *
+ * During boot, brk_alloc() allocates memory from the reserved space.  Such
+ * allocations are good for the lifetime of Xen.  Subsystems MUST NOT
+ * brk_alloc() more memory than they reserved.
+ *
+ * When the main heap is set up, brk allocations become unavailable.  Reserved
+ * but unallocated space is, as far as possible, handed to the main heap, so it
+ * doesn't go to waste.
+ */
+
+#ifndef XEN_BRK_H
+#define XEN_BRK_H
+
+#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_cease(void);
+void brk_free_unused(void);
+
+#endif /* XEN_BRK_H */
xen/include/xen/lib.h +10 -0
@@ -79,6 +79,16 @@ void vprintk(const char *fmt, va_list ar
     }                                           \
 })
 
+#define printk_once_init(fmt, args...)          \
+({                                              \
+    static bool __initdata once_;               \
+    if ( unlikely(!once_) )                     \
+    {                                           \
+        once_ = true;                           \
+        printk(fmt, ## args);                   \
+    }                                           \
+})
+
 extern void guest_printk(const struct domain *d, const char *fmt, ...)
     __attribute__ ((format (printf, 2, 3)));
 extern void noreturn panic(const char *fmt, ...)
xen/include/xen/xen.lds.h +12 -0
@@ -221,6 +221,18 @@
        . = ALIGN(SMP_CACHE_BYTES); \
        __per_cpu_data_end = .;     \
 
+#define BSS_WITH_BRK               \
+       __bss_start = .;            \
+       *(.bss.page_aligned*)       \
+       PERCPU_BSS                  \
+       *(.bss .bss.[a-zA-Z0-9_]*)  \
+       . = ALIGN(PAGE_SIZE);       \
+       __brk_start = .;            \
+       *(.bss..brk.page_aligned*)  \
+       *(.bss..brk*)               \
+       . = ALIGN(POINTER_ALIGN);   \
+       __bss_end = .;
+
 #ifdef CONFIG_HAS_VPCI
 #define VPCI_ARRAY               \
        . = ALIGN(POINTER_ALIGN); \
#2 lore raw

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

Quoted 47 lines · Jan Beulich

... to replace (on x86) 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 be 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 on x86. 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.

When linking xen.efi, GNU ld prior to 2.46 produces a base relocation for the reference to __subsystem__, which is wrong (that's an absolute symbol, after all). That does no harm for our purposes, though.
---
v3: Move to common code and introduce BSS_WITH_BRK. Rename
brk_get_unused_start() to brk_cease(). Add header guard to brk.h. In brk_alloc() don't calculate result too early. Add printk_once_init(), for use in brk_alloc(). Move introduction of DEFINE_BRK() here; add comment.

xen/arch/x86/Kconfig +1 -0
@@ -15,6 +15,7 @@ config X86
        select FUNCTION_ALIGNMENT_16B
        select GENERIC_BUG_FRAME
        select HAS_ALTERNATIVE
+       select HAS_BRK_ALLOCATOR
        select HAS_COMPAT
        select HAS_CPUFREQ
        select HAS_DIT
xen/arch/x86/include/asm/brk.h new file +8 -0
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef X86_BRK_H
+#define X86_BRK_H
+
+static inline unsigned long brk_adjust_start(unsigned long start)
+{
+    unsigned int subsys;
+

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

Quoted 10 lines
xen/arch/x86/include/asm/brk.h new file +10 -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) );
+
+    /* using_2M_mapping() isn't available here. */
+    if ( IS_ENABLED(CONFIG_XEN_ALIGN_2M) || subsys )
+        start = PAGE_ALIGN_2M(start);

This seems a bit fragile: As it happens this corresponds to the clip to brk_end in setup.c in patch 2, but if it didn't, then the boundary in the resulting e820 reservation and what gets unmapped in brk_free_unsed() wouldn't match.

Claude seems to think you could make using_2M_mapping() available here; and then brk_adjust_start() becomes return using_2M_mapping() ? PAGE_ALIGN_2M(start) : start. That drops the ASM, and the comment about the linker producing a bogus relocation. 2/3 could then do brk_end = brk_adjust_start(brk_cease()), and guaranteeing that the two are the same.

Quoted 34 lines
xen/common/brk.c new file +31 -0
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <xen/brk.h>
+#include <xen/efi.h>
+#include <xen/lib.h>
+#include <xen/mm.h>
+#include <xen/page-defs.h>
+
+#if __has_include(<asm/brk.h>)
+# include <asm/brk.h>
+#endif
+
+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;
+
+    if ( finished )
+        return NULL;
+
+    /* Allocations PAGE_SIZE and up will be page-aligned. */
+    if ( size >= PAGE_SIZE )
+        allocated = ROUNDUP(allocated, PAGE_SIZE);
+
+    ptr = __brk_start + allocated;
+

xen/common/brk.c line 41

Quoted 10 lines
xen/common/brk.c new file +10 -0
+    allocated += ROUNDUP(size, sizeof(void *));
+
+    if ( allocated > __bss_end - __brk_start )
+    {
+        printk_once_init(
+            "%pS: over-sized BRK request: %zu bytes\n",
+            __builtin_extract_return_addr(__builtin_return_address(0)),
+            size);
+        return NULL;
+    }

You bump the global allocated variable here before the check, and
don't reset it afterwards. All future allocation attempts will
continue to bump this value, and
continue to fail. So now allocated + brk_start is past _bbs_end, brk_cease() will return something past bss_end, and (in patch 2) pass that as reserve_e820_ram(). So now, so far from reducing the size reserved to Xen by , we may be *increasing* the size reserved to Xen with unused pages. Worse, if it ends up overshooting the region Xen is in, then reserve_e820_ram() will fail, and the host will panic.

This should probably use a local variable, then set the global variable after we've determined that the check has succeeded.

The other thing Claude points out is that is_xen_fixed_mfn() isn't updated, so if ever any of these pages want to be offlined for any reason, it will be blocked, claiming that they're part of Xen, even if they've already been assigned to a guest. This is a pre-existing situation that is extended by this patch.

-George

#3 lore raw

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

Quoted 28 lines · George Dunlap
Quoted 11 lines · Jan Beulich
xen/arch/x86/include/asm/brk.h new file +8 -0
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef X86_BRK_H
+#define X86_BRK_H
+
+static inline unsigned long brk_adjust_start(unsigned long start)
+{
+    unsigned int subsys;
+

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

Quoted 10 lines
xen/arch/x86/include/asm/brk.h new file +10 -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) );
+
+    /* using_2M_mapping() isn't available here. */
+    if ( IS_ENABLED(CONFIG_XEN_ALIGN_2M) || subsys )
+        start = PAGE_ALIGN_2M(start);

This seems a bit fragile: As it happens this corresponds to the clip to brk_end in setup.c in patch 2, but if it didn't, then the boundary in the resulting e820 reservation and what gets unmapped in brk_free_unsed() wouldn't match.

Quoted 6 lines

Claude seems to think you could make using_2M_mapping() available here; and then brk_adjust_start() becomes return using_2M_mapping() ? PAGE_ALIGN_2M(start) : start. That drops the ASM, and the comment about the linker producing a bogus relocation. 2/3 could then do brk_end = brk_adjust_start(brk_cease()), and guaranteeing that the two are the same.

I don't think exposing using_2M_mapping() globally would be a good idea.

Quoted 55 lines
Quoted 34 lines
xen/common/brk.c new file +31 -0
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <xen/brk.h>
+#include <xen/efi.h>
+#include <xen/lib.h>
+#include <xen/mm.h>
+#include <xen/page-defs.h>
+
+#if __has_include(<asm/brk.h>)
+# include <asm/brk.h>
+#endif
+
+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;
+
+    if ( finished )
+        return NULL;
+
+    /* Allocations PAGE_SIZE and up will be page-aligned. */
+    if ( size >= PAGE_SIZE )
+        allocated = ROUNDUP(allocated, PAGE_SIZE);
+
+    ptr = __brk_start + allocated;
+

xen/common/brk.c line 41

Quoted 10 lines
xen/common/brk.c new file +10 -0
+    allocated += ROUNDUP(size, sizeof(void *));
+
+    if ( allocated > __bss_end - __brk_start )
+    {
+        printk_once_init(
+            "%pS: over-sized BRK request: %zu bytes\n",
+            __builtin_extract_return_addr(__builtin_return_address(0)),
+            size);
+        return NULL;
+    }

You bump the global allocated variable here before the check, and
don't reset it afterwards. All future allocation attempts will
continue to bump this value, and
continue to fail. So now allocated + brk_start is past _bbs_end, brk_cease() will return something past bss_end, and (in patch 2) pass that as reserve_e820_ram(). So now, so far from reducing the size reserved to Xen by , we may be *increasing* the size reserved to Xen with unused pages. Worse, if it ends up overshooting the region Xen is in, then reserve_e820_ram() will fail, and the host will panic.

Quoted 2 lines

This should probably use a local variable, then set the global variable after we've determined that the check has succeeded.

Could be done, but did Claude also realize that failure to set aside enough space so allocations can't fail is, effectively, a fatal bug? The printk_once_init() isn't a panic() here merely because panic() isn't going to work very well when still in EFI boot services: We shouldn't halt the system there, we should only yield control back to EFI (such that another boot attempt can be made). See patch 2.

Quoted 5 lines

The other thing Claude points out is that is_xen_fixed_mfn() isn't updated, so if ever any of these pages want to be offlined for any reason, it will be blocked, claiming that they're part of Xen, even if they've already been assigned to a guest. This is a pre-existing situation that is extended by this patch.

This indeed needs taking care of, albeit in patch 2. It's not clear to me though why you (they?) say this would be a pre-existing situation. The use of __2M_rwdata_end there looks correct to me prior to this series.

Jan

Reviewed-by on v3 1/3
#4 lore raw

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

In reply to Jan Beulich
Quoted 26 lines · Jan Beulich

... to replace (on x86) 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 be 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 on x86. 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.

When linking xen.efi, GNU ld prior to 2.46 produces a base relocation for the reference to __subsystem__, which is wrong (that's an absolute symbol, after all). That does no harm for our purposes, though.

xen/arch/x86/include/asm/brk.h new file +4 -0
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef X86_BRK_H
+#define X86_BRK_H
+

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

Quoted 10 lines
xen/arch/x86/include/asm/brk.h new file +10 -0
+static inline unsigned long brk_adjust_start(unsigned long start)
+{
+    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) );

This seems like an indirect way of determining if xen.efi is running. Your comment explains this, so it's ok. It seems to me that an alternative would be to add an explicit symbol to the xen.efi linking instead of this implicit one. Though that would just be replacing __subsystem__ with something else, so maybe it's not worth it.

Regards,
Jason

#5 lore raw

PATCH v3 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.
---
v3: Re-base, in particular over moving of brk.c to common/.
v2: Re-base.

xen/Rules.mk +1 -1
@@ -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; \
xen/arch/x86/efi/efi-boot.h +13 -0
@@ -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;
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/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 +13 -36

92 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
@@ -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;
 
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);
 unsigned long efi_get_time(void);
 void efi_reset_system(bool warm);
 #ifndef COMPAT
#6 lore raw

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

---
v3: Re-base over moving of brk.c to common/.

xen/drivers/char/xhci-dbc.c +23 -13
@@ -19,6 +19,7 @@
  * Copyright (c) 2019 Assured Information Security.
  */
 
+#include <xen/brk.h>
 #include <xen/delay.h>
 #include <xen/iommu.h>
 #include <xen/mm.h>
@@ -27,6 +28,7 @@
 #include <xen/serial.h>
 #include <xen/timer.h>
 #include <xen/types.h>
+
 #include <asm/fixmap.h>
 #include <asm/io.h>
 #include <asm/string.h>
@@ -1332,7 +1334,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];
@@ -1346,8 +1348,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)
 {
@@ -1424,24 +1425,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);