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 <tony.luck@intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Elliott, Robert (Persistent Memory) <elliott@hpe.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-mm@kvack.org
Cc: stable@vger.kernel.org
Link: http://lkml.kernel.org/r/20170816171803.28342-1-tony.luck@intel.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Tony Luck and committed by
Ingo Molnar
ce0fa3e5 57bd1905

+55
+4
arch/x86/include/asm/page_64.h
··· 51 51 52 52 void copy_page(void *to, void *from); 53 53 54 + #ifdef CONFIG_X86_MCE 55 + #define arch_unmap_kpfn arch_unmap_kpfn 56 + #endif 57 + 54 58 #endif /* !__ASSEMBLY__ */ 55 59 56 60 #ifdef CONFIG_X86_VSYSCALL_EMULATION
+43
arch/x86/kernel/cpu/mcheck/mce.c
··· 51 51 #include <asm/mce.h> 52 52 #include <asm/msr.h> 53 53 #include <asm/reboot.h> 54 + #include <asm/set_memory.h> 54 55 55 56 #include "mce-internal.h" 56 57 ··· 1051 1050 pr_err("Memory error not recovered"); 1052 1051 return ret; 1053 1052 } 1053 + 1054 + #if defined(arch_unmap_kpfn) && defined(CONFIG_MEMORY_FAILURE) 1055 + 1056 + void arch_unmap_kpfn(unsigned long pfn) 1057 + { 1058 + unsigned long decoy_addr; 1059 + 1060 + /* 1061 + * Unmap this page from the kernel 1:1 mappings to make sure 1062 + * we don't log more errors because of speculative access to 1063 + * the page. 1064 + * We would like to just call: 1065 + * set_memory_np((unsigned long)pfn_to_kaddr(pfn), 1); 1066 + * but doing that would radically increase the odds of a 1067 + * speculative access to the posion page because we'd have 1068 + * the virtual address of the kernel 1:1 mapping sitting 1069 + * around in registers. 1070 + * Instead we get tricky. We create a non-canonical address 1071 + * that looks just like the one we want, but has bit 63 flipped. 1072 + * This relies on set_memory_np() not checking whether we passed 1073 + * a legal address. 1074 + */ 1075 + 1076 + /* 1077 + * Build time check to see if we have a spare virtual bit. Don't want 1078 + * to leave this until run time because most developers don't have a 1079 + * system that can exercise this code path. This will only become a 1080 + * problem if/when we move beyond 5-level page tables. 1081 + * 1082 + * Hard code "9" here because cpp doesn't grok ilog2(PTRS_PER_PGD) 1083 + */ 1084 + #if PGDIR_SHIFT + 9 < 63 1085 + decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63)); 1086 + #else 1087 + #error "no unused virtual bit available" 1088 + #endif 1089 + 1090 + if (set_memory_np(decoy_addr, 1)) 1091 + pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn); 1092 + 1093 + } 1094 + #endif 1054 1095 1055 1096 /* 1056 1097 * The actual machine check handler. This only handles real
+6
include/linux/mm_inline.h
··· 126 126 127 127 #define lru_to_page(head) (list_entry((head)->prev, struct page, lru)) 128 128 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 + 129 135 #endif
+2
mm/memory-failure.c
··· 1146 1146 return 0; 1147 1147 } 1148 1148 1149 + arch_unmap_kpfn(pfn); 1150 + 1149 1151 orig_head = hpage = compound_head(p); 1150 1152 num_poisoned_pages_inc(); 1151 1153