Skip to content

Commit a799c2b

Browse files
rpptsuryasaimadhu
authored andcommitted
x86/setup: Consolidate early memory reservations
The early reservations of memory areas used by the firmware, bootloader, kernel text and data are spread over setup_arch(). Moreover, some of them happen *after* memblock allocations, e.g trim_platform_memory_ranges() and trim_low_memory_range() are called after reserve_real_mode() that allocates memory. There was no corruption of these memory regions because memblock always allocates memory either from the end of memory (in top-down mode) or above the kernel image (in bottom-up mode). However, the bottom up mode is going to be updated to span the entire memory [1] to avoid limitations caused by KASLR. Consolidate early memory reservations in a dedicated function to improve robustness against future changes. Having the early reservations in one place also makes it clearer what memory must be reserved before memblock allocations are allowed. Signed-off-by: Mike Rapoport <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Baoquan He <[email protected]> Acked-by: Borislav Petkov <[email protected]> Acked-by: David Hildenbrand <[email protected]> Link: [1] https://lore.kernel.org/lkml/[email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent e14cfb3 commit a799c2b

File tree

1 file changed

+44
-48
lines changed

1 file changed

+44
-48
lines changed

arch/x86/kernel/setup.c

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -645,18 +645,6 @@ static void __init trim_snb_memory(void)
645645
}
646646
}
647647

648-
/*
649-
* Here we put platform-specific memory range workarounds, i.e.
650-
* memory known to be corrupt or otherwise in need to be reserved on
651-
* specific platforms.
652-
*
653-
* If this gets used more widely it could use a real dispatch mechanism.
654-
*/
655-
static void __init trim_platform_memory_ranges(void)
656-
{
657-
trim_snb_memory();
658-
}
659-
660648
static void __init trim_bios_range(void)
661649
{
662650
/*
@@ -729,7 +717,38 @@ static void __init trim_low_memory_range(void)
729717
{
730718
memblock_reserve(0, ALIGN(reserve_low, PAGE_SIZE));
731719
}
732-
720+
721+
static void __init early_reserve_memory(void)
722+
{
723+
/*
724+
* Reserve the memory occupied by the kernel between _text and
725+
* __end_of_kernel_reserve symbols. Any kernel sections after the
726+
* __end_of_kernel_reserve symbol must be explicitly reserved with a
727+
* separate memblock_reserve() or they will be discarded.
728+
*/
729+
memblock_reserve(__pa_symbol(_text),
730+
(unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
731+
732+
/*
733+
* Make sure page 0 is always reserved because on systems with
734+
* L1TF its contents can be leaked to user processes.
735+
*/
736+
memblock_reserve(0, PAGE_SIZE);
737+
738+
early_reserve_initrd();
739+
740+
if (efi_enabled(EFI_BOOT))
741+
efi_memblock_x86_reserve_range();
742+
743+
memblock_x86_reserve_range_setup_data();
744+
745+
reserve_ibft_region();
746+
reserve_bios_regions();
747+
748+
trim_snb_memory();
749+
trim_low_memory_range();
750+
}
751+
733752
/*
734753
* Dump out kernel offset information on panic.
735754
*/
@@ -764,29 +783,6 @@ dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
764783

765784
void __init setup_arch(char **cmdline_p)
766785
{
767-
/*
768-
* Reserve the memory occupied by the kernel between _text and
769-
* __end_of_kernel_reserve symbols. Any kernel sections after the
770-
* __end_of_kernel_reserve symbol must be explicitly reserved with a
771-
* separate memblock_reserve() or they will be discarded.
772-
*/
773-
memblock_reserve(__pa_symbol(_text),
774-
(unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
775-
776-
/*
777-
* Make sure page 0 is always reserved because on systems with
778-
* L1TF its contents can be leaked to user processes.
779-
*/
780-
memblock_reserve(0, PAGE_SIZE);
781-
782-
early_reserve_initrd();
783-
784-
/*
785-
* At this point everything still needed from the boot loader
786-
* or BIOS or kernel text should be early reserved or marked not
787-
* RAM in e820. All other memory is free game.
788-
*/
789-
790786
#ifdef CONFIG_X86_32
791787
memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
792788

@@ -910,8 +906,18 @@ void __init setup_arch(char **cmdline_p)
910906

911907
parse_early_param();
912908

913-
if (efi_enabled(EFI_BOOT))
914-
efi_memblock_x86_reserve_range();
909+
/*
910+
* Do some memory reservations *before* memory is added to
911+
* memblock, so memblock allocations won't overwrite it.
912+
* Do it after early param, so we could get (unlikely) panic from
913+
* serial.
914+
*
915+
* After this point everything still needed from the boot loader or
916+
* firmware or kernel text should be early reserved or marked not
917+
* RAM in e820. All other memory is free game.
918+
*/
919+
early_reserve_memory();
920+
915921
#ifdef CONFIG_MEMORY_HOTPLUG
916922
/*
917923
* Memory used by the kernel cannot be hot-removed because Linux
@@ -938,9 +944,6 @@ void __init setup_arch(char **cmdline_p)
938944

939945
x86_report_nx();
940946

941-
/* after early param, so could get panic from serial */
942-
memblock_x86_reserve_range_setup_data();
943-
944947
if (acpi_mps_check()) {
945948
#ifdef CONFIG_X86_LOCAL_APIC
946949
disable_apic = 1;
@@ -1032,8 +1035,6 @@ void __init setup_arch(char **cmdline_p)
10321035
*/
10331036
find_smp_config();
10341037

1035-
reserve_ibft_region();
1036-
10371038
early_alloc_pgt_buf();
10381039

10391040
/*
@@ -1054,8 +1055,6 @@ void __init setup_arch(char **cmdline_p)
10541055
*/
10551056
sev_setup_arch();
10561057

1057-
reserve_bios_regions();
1058-
10591058
efi_fake_memmap();
10601059
efi_find_mirror();
10611060
efi_esrt_init();
@@ -1081,9 +1080,6 @@ void __init setup_arch(char **cmdline_p)
10811080

10821081
reserve_real_mode();
10831082

1084-
trim_platform_memory_ranges();
1085-
trim_low_memory_range();
1086-
10871083
init_mem_mapping();
10881084

10891085
idt_setup_early_pf();

0 commit comments

Comments
 (0)