Skip to contentSkip to conversation

"brk" allocator

PATCH In review Jan Beulich 2 participants 9 messages Nov 25 – Nov 27

lore
Share & tools
lore
#0 lore raw

v5 cover "brk" allocator

v5 addresses (hopefully) the final bits of feedback that kept trickling in.

1: build: permit .data.ro_after_init in *.init.o
2: common: introduce brk_end
3: x86/EFI: replace ebmalloc()

Jan

#1 lore raw

PATCH v5 1/3 build: permit .data.ro_after_init in *.init.o

Setting __ro_after_init variables is one of the purposes of __init code. Permit such data to be present in objects to be converted to *.init.o.

---
v5: New.

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.*|.data.ro_after_init) ;; \
         .text|.text.*|.data|.data.*|.bss|.bss.*) \
             test $$(echo $$sz | sed 's,00*,0,') != 0 || continue; \
             echo "Error: size of $<:$$name is 0x$$sz" >&2; \
Acked-by on v5 1/3
#2 lore raw

Re: [PATCH v5 1/3] build: permit .data.ro_after_init in *.init.o

Quoted 4 lines · Jan Beulich

Setting __ro_after_init variables is one of the purposes of __init code. Permit such data to be present in objects to be converted to *.init.o.

xen/Rules.mk quiet_cmd_obj_init_o = INIT_O $@ · line 266

Quoted 11 lines

---
v5: New.

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.*|.data.ro_after_init) ;; \

... won't we want .data.ro_after_init.* eventually when we get --gc-sections fully working?

~Andrew

Quoted 4 lines
xen/Rules.mk +0 -0
         .text|.text.*|.data|.data.*|.bss|.bss.*) \
             test $$(echo $$sz | sed 's,00*,0,') != 0 || continue; \
             echo "Error: size of $<:$$name is 0x$$sz" >&2; \
#3 lore raw

Re: [PATCH v5 1/3] build: permit .data.ro_after_init in *.init.o

Quoted 7 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

Setting __ro_after_init variables is one of the purposes of __init code. Permit such data to be present in objects to be converted to *.init.o.

Thanks.

Quoted 14 lines

xen/Rules.mk quiet_cmd_obj_init_o = INIT_O $@ · line 266

Quoted 11 lines

---
v5: New.

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.*|.data.ro_after_init) ;; \

... won't we want .data.ro_after_init.* eventually when we get --gc-sections fully working?

Likely, yet many more changes are going to be needed for that to work. .init.data.* and .init.text.*, for example, also require dealing with afaics.

Jan

#4 lore raw

PATCH v5 2/3 common: introduce brk_end

In reply to Jan Beulich

It is anticipated that all architectures meaning to use the BRK allocator will have a need for this variable. With this there's then also no need for brk_cease() to have a return value; it simply sets the variable.

---
Looks like "finished" could then be dropped.
---
v5: New.

xen/common/brk.c +8 -4
@@ -18,6 +18,8 @@ static inline unsigned long brk_adjust_s
 extern char __brk_start[];
 extern const char __bss_end[];
 
+unsigned long __ro_after_init brk_end;
+
 static unsigned long __initdata allocated;
 static bool __initdata finished;
 
@@ -52,20 +54,22 @@ void *__init brk_alloc(size_t size)
     return ptr;
 }
 
-unsigned long __init brk_cease(void)
+void __init brk_cease(void)
 {
     finished = true;
 
     allocated = ROUNDUP(allocated, PAGE_SIZE);
 
-    return (unsigned long)__brk_start + allocated;
+    brk_end = (unsigned long)__brk_start + allocated;
 }
 
 void __init brk_free_unused(void)
 {
-    unsigned long start = brk_cease(), end = (unsigned long)__bss_end;
+    unsigned long start, end = (unsigned long)__bss_end;
+
+    brk_cease();
 
-    start = brk_adjust_start(start);
+    start = brk_adjust_start(brk_end);
 
     if ( start >= end )
         return;
xen/include/xen/brk.h +3 -1
@@ -24,8 +24,10 @@
     static char __section(".bss..brk.page_aligned") __aligned(PAGE_SIZE) \
         __used var ## _brk_[size]
 
+extern unsigned long brk_end;
+
 void *brk_alloc(size_t size);
-unsigned long brk_cease(void);
+void brk_cease(void);
 void brk_free_unused(void);
 
 #endif /* XEN_BRK_H */
Reviewed-by on v5 2/3
#5 lore raw

Re: [PATCH v5 2/3] common: introduce brk_end

Quoted 6 lines · Jan Beulich

It is anticipated that all architectures meaning to use the BRK allocator will have a need for this variable. With this there's then also no need for brk_cease() to have a return value; it simply sets the variable.

Quoted 2 lines

---
Looks like "finished" could then be dropped.

Yeah.  Probably best to fold into this patch.

Quoted 44 lines

---
v5: New.

xen/common/brk.c +8 -4
@@ -18,6 +18,8 @@ static inline unsigned long brk_adjust_s
 extern char __brk_start[];
 extern const char __bss_end[];
 
+unsigned long __ro_after_init brk_end;
+
 static unsigned long __initdata allocated;
 static bool __initdata finished;
 
@@ -52,20 +54,22 @@ void *__init brk_alloc(size_t size)
     return ptr;
 }
 
