Skip to contentSkip to conversation

EFI: adjust cfg file buffer freeing

PATCH In review Jan Beulich 4 participants 7 messages Apr 22 – May 5

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 EFI: adjust cfg file buffer freeing

The boot services FreePages() needs passing the size. Since we allocated one more byte to put a trailing nul there, we also need to bump the size passed there. Make a small helper function to centralize this.

Note that there's no permanent memory leak because of the oversight: The allocation is done using EfiLoaderData, and all memory of that type is later reclaimed anyway.

Fixes df75f77092c1 ("EFI: avoid OOB config file reads")

---
This is an alternative proposal to
https://lists.xen.org/archives/html/xen-devel/2026-04/msg01044.html.

xen/common/efi/boot.c +13 -12
@@ -778,6 +778,16 @@ static void __init efi_relocate_esrt(EFI
  */
 #include "efi-boot.h"
 
+static void __init free_cfg(void)
+{
+    if ( !cfg.need_to_free )
+        return;
+
+    /* One extra byte was allocated to put a nul character there. */
+    efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size + 1));
+    cfg.need_to_free = false;
+}
+
 void __init noreturn blexit(const CHAR16 *str)
 {
     if ( str )
@@ -787,8 +797,7 @@ void __init noreturn blexit(const CHAR16
     if ( !efi_bs )
         efi_arch_halt();
 
-    if ( cfg.need_to_free )
-        efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size));
+    free_cfg();
     if ( kernel.need_to_free )
         efi_bs->FreePages(kernel.addr, PFN_UP(kernel.size));
     if ( ramdisk.need_to_free )
@@ -1557,11 +1566,7 @@ void EFIAPI __init noreturn efi_start(EF
             name.s = get_value(&cfg, "global", "chain");
             if ( !name.s )
                 break;
-            if ( cfg.need_to_free )
-            {
-                efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size));
-                cfg.need_to_free = false;
-            }
+            free_cfg();
             if ( !read_file(dir_handle, s2w(&name), &cfg, NULL) )
             {
                 PrintStr(L"Chained configuration file '");
@@ -1631,11 +1636,7 @@ void EFIAPI __init noreturn efi_start(EF
 
         efi_arch_cfg_file_late(loaded_image, dir_handle, section.s);
 
-        if ( cfg.need_to_free )
-        {
-            efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size));
-            cfg.need_to_free = false;
-        }
+        free_cfg();
 
         if ( dir_handle )
             dir_handle->Close(dir_handle);
#1 lore raw
Quoted 7 lines · Jan Beulich

The boot services FreePages() needs passing the size. Since we allocated one more byte to put a trailing nul there, we also need to bump the size passed there. Make a small helper function to centralize this.

Note that there's no permanent memory leak because of the oversight: The allocation is done using EfiLoaderData, and all memory of that type is later reclaimed anyway.

This depends on -mapbs at a minimum.

Quoted 7 lines
Fixes df75f77092c1 ("EFI: avoid OOB config file reads")

---
This is an alternative proposal to
https://lists.xen.org/archives/html/xen-devel/2026-04/msg01044.html.

One thing this patch does not do is fix the boundary passed to efi_arch_flush_dcache_area().

I find it hard to believe that cache maintenance is actually needed, but given it is present in the code currently it needs to stay correct.

Or, if it's not needed, it should be dropped in a separate patch.

~Andrew

#2 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich

The boot services FreePages() needs passing the size. Since we allocated one more byte to put a trailing nul there, we also need to bump the size passed there. Make a small helper function to centralize this.

Note that there's no permanent memory leak because of the oversight: The allocation is done using EfiLoaderData, and all memory of that type is later reclaimed anyway.

This depends on -mapbs at a minimum.

But that's affecting only EfiBootServices{Code,Data}, isn't it?

Quoted 9 lines
Quoted 6 lines
Fixes df75f77092c1 ("EFI: avoid OOB config file reads")

---
This is an alternative proposal to
https://lists.xen.org/archives/html/xen-devel/2026-04/msg01044.html.

One thing this patch does not do is fix the boundary passed to efi_arch_flush_dcache_area().

Deliberately so, and I commented to this effect in reply to Bernhard's patch. I do think ...

Quoted 4 lines

I find it hard to believe that cache maintenance is actually needed, but given it is present in the code currently it needs to stay correct.

Or, if it's not needed, it should be dropped in a separate patch.

... this is the way to go, but it'll need input from Arm folks (as requested there).

Jan

Reviewed-by
#3 lore raw
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich

The boot services FreePages() needs passing the size. Since we allocated one more byte to put a trailing nul there, we also need to bump the size passed there. Make a small helper function to centralize this.

