When EFI_UNACCEPTED_MEMORY is present in the EFI memory map or the
platform supports dynamic memory unacceptance (such as AMD SEV-SNP),
start the EFI unaccepted memory bitmap at physical address 0
(phys_base = 0) instead of the lowest initial unaccepted address.

This ensures that pre-accepted low memory below the initial unaccepted
region is addressable in the bitmap so it can be dynamically marked
unaccepted and re-accepted later via virtio-balloon or free page
reporting. Non-confidential guests without EFI_UNACCEPTED_MEMORY
continue to skip allocating the table.

Signed-off-by: Kevin Loughlin <[email protected]>
---
 .../firmware/efi/libstub/unaccepted_memory.c  | 45 ++++++++++++++-----
 1 file changed, 33 insertions(+), 12 deletions(-)

diff --git a/drivers/firmware/efi/libstub/unaccepted_memory.c 
b/drivers/firmware/efi/libstub/unaccepted_memory.c
index 757dbe734a47..fcb622ac0564 100644
--- a/drivers/firmware/efi/libstub/unaccepted_memory.c
+++ b/drivers/firmware/efi/libstub/unaccepted_memory.c
@@ -2,15 +2,28 @@
 
 #include <linux/efi.h>
 #include <asm/efi.h>
+#if defined(CONFIG_X86) && defined(CONFIG_AMD_MEM_ENCRYPT)
+#include <asm/sev.h>
+#endif
 #include "efistub.h"
 
 struct efi_unaccepted_memory *unaccepted_table;
 
+static bool efi_stub_has_dynamic_unaccepted_memory(void)
+{
+#if defined(CONFIG_X86) && defined(CONFIG_AMD_MEM_ENCRYPT)
+       return sev_get_status() & MSR_AMD64_SEV_SNP_ENABLED;
+#else
+       return false;
+#endif
+}
+
 efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc,
                                        struct efi_boot_memmap *map)
 {
        efi_guid_t unaccepted_table_guid = LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID;
-       u64 unaccepted_start = ULLONG_MAX, unaccepted_end = 0, bitmap_size;
+       bool has_unaccepted = efi_stub_has_dynamic_unaccepted_memory();
+       u64 unaccepted_end = 0, bitmap_size;
        efi_status_t status;
        int i;
 
@@ -30,19 +43,27 @@ efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc,
                unsigned long m = (unsigned long)map->map;
 
                d = efi_memdesc_ptr(m, map->desc_size, i);
-               if (d->type != EFI_UNACCEPTED_MEMORY)
-                       continue;
-
-               unaccepted_start = min(unaccepted_start, d->phys_addr);
-               unaccepted_end = max(unaccepted_end,
-                                    d->phys_addr + d->num_pages * PAGE_SIZE);
+               switch (d->type) {
+               case EFI_UNACCEPTED_MEMORY:
+                       has_unaccepted = true;
+                       fallthrough;
+               case EFI_LOADER_CODE:
+               case EFI_LOADER_DATA:
+               case EFI_BOOT_SERVICES_CODE:
+               case EFI_BOOT_SERVICES_DATA:
+               case EFI_CONVENTIONAL_MEMORY:
+                       unaccepted_end = max(unaccepted_end,
+                                            d->phys_addr +
+                                            d->num_pages * EFI_PAGE_SIZE);
+                       break;
+               default:
+                       break;
+               }
        }
 
-       if (unaccepted_start == ULLONG_MAX)
+       if (!has_unaccepted || unaccepted_end == 0)
                return EFI_SUCCESS;
 
-       unaccepted_start = round_down(unaccepted_start,
-                                     EFI_UNACCEPTED_UNIT_SIZE);
        unaccepted_end = round_up(unaccepted_end, EFI_UNACCEPTED_UNIT_SIZE);
 
        /*
@@ -59,7 +80,7 @@ efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc,
         * The bitmap will be populated in setup_e820() according to the memory
         * map after efi_exit_boot_services().
         */
-       bitmap_size = DIV_ROUND_UP(unaccepted_end - unaccepted_start,
+       bitmap_size = DIV_ROUND_UP(unaccepted_end,
                                   EFI_UNACCEPTED_UNIT_SIZE * BITS_PER_BYTE);
 
        status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
@@ -72,7 +93,7 @@ efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc,
 
        unaccepted_table->version = 1;
        unaccepted_table->unit_size = EFI_UNACCEPTED_UNIT_SIZE;
-       unaccepted_table->phys_base = unaccepted_start;
+       unaccepted_table->phys_base = 0;
        unaccepted_table->size = bitmap_size;
        memset(unaccepted_table->bitmap, 0, bitmap_size);
 
-- 
2.56.0.307.g5fe7bd680c-goog


Reply via email to