mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Masami Hiramatsu <mhiramat@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, Jiri Olsa <jolsa@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	lkml <linux-kernel@vger.kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@chromium.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	"Naveen N . Rao" <naveen.n.rao@linux.ibm.com>,
	Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
	"David S . Miller" <davem@davemloft.net>
Subject: Re: [PATCH v13 bpf-next 1/1] rethook: x86: Add rethook x86 implementation
Date: Wed, 23 Mar 2022 20:41:19 +0900	[thread overview]
Message-ID: <20220323204119.1feac1af0a1d58b8e63acd5d@kernel.org> (raw)
In-Reply-To: <YjrUxmABaohh1I8W@hirez.programming.kicks-ass.net>

On Wed, 23 Mar 2022 09:05:26 +0100
Peter Zijlstra <peterz@infradead.org> wrote:

> On Wed, Mar 23, 2022 at 11:34:59AM +0900, Masami Hiramatsu wrote:
> > Add rethook for x86 implementation. Most of the code has been copied from
> > kretprobes on x86.
> 
> Right; as said, I'm really unhappy with growing a carbon copy of this
> stuff instead of sharing. Can we *please* keep it a single instance?

OK, then let me update the kprobe side too.

> Them being basically indentical, it should be trivial to have
> CONFIG_KPROBE_ON_RETHOOK (or somesuch) and just share this.

Yes, ideally it should use CONFIG_HAVE_RETHOOK since the rethook arch port
must be a copy of the kretprobe implementation. But for safety, I think
having CONFIG_KPROBE_ON_RETHOOK is a good idea until replacing all kretprobe
implementations.

> 
> Also, what's rethook for anyway?

Rethook is a feature which hooks the function return. Most of the
logic came from the kretprobe. Simply to say, 'kretprobe - kprobe' is 
the rethook :)

Thank you,

