Message ID | 20210326000235.370514-4-tony.luck@intel.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Fix machine check recovery for copy_from_user | expand |
On Thu, Mar 25, 2021 at 05:02:34PM -0700, Tony Luck wrote: > Andy Lutomirski pointed out that sending SIGBUS to tasks that > hit poison in the kernel copying syscall parameters from user > address space is not the right semantic. What does that mean exactly? From looking at the code, that is this conditional: if (t == EX_HANDLER_UACCESS && regs && is_copy_from_user(regs)) { m->kflags |= MCE_IN_KERNEL_RECOV; m->kflags |= MCE_IN_KERNEL_COPYIN; so what does the above have to do with syscall params? If it is about us being in ring 0 and touching user memory and eating poison in same *user* memory while doing so, then sure, that makes sense. > So stop doing that. Add a new kill_me_never() call back that > simply unmaps and offlines the poison page. Right, that's the same as handling poisoned user memory. Thx.
On Wed, Apr 07, 2021 at 11:18:16PM +0200, Borislav Petkov wrote: > On Thu, Mar 25, 2021 at 05:02:34PM -0700, Tony Luck wrote: > > Andy Lutomirski pointed out that sending SIGBUS to tasks that > > hit poison in the kernel copying syscall parameters from user > > address space is not the right semantic. > > What does that mean exactly? Andy said that a task could check a memory range for poison by doing: ret = write(fd, buf, size); if (ret == size) { memory range is all good } That doesn't work if the kernel sends a SIGBUS. It doesn't seem a likely scenario ... but Andy is correct that the above ought to work. > > From looking at the code, that is this conditional: > > if (t == EX_HANDLER_UACCESS && regs && is_copy_from_user(regs)) { > m->kflags |= MCE_IN_KERNEL_RECOV; > m->kflags |= MCE_IN_KERNEL_COPYIN; > > so what does the above have to do with syscall params? Most "copy from user" instances are the result of a system call parameter (e.g. "buf" in the write(2) example above). > If it is about us being in ring 0 and touching user memory and eating > poison in same *user* memory while doing so, then sure, that makes > sense. Yes. This is for kernel reading memory belongng to "current" task. > > So stop doing that. Add a new kill_me_never() call back that > > simply unmaps and offlines the poison page. > > Right, that's the same as handling poisoned user memory. Same in that the page gets unmapped. Different in that there is no SIGBUS if the kernel did the access for the user. -Tony
On Wed, Apr 07, 2021 at 02:43:10PM -0700, Luck, Tony wrote: > On Wed, Apr 07, 2021 at 11:18:16PM +0200, Borislav Petkov wrote: > > On Thu, Mar 25, 2021 at 05:02:34PM -0700, Tony Luck wrote: > > > Andy Lutomirski pointed out that sending SIGBUS to tasks that > > > hit poison in the kernel copying syscall parameters from user > > > address space is not the right semantic. > > > > What does that mean exactly? > > Andy said that a task could check a memory range for poison by > doing: > > ret = write(fd, buf, size); > if (ret == size) { > memory range is all good > } > > That doesn't work if the kernel sends a SIGBUS. > > It doesn't seem a likely scenario ... but Andy is correct that > the above ought to work. We need to document properly what this is aiming to fix. He said something yesterday along the lines of kthread_use_mm() hitting a SIGBUS when a kthread "attaches" to an address space. I'm still unclear as to how exactly that happens - there are only a handful of kthread_use_mm() users in the tree... > Yes. This is for kernel reading memory belongng to "current" task. Provided "current" is really the task to which the poison page belongs. That kthread_use_mm() thing sounded like the wrong task gets killed. But that needs more details. > Same in that the page gets unmapped. Different in that there > is no SIGBUS if the kernel did the access for the user. What is even the actual use case with sending tasks SIGBUS on poison consumption? KVM? Others? Are we documenting somewhere: "if your process gets a SIGBUS and this and that, which means your page got offlined, you should do this and that to recover"? Thx.
On Thu, Apr 08, 2021 at 10:49:58AM +0200, Borislav Petkov wrote: > On Wed, Apr 07, 2021 at 02:43:10PM -0700, Luck, Tony wrote: > > On Wed, Apr 07, 2021 at 11:18:16PM +0200, Borislav Petkov wrote: > > > On Thu, Mar 25, 2021 at 05:02:34PM -0700, Tony Luck wrote: > > > > Andy Lutomirski pointed out that sending SIGBUS to tasks that > > > > hit poison in the kernel copying syscall parameters from user > > > > address space is not the right semantic. > > > > > > What does that mean exactly? > > > > Andy said that a task could check a memory range for poison by > > doing: > > > > ret = write(fd, buf, size); > > if (ret == size) { > > memory range is all good > > } > > > > That doesn't work if the kernel sends a SIGBUS. > > > > It doesn't seem a likely scenario ... but Andy is correct that > > the above ought to work. > > We need to document properly what this is aiming to fix. He said > something yesterday along the lines of kthread_use_mm() hitting a SIGBUS > when a kthread "attaches" to an address space. I'm still unclear as to > how exactly that happens - there are only a handful of kthread_use_mm() > users in the tree... Also not clear to me either ... but sending a SIGBUS to a kthread isn't going to do anything useful. So avoiding doing that is another worthy goal. > > Yes. This is for kernel reading memory belongng to "current" task. > > Provided "current" is really the task to which the poison page belongs. > That kthread_use_mm() thing sounded like the wrong task gets killed. But that > needs more details. With these patches nothing gets killed when kernel touches user poison. If this is in a regular system call then these patches will return EFAULT to the user (but now that I see EHWPOISON exists that looks like a better choice - so applications can distinguish the "I just used an invalid address in a parameter to a syscall" from "This isn't my fault, the memory broke". > > Same in that the page gets unmapped. Different in that there > > is no SIGBUS if the kernel did the access for the user. > > What is even the actual use case with sending tasks SIGBUS on poison > consumption? KVM? Others? KVM apparently passes a machine check into the guest. Though it seems to be misisng the MCG_STATUS information to tell the guest whether this is an "Action Required" machine check, or an "Action Optional" (i.e. whether the poison was found synchonously by execution of the current instruction, or asynchronously). > Are we documenting somewhere: "if your process gets a SIGBUS and this > and that, which means your page got offlined, you should do this and > that to recover"? There is the ancient Documentation/vm/hwpoison.rst from 2009 ... nothing seems wrong in that, but could use some updates. I don't know how much detail we might want to go into on recovery stratgies for applications. In terms of production s/w there was one ISV who prototyped recovery for their application but last time I checked didn't enable it in the production version. Essentially it boils down to: SIGBUS handler gets additional data giving virtual address that has gone away 1) Can the application replace the lost page? Use mmap(addr, MAP_FIXED, ...) to map a fresh page into the gap and fill with replacement data. This case can return from SIGBUS handler to re-execute failed instruction 2) Can the application continue in degraded mode w/o the lost page? Hunt down pointers to lost page and update structures to say "this data lost". Use siglongjmp() to go to preset recovery path 3) Can the application shut down gracefully? Record details of the lost page. Inform next-of-kin. Exit. 4) Default - just exit -Tony
On Thu, Apr 08, 2021 at 10:08:52AM -0700, Luck, Tony wrote: > Also not clear to me either ... but sending a SIGBUS to a kthread isn't > going to do anything useful. So avoiding doing that is another worthy > goal. Ack. > With these patches nothing gets killed when kernel touches user poison. > If this is in a regular system call then these patches will return EFAULT > to the user (but now that I see EHWPOISON exists that looks like a better > choice - so applications can distinguish the "I just used an invalid address in > a parameter to a syscall" from "This isn't my fault, the memory broke". Yap, makes sense. > KVM apparently passes a machine check into the guest. Ah, there it is: static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk) { send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, PAGE_SHIFT, tsk); } > Though it seems to be misisng the MCG_STATUS information to tell > the guest whether this is an "Action Required" machine check, or an > "Action Optional" (i.e. whether the poison was found synchonously by > execution of the current instruction, or asynchronously). Yeah, someone needs to deal with that sooner or later, considering how virt is becoming ubiquitous. > There is the ancient Documentation/vm/hwpoison.rst from 2009 ... nothing > seems wrong in that, but could use some updates. Ah yap, that. So what I'm missing with all this fun is, yeah, sure, we have this facility out there but who's using it? Is anyone even using it at all? If so, does it even make sense, does it need improvements, etc? Because from where I stand it all looks like we do all these fancy recovery things but is userspace even paying attention or using them or whatever... > I don't know how much detail we might want to go into on recovery > stratgies for applications. Perhaps an example or two how userspace is supposed to use it... > In terms of production s/w there was one ISV who prototyped recovery > for their application but last time I checked didn't enable it in the > production version. See, one and it hasn't even enabled it. So it all feels like a lot of wasted energy to me to do all those and keep 'em working. But maybe I'm missing the big picture and someone would come and say, no, Boris, we use this and this and that is our feedback... > Essentially it boils down to: > SIGBUS handler gets additional data giving virtual address that has gone away > > 1) Can the application replace the lost page? > Use mmap(addr, MAP_FIXED, ...) to map a fresh page into the gap > and fill with replacement data. This case can return from SIGBUS > handler to re-execute failed instruction > 2) Can the application continue in degraded mode w/o the lost page? > Hunt down pointers to lost page and update structures to say > "this data lost". Use siglongjmp() to go to preset recovery path > 3) Can the application shut down gracefully? > Record details of the lost page. Inform next-of-kin. Exit. > 4) Default - just exit Right and this should probably start with "Hey userspace folks, here's what you can do when you get a hwpoison signal and here's how we envision this recovery to work" and then we can all discuss and converge on an agreeable solution which is actually used and there will be selftests and so on and so on... But what the h*ll do I know. Thx.
> So what I'm missing with all this fun is, yeah, sure, we have this > facility out there but who's using it? Is anyone even using it at all? Even if no applications ever do anything with it, it is still useful to avoid crashing the whole system and just terminate one application/guest. > If so, does it even make sense, does it need improvements, etc? There's one more item on my long term TODO list. Add fixups so that copy_to_user() from poison in the page cache doesn't crash, but just checks to see if the page was clean .. .in which case re-read from the filesystem into a different physical page and retire the old page ... the read can now succeed. If the page is dirty, then fail the read (and retire the page ... need to make sure filesystem knows the data for the page was lost so subsequent reads return -EIO or something). Page cache occupies enough memory that it is a big enough source of system crashes that could be avoided. I'm not sure if there are any other obvious cases after this ... it all gets into diminishing returns ... not really worth it to handle a case that only occupies 0.00002% of memory. > Because from where I stand it all looks like we do all these fancy > recovery things but is userspace even paying attention or using them or > whatever... See above. With core counts continuing to increase, the cloud service providers really want to see fewer events that crash the whole physical machine (taking down dozens, or hundreds, of guest VMs). -Tony
On Tue, Apr 13, 2021 at 04:13:03PM +0000, Luck, Tony wrote: > Even if no applications ever do anything with it, it is still useful to avoid > crashing the whole system and just terminate one application/guest. True. > There's one more item on my long term TODO list. Add fixups so that > copy_to_user() from poison in the page cache doesn't crash, but just > checks to see if the page was clean .. .in which case re-read from the > filesystem into a different physical page and retire the old page ... the > read can now succeed. If the page is dirty, then fail the read (and retire > the page ... need to make sure filesystem knows the data for the page > was lost so subsequent reads return -EIO or something). Makes sense. > Page cache occupies enough memory that it is a big enough > source of system crashes that could be avoided. I'm not sure > if there are any other obvious cases after this ... it all gets into > diminishing returns ... not really worth it to handle a case that > only occupies 0.00002% of memory. Ack. > See above. With core counts continuing to increase, the cloud service > providers really want to see fewer events that crash the whole physical > machine (taking down dozens, or hundreds, of guest VMs). Yap. Thx.
diff --git a/arch/x86/kernel/cpu/mce/core.c b/arch/x86/kernel/cpu/mce/core.c index 7962355436da..1570310cadab 100644 --- a/arch/x86/kernel/cpu/mce/core.c +++ b/arch/x86/kernel/cpu/mce/core.c @@ -1263,32 +1263,32 @@ static void kill_me_maybe(struct callback_head *cb) if (!p->mce_ripv) flags |= MF_MUST_KILL; - if (!memory_failure(p->mce_addr >> PAGE_SHIFT, flags) && - !(p->mce_kflags & MCE_IN_KERNEL_COPYIN)) { + if (!memory_failure(p->mce_addr >> PAGE_SHIFT, flags)) { set_mce_nospec(p->mce_addr >> PAGE_SHIFT, p->mce_whole_page); sync_core(); return; } - if (p->mce_vaddr != (void __user *)-1l) { - force_sig_mceerr(BUS_MCEERR_AR, p->mce_vaddr, PAGE_SHIFT); - } else { - pr_err("Memory error not recovered"); - kill_me_now(cb); - } + pr_err("Memory error not recovered"); + kill_me_now(cb); } -static void queue_task_work(struct mce *m, int kill_current_task) +static void kill_me_never(struct callback_head *cb) +{ + struct task_struct *p = container_of(cb, struct task_struct, mce_kill_me); + + pr_err("Kernel accessed poison in user space at %llx\n", p->mce_addr); + if (!memory_failure(p->mce_addr >> PAGE_SHIFT, 0)) + set_mce_nospec(p->mce_addr >> PAGE_SHIFT, p->mce_whole_page); +} + +static void queue_task_work(struct mce *m, void (*func)(struct callback_head *)) { current->mce_addr = m->addr; current->mce_kflags = m->kflags; current->mce_ripv = !!(m->mcgstatus & MCG_STATUS_RIPV); current->mce_whole_page = whole_page(m); - - if (kill_current_task) - current->mce_kill_me.func = kill_me_now; - else - current->mce_kill_me.func = kill_me_maybe; + current->mce_kill_me.func = func; task_work_add(current, ¤t->mce_kill_me, TWA_RESUME); } @@ -1426,7 +1426,10 @@ noinstr void do_machine_check(struct pt_regs *regs) /* If this triggers there is no way to recover. Die hard. */ BUG_ON(!on_thread_stack() || !user_mode(regs)); - queue_task_work(&m, kill_current_task); + if (kill_current_task) + queue_task_work(&m, kill_me_now); + else + queue_task_work(&m, kill_me_maybe); } else { /* @@ -1444,7 +1447,7 @@ noinstr void do_machine_check(struct pt_regs *regs) } if (m.kflags & MCE_IN_KERNEL_COPYIN) - queue_task_work(&m, kill_current_task); + queue_task_work(&m, kill_me_never); } out: mce_wrmsrl(MSR_IA32_MCG_STATUS, 0); diff --git a/arch/x86/kernel/cpu/mce/severity.c b/arch/x86/kernel/cpu/mce/severity.c index 83df991314c5..47810d12f040 100644 --- a/arch/x86/kernel/cpu/mce/severity.c +++ b/arch/x86/kernel/cpu/mce/severity.c @@ -251,8 +251,6 @@ static bool is_copy_from_user(struct pt_regs *regs) if (fault_in_kernel_space(addr)) return false; - current->mce_vaddr = (void __user *)addr; - return true; } diff --git a/include/linux/sched.h b/include/linux/sched.h index ef00bb22164c..2d213b52730c 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1358,7 +1358,6 @@ struct task_struct { #endif #ifdef CONFIG_X86_MCE - void __user *mce_vaddr; __u64 mce_kflags; u64 mce_addr; __u64 mce_ripv : 1,