linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Masami Hiramatsu <mhiramat@kernel.org>
To: Andy Lutomirski <luto@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
	"David S. Miller" <davem@davemloft.net>, X86 ML <x86@kernel.org>,
	Andrew Cooper <andrew.cooper3@citrix.com>
Subject: Re: Why do kprobes and uprobes singlestep?
Date: Wed, 24 Feb 2021 10:17:56 +0900	[thread overview]
Message-ID: <20210224101756.bbdf95b9b6dfc982bff21324@kernel.org> (raw)
In-Reply-To: <CALCETrXzXv-V3A3SpN_Pdj_PNG8Gw0AVsZD7+VO-q_xCAu2T2A@mail.gmail.com>

On Tue, 23 Feb 2021 15:24:19 -0800
Andy Lutomirski <luto@kernel.org> wrote:

> A while back, I let myself be convinced that kprobes genuinely need to
> single-step the kernel on occasion, and I decided that this sucked but
> I could live with it.  it would, however, be Really Really Nice (tm)
> if we could have a rule that anyone running x86 Linux who single-steps
> the kernel (e.g. kgdb and nothing else) gets to keep all the pieces
> when the system falls apart around them.  Specifically, if we don't
> allow kernel single-stepping and if we suitably limit kernel
> instruction breakpoints (the latter isn't actually a major problem),
> then we don't really really need to use IRET to return to the kernel,
> and that means we can avoid some massive NMI nastiness.

Would you mean using "pop regs + popf + ret" instead of IRET after
int3 handled for avoiding IRET releasing the NMI mask? Yeah, it is
possible. I don't complain about that.

However, what is the relationship between the IRET and single-stepping?
I think we can do same thing in do_debug...

> But I was contemplating the code, and I'm no longer convinced.
> Uprobes seem to single-step user code for no discernable reason.
> (They want to trap after executing an out of line instruction, AFAICT.
> Surely INT3 or even CALL after the out-of-line insn would work as well
> or better.)  Why does kprobe single-step?  I spend a while staring at
> the code, and it was entirely unclear to me what the purpose of the
> single-step is.

For kprobes, there are 2 major reasons for (still relaying on) single stepping.
One is to provide post_handler, another is executing the original code,
which is replaced by int3, without modifying code nor emulation.
Indeed, most of the instructions actually not depends on the ip register,
in that case (and user doesn't set post_handler), kprobe already skips
single stepping (a.k.a. kprobe booster, jump back to the kernel code after
executing out-of-line instruction.)
However, since some instructions, e.g. jump, call and ret, changes the ip
register (and stack), we have to do a fixup afterwards. 

But yes, it is possible to emulate, as same as arm/arm64 does. I just
concern about side-effects of the emulation, need to be carefully
implemented.

Thank you,

-- 
Masami Hiramatsu <mhiramat@kernel.org>

  reply	other threads:[~2021-02-24  1:25 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-23 23:24 Why do kprobes and uprobes singlestep? Andy Lutomirski
2021-02-24  1:17 ` Masami Hiramatsu [this message]
2021-02-24 19:45   ` Andy Lutomirski
2021-02-25  2:22     ` Masami Hiramatsu
2021-02-25  6:03       ` Andy Lutomirski
2021-02-25  9:11         ` Masami Hiramatsu
2021-03-01 14:08       ` [RFC PATCH 0/1] x86/kprobes: Remoev single-step trap from x86 kprobes Masami Hiramatsu
2021-03-01 14:08         ` [RFC PATCH 1/1] x86/kprobes: Use int3 instead of debug trap for single-step Masami Hiramatsu
2021-03-02  8:06           ` Peter Zijlstra
2021-03-02  8:38           ` Peter Zijlstra
2021-03-02  8:41           ` Peter Zijlstra
2021-03-02  8:54             ` Peter Zijlstra
2021-03-02 12:51               ` Masami Hiramatsu
2021-03-02 13:58               ` Peter Zijlstra
2021-03-02 15:25       ` [PATCH -tip 0/3] x86/kprobes: Remoev single-step trap from x86 kprobes Masami Hiramatsu
2021-03-02 15:25         ` [PATCH -tip 1/3] x86/kprobes: Retrieve correct opcode for group instruction Masami Hiramatsu
2021-03-23 15:15           ` [tip: x86/core] " tip-bot2 for Masami Hiramatsu
2021-03-02 15:25         ` [PATCH -tip 2/3] x86/kprobes: Identify far indirect JMP correctly Masami Hiramatsu
2021-03-23 15:15           ` [tip: x86/core] " tip-bot2 for Masami Hiramatsu
2021-03-02 15:25         ` [PATCH -tip 3/3] x86/kprobes: Use int3 instead of debug trap for single-step Masami Hiramatsu
2021-03-23 15:15           ` [tip: x86/core] " tip-bot2 for Masami Hiramatsu
2021-03-17 14:55         ` [PATCH -tip 0/3] x86/kprobes: Remoev single-step trap from x86 kprobes Masami Hiramatsu
2021-03-17 16:26           ` Peter Zijlstra
2021-03-17 17:45             ` Andy Lutomirski
2021-02-25  9:59     ` Why do kprobes and uprobes singlestep? Peter Zijlstra
2021-03-01 16:51 ` Oleg Nesterov
2021-03-02  1:36   ` Andy Lutomirski
2021-03-02 20:24     ` Alexei Starovoitov
2021-03-02 21:02       ` Andy Lutomirski
2021-03-03  1:22         ` Alexei Starovoitov
2021-03-03  1:46           ` Andy Lutomirski
2021-03-03  2:18             ` Alexei Starovoitov
2021-03-03 13:27               ` Oleg Nesterov
2021-03-03 18:11               ` Daniel Xu
2021-03-03 19:14                 ` Andy Lutomirski
2021-03-02 20:25     ` Oleg Nesterov
2021-03-02 20:35       ` Andy Lutomirski
2021-03-02 20:28     ` Oleg Nesterov
2021-03-02  2:22   ` Masami Hiramatsu
2021-03-02  2:48     ` Andy Lutomirski
2021-03-02 20:31     ` Oleg Nesterov

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=20210224101756.bbdf95b9b6dfc982bff21324@kernel.org \
    --to=mhiramat@kernel.org \
    --cc=andrew.cooper3@citrix.com \
    --cc=anil.s.keshavamurthy@intel.com \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.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).