[PATCH v2 1/3] x86/boot: Move accept_memory() into decompressor

From: Ard Biesheuvel
Date: Fri Apr 04 2025 - 04:30:41 EST


From: Ard Biesheuvel <ardb@xxxxxxxxxx>

accept_memory() is only called from the decompressor, and uses an API
that will become specific to the decompressor as well, given that the
EFI stub will need to switch to a special memory acceptance API that can
be called while running in the firmware context with the firmware's page
tables.

So move the function into arch/x86/boot/compressed/mem.c

Signed-off-by: Ard Biesheuvel <ardb@xxxxxxxxxx>
---
arch/x86/boot/compressed/mem.c | 45 ++++++++++++++++++++
drivers/firmware/efi/libstub/unaccepted_memory.c | 45 --------------------
2 files changed, 45 insertions(+), 45 deletions(-)

diff --git a/arch/x86/boot/compressed/mem.c b/arch/x86/boot/compressed/mem.c
index dbba332e4a12..6a888b80669e 100644
--- a/arch/x86/boot/compressed/mem.c
+++ b/arch/x86/boot/compressed/mem.c
@@ -84,3 +84,48 @@ bool init_unaccepted_memory(void)

return true;
}
+
+void accept_memory(phys_addr_t start, unsigned long size)
+{
+ unsigned long range_start, range_end;
+ phys_addr_t end = start + size;
+ unsigned long bitmap_size;
+ u64 unit_size;
+
+ if (!unaccepted_table)
+ return;
+
+ unit_size = unaccepted_table->unit_size;
+
+ /*
+ * Only care for the part of the range that is represented
+ * in the bitmap.
+ */
+ if (start < unaccepted_table->phys_base)
+ start = unaccepted_table->phys_base;
+ if (end < unaccepted_table->phys_base)
+ return;
+
+ /* Translate to offsets from the beginning of the bitmap */
+ start -= unaccepted_table->phys_base;
+ end -= unaccepted_table->phys_base;
+
+ /* Make sure not to overrun the bitmap */
+ if (end > unaccepted_table->size * unit_size * BITS_PER_BYTE)
+ end = unaccepted_table->size * unit_size * BITS_PER_BYTE;
+
+ range_start = start / unit_size;
+ bitmap_size = DIV_ROUND_UP(end, unit_size);
+
+ for_each_set_bitrange_from(range_start, range_end,
+ unaccepted_table->bitmap, bitmap_size) {
+ unsigned long phys_start, phys_end;
+
+ phys_start = range_start * unit_size + unaccepted_table->phys_base;
+ phys_end = range_end * unit_size + unaccepted_table->phys_base;
+
+ arch_accept_memory(phys_start, phys_end);
+ bitmap_clear(unaccepted_table->bitmap,
+ range_start, range_end - range_start);
+ }
+}
diff --git a/drivers/firmware/efi/libstub/unaccepted_memory.c b/drivers/firmware/efi/libstub/unaccepted_memory.c
index 757dbe734a47..02040bd6a330 100644
--- a/drivers/firmware/efi/libstub/unaccepted_memory.c
+++ b/drivers/firmware/efi/libstub/unaccepted_memory.c
@@ -176,48 +176,3 @@ void process_unaccepted_memory(u64 start, u64 end)
bitmap_set(unaccepted_table->bitmap,
start / unit_size, (end - start) / unit_size);
}
-
-void accept_memory(phys_addr_t start, unsigned long size)
-{
- unsigned long range_start, range_end;
- phys_addr_t end = start + size;
- unsigned long bitmap_size;
- u64 unit_size;
-
- if (!unaccepted_table)
- return;
-
- unit_size = unaccepted_table->unit_size;
-
- /*
- * Only care for the part of the range that is represented
- * in the bitmap.
- */
- if (start < unaccepted_table->phys_base)
- start = unaccepted_table->phys_base;
- if (end < unaccepted_table->phys_base)
- return;
-
- /* Translate to offsets from the beginning of the bitmap */
- start -= unaccepted_table->phys_base;
- end -= unaccepted_table->phys_base;
-
- /* Make sure not to overrun the bitmap */
- if (end > unaccepted_table->size * unit_size * BITS_PER_BYTE)
- end = unaccepted_table->size * unit_size * BITS_PER_BYTE;
-
- range_start = start / unit_size;
- bitmap_size = DIV_ROUND_UP(end, unit_size);
-
- for_each_set_bitrange_from(range_start, range_end,
- unaccepted_table->bitmap, bitmap_size) {
- unsigned long phys_start, phys_end;
-
- phys_start = range_start * unit_size + unaccepted_table->phys_base;
- phys_end = range_end * unit_size + unaccepted_table->phys_base;
-
- arch_accept_memory(phys_start, phys_end);
- bitmap_clear(unaccepted_table->bitmap,
- range_start, range_end - range_start);
- }
-}
--
2.49.0.504.g3bcea36a83-goog