linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Masami Hiramatsu <mhiramat@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: tglx@linutronix.de, luto@kernel.org, me@kylehuey.com,
	x86@kernel.org, linux-kernel@vger.kernel.org,
	torvalds@linux-foundation.org, rocallahan@gmail.com,
	alexandre.chartre@oracle.com, paulmck@kernel.org,
	frederic@kernel.org, pbonzini@redhat.com,
	sean.j.christopherson@intel.com, pmladek@suse.com,
	joel@joelfernandes.org, rostedt@goodmis.org,
	boris.ostrovsky@oracle.com, jgross@suse.com, brgerst@gmail.com,
	jpoimboe@redhat.com, daniel.thompson@linaro.org,
	julliard@winehq.org, pgofman@codeweavers.com
Subject: Re: [PATCH 1/3] x86/debug: Fix BTF handling
Date: Wed, 28 Oct 2020 21:11:09 +0900	[thread overview]
Message-ID: <20201028211109.a25f52fa6fb0412e3a65ea52@kernel.org> (raw)
In-Reply-To: <20201028095919.GX2628@hirez.programming.kicks-ass.net>

On Wed, 28 Oct 2020 10:59:19 +0100
Peter Zijlstra <peterz@infradead.org> wrote:

> On Wed, Oct 28, 2020 at 06:20:25PM +0900, Masami Hiramatsu wrote:
> > On Tue, 27 Oct 2020 20:41:26 +0100
> > Peter Zijlstra <peterz@infradead.org> wrote:
> > 
> > > On Tue, Oct 27, 2020 at 10:15:05AM +0100, Peter Zijlstra wrote:
> > > 
> > > > @@ -873,6 +866,20 @@ static __always_inline void exc_debug_ke
> > > >  	 */
> > > >  	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.
> > > 
> > > Masami, how does BTF interact with !optimized kprobes that single-step?
> > 
> > Good question, BTF is cleared right before single-stepping and restored
> > after single-stepping. It will be done accoding to TIF_BLOCKSTEP bit as below.
> > 
> > (in arch/x86/kernel/kprobes/core.c)
> > 
> > static nokprobe_inline void clear_btf(void)
> > {
> >         if (test_thread_flag(TIF_BLOCKSTEP)) {
> >                 unsigned long debugctl = get_debugctlmsr();
> > 
> >                 debugctl &= ~DEBUGCTLMSR_BTF;
> >                 update_debugctlmsr(debugctl);
> >         }
> > }
> > 
> > static nokprobe_inline void restore_btf(void)
> > {
> >         if (test_thread_flag(TIF_BLOCKSTEP)) {
> >                 unsigned long debugctl = get_debugctlmsr();
> > 
> >                 debugctl |= DEBUGCTLMSR_BTF;
> >                 update_debugctlmsr(debugctl);
> >         }
> > }
> > 
> > Hrm, so it seems that we do same ... maybe we don't need clear_btf() too?
> 
> No, I think you do very much need clear_btf(). But with my patch perhaps
> restore_btf() is no longer needed. Is there only a single single-step
> between setup_singlestep() and resume_execution() ? (I think so).

It depends on what the single step instruction does, if it access to the non-present
memory (like user-memory) it kicks the fault handler instead of debug handler.
(e.g. putting a kprobe on the fixup source address in copy_from_user() )
Hmm, on this path, it seems not calling restore_btf()...

Thanks,

> Also, I note that we should employ get_debugctlmsr() more consistently.
> 
> > > The best answer I can come up with is 'poorly' :/
> > 
> > Is this what you expected? :)
> 
> Nah, I missed the above, you seems to do the right thing.


-- 
Masami Hiramatsu <mhiramat@kernel.org>

  reply	other threads:[~2020-10-29  0:59 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-27  9:15 [PATCH 0/3] x86/debug: Fixes Peter Zijlstra
2020-10-27  9:15 ` [PATCH 1/3] x86/debug: Fix BTF handling Peter Zijlstra
2020-10-27 19:41   ` Peter Zijlstra
2020-10-28  9:20     ` Masami Hiramatsu
2020-10-28  9:59       ` Peter Zijlstra
2020-10-28 12:11         ` Masami Hiramatsu [this message]
2020-10-28 14:31           ` [PATCH] x86/kprobes: Restore BTF if the single-stepping is cancelled Masami Hiramatsu
2020-12-07 23:22             ` Steven Rostedt
2020-12-09 18:44             ` [tip: perf/core] " tip-bot2 for Masami Hiramatsu
2020-10-27 22:19   ` [tip: x86/urgent] x86/debug: Fix BTF handling tip-bot2 for Peter Zijlstra
2020-10-27  9:15 ` [PATCH 2/3] x86/debug: Only clear/set ->virtual_dr6 for userspace #DB Peter Zijlstra
2020-10-27 22:19   ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2020-10-27  9:15 ` [PATCH 3/3] x86/debug: Fix PTRACE_{BLOCK,SINGLE}STEP vs ptrace_get_debugreg(6) Peter Zijlstra
2020-10-27 17:22   ` Kyle Huey
2020-10-27 18:33 ` [PATCH v2 3/3] Fix DR_STEP " Peter Zijlstra
2020-10-27 22:19   ` [tip: x86/urgent] x86/debug: " tip-bot2 for Peter Zijlstra

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=20201028211109.a25f52fa6fb0412e3a65ea52@kernel.org \
    --to=mhiramat@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=julliard@winehq.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=me@kylehuey.com \
    --cc=paulmck@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=pgofman@codeweavers.com \
    --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).