Skip to content

Commit ce0fa3e

Browse files
aeglIngo Molnar
authored andcommitted
x86/mm, mm/hwpoison: Clear PRESENT bit for kernel 1:1 mappings of poison pages
Speculative processor accesses may reference any memory that has a valid page table entry. While a speculative access won't generate a machine check, it will log the error in a machine check bank. That could cause escalation of a subsequent error since the overflow bit will be then set in the machine check bank status register. Code has to be double-plus-tricky to avoid mentioning the 1:1 virtual address of the page we want to map out otherwise we may trigger the very problem we are trying to avoid. We use a non-canonical address that passes through the usual Linux table walking code to get to the same "pte". Thanks to Dave Hansen for reviewing several iterations of this. Also see: http://marc.info/?l=linux-mm&m=149860136413338&w=2 Signed-off-by: Tony Luck <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Brian Gerst <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Denys Vlasenko <[email protected]> Cc: Elliott, Robert (Persistent Memory) <[email protected]> Cc: H. Peter Anvin <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Naoya Horiguchi <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: [email protected] Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 57bd190 commit ce0fa3e

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

arch/x86/include/asm/page_64.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ static inline void clear_page(void *page)
5151

5252
void copy_page(void *to, void *from);
5353

54+
#ifdef CONFIG_X86_MCE
55+
#define arch_unmap_kpfn arch_unmap_kpfn
56+
#endif
57+
5458
#endif /* !__ASSEMBLY__ */
5559

5660
#ifdef CONFIG_X86_VSYSCALL_EMULATION

arch/x86/kernel/cpu/mcheck/mce.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <asm/mce.h>
5252
#include <asm/msr.h>
5353
#include <asm/reboot.h>
54+
#include <asm/set_memory.h>
5455

5556
#include "mce-internal.h"
5657

@@ -1051,6 +1052,48 @@ static int do_memory_failure(struct mce *m)
10511052
return ret;
10521053
}
10531054

1055+
#if defined(arch_unmap_kpfn) && defined(CONFIG_MEMORY_FAILURE)
1056+
1057+
void arch_unmap_kpfn(unsigned long pfn)
1058+
{
1059+
unsigned long decoy_addr;
1060+
1061+
/*
1062+
* Unmap this page from the kernel 1:1 mappings to make sure
1063+
* we don't log more errors because of speculative access to
1064+
* the page.
1065+
* We would like to just call:
1066+
* set_memory_np((unsigned long)pfn_to_kaddr(pfn), 1);
1067+
* but doing that would radically increase the odds of a
1068+
* speculative access to the posion page because we'd have
1069+
* the virtual address of the kernel 1:1 mapping sitting
1070+
* around in registers.
1071+
* Instead we get tricky. We create a non-canonical address
1072+
* that looks just like the one we want, but has bit 63 flipped.
1073+
* This relies on set_memory_np() not checking whether we passed
1074+
* a legal address.
1075+
*/
1076+
1077+
/*
1078+
* Build time check to see if we have a spare virtual bit. Don't want
1079+
* to leave this until run time because most developers don't have a
1080+
* system that can exercise this code path. This will only become a
1081+
* problem if/when we move beyond 5-level page tables.
1082+
*
1083+
* Hard code "9" here because cpp doesn't grok ilog2(PTRS_PER_PGD)
1084+
*/
1085+
#if PGDIR_SHIFT + 9 < 63
1086+
decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
1087+
#else
1088+
#error "no unused virtual bit available"
1089+
#endif
1090+
1091+
if (set_memory_np(decoy_addr, 1))
1092+
pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
1093+
1094+
}
1095+
#endif
1096+
10541097
/*
10551098
* The actual machine check handler. This only handles real
10561099
* exceptions when something got corrupted coming in through int 18.

include/linux/mm_inline.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,10 @@ static __always_inline enum lru_list page_lru(struct page *page)
126126

127127
#define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
128128

129+
#ifdef arch_unmap_kpfn
130+
extern void arch_unmap_kpfn(unsigned long pfn);
131+
#else
132+
static __always_inline void arch_unmap_kpfn(unsigned long pfn) { }
133+
#endif
134+
129135
#endif

mm/memory-failure.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,8 @@ int memory_failure(unsigned long pfn, int trapno, int flags)
11461146
return 0;
11471147
}
11481148

1149+
arch_unmap_kpfn(pfn);
1150+
11491151
orig_head = hpage = compound_head(p);
11501152
num_poisoned_pages_inc();
11511153

0 commit comments

Comments
 (0)