Note that there's no permanent memory leak because of the oversight: The allocation is done using EfiLoaderData, and all memory of that type is later reclaimed anyway.

This depends on -mapbs at a minimum.

This memory is allocated allocated using EfiLoaderData and 'map_bs' is for EfiBootServicesCode and Data, so at least this case does not apply to it:

        case EfiBootServicesCode:
        case EfiBootServicesData:
            if ( map_bs )
            {
                type = E820_RESERVED;
                break;
            }
Quoted 1 line

But that's affecting only EfiBootServices{Code,Data}, isn't it?

As allocated using EfiLoaderData, after ExitBootServices, it will show up as EfiLoaderData, and will be processed when iterating over the memory areas during boot.

On x86, Xen uses efi_arch_process_memory_map() for this, which handles it like EfiConventionalMemory, where, if it has desc->Attribute & EFI_MEMORY_WB, (which it should be as it is regular RAM) it is set to type E820_RAM by Xen.

Unless merged with adjacent areas it would show in the E820 map as RAM. AFAICS, it would be reclaimed by Xen/x86 this way. On ARM, EfiLoaderData is added using meminfo_add_bank(), so it would be added as a memory bank.

Quoted 13 lines
Quoted 9 lines
Quoted 6 lines
Fixes df75f77092c1 ("EFI: avoid OOB config file reads")

---
This is an alternative proposal to
https://lists.xen.org/archives/html/xen-devel/2026-04/msg01044.html.

One thing this patch does not do is fix the boundary passed to efi_arch_flush_dcache_area().

Deliberately so, and I commented to this effect in reply to Bernhard's patch. I do think ...

Quoted 7 lines
Quoted 4 lines

I find it hard to believe that cache maintenance is actually needed, but given it is present in the code currently it needs to stay correct.

Or, if it's not needed, it should be dropped in a separate patch.

... this is the way to go, but it'll need input from Arm folks (as requested there).

If in doubt, you may consult the gitweb search (see below) for the function and take the safe route of not removing it.

Flushing the dcache may also be needed on CPUs with a split D/I-cache and when using special code or HW on the SoC or board(e.g. a DMA engine) or something else external to the dcache to access the loaded data.

Ordering efi_arch_flush_dcache_area() before re-dirtying the dcache looks counter-intuitive which is why I updated the order to match the logical flow of flushing the dcache after the last change.

Per the git history (see below), it was needed for copy_from_paddr() on ARM.

You might skip fixing up the cache flushing order. It was just a "while at it" to be safe.

As we don't use copy_from_paddr() or similar to read or copy the config. However, if copy_from_paddr() would be used for the config, at this point, the dcache flush would be a factor to be done right (according to the git history).

Moving the two calls to FreePages() for cfg is a good cleanup for me, so:

Bernhard

PS: Gitweb finds this commit for adding the function:

https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=0d6a3c755374f04f6dd25373da28291a8f35bede

efi: introduce efi_arch_flush_dcache_area

Objects loaded by FileHandle->Read need to be flushed from dcache, otherwise copy_from_paddr will read stale data when copying the kernel, causing a failure to boot.

Introduce efi_arch_flush_dcache_area and call it from read_file.

This commit introduces no functional changes on x86.

#4 lore raw
Quoted 10 lines · Bernhard Kaindl

Jan Beulich wrote:
PS: Gitweb finds this commit for adding the function:

https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=0d6a3c755374f04f6dd25373da28291a8f35bede

efi: introduce efi_arch_flush_dcache_area

Objects loaded by FileHandle->Read need to be flushed from dcache, otherwise copy_from_paddr will read stale data when copying the kernel, causing a failure to boot.

I.e. irrelevant to the cfg file buffer. So what we could do there to make things less questionable (as to ordering) is

    if ( file == &cfg )
        file->str[file->size] = 0;
    else
        efi_arch_flush_dcache_area(file->ptr, file->size);
Quoted 6 lines

Introduce efi_arch_flush_dcache_area and call it from read_file.

This commit introduces no functional changes on x86.

Stefano, any thoughts?

Jan

#5 lore raw

Ping: [PATCH] EFI: adjust cfg file buffer freeing

In reply to Jan Beulich
Quoted 11 lines · Jan Beulich

The boot services FreePages() needs passing the size. Since we allocated one more byte to put a trailing nul there, we also need to bump the size passed there. Make a small helper function to centralize this.

Note that there's no permanent memory leak because of the oversight: The allocation is done using EfiLoaderData, and all memory of that type is later reclaimed anyway.

Fixes df75f77092c1 ("EFI: avoid OOB config file reads")

Marek, Daniel?

