All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: tglx@linutronix.de, jpoimboe@redhat.com,
	linux-kernel@vger.kernel.org, x86@kernel.org,
	mhiramat@kernel.org, mbenes@suse.cz, jthierry@redhat.com,
	alexandre.chartre@oracle.com
Subject: Re: [PATCH v5 04/17] x86,ftrace: Fix ftrace_regs_caller() unwind
Date: Wed, 22 Apr 2020 11:44:46 +0200	[thread overview]
Message-ID: <20200422094446.GY20730@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <20200421203345.4e165c4b@oasis.local.home>

On Tue, Apr 21, 2020 at 08:33:45PM -0400, Steven Rostedt wrote:
> Hi Peter,
> 
> After looking at the code, I realized that the only possible way to do
> the "direct call" part, is if the direct function helper is executed
> there. All other ftrace_ops, will not call that path.
> 
> I added a trampoline that points to ftrace_regs_caller to the direct
> ftrace_ops, to force it never to allocate its own trampoline, and thus
> no created trampoline should ever do the jump for a direct caller.
> 
> By doing this, I was able to move the code around to be a bit simpler,
> and not need the double modifications (the double ret).
> 
> Of course, if any created trampoline were to touch the ORIG_RAX, then
> it would crash. We could always nop that compare in the created
> trampoline if that is a concern.
> 
> Anyway, I added the below patch on top of your patches and it appears
> to work. Thoughts?

So can I push out those patches as is? :-) We can do this on top I
suppose.

Firstly; your patch isn't objtool clean:

  arch/x86/kernel/ftrace_64.o: warning: objtool: ftrace_regs_caller()+0x199: sibling call from callable instruction with modified stack frame

So 10 points deduction for that :-). You got the RET_OFFSET hint on the
wrong sibling call.

Secondly, yes, that ORIG_RAX issue for copies, that _might_ crash and
burn, but who knows, you're jumping into uninitialized memory afaict. So
that definitely needs fixing. I'm also not sure of stubbing out the
test, that's actually more work than poking in the 1 extra ret and also
gives unexpected behaviour. If anything we should poke an UD2 at the 1:
location in the copy.

Over all, I'm thinking we should keep it as is. If you want to simplify
the poking we can do something like the below; reading a known retq is
daft.

---
diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c
index 867c126ddabe..b334adbacb0e 100644
--- a/arch/x86/kernel/ftrace.c
+++ b/arch/x86/kernel/ftrace.c
@@ -307,8 +307,6 @@ union ftrace_op_code_union {
 	} __attribute__((packed));
 };
 
-#define RET_SIZE		1
-
 static unsigned long
 create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
 {
@@ -319,7 +317,6 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
 	unsigned long offset;
 	unsigned long npages;
 	unsigned long size;
-	unsigned long retq;
 	unsigned long *ptr;
 	void *trampoline;
 	void *ip;
@@ -347,11 +344,11 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
 	 * the iret , as well as the address of the ftrace_ops this
 	 * trampoline is used for.
 	 */
-	trampoline = alloc_tramp(size + RET_SIZE + sizeof(void *));
+	trampoline = alloc_tramp(size + RET_INSN_SIZE + sizeof(void *));
 	if (!trampoline)
 		return 0;
 
-	*tramp_size = size + RET_SIZE + sizeof(void *);
+	*tramp_size = size + RET_INSN_SIZE + sizeof(void *);
 	npages = DIV_ROUND_UP(*tramp_size, PAGE_SIZE);
 
 	/* Copy ftrace_caller onto the trampoline memory */
@@ -359,19 +356,13 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
 	if (WARN_ON(ret < 0))
 		goto fail;
 
-	ip = trampoline + size;
-
 	/* The trampoline ends with ret(q) */
-	retq = (unsigned long)ftrace_stub;
-	ret = probe_kernel_read(ip, (void *)retq, RET_SIZE);
-	if (WARN_ON(ret < 0))
-		goto fail;
+	ip = trampoline + size;
+	*(u8 *)ip = RET_INSN_OPCODE;
 
 	if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
 		ip = trampoline + (ftrace_regs_caller_ret - ftrace_regs_caller);
-		ret = probe_kernel_read(ip, (void *)retq, RET_SIZE);
-		if (WARN_ON(ret < 0))
-			goto fail;
+		*(u8 *)ip = RET_INSN_OPCODE;
 	}
 
 	/*
@@ -382,7 +373,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
 	 * the global function_trace_op variable.
 	 */
 
-	ptr = (unsigned long *)(trampoline + size + RET_SIZE);
+	ptr = (unsigned long *)(trampoline + size + RET_INSN_SIZE);
 	*ptr = (unsigned long)ops;
 
 	op_offset -= start_offset;

  reply	other threads:[~2020-04-22  9:45 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-16 11:47 [PATCH v5 00/17] objtool: vmlinux.o and noinstr validation Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 01/17] objtool: Support multiple stack_op per instruction Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 02/17] objtool: Better handle IRET Peter Zijlstra
2020-04-17 11:29   ` Miroslav Benes
2020-04-17 12:25     ` Peter Zijlstra
2020-04-17 12:35       ` Miroslav Benes
2020-04-17 17:37   ` Alexandre Chartre
2020-04-17 18:23     ` Peter Zijlstra
2020-04-17 23:53       ` Andy Lutomirski
2020-04-18 17:18         ` Josh Poimboeuf
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 03/17] objtool: Introduce HINT_RET_OFFSET Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 04/17] x86,ftrace: Fix ftrace_regs_caller() unwind Peter Zijlstra
2020-04-17 19:24   ` Alexandre Chartre
2020-04-22  0:33   ` Steven Rostedt
2020-04-22  9:44     ` Peter Zijlstra [this message]
2020-04-22 13:33       ` Steven Rostedt
2020-04-22 20:20       ` Steven Rostedt
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 05/17] x86,ftrace: Use SIZEOF_PTREGS Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 06/17] x86,ftrace: Shrink ftrace_regs_caller() by one byte Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 07/17] objtool: Remove SAVE/RESTORE hints Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 08/17] objtool: Rename struct cfi_state Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 09/17] objtool: Fix !CFI insn_state propagation Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 10/17] objtool: Implement noinstr validation Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 11/17] objtool: Optimize !vmlinux.o again Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 12/17] objtool: Use sec_offset_hash() for insn_hash Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 13/17] kbuild/objtool: Add objtool-vmlinux.o pass Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 14/17] objtool: Avoid iterating !text section symbols Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 15/17] objtool: Rearrange validate_section() Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 16/17] objtool: Add STT_NOTYPE noinstr validation Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2020-04-23  7:49   ` tip-bot2 for Peter Zijlstra
2020-04-16 11:47 ` [PATCH v5 17/17] objtool: Also consider .entry.text as noinstr Peter Zijlstra
2020-04-22 22:24   ` [tip: objtool/core] " tip-bot2 for Thomas Gleixner
2020-04-23  7:49   ` tip-bot2 for Thomas Gleixner
2020-04-17 12:33 ` [PATCH v5 00/17] objtool: vmlinux.o and noinstr validation Miroslav Benes
2020-04-17 20:22 ` Alexandre Chartre

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=20200422094446.GY20730@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=alexandre.chartre@oracle.com \
    --cc=jpoimboe@redhat.com \
    --cc=jthierry@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.