Skip to content

Commit 9b9eaee

Browse files
committed
arm64: efi: Fix handling of misaligned runtime regions and drop warning
Currently, when mapping the EFI runtime regions in the EFI page tables, we complain about misaligned regions in a rather noisy way, using WARN(). Not only does this produce a lot of irrelevant clutter in the log, it is factually incorrect, as misaligned runtime regions are actually allowed by the EFI spec as long as they don't require conflicting memory types within the same 64k page. So let's drop the warning, and tweak the code so that we - take both the start and end of the region into account when checking for misalignment - only revert to RWX mappings for non-code regions if misaligned code regions are also known to exist. Cc: <[email protected]> Acked-by: Linus Torvalds <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]>
1 parent 550b33c commit 9b9eaee

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

arch/arm64/kernel/efi.c

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
#include <asm/efi.h>
1515

16+
static bool region_is_misaligned(const efi_memory_desc_t *md)
17+
{
18+
if (PAGE_SIZE == EFI_PAGE_SIZE)
19+
return false;
20+
return !PAGE_ALIGNED(md->phys_addr) ||
21+
!PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT);
22+
}
23+
1624
/*
1725
* Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
1826
* executable, everything else can be mapped with the XN bits
@@ -26,14 +34,22 @@ static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)
2634
if (type == EFI_MEMORY_MAPPED_IO)
2735
return PROT_DEVICE_nGnRE;
2836

29-
if (WARN_ONCE(!PAGE_ALIGNED(md->phys_addr),
30-
"UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?"))
37+
if (region_is_misaligned(md)) {
38+
static bool __initdata code_is_misaligned;
39+
3140
/*
32-
* If the region is not aligned to the page size of the OS, we
33-
* can not use strict permissions, since that would also affect
34-
* the mapping attributes of the adjacent regions.
41+
* Regions that are not aligned to the OS page size cannot be
42+
* mapped with strict permissions, as those might interfere
43+
* with the permissions that are needed by the adjacent
44+
* region's mapping. However, if we haven't encountered any
45+
* misaligned runtime code regions so far, we can safely use
46+
* non-executable permissions for non-code regions.
3547
*/
36-
return pgprot_val(PAGE_KERNEL_EXEC);
48+
code_is_misaligned |= (type == EFI_RUNTIME_SERVICES_CODE);
49+
50+
return code_is_misaligned ? pgprot_val(PAGE_KERNEL_EXEC)
51+
: pgprot_val(PAGE_KERNEL);
52+
}
3753

3854
/* R-- */
3955
if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
@@ -64,19 +80,16 @@ int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
6480
bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE ||
6581
md->type == EFI_RUNTIME_SERVICES_DATA);
6682

67-
if (!PAGE_ALIGNED(md->phys_addr) ||
68-
!PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT)) {
69-
/*
70-
* If the end address of this region is not aligned to page
71-
* size, the mapping is rounded up, and may end up sharing a
72-
* page frame with the next UEFI memory region. If we create
73-
* a block entry now, we may need to split it again when mapping
74-
* the next region, and support for that is going to be removed
75-
* from the MMU routines. So avoid block mappings altogether in
76-
* that case.
77-
*/
83+
/*
84+
* If this region is not aligned to the page size used by the OS, the
85+
* mapping will be rounded outwards, and may end up sharing a page
86+
* frame with an adjacent runtime memory region. Given that the page
87+
* table descriptor covering the shared page will be rewritten when the
88+
* adjacent region gets mapped, we must avoid block mappings here so we
89+
* don't have to worry about splitting them when that happens.
90+
*/
91+
if (region_is_misaligned(md))
7892
page_mappings_only = true;
79-
}
8093

8194
create_pgd_mapping(mm, md->phys_addr, md->virt_addr,
8295
md->num_pages << EFI_PAGE_SHIFT,
@@ -103,6 +116,9 @@ int __init efi_set_mapping_permissions(struct mm_struct *mm,
103116
BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
104117
md->type != EFI_RUNTIME_SERVICES_DATA);
105118

119+
if (region_is_misaligned(md))
120+
return 0;
121+
106122
/*
107123
* Calling apply_to_page_range() is only safe on regions that are
108124
* guaranteed to be mapped down to pages. Since we are only called

0 commit comments

Comments
 (0)