When one page is already hwpoisoned by AO action, process may not be
killed, the process mapping this page may make a syscall include this
page and result to trigger a VM_FAULT_HWPOISON fault, if it's in kernel
mode it may be fixed by fixup_exception. Current code will just return
error code to user process.

This is not sufficient, we should send a SIGBUS to the process and log
the info to console, as we can't trust the process will handle the error
correctly.

Suggested-by: Feng Yang <yangfe...@kingsoft.com>
Signed-off-by: Aili Yao <yaoa...@kingsoft.com>
---
 arch/x86/mm/fault.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
index f1f1b5a0956a..23095b94cf42 100644
--- a/arch/x86/mm/fault.c
+++ b/arch/x86/mm/fault.c
@@ -631,7 +631,7 @@ static void set_signal_archinfo(unsigned long address,
 
 static noinline void
 no_context(struct pt_regs *regs, unsigned long error_code,
-          unsigned long address, int signal, int si_code)
+          unsigned long address, int signal, int si_code, vm_fault_t fault)
 {
        struct task_struct *tsk = current;
        unsigned long flags;
@@ -662,12 +662,32 @@ no_context(struct pt_regs *regs, unsigned long error_code,
                 * In this case we need to make sure we're not recursively
                 * faulting through the emulate_vsyscall() logic.
                 */
+
+               if (IS_ENABLED(CONFIG_MEMORY_FAILURE) &&
+                   fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
+                       unsigned int lsb = 0;
+
+                       pr_err("MCE: Killing %s:%d due to hardware memory 
corruption fault at %lx\n",
+                               current->comm, current->pid, address);
+
+                       sanitize_error_code(address, &error_code);
+                       set_signal_archinfo(address, error_code);
+
+                       if (fault & VM_FAULT_HWPOISON_LARGE)
+                               lsb = 
hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
+                       if (fault & VM_FAULT_HWPOISON)
+                               lsb = PAGE_SHIFT;
+
+                       force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, 
lsb);
+
+                       return;
+               }
+
                if (current->thread.sig_on_uaccess_err && signal) {
                        sanitize_error_code(address, &error_code);
 
                        set_signal_archinfo(address, error_code);
 
-                       /* XXX: hwpoison faults will set the wrong code. */
                        force_sig_fault(signal, si_code, (void __user 
*)address);
                }
 
@@ -836,7 +856,7 @@ __bad_area_nosemaphore(struct pt_regs *regs, unsigned long 
error_code,
        if (is_f00f_bug(regs, address))
                return;
 
-       no_context(regs, error_code, address, SIGSEGV, si_code);
+       no_context(regs, error_code, address, SIGSEGV, si_code, 0);
 }
 
 static noinline void
@@ -927,7 +947,7 @@ do_sigbus(struct pt_regs *regs, unsigned long error_code, 
unsigned long address,
 {
        /* Kernel mode? Handle exceptions or die: */
        if (!(error_code & X86_PF_USER)) {
-               no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
+               no_context(regs, error_code, address, SIGBUS, BUS_ADRERR, 
fault);
                return;
        }
 
@@ -966,7 +986,7 @@ mm_fault_error(struct pt_regs *regs, unsigned long 
error_code,
               unsigned long address, vm_fault_t fault)
 {
        if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) {
-               no_context(regs, error_code, address, 0, 0);
+               no_context(regs, error_code, address, 0, 0, 0);
                return;
        }
 
@@ -974,7 +994,7 @@ mm_fault_error(struct pt_regs *regs, unsigned long 
error_code,
                /* Kernel mode? Handle exceptions or die: */
                if (!(error_code & X86_PF_USER)) {
                        no_context(regs, error_code, address,
-                                  SIGSEGV, SEGV_MAPERR);
+                                  SIGSEGV, SEGV_MAPERR, 0);
                        return;
                }
 
@@ -1396,7 +1416,7 @@ void do_user_addr_fault(struct pt_regs *regs,
        if (fault_signal_pending(fault, regs)) {
                if (!user_mode(regs))
                        no_context(regs, hw_error_code, address, SIGBUS,
-                                  BUS_ADRERR);
+                                  BUS_ADRERR, 0);
                return;
        }
 
-- 
2.25.1

Reply via email to