EFI memmap fix
Borislav Petkov
maintainercover EFI memmap fix
From: Borislav Petkov <[email protected]>
Hi guys,
this is the result of Toshi and me debugging a #GP on one of his big HP boxes sporting UEFI. Each commit message should be self-explanatory so please look there.
This has more or less an RFC nature thus I'm sending it now to collect feedback. It is going to wait in the EFI queue anyway after the kexec stuff gets sorted out first.
Comments and suggestions as always are very much appreciated.
Thanks.
Borislav Petkov (3):
x86, ptdump: Add the functionality to dump an arbitrary pagetable
efi: Dump the EFI page table
efi: Make efi virtual runtime map passing more robust
arch/x86/include/asm/pgtable.h | 3 +- arch/x86/mm/dump_pagetables.c | 77 ++++++++++++++++++++++++++++-------------- arch/x86/platform/efi/efi.c | 68 ++++++++++++++++++++++++++++++++----- 3 files changed, 112 insertions(+), 36 deletions(-)
Signature · 1 line
1.8.4
Borislav Petkov
maintainerPATCH 1/3 x86, ptdump: Add the functionality to dump an arbitrary pagetable
From: Borislav Petkov <[email protected]>
With reusing the ->trampoline_pgd page table for mapping EFI regions in order to use them after having switched to EFI virtual mode, it is very useful to be able to dump aforementioned page table in dmesg. This adds that functionality through the walk_pgd_level() interface which can be called from somewhere else.
The original functionality of dumping to debugfs remains untouched.
--- arch/x86/include/asm/pgtable.h | 3 +- arch/x86/mm/dump_pagetables.c | 77 ++++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 27 deletions(-)
arch/x86/include/asm/pgtable.h +2 -1
@@ -15,9 +15,10 @@ : (prot))#ifndef __ASSEMBLY__ -#include <asm/x86_init.h> +void walk_pgd_level (struct seq_file *m,pgd_t *pgd); +/* * ZERO_PAGE is a global shared page that is always zero: used * for zero-mapped memory areas etc..
arch/x86/mm/dump_pagetables.c +51 -26
160 lines not on this page · raw message
Signature · 1 line
1.8.4
Borislav Petkov
maintainerPATCH 2/3 efi: Dump the EFI page table
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
This is very useful for debugging issues with the recently added pagetable switching code for EFI virtual mode.
--- arch/x86/platform/efi/efi.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
arch/x86/platform/efi/efi.c +11 -0
@@ -740,6 +740,15 @@ void efi_memory_uc(u64 addr, unsigned long size)set_memory_uc (addr, npages); } +static void efi_dump_pagetable (void ) +{ +#if defined(EFI_DEBUG ) && defined(CONFIG_X86_PTDUMP ) +pgd_t *pgd = (pgd_t *)__va (real_mode_header->trampoline_pgd); + +walk_pgd_level (NULL , pgd); +#endif +} +void __init old_map_region (efi_memory_desc_t *md) {u64 start_pfn, end_pfn, end;
@@ -869,6 +878,8 @@ void __init efi_enter_virtual_mode(void)efi_setup_page_tables ();efi_sync_low_kernel_mappings (); +efi_dump_pagetable (); + status = phys_efi_set_virtual_address_map( memmap.desc_size * count, memmap.desc_size,
Signature · 1 line
1.8.4
Borislav Petkov
maintainerPATCH 3/3 efi: Make efi virtual runtime map passing more robust
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
Currently, running SetVirtualAddressMap() and passing the physical address of the virtual map array was working only by a lucky coincidence because the memory was present in the EFI page table too. Until Toshi went and booted this on a big HP box - the krealloc() manner of resizing the memmap we're doing did allocate from such physical addresses which were not mapped anymore and boom:
http://lkml.kernel.org/r/[email protected]
One way to take care of that issue is to reimplement the krealloc thing but with pages. We start with contiguous pages of order 1, i.e. 2 pages, and when we deplete that memory (shouldn't happen all that often but you know firmware) we realloc the next power-of-two pages.
Having the pages, it is much more handy and easy to map them into the EFI page table with the already existing mapping code which we're using for building the virtual mappings.
And, it doesn't matter all that much how much pages we've used as we're freeing them right after they've fulfilled their purpose at the end of the function anyway.
--- arch/x86/platform/efi/efi.c | 57 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 9 deletions(-)
arch/x86/platform/efi/efi.c +48 -9
@@ -112,7 +112,6 @@ static int __init setup_storage_paranoia(char *arg) }early_param ("efi_no_storage_paranoia" , setup_storage_paranoia); -static efi_status_t virt_efi_get_time (efi_time_t *tm,efi_time_cap_t *tc) {unsigned long flags;
@@ -775,6 +774,27 @@ void __init old_map_region(efi_memory_desc_t *md) (unsigned long long)md->phys_addr); } +static void *realloc_pages (void *old_memmap,int old_shift) +{ +void *ret; + + ret = (void *)__get_free_pages (GFP_KERNEL , old_shift +1 ); +if (!ret) + goto out; + +/* +* A first-time allocation doesn't have anything to copy. +*/ +if (!old_memmap) +return ret; + +memcpy (ret, old_memmap,PAGE_SIZE << old_shift); + +out: +__free_pages (old_memmap, old_shift); +return ret; +} +/* * This function will switch the EFI runtime services to virtual mode. * Essentially, we look through the EFI memmap and map every region that
@@ -794,12 +814,13 @@ void __init old_map_region(efi_memory_desc_t *md)*/ void __init efi_enter_virtual_mode (void ) { +pgd_t *pgd = (pgd_t *)__va (real_mode_header->trampoline_pgd); +unsigned long size, new_memmap_left =0 ;efi_memory_desc_t *md, *prev_md =NULL ; +int count =0 , new_memmap_shift =0 ;void *p, *new_memmap =NULL ; -unsigned long size;efi_status_t status;u64 end, systab; -int count =0 ; efi.systab =NULL ;
@@ -862,14 +883,19 @@ void __init efi_enter_virtual_mode(void) efi.systab = (efi_system_table_t *) (unsigned long ) systab; } - new_memmap =krealloc (new_memmap, - (count +1 ) * memmap.desc_size, -GFP_KERNEL ); -if (!new_memmap) - goto err_out; +if (new_memmap_left < memmap.desc_size) { + new_memmap =realloc_pages (new_memmap, new_memmap_shift); +if (!new_memmap) + goto err_out; + + new_memmap_shift++; + new_memmap_left +=PAGE_SIZE << new_memmap_shift; + }memcpy (new_memmap + (count * memmap.desc_size), md, memmap.desc_size); + + new_memmap_left -= memmap.desc_size; count++; }
@@ -880,6 +906,19 @@ void __init efi_enter_virtual_mode(void)efi_dump_pagetable (); +/* +* It can happen that the physical address of new_memmap lands in memory +* which is not mapped in the EFI page table. Therefore we need to go +* and ident-map those pages containing the map before calling +* phys_efi_set_virtual_address_map(). +*/ +if (kernel_map_pages_in_pgd (pgd,__pa (new_memmap),__pa (new_memmap), +1 << new_memmap_shift, _PAGE_NX)) { +pr_err ("Error ident-mapping new memmap (0x%lx)!\n" , +__pa (new_memmap)); + goto err_out; + } + status = phys_efi_set_virtual_address_map( memmap.desc_size * count, memmap.desc_size,
@@ -916,7 +955,7 @@ void __init efi_enter_virtual_mode(void)if (efi_enabled (EFI_OLD_MEMMAP ) && (__supported_pte_mask & _PAGE_NX))runtime_code_page_mkexec (); -kfree (new_memmap); +__free_pages (new_memmap, new_memmap_shift);/* clean DUMMY object */ efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
Signature · 1 line
1.8.4
Borislav Petkov
maintainerRe: [PATCH 3/3] efi: Make efi virtual runtime map passing more robust
Quoted 21 lines · Borislav Petkov
arch/x86/platform/efi/efi.c +13
-0@@ -880,6 +906,19 @@ void __init efi_enter_virtual_mode(void)efi_dump_pagetable (); +/* +* It can happen that the physical address of new_memmap lands in memory +* which is not mapped in the EFI page table. Therefore we need to go +* and ident-map those pages containing the map before calling +* phys_efi_set_virtual_address_map(). +*/ +if (kernel_map_pages_in_pgd (pgd,__pa (new_memmap),__pa (new_memmap), +1 << new_memmap_shift, _PAGE_NX)) { +pr_err ("Error ident-mapping new memmap (0x%lx)!\n" , +__pa (new_memmap)); + goto err_out; + } + status = phys_efi_set_virtual_address_map( memmap.desc_size * count, memmap.desc_size,
arch/x86/platform/efi/efi.c void __init efi_enter_virtual_mode(void) · line 958
Quoted 5 lines
arch/x86/platform/efi/efi.c +1
-1@@ -916,7 +955,7 @@ void __init efi_enter_virtual_mode(void)if (efi_enabled (EFI_OLD_MEMMAP ) && (__supported_pte_mask & _PAGE_NX))runtime_code_page_mkexec (); -kfree (new_memmap); +__free_pages (new_memmap, new_memmap_shift);
Note to self: carve out the error path of populate_pgd() into a separate function and call it here to unmap the pages previously mapped above as we're freeing them here.
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--Re: [PATCH 3/3] efi: Make efi virtual runtime map passing more robust
In reply to Borislav PetkovQuoted 29 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
Currently, running
SetVirtualAddressMap()and passing the physical address of the virtual map array was working only by a lucky coincidence because the memory was present in the EFI page table too. Until Toshi went and booted this on a big HP box - thekrealloc()manner of resizing the memmap we're doing did allocate from such physical addresses which were not mapped anymore and boom:http://lkml.kernel.org/r/1386806463.1791.295.camel-RbGIw1UOYPVo/[email protected]
One way to take care of that issue is to reimplement the krealloc thing but with pages. We start with contiguous pages of order 1, i.e. 2 pages, and when we deplete that memory (shouldn't happen all that often but you know firmware) we realloc the next power-of-two pages.
Having the pages, it is much more handy and easy to map them into the EFI page table with the already existing mapping code which we're using for building the virtual mappings.
And, it doesn't matter all that much how much pages we've used as we're freeing them right after they've fulfilled their purpose at the end of the function anyway.
Reported-by Toshi Kani <[email protected]>Signed-off-by Borislav Petkov <[email protected]>--- arch/x86/platform/efi/efi.c | 57 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 9 deletions(-)
[...]
Quoted 5 lines
arch/x86/platform/efi/efi.c +1
-0@@ -794,12 +814,13 @@ void __init old_map_region(efi_memory_desc_t *md)*/ void __init efi_enter_virtual_mode (void ) { +pgd_t *pgd = (pgd_t *)__va (real_mode_header->trampoline_pgd);
arch/x86/platform/efi/efi.c void __init old_map_region(efi_memory_desc_t *md) · line 825
Quoted 10 lines
arch/x86/platform/efi/efi.c +2
-2+unsigned long size, new_memmap_left =0 ;efi_memory_desc_t *md, *prev_md =NULL ; +int count =0 , new_memmap_shift =0 ;void *p, *new_memmap =NULL ; -unsigned long size;efi_status_t status;u64 end, systab; -int count =0 ; efi.systab =NULL ;
You sunk my i386 battleship,
/home/build/git/efi/arch/x86/platform/efi/efi.c:824:24: error: ‘struct real_mode_header’ has no member named ‘trampoline_pgd’ make[4]: *** [arch/x86/platform/efi/efi.o] Error 1
make[3]: *** [arch/x86/platform/efi] Error 2
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Borislav Petkov
maintainerRe: [PATCH 3/3] efi: Make efi virtual runtime map passing more robust
Quoted 5 lines · Matt Fleming
You sunk my i386 battleship,
/home/build/git/efi/arch/x86/platform/efi/efi.c:824:24: error: ‘struct real_mode_header’ has no member named ‘trampoline_pgd’ make[4]: *** [arch/x86/platform/efi/efi.o] Error 1
make[3]: *** [arch/x86/platform/efi] Error 2
Right, this is a recurring issue with the trampoline_pgd. What do you
prefer: an #ifdef CONFIG_X86_64 or a separate function in efi_$(BITS).c?
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--Re: [PATCH 3/3] efi: Make efi virtual runtime map passing more robust
Quoted 9 lines · Borislav Petkov
Quoted 5 lines · Matt Fleming
You sunk my i386 battleship,
/home/build/git/efi/arch/x86/platform/efi/efi.c:824:24: error: ‘struct real_mode_header’ has no member named ‘trampoline_pgd’ make[4]: *** [arch/x86/platform/efi/efi.o] Error 1
make[3]: *** [arch/x86/platform/efi] Error 2Right, this is a recurring issue with the
trampoline_pgd. What do you
prefer: an #ifdefCONFIG_X86_64or a separate function in efi_$(BITS).c?
Couldn't we do the mapping in efi_setup_page_tables() if we pass __pa(new_memmap) and 1 << new_memmap_shift as arguments?
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Borislav Petkov
maintainerRe: [PATCH 3/3] efi: Make efi virtual runtime map passing more robust
Quoted 2 lines · Matt Fleming
Couldn't we do the mapping in
efi_setup_page_tables()if we pass__pa(new_memmap)and 1 <<new_memmap_shiftas arguments?
Yep, not a bad idea. I'll give it a try.
Thanks.
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--Re: [PATCH 3/3] efi: Make efi virtual runtime map passing more robust
In reply to Borislav PetkovQuoted 9 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
Currently, running
SetVirtualAddressMap()and passing the physical address of the virtual map array was working only by a lucky coincidence because the memory was present in the EFI page table too. Until Toshi went and booted this on a big HP box - thekrealloc()manner of resizing the memmap we're doing did allocate from such physical addresses which were not mapped anymore and boom:
Quoted 6 lines
http://lkml.kernel.org/r/[email protected]
One way to take care of that issue is to reimplement the krealloc thing but with pages. We start with contiguous pages of order 1, i.e. 2 pages, and when we deplete that memory (shouldn't happen all that often but you know firmware) we realloc the next power-of-two pages.
How about firstly count the md numbers in the 1st loop, then get/roundup the total size, alloc the pages, map the mds one by one in another loop.
Thanks
Dave
Borislav Petkov
maintainerRe: [PATCH 3/3] efi: Make efi virtual runtime map passing more robust
Quoted 3 lines · Dave Young
How about firstly count the md numbers in the 1st loop, then get/roundup the total size, alloc the pages, map the mds one by one in another loop.
No need as most systems would never need to reallocate. Even on Toshi's big system, the mds fit in less than one page - having a second one is an additional safeguard, i.e. 2 pages should be more than enough in the majority of the cases.
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--Quoted 21 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
Hi guys,
this is the result of Toshi and me debugging a #GP on one of his big HP boxes sporting UEFI. Each commit message should be self-explanatory so please look there.
This has more or less an RFC nature thus I'm sending it now to collect feedback. It is going to wait in the EFI queue anyway after the kexec stuff gets sorted out first.
Comments and suggestions as always are very much appreciated.
Thanks.
Borislav Petkov (3):
x86, ptdump: Add the functionality to dump an arbitrary pagetable
efi: Dump the EFI page table
efi: Make efi virtual runtime map passing more robust
Quoted 5 lines
arch/x86/include/asm/pgtable.h | 3 +- arch/x86/mm/dump_pagetables.c | 77 ++++++++++++++++++++++++++++-------------- arch/x86/platform/efi/efi.c | 68 ++++++++++++++++++++++++++++++++----- 3 files changed, 112 insertions(+), 36 deletions(-)
Thanks for the quick turnaround! For the series:
Toshi