linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Andy Lutomirski <luto@kernel.org>,
	Nicolai Stange <nstange@suse.de>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	"H. Peter Anvin" <hpa@zytor.com>,
	the arch/x86 maintainers <x86@kernel.org>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Jiri Kosina <jikos@kernel.org>, Miroslav Benes <mbenes@suse.cz>,
	Petr Mladek <pmladek@suse.com>,
	Joe Lawrence <joe.lawrence@redhat.com>,
	Shuah Khan <shuah@kernel.org>,
	Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>,
	Tim Chen <tim.c.chen@linux.intel.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Mimi Zohar <zohar@linux.ibm.com>, Juergen Gross <jgross@suse.com>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Nayna Jain <nayna@linux.ibm.com>,
	Masahiro Yamada <yamada.masahiro@socionext.com>,
	Joerg Roedel <jroedel@suse.de>,
	"open list:KERNEL SELFTEST FRAMEWORK" 
	<linux-kselftest@vger.kernel.org>,
	stable@vger.kernel.org
Subject: [RFC][PATCH 2/2] ftrace/x86: Emulate call function while updating in breakpoint handler
Date: Wed, 01 May 2019 16:28:32 -0400	[thread overview]
Message-ID: <20190501203152.561841784@goodmis.org> (raw)
In-Reply-To: 20190501202830.347656894@goodmis.org

From: Peter Zijlstra <peterz@infradead.org>

Nicolai Stange discovered[1] that if live kernel patching is enabled, and the
function tracer started tracing the same function that was patched, the
conversion of the fentry call site during the translation of going from
calling the live kernel patch trampoline to the iterator trampoline, would
have as slight window where it didn't call anything.

As live kernel patching depends on ftrace to always call its code (to
prevent the function being traced from being called, as it will redirect
it). This small window would allow the old buggy function to be called, and
this can cause undesirable results.

Nicolai submitted new patches[2] but these were controversial. As this is
similar to the static call emulation issues that came up a while ago[3].
But after some debate[4][5] adding a gap in the stack when entering the
breakpoint handler allows for pushing the return address onto the stack to
easily emulate a call.

[1] http://lkml.kernel.org/r/20180726104029.7736-1-nstange@suse.de
[2] http://lkml.kernel.org/r/20190427100639.15074-1-nstange@suse.de
[3] http://lkml.kernel.org/r/3cf04e113d71c9f8e4be95fb84a510f085aa4afa.1541711457.git.jpoimboe@redhat.com
[4] http://lkml.kernel.org/r/CAHk-=wh5OpheSU8Em_Q3Hg8qw_JtoijxOdPtHru6d+5K8TWM=A@mail.gmail.com
[5] http://lkml.kernel.org/r/CAHk-=wjvQxY4DvPrJ6haPgAa6b906h=MwZXO6G8OtiTGe=N7_w@mail.gmail.com

Cc: Andy Lutomirski <luto@kernel.org>
Cc: Nicolai Stange <nstange@suse.de>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: the arch/x86 maintainers <x86@kernel.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Miroslav Benes <mbenes@suse.cz>
Cc: Petr Mladek <pmladek@suse.com>
Cc: Joe Lawrence <joe.lawrence@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Mimi Zohar <zohar@linux.ibm.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Nayna Jain <nayna@linux.ibm.com>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: Joerg Roedel <jroedel@suse.de>
Cc: "open list:KERNEL SELFTEST FRAMEWORK" <linux-kselftest@vger.kernel.org>
Cc: stable@vger.kernel.org
Fixes: b700e7f03df5 ("livepatch: kernel: add support for live patching")
Signed-off-by: *** Need SoB From Peter Zijlstra ***
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 arch/x86/kernel/ftrace.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c
index ef49517f6bb2..fd152f5a937b 100644
--- a/arch/x86/kernel/ftrace.c
+++ b/arch/x86/kernel/ftrace.c
@@ -29,6 +29,7 @@
 #include <asm/kprobes.h>
 #include <asm/ftrace.h>
 #include <asm/nops.h>
+#include <asm/text-patching.h>
 
 #ifdef CONFIG_DYNAMIC_FTRACE
 
@@ -231,6 +232,7 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
 }
 
 static unsigned long ftrace_update_func;
