From: Andy Lutomirski <luto@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Kyle Huey <me@kylehuey.com>,
open list <linux-kernel@vger.kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
"maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)"
<x86@kernel.org>, Linus Torvalds <torvalds@linux-foundation.org>,
"Robert O'Callahan" <rocallahan@gmail.com>,
Alexandre Chartre <alexandre.chartre@oracle.com>,
"Paul E. McKenney" <paulmck@kernel.org>,
Frederic Weisbecker <frederic@kernel.org>,
Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <sean.j.christopherson@intel.com>,
Masami Hiramatsu <mhiramat@kernel.org>,
Petr Mladek <pmladek@suse.com>,
Joel Fernandes <joel@joelfernandes.org>,
Steven Rostedt <rostedt@goodmis.org>,
Boris Ostrovsky <boris.ostrovsky@oracle.com>,
Juergen Gross <jgross@suse.com>, Brian Gerst <brgerst@gmail.com>,
Andy Lutomirski <luto@kernel.org>,
Josh Poimboeuf <jpoimboe@redhat.com>,
Daniel Thompson <daniel.thompson@linaro.org>
Subject: Re: [REGRESSION] x86/debug: After PTRACE_SINGLESTEP DR_STEP is no longer reported in dr6
Date: Mon, 26 Oct 2020 16:30:32 -0700 [thread overview]
Message-ID: <CALCETrVwzcpk88jWeNb+iCGBFsyzgbZ0E9_x330A2P-CMzSr4g@mail.gmail.com> (raw)
In-Reply-To: <20201026165519.GD2651@hirez.programming.kicks-ass.net>
On Mon, Oct 26, 2020 at 9:55 AM Peter Zijlstra <peterz@infradead.org> wrote:
>
> On Mon, Oct 26, 2020 at 05:31:00PM +0100, Peter Zijlstra wrote:
> > In that respect, I think the current virtual_dr6 = 0 is placed wrong, it
> > should only be in exc_debug_user(). The only 'problem' then is that we
> > seem to be able to loose BTF, but perhaps that is already an extant bug.
> >
> > Consider:
> >
> > - perf: setup in-kernel #DB
> > - tracer: ptrace(PTRACE_SINGLEBLOCK)
> > - tracee: #DB on perf breakpoint, looses BTF
> > - tracee .. never triggers actual blockstep
> >
> > Hmm ? Should we re-set BTF when TIF_BLOCKSTEP && !user_mode(regs) ?
>
> Something like so then.
>
> Or sould we also have the userspace #DB re-set BTF when it was !DR_STEP?
> I need to go untangle that ptrace stuff :/
>
> diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
> index 3c70fb34028b..31de8b0980ca 100644
> --- a/arch/x86/kernel/traps.c
> +++ b/arch/x86/kernel/traps.c
> @@ -793,19 +793,6 @@ static __always_inline unsigned long debug_read_clear_dr6(void)
> set_debugreg(DR6_RESERVED, 6);
> dr6 ^= DR6_RESERVED; /* Flip to positive polarity */
>
> - /*
> - * Clear the virtual DR6 value, ptrace routines will set bits here for
> - * things we want signals for.
> - */
> - current->thread.virtual_dr6 = 0;
> -
> - /*
> - * The SDM says "The processor clears the BTF flag when it
> - * generates a debug exception." Clear TIF_BLOCKSTEP to keep
> - * TIF_BLOCKSTEP in sync with the hardware BTF flag.
> - */
> - clear_thread_flag(TIF_BLOCKSTEP);
> -
> return dr6;
> }
>
> @@ -873,6 +860,20 @@ static __always_inline void exc_debug_kernel(struct pt_regs *regs,
> */
> WARN_ON_ONCE(user_mode(regs));
>
> + if (test_thread_flag(TIF_BLOCKSTEP)) {
> + /*
> + * The SDM says "The processor clears the BTF flag when it
> + * generates a debug exception." but PTRACE_BLOCKSTEP requested
> + * it for userspace, but we just took a kernel #DB, so re-set
> + * BTF.
> + */
> + unsigned long debugctl;
> +
> + rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
> + debugctl |= DEBUGCTLMSR_BTF;
> + wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
> + }
> +
> /*
> * Catch SYSENTER with TF set and clear DR_STEP. If this hit a
> * watchpoint at the same time then that will still be handled.
> @@ -935,6 +936,26 @@ static __always_inline void exc_debug_user(struct pt_regs *regs,
> irqentry_enter_from_user_mode(regs);
> instrumentation_begin();
>
> + /*
> + * Clear the virtual DR6 value, ptrace routines will set bits here for
> + * things we want signals for.
> + */
> + current->thread.virtual_dr6 = 0;
> +
> + /*
> + * If PTRACE requested SINGLE(BLOCK)STEP, make sure to reflect that in
> + * the ptrace visible DR6 copy.
> + */
> + if (test_thread_flag(TIF_BLOCKSTEP) || test_thread_flag(TIF_SINGLESTEP))
> + current->thread.virtual_dr6 |= (dr6 & DR_STEP);
I'm guessing that this would fail a much simpler test, though: have a
program use PUSHF to set TF and then read out DR6 from the SIGTRAP. I
can whip up such a test if you like.
Is there any compelling reason not to just drop the condition and do:
current->thread.virtual_dr6 |= (dr6 & DR_STEP);
unconditionally? This DR6 cause, along with ICEBP, have the
regrettable distinctions of being the only causes that a user program
can trigger all on its own without informing the kernel first. This
means that we can't fully separate the concept of "user mode is
single-stepping itself" from "ptrace or something else is causing the
kernel to single step a program."
I bet that, without making this tweak, the virtual_dr6 change will
regress some horrific Wine use case.
--Andy
next prev parent reply other threads:[~2020-10-26 23:30 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-26 14:33 [REGRESSION] x86/debug: After PTRACE_SINGLESTEP DR_STEP is no longer reported in dr6 Kyle Huey
2020-10-26 15:55 ` Peter Zijlstra
2020-10-26 16:05 ` Peter Zijlstra
2020-10-26 16:14 ` Kyle Huey
2020-10-26 16:31 ` Peter Zijlstra
2020-10-26 16:55 ` Peter Zijlstra
2020-10-26 17:12 ` Kyle Huey
2020-10-26 17:17 ` Peter Zijlstra
2020-10-26 18:36 ` Kyle Huey
2020-10-26 23:30 ` Andy Lutomirski [this message]
2020-10-26 23:45 ` Andy Lutomirski
2020-10-27 8:15 ` Peter Zijlstra
2020-10-27 8:19 ` Peter Zijlstra
2020-10-27 18:00 ` Andy Lutomirski
2020-10-27 19:20 ` Peter Zijlstra
2020-10-27 9:08 ` Peter Zijlstra
2020-10-27 17:56 ` Peter Zijlstra
2020-10-26 16:08 ` Kyle Huey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CALCETrVwzcpk88jWeNb+iCGBFsyzgbZ0E9_x330A2P-CMzSr4g@mail.gmail.com \
--to=luto@kernel.org \
--cc=alexandre.chartre@oracle.com \
--cc=boris.ostrovsky@oracle.com \
--cc=brgerst@gmail.com \
--cc=daniel.thompson@linaro.org \
--cc=frederic@kernel.org \
--cc=jgross@suse.com \
--cc=joel@joelfernandes.org \
--cc=jpoimboe@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=me@kylehuey.com \
--cc=mhiramat@kernel.org \
--cc=paulmck@kernel.org \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=pmladek@suse.com \
--cc=rocallahan@gmail.com \
--cc=rostedt@goodmis.org \
--cc=sean.j.christopherson@intel.com \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).