Re: [RFC][PATCH 4/7] x86/debug: Move historical SYSENTER junk into exc_debug_kernel()

From: peterz
Date: Thu Aug 20 2020 - 11:52:06 EST


On Thu, Aug 20, 2020 at 04:28:28PM +0100, Daniel Thompson wrote:
> On Thu, Aug 20, 2020 at 12:38:36PM +0200, Peter Zijlstra wrote:
> >
> > Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
> > ---
> > arch/x86/kernel/traps.c | 24 ++++++++++++------------
> > 1 file changed, 12 insertions(+), 12 deletions(-)
> >
> > --- a/arch/x86/kernel/traps.c
> > +++ b/arch/x86/kernel/traps.c
> > @@ -820,18 +820,6 @@ static void handle_debug(struct pt_regs
> > goto out;
> > }
> >
> > - if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
> > - /*
> > - * Historical junk that used to handle SYSENTER single-stepping.
> > - * This should be unreachable now. If we survive for a while
> > - * without anyone hitting this warning, we'll turn this into
> > - * an oops.
> > - */
> > - tsk->thread.debugreg6 &= ~DR_STEP;
> > - set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
> > - regs->flags &= ~X86_EFLAGS_TF;
> > - }
> > -
> > si_code = get_si_code(tsk->thread.debugreg6);
> > if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
> > send_sigtrap(regs, 0, si_code);
> > @@ -874,6 +862,18 @@ static __always_inline void exc_debug_ke
> > if (kprobe_debug_handler(regs))
> > goto out;
> >
> > + if (WARN_ON_ONCE(dr6 & DR_STEP)) {
> > + /*
> > + * Historical junk that used to handle SYSENTER single-stepping.
> > + * This should be unreachable now. If we survive for a while
> > + * without anyone hitting this warning, we'll turn this into
> > + * an oops.
> > + */
> > + dr6 &= ~DR_STEP;
> > + set_thread_flag(TIF_SINGLESTEP);
> > + regs->flags &= ~X86_EFLAGS_TF;
> > + }
> > +
>
> I was hoping just to share a Tested-by: on the patchset but
> unfortunately it wasn't to be. After a quick bisect this patch comes
> back as causing kdb single stepping to fail.

Yeah, Josh just asked me about this:

https://lkml.kernel.org/r/20200820152111.GC1362448@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

How's this on top of the lot?

---

--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -828,25 +828,24 @@ static __always_inline void exc_debug_ke
if (kprobe_debug_handler(regs))
goto out;

- if (WARN_ON_ONCE(dr6 & DR_STEP)) {
- /*
- * Historical junk that used to handle SYSENTER single-stepping.
- * This should be unreachable now. If we survive for a while
- * without anyone hitting this warning, we'll turn this into
- * an oops.
- */
- dr6 &= ~DR_STEP;
- set_thread_flag(TIF_SINGLESTEP);
- regs->flags &= ~X86_EFLAGS_TF;
- }
-
/*
* The kernel doesn't use INT1
*/
if (!dr6)
goto out;

- notify_debug(regs, dr6);
+ if (notify_debug(regs, dr6))
+ goto out;
+
+ /*
+ * The kernel doesn't use TF single-step outside of:
+ *
+ * - Kprobes, consumed through kprobe_debug_handler()
+ * - KGDB, consumed through notify_debug()
+ *
+ * So if we get here with DR_STEP set, something is wonky.
+ */
+ BUG_ON(current->thread.debugreg6 & DR_STEP);

out:
instrumentation_end();