+static unsigned long ftrace_update_func_call;
 
 static int update_ftrace_func(unsigned long ip, void *new)
 {
@@ -259,6 +261,8 @@ int ftrace_update_ftrace_func(ftrace_func_t func)
 	unsigned char *new;
 	int ret;
 
+	ftrace_update_func_call = (unsigned long)func;
+
 	new = ftrace_call_replace(ip, (unsigned long)func);
 	ret = update_ftrace_func(ip, new);
 
@@ -294,13 +298,21 @@ int ftrace_int3_handler(struct pt_regs *regs)
 	if (WARN_ON_ONCE(!regs))
 		return 0;
 
-	ip = regs->ip - 1;
-	if (!ftrace_location(ip) && !is_ftrace_caller(ip))
-		return 0;
+	ip = regs->ip - INT3_INSN_SIZE;
 
-	regs->ip += MCOUNT_INSN_SIZE - 1;
+	if (ftrace_location(ip)) {
+		int3_emulate_call(regs, (unsigned long)ftrace_regs_caller);
+		return 1;
+	} else if (is_ftrace_caller(ip)) {
+		if (!ftrace_update_func_call) {
+			int3_emulate_jmp(regs, ip + CALL_INSN_SIZE);
+			return 1;
+		}
+		int3_emulate_call(regs, ftrace_update_func_call);
+		return 1;
+	}
 
-	return 1;
+	return 0;
 }
 NOKPROBE_SYMBOL(ftrace_int3_handler);
 
@@ -859,6 +871,8 @@ void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
 
 	func = ftrace_ops_get_func(ops);
 
+	ftrace_update_func_call = (unsigned long)func;
+
 	/* Do a safe modify in case the trampoline is executing */
 	new = ftrace_call_replace(ip, (unsigned long)func);
 	ret = update_ftrace_func(ip, new);
