Skip to content

Commit 3486eed

Browse files
aeglgregkh
authored andcommitted
x86/{mce,mm}: Unmap the entire page if the whole page is affected and poisoned
commit 17fae12 upstream. An interesting thing happened when a guest Linux instance took a machine check. The VMM unmapped the bad page from guest physical space and passed the machine check to the guest. Linux took all the normal actions to offline the page from the process that was using it. But then guest Linux crashed because it said there was a second machine check inside the kernel with this stack trace: do_memory_failure set_mce_nospec set_memory_uc _set_memory_uc change_page_attr_set_clr cpa_flush clflush_cache_range_opt This was odd, because a CLFLUSH instruction shouldn't raise a machine check (it isn't consuming the data). Further investigation showed that the VMM had passed in another machine check because is appeared that the guest was accessing the bad page. Fix is to check the scope of the poison by checking the MCi_MISC register. If the entire page is affected, then unmap the page. If only part of the page is affected, then mark the page as uncacheable. This assumes that VMMs will do the logical thing and pass in the "whole page scope" via the MCi_MISC register (since they unmapped the entire page). [ bp: Adjust to x86/entry changes. ] Fixes: 284ce40 ("x86/memory_failure: Introduce {set, clear}_mce_nospec()") Reported-by: Jue Wang <juew@google.com> Signed-off-by: Tony Luck <tony.luck@intel.com> Signed-off-by: Borislav Petkov <bp@suse.de> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Tested-by: Jue Wang <juew@google.com> Cc: <stable@vger.kernel.org> Link: https://lkml.kernel.org/r/20200520163546.GA7977@agluck-desk2.amr.corp.intel.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 986fd58 commit 3486eed

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

‎arch/x86/include/asm/set_memory.h‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,35 @@ int set_direct_map_default_noflush(struct page *page);
8686
extern int kernel_set_to_readonly;
8787

8888
#ifdef CONFIG_X86_64
89-
static inline int set_mce_nospec(unsigned long pfn)
89+
/*
90+
* Prevent speculative access to the page by either unmapping
91+
* it (if we do not require access to any part of the page) or
92+
* marking it uncacheable (if we want to try to retrieve data
93+
* from non-poisoned lines in the page).
94+
*/
95+
static inline int set_mce_nospec(unsigned long pfn, bool unmap)
9096
{
9197
unsigned long decoy_addr;
9298
int rc;
9399

94100
/*
95-
* Mark the linear address as UC to make sure we don't log more
96-
* errors because of speculative access to the page.
97101
* We would like to just call:
98-
* set_memory_uc((unsigned long)pfn_to_kaddr(pfn), 1);
102+
* set_memory_XX((unsigned long)pfn_to_kaddr(pfn), 1);
99103
* but doing that would radically increase the odds of a
100104
* speculative access to the poison page because we'd have
101105
* the virtual address of the kernel 1:1 mapping sitting
102106
* around in registers.
103107
* Instead we get tricky. We create a non-canonical address
104108
* that looks just like the one we want, but has bit 63 flipped.
105-
* This relies on set_memory_uc() properly sanitizing any __pa()
109+
* This relies on set_memory_XX() properly sanitizing any __pa()
106110
* results with __PHYSICAL_MASK or PTE_PFN_MASK.
107111
*/
108112
decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
109113

110-
rc = set_memory_uc(decoy_addr, 1);
114+
if (unmap)
115+
rc = set_memory_np(decoy_addr, 1);
116+
else
117+
rc = set_memory_uc(decoy_addr, 1);
111118
if (rc)
112119
pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
113120
return rc;

‎arch/x86/kernel/cpu/mce/core.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,13 @@ bool mce_is_memory_error(struct mce *m)
529529
}
530530
EXPORT_SYMBOL_GPL(mce_is_memory_error);
531531

532+
static bool whole_page(struct mce *m)
533+
{
534+
if (!mca_cfg.ser || !(m->status & MCI_STATUS_MISCV))
535+
return true;
536+
return MCI_MISC_ADDR_LSB(m->misc) >= PAGE_SHIFT;
537+
}
538+
532539
bool mce_is_correctable(struct mce *m)
533540
{
534541
if (m->cpuvendor == X86_VENDOR_AMD && m->status & MCI_STATUS_DEFERRED)
@@ -600,7 +607,7 @@ static int uc_decode_notifier(struct notifier_block *nb, unsigned long val,
600607

601608
pfn = mce->addr >> PAGE_SHIFT;
602609
if (!memory_failure(pfn, 0))
603-
set_mce_nospec(pfn);
610+
set_mce_nospec(pfn, whole_page(mce));
604611

605612
return NOTIFY_OK;
606613
}
@@ -1098,7 +1105,7 @@ static int do_memory_failure(struct mce *m)
10981105
if (ret)
10991106
pr_err("Memory error not recovered");
11001107
else
1101-
set_mce_nospec(m->addr >> PAGE_SHIFT);
1108+
set_mce_nospec(m->addr >> PAGE_SHIFT, whole_page(m));
11021109
return ret;
11031110
}
11041111

‎include/linux/set_memory.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static inline int set_direct_map_default_noflush(struct page *page)
2626
#endif
2727

2828
#ifndef set_mce_nospec
29-
static inline int set_mce_nospec(unsigned long pfn)
29+
static inline int set_mce_nospec(unsigned long pfn, bool unmap)
3030
{
3131
return 0;
3232
}

0 commit comments

Comments
 (0)