-unsigned long __init brk_cease(void)
+void __init brk_cease(void)
 {
     finished = true;
 
     allocated = ROUNDUP(allocated, PAGE_SIZE);
 
-    return (unsigned long)__brk_start + allocated;
+    brk_end = (unsigned long)__brk_start + allocated;
 }
 
 void __init brk_free_unused(void)
 {
-    unsigned long start = brk_cease(), end = (unsigned long)__bss_end;
+    unsigned long start, end = (unsigned long)__bss_end;
+
+    brk_cease();
 
-    start = brk_adjust_start(start);
+    start = brk_adjust_start(brk_end);
 
     if ( start >= end )
         return;

xen/include/xen/brk.h line 26

Quoted 3 lines
xen/include/xen/brk.h +0 -0
@@ -24,8 +24,10 @@
     static char __section(".bss..brk.page_aligned") __aligned(PAGE_SIZE) \
         __used var ## _brk_[size]
 

/* Set by brk_cease(), and becomes a permanent extension to the Xen image. */

or words to this effect.

~Andrew

Quoted 9 lines
xen/include/xen/brk.h +3 -1
+extern unsigned long brk_end;
+
 void *brk_alloc(size_t size);
-unsigned long brk_cease(void);
+void brk_cease(void);
 void brk_free_unused(void);
 
 #endif /* XEN_BRK_H */
#6 lore raw

PATCH v5 3/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.
---
v5: Re-base over changes earlier in the series.
v4: Also adjust is_xen_fixed_mfn().
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.*|.data.ro_after_init) ;; \
+        .*.local|.data.rel.ro|.data.rel.ro.*|.data.ro_after_init|.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/mm.h +1 -1
@@ -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 */
 
xen/arch/x86/setup.c +11 -36

84 lines not on this page · raw message

xen/arch/x86/tboot.c +3 -12
@@ -1,4 +1,5 @@
 #include <xen/acpi.h>
+#include <xen/brk.h>
 #include <xen/domain_page.h>
 #include <xen/efi.h>
 #include <xen/init.h>
@@ -321,8 +322,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 +335,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/common/page_alloc.c +1 -0
@@ -120,6 +120,7 @@
  *   regions within it.
  */
 
+#include <xen/brk.h>
 #include <xen/domain_page.h>
 #include <xen/event.h>
 #include <xen/init.h>
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
#8 lore raw

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

In reply to Jan Beulich
Quoted 5 lines · Jan Beulich

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

From AI review, two issues:

  1. brk_end loses its 2M rounding (18b2e1fc6e "x86/EFI: replace ebmalloc()", together with 82cab821d0 "common: introduce brk_end")

  The order of events in __start_xen():
  1. setup.c:1671 calls brk_cease(), then rounds up with brk_end = PAGE_ALIGN_2M(brk_end) when 2M mappings are in use, which is always the case for xen.efi. The E820 reservation uses that rounded value.
  2. Later, arch_init_memory() (line 2102) calls brk_free_unused(). That calls brk_cease() again, which unconditionally sets brk_end = __brk_start + allocated, discarding the rounding.
  3. Everything after that sees the unrounded value:
     - remove_xen_ranges(), which runs during create_dom0() at line 2211;
     - is_xen_fixed_mfn();
     - the S3 integrity-measurement (MAC) region size in tboot_shutdown().

  The range [brk_end, PAGE_ALIGN_2M(brk_end)) is then reserved in E820 and still mapped by Xen's 2M mapping, but it isn't treated as Xen memory:
  - With dom0-iommu=map-inclusive or map-reserved, the IOMMU set-up in iommu.c:486 relies on remove_xen_ranges() to cut Xen out of dom0's mappings. These pages would stay mapped for dom0's devices.
  - Page offlining wouldn't refuse them as Xen pages.

  The pages are unused brk space and padding, so the impact is small, but it breaks the "permanent bound of the Xen image" promise in brk.h. A possible fix is to make brk_cease() do nothing once brk_end is set, or to apply the
  2M rounding inside brk_cease() via brk_adjust_start() so there's a single place that computes the value.

  2. Stale MISRA entry (18b2e1fc6e):
docs/misra/function-macro-properties.json:499 still lists ^.*efi_boot_mem_unused.*$, but that function was removed. Nothing in automation/eclair_analysis or the other docs/misra files mentions ebmalloc or efi_boot_mem_unused.

~Andrew