@@ -960,6 +974,7 @@ static int ftrace_mod_jmp(unsigned long ip, void *func)
 {
 	unsigned char *new;
 
+	ftrace_update_func_call = 0UL;
 	new = ftrace_jmp_replace(ip, (unsigned long)func);
 
 	return update_ftrace_func(ip, new);
-- 
2.20.1



  parent reply	other threads:[~2019-05-01 20:31 UTC|newest]

Thread overview: 103+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-01 20:28 [RFC][PATCH 0/2] ftrace/x86: Allow for breakpoint handlers to emulate call functions Steven Rostedt
2019-05-01 20:28 ` [RFC][PATCH 1/2] x86: Allow breakpoints " Steven Rostedt
2019-05-02  3:24   ` Steven Rostedt
2019-05-02 16:21     ` Peter Zijlstra
2019-05-02 16:29       ` Peter Zijlstra
2019-05-02 18:02       ` Linus Torvalds
2019-05-02 18:18         ` Peter Zijlstra
2019-05-02 18:30           ` Peter Zijlstra
2019-05-02 18:43           ` Linus Torvalds
2019-05-02 19:28             ` Jiri Kosina
2019-05-02 20:25               ` Andy Lutomirski
2019-05-02 20:21             ` Peter Zijlstra
2019-05-02 20:49               ` Linus Torvalds
2019-05-02 21:32                 ` Peter Zijlstra
2019-05-03 19:24                 ` Steven Rostedt
2019-05-03 21:46                   ` Linus Torvalds
2019-05-03 22:49                     ` Steven Rostedt
2019-05-03 23:07                       ` Linus Torvalds
2019-05-04  4:17                         ` Steven Rostedt
     [not found]                           ` <CAHk-=wiuSFbv_rELND-BLWcP0GSZ0yF=xOAEcf61GE3bU9d=yg@mail.gmail.com>
2019-05-04 18:59                             ` Linus Torvalds
2019-05-04 20:12                               ` Andy Lutomirski
2019-05-04 20:28                                 ` Linus Torvalds
2019-05-04 20:36                                 ` Linus Torvalds
2019-05-03 22:55                     ` Andy Lutomirski
2019-05-03 23:16                       ` Linus Torvalds
2019-05-03 23:32                         ` Andy Lutomirski
2019-05-02 22:52               ` Steven Rostedt
2019-05-02 23:31                 ` Steven Rostedt
2019-05-02 23:50                   ` Steven Rostedt
2019-05-03  1:51                     ` [RFC][PATCH 1/2 v2] " Steven Rostedt
2019-05-03  9:29                     ` [RFC][PATCH 1/2] " Peter Zijlstra
2019-05-03 13:22                       ` Steven Rostedt
2019-05-03 16:20                         ` Andy Lutomirski
2019-05-03 16:31                           ` Steven Rostedt
2019-05-03 16:35                             ` Peter Zijlstra
2019-05-03 16:44                               ` Andy Lutomirski
2019-05-03 16:49                                 ` Steven Rostedt
2019-05-03 16:32                           ` Peter Zijlstra
2019-05-03 18:57                           ` Linus Torvalds
2019-05-06  8:19                             ` Peter Zijlstra
2019-05-06 13:56                               ` Steven Rostedt
2019-05-06 16:17                                 ` Linus Torvalds
2019-05-06 16:19                                   ` Linus Torvalds
2019-05-06 17:06                                   ` Steven Rostedt
2019-05-06 18:06                                     ` Linus Torvalds
2019-05-06 18:57                                       ` Steven Rostedt
2019-05-06 19:46                                         ` Linus Torvalds
2019-05-06 20:29                                           ` Steven Rostedt
2019-05-06 20:42                                             ` Linus Torvalds
2019-05-06 20:44                                               ` Linus Torvalds
2019-05-06 21:45                                               ` Steven Rostedt
2019-05-06 22:06                                                 ` Linus Torvalds
2019-05-06 22:31                                                   ` Linus Torvalds
2019-05-07  0:10                                                     ` Steven Rostedt
2019-05-07  1:06                                                       ` Linus Torvalds
2019-05-07  1:04                                                   ` Steven Rostedt
2019-05-07  1:34                                                     ` Steven Rostedt
2019-05-07  1:34                                                     ` Linus Torvalds
2019-05-07  1:53                                                       ` Steven Rostedt
2019-05-07  2:22                                                         ` Linus Torvalds
2019-05-07  2:58                                                           ` Steven Rostedt
2019-05-07  3:05                                                             ` Linus Torvalds
2019-05-07  3:21                                                               ` Steven Rostedt
2019-05-07  3:28                                                                 ` Linus Torvalds
2019-05-07 14:54                                                                   ` Linus Torvalds
2019-05-07 15:12                                                                     ` Steven Rostedt
2019-05-07 15:25                                                                       ` Steven Rostedt
2019-05-07 16:25                                                                         ` Steven Rostedt
2019-05-07 15:31                                                                       ` Linus Torvalds
2019-05-07 15:45                                                                         ` Steven Rostedt
2019-05-07 16:34                                                                         ` Peter Zijlstra
2019-05-07 17:08                                                                           ` Linus Torvalds
2019-05-07 17:21                                                                             ` Josh Poimboeuf
2019-05-07 21:24                                                                               ` Steven Rostedt
2019-05-08  4:50                                                                                 ` Linus Torvalds
2019-05-08 16:37                                                                                   ` Steven Rostedt
2019-05-07 17:38                                                                             ` Peter Zijlstra
2019-05-07  9:51                                                           ` Peter Zijlstra
2019-05-07 14:48                                                           ` Andy Lutomirski
2019-05-07 14:57                                                             ` Linus Torvalds
2019-05-07 14:13                                                 ` Masami Hiramatsu
2019-05-07 17:15                                                   ` Masami Hiramatsu
2019-05-06 14:22                               ` Peter Zijlstra
2019-05-07  8:57                               ` Peter Zijlstra
2019-05-07  9:18                                 ` David Laight
2019-05-07 11:30                                   ` Peter Zijlstra
2019-05-07 12:57                                     ` David Laight
2019-05-07 13:14                                       ` Steven Rostedt
2019-05-07 14:50                                         ` David Laight
2019-05-07 14:57                                           ` Steven Rostedt
2019-05-07 15:46                                             ` David Laight
2019-05-07 13:32                                       ` Peter Zijlstra
2019-05-07  9:27                                 ` Peter Zijlstra
2019-05-07 12:27                                   ` Steven Rostedt
2019-05-07 12:41                                     ` Peter Zijlstra
2019-05-07 12:54                                       ` Steven Rostedt
2019-05-07 17:22                                         ` Masami Hiramatsu
2019-05-07 14:28                                 ` Peter Zijlstra
2019-05-02 20:48         ` Steven Rostedt
2019-05-06 15:14         ` Josh Poimboeuf
2019-05-01 20:28 ` Steven Rostedt [this message]
2019-05-03 10:22 ` [RFC][PATCH 1.5/2] x86: Add int3_emulate_call() selftest Peter Zijlstra
2019-05-03 18:46   ` Steven Rostedt

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=20190501203152.561841784@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=bigeasy@linutronix.de \
    --cc=bp@alien8.de \
    --cc=hpa@zytor.com \
    --cc=jgross@suse.com \
    --cc=jikos@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@redhat.com \
    --cc=jroedel@suse.de \
    --cc=konrad.wilk@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mbenes@suse.cz \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.com \
    --cc=nayna@linux.ibm.com \
    --cc=ndesaulniers@google.com \
    --cc=nstange@suse.de \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=shuah@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=tim.c.chen@linux.intel.com \
    --cc=torvalds@linux-foundation.org \
    --cc=x86@kernel.org \
    --cc=yamada.masahiro@socionext.com \
    --cc=zohar@linux.ibm.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;
as well as URLs for NNTP newsgroup(s).