Thanks, Jan

Quoted 59 lines

---
This is an alternative proposal to
https://lists.xen.org/archives/html/xen-devel/2026-04/msg01044.html.

xen/common/efi/boot.c +13 -12
@@ -778,6 +778,16 @@ static void __init efi_relocate_esrt(EFI
  */
 #include "efi-boot.h"
 
+static void __init free_cfg(void)
+{
+    if ( !cfg.need_to_free )
+        return;
+
+    /* One extra byte was allocated to put a nul character there. */
+    efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size + 1));
+    cfg.need_to_free = false;
+}
+
 void __init noreturn blexit(const CHAR16 *str)
 {
     if ( str )
@@ -787,8 +797,7 @@ void __init noreturn blexit(const CHAR16
     if ( !efi_bs )
         efi_arch_halt();
 
-    if ( cfg.need_to_free )
-        efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size));
+    free_cfg();
     if ( kernel.need_to_free )
         efi_bs->FreePages(kernel.addr, PFN_UP(kernel.size));
     if ( ramdisk.need_to_free )
@@ -1557,11 +1566,7 @@ void EFIAPI __init noreturn efi_start(EF
             name.s = get_value(&cfg, "global", "chain");
             if ( !name.s )
                 break;
-            if ( cfg.need_to_free )
-            {
-                efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size));
-                cfg.need_to_free = false;
-            }
+            free_cfg();
             if ( !read_file(dir_handle, s2w(&name), &cfg, NULL) )
             {
                 PrintStr(L"Chained configuration file '");
@@ -1631,11 +1636,7 @@ void EFIAPI __init noreturn efi_start(EF
 
         efi_arch_cfg_file_late(loaded_image, dir_handle, section.s);
 
-        if ( cfg.need_to_free )
-        {
-            efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size));
-            cfg.need_to_free = false;
-        }
+        free_cfg();
 
         if ( dir_handle )
             dir_handle->Close(dir_handle);
#6 lore raw

Re: Ping: [PATCH] EFI: adjust cfg file buffer freeing

Quoted 74 lines · Jan Beulich
Quoted 11 lines · Jan Beulich

The boot services FreePages() needs passing the size. Since we allocated one more byte to put a trailing nul there, we also need to bump the size passed there. Make a small helper function to centralize this.

Note that there's no permanent memory leak because of the oversight: The allocation is done using EfiLoaderData, and all memory of that type is later reclaimed anyway.

Fixes df75f77092c1 ("EFI: avoid OOB config file reads")

Marek, Daniel?

Thanks, Jan

Quoted 57 lines

---
This is an alternative proposal to
https://lists.xen.org/archives/html/xen-devel/2026-04/msg01044.html.

xen/common/efi/boot.c +13 -12
@@ -778,6 +778,16 @@ static void __init efi_relocate_esrt(EFI
   */
  #include "efi-boot.h"
 
+static void __init free_cfg(void)
+{
+    if ( !cfg.need_to_free )
+        return;
+
+    /* One extra byte was allocated to put a nul character there. */
+    efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size + 1));
+    cfg.need_to_free = false;
+}
+
  void __init noreturn blexit(const CHAR16 *str)
  {
      if ( str )
@@ -787,8 +797,7 @@ void __init noreturn blexit(const CHAR16
      if ( !efi_bs )
          efi_arch_halt();
 
-    if ( cfg.need_to_free )
-        efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size));
+    free_cfg();
      if ( kernel.need_to_free )
          efi_bs->FreePages(kernel.addr, PFN_UP(kernel.size));
      if ( ramdisk.need_to_free )
@@ -1557,11 +1566,7 @@ void EFIAPI __init noreturn efi_start(EF
              name.s = get_value(&cfg, "global", "chain");
              if ( !name.s )
                  break;
-            if ( cfg.need_to_free )
-            {
-                efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size));
-                cfg.need_to_free = false;
-            }
+            free_cfg();
              if ( !read_file(dir_handle, s2w(&name), &cfg, NULL) )
              {
                  PrintStr(L"Chained configuration file '");
@@ -1631,11 +1636,7 @@ void EFIAPI __init noreturn efi_start(EF
 
          efi_arch_cfg_file_late(loaded_image, dir_handle, section.s);
 
-        if ( cfg.need_to_free )
-        {
-            efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size));
-            cfg.need_to_free = false;
-        }
+        free_cfg();
 
Quoted 3 lines

xen/common/efi/boot.c void EFIAPI __init noreturn efi_start(EF · line 1642

Quoted 2 lines
xen/common/efi/boot.c +0 -0
          if ( dir_handle )
              dir_handle->Close(dir_handle);