> 
> > diff --git a/arch/x86/kernel/kprobes/common.h b/arch/x86/kernel/kprobes/common.h
> > index 7d3a2e2daf01..c993521d4933 100644
> > --- a/arch/x86/kernel/kprobes/common.h
> > +++ b/arch/x86/kernel/kprobes/common.h
> > @@ -6,6 +6,7 @@
> >  
> >  #include <asm/asm.h>
> >  #include <asm/frame.h>
> > +#include <asm/insn.h>
> >  
> >  #ifdef CONFIG_X86_64
> >  
> > diff --git a/arch/x86/kernel/rethook.c b/arch/x86/kernel/rethook.c
> > new file mode 100644
> > index 000000000000..3e916361c33b
> > --- /dev/null
> > +++ b/arch/x86/kernel/rethook.c
> > @@ -0,0 +1,121 @@
> > +// SPDX-License-Identifier: GPL-2.0-or-later
> > +/*
> > + * x86 implementation of rethook. Mostly copied from arch/x86/kernel/kprobes/core.c.
> > + */
> > +#include <linux/bug.h>
> > +#include <linux/rethook.h>
> > +#include <linux/kprobes.h>
> > +#include <linux/objtool.h>
> > +
> > +#include "kprobes/common.h"
> > +
> > +__visible void arch_rethook_trampoline_callback(struct pt_regs *regs);
> > +
> > +/*
> > + * When a target function returns, this code saves registers and calls
> > + * arch_rethook_trampoline_callback(), which calls the rethook handler.
> > + */
> > +asm(
> > +	".text\n"
> > +	".global arch_rethook_trampoline\n"
> > +	".type arch_rethook_trampoline, @function\n"
> > +	"arch_rethook_trampoline:\n"
> > +#ifdef CONFIG_X86_64
> > +	ANNOTATE_NOENDBR	/* This is only jumped from ret instruction */
> > +	/* Push a fake return address to tell the unwinder it's a kretprobe. */
> > +	"	pushq $arch_rethook_trampoline\n"
> > +	UNWIND_HINT_FUNC
> 	"	pushq $" __stringify(__KERNEL_DS) "\n" /* %ss */
> 	/* Save the 'sp - 16', this will be fixed later. */
> > +	"	pushq %rsp\n"
> > +	"	pushfq\n"
> > +	SAVE_REGS_STRING
> > +	"	movq %rsp, %rdi\n"
> > +	"	call arch_rethook_trampoline_callback\n"
> > +	RESTORE_REGS_STRING
> 	/* In the callback function, 'regs->flags' is copied to 'regs->ss'. */
> 
> 	this comment could do with a 'why' though... Because neither
> 	this nor the one in the handler really explains why it is
> 	important to have popf last
> 
> 	"	addq $16, %rsp\n"
> > +	"	popfq\n"
> > +#else
> 
> same for i386:
> 
> > +	/* Push a fake return address to tell the unwinder it's a kretprobe. */
> > +	"	pushl $arch_rethook_trampoline\n"
> > +	UNWIND_HINT_FUNC
> 	/* Save the 'sp - 8', this will be fixed later. */
> 	"	pushl %ss\n"
> > +	"	pushl %esp\n"
> > +	"	pushfl\n"
> > +	SAVE_REGS_STRING
> > +	"	movl %esp, %eax\n"
> > +	"	call arch_rethook_trampoline_callback\n"
> > +	RESTORE_REGS_STRING
> 	/* In the callback function, 'regs->flags' is copied to 'regs->ss'. */
> 	"	addl $8, %esp\n"
> > +	"	popfl\n"
> > +#endif
> > +	ASM_RET
> > +	".size arch_rethook_trampoline, .-arch_rethook_trampoline\n"
> > +);
> > +NOKPROBE_SYMBOL(arch_rethook_trampoline);
> > +
> > +/*
> > + * Called from arch_rethook_trampoline
> > + */
> > +__used __visible void arch_rethook_trampoline_callback(struct pt_regs *regs)
> > +{
> > +	unsigned long *frame_pointer;
> > +
> > +	/* fixup registers */
> > +	regs->cs = __KERNEL_CS;
> > +#ifdef CONFIG_X86_32
> > +	regs->gs = 0;
> > +#endif
> > +	regs->ip = (unsigned long)&arch_rethook_trampoline;
> > +	regs->orig_ax = ~0UL;
> 	regs->sp += 2*sizeof(long);
> > +	frame_pointer = &regs->sp + 1;
> > +
> > +	/*
> > +	 * The return address at 'frame_pointer' is recovered by the
> > +	 * arch_rethook_fixup_return() which called from this
> > +	 * rethook_trampoline_handler().
> > +	 */
> > +	rethook_trampoline_handler(regs, (unsigned long)frame_pointer);
> > +
> > +	/*
> > +	 * Copy FLAGS to 'pt_regs::sp' so that arch_rethook_trapmoline()
> > +	 * can do RET right after POPF.
> > +	 */
> 	regs->ss = regs->flags;
> > +}
> > +NOKPROBE_SYMBOL(arch_rethook_trampoline_callback);


-- 
Masami Hiramatsu <mhiramat@kernel.org>

  reply	other threads:[~2022-03-23 11:41 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-23  2:34 [PATCH v13 bpf-next 0/1] fprobe: Introduce fprobe function entry/exit probe Masami Hiramatsu
2022-03-23  2:34 ` [PATCH v13 bpf-next 1/1] rethook: x86: Add rethook x86 implementation Masami Hiramatsu
2022-03-23  8:05   ` Peter Zijlstra
2022-03-23 11:41     ` Masami Hiramatsu [this message]
2022-03-23 12:34       ` Peter Zijlstra
2022-03-23 15:14         ` Masami Hiramatsu
2022-03-25  2:03       ` Alexei Starovoitov
2022-03-25  2:21         ` Masami Hiramatsu
2022-03-25  2:41           ` Alexei Starovoitov
2022-03-23  5:42 ` [PATCH v13 bpf-next 0/1] fprobe: Introduce fprobe function entry/exit probe Masami Hiramatsu
2022-03-23 14:18 ` Mark Rutland
2022-03-23 14:55   ` Masami Hiramatsu
2022-03-23 16:47     ` Mark Rutland

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=20220323204119.1feac1af0a1d58b8e63acd5d@kernel.org \
    --to=mhiramat@kernel.org \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=anil.s.keshavamurthy@intel.com \
    --cc=ast@kernel.org \
    --cc=bp@alien8.de \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=davem@davemloft.net \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=naveen.n.rao@linux.ibm.com \
    --cc=netdev@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=songliubraving@fb.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    --cc=yhs@fb.com \
    /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

Powered by JetHome