All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexei Starovoitov <ast@kernel.org>
To: <davem@davemloft.net>
Cc: <daniel@iogearbox.net>, <peterz@infradead.org>,
	<rostedt@goodmis.org>, <x86@kernel.org>, <netdev@vger.kernel.org>,
	<bpf@vger.kernel.org>, <kernel-team@fb.com>
Subject: [PATCH bpf-next 1/7] bpf, ftrace: temporary workaround
Date: Sat, 2 Nov 2019 15:00:19 -0700	[thread overview]
Message-ID: <20191102220025.2475981-2-ast@kernel.org> (raw)
In-Reply-To: <20191102220025.2475981-1-ast@kernel.org>

Add temporary workaround until proper register_ftrace_direct() api lands. This
commit must be reverted during upcoming merge window. The hack functions don't
have safety checks that ftrace api performs.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 arch/x86/kernel/ftrace.c | 36 ++++++++++++++++++++++++++++++++++++
 include/linux/bpf.h      |  5 +++++
 kernel/bpf/core.c        | 30 ++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)

diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c
index 024c3053dbba..0fd7643da92d 100644
--- a/arch/x86/kernel/ftrace.c
+++ b/arch/x86/kernel/ftrace.c
@@ -265,6 +265,42 @@ static int update_ftrace_func(unsigned long ip, void *new)
 	return ret;
 }
 
+/*
+ * There are no safety checks here. It's a temporary hack until proper
+ * register_ftrace_direct() api lands. These functions blindly rewrite kernel
+ * text. Proper register_ftrace_direct() should check that IP is one of allowed
+ * fentry points. unregister_ftrace_direct() and modify_ftrace_direct() should
+ * do similar checks.
+ */
+int arch_register_ftrace_hack(unsigned long ip, unsigned long addr)
+{
+	u8 *new;
+	int ret;
+
+	ftrace_arch_code_modify_prepare();
+	new = ftrace_call_replace(ip, addr);
+	ret = update_ftrace_func(ip, new);
+	ftrace_arch_code_modify_post_process();
+	return ret;
+}
+
+int arch_unregister_ftrace_hack(unsigned long ip, unsigned long addr)
+{
+	u8 *old;
+	int ret;
+
+	ftrace_arch_code_modify_prepare();
+	old = (void *)ftrace_nop_replace();
+	ret = update_ftrace_func(ip, old);
+	ftrace_arch_code_modify_post_process();
+	return ret;
+}
+
+int arch_modify_ftrace_hack(unsigned long ip, unsigned long addr)
+{
+	return arch_register_ftrace_hack(ip, addr);
+}
+
 int ftrace_update_ftrace_func(ftrace_func_t func)
 {
 	unsigned long ip = (unsigned long)(&ftrace_call);
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 7c7f518811a6..a8941f113298 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1157,4 +1157,9 @@ static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
 }
 #endif /* CONFIG_INET */
 
+/* workaround */
+int register_ftrace_direct(unsigned long ip, unsigned long addr);
+int unregister_ftrace_direct(unsigned long ip, unsigned long addr);
+int modify_ftrace_direct(unsigned long ip, unsigned long addr);
+
 #endif /* _LINUX_BPF_H */
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index df82d5a42b23..1bacf70e6509 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -2140,6 +2140,36 @@ int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
 	return -EFAULT;
 }
 
+int __weak arch_register_ftrace_hack(unsigned long ip, unsigned long addr)
+{
+	return -ENOTSUPP;
+}
+
+int __weak register_ftrace_direct(unsigned long ip, unsigned long addr)
+{
+	return arch_register_ftrace_hack(ip, addr);
+}
+
+int __weak arch_unregister_ftrace_hack(unsigned long ip, unsigned long addr)
+{
+	return -ENOTSUPP;
+}
+
+int __weak unregister_ftrace_direct(unsigned long ip, unsigned long addr)
+{
+	return arch_unregister_ftrace_hack(ip, addr);
+}
+
+int __weak arch_modify_ftrace_hack(unsigned long ip, unsigned long addr)
+{
+	return -ENOTSUPP;
+}
+
+int __weak modify_ftrace_direct(unsigned long ip, unsigned long addr)
+{
+	return arch_modify_ftrace_hack(ip, addr);
+}
+
 DEFINE_STATIC_KEY_FALSE(bpf_stats_enabled_key);
 EXPORT_SYMBOL(bpf_stats_enabled_key);
 
-- 
2.23.0


  reply	other threads:[~2019-11-02 22:01 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-02 22:00 [PATCH bpf-next 0/7] Introduce BPF trampoline Alexei Starovoitov
2019-11-02 22:00 ` Alexei Starovoitov [this message]
2019-11-02 22:00 ` [PATCH bpf-next 2/7] bpf: refactor x86 JIT into helpers Alexei Starovoitov
2019-11-02 22:00 ` [PATCH bpf-next 3/7] bpf: Introduce BPF trampoline Alexei Starovoitov
2019-11-05 19:51   ` Andrii Nakryiko
2019-11-02 22:00 ` [PATCH bpf-next 4/7] libbpf: Add support to attach to fentry/fexit tracing progs Alexei Starovoitov
2019-11-05 21:17   ` Andrii Nakryiko
2019-11-05 23:17     ` Alexei Starovoitov
2019-11-02 22:00 ` [PATCH bpf-next 5/7] selftest/bpf: Simple test for fentry/fexit Alexei Starovoitov
2019-11-05 21:37   ` Andrii Nakryiko
2019-11-02 22:00 ` [PATCH bpf-next 6/7] bpf: Add kernel test functions for fentry testing Alexei Starovoitov
2019-11-02 22:00 ` [PATCH bpf-next 7/7] selftests/bpf: Add test for BPF trampoline Alexei Starovoitov
2019-11-05 21:50   ` Andrii Nakryiko
2019-11-05 14:31 ` [PATCH bpf-next 0/7] Introduce " Alexei Starovoitov
2019-11-05 15:40   ` Steven Rostedt
2019-11-05 15:47     ` Alexei Starovoitov
2019-11-05 16:00       ` Steven Rostedt
2019-11-05 16:28         ` Alexei Starovoitov
2019-11-05 17:26           ` Steven Rostedt
2019-11-05 17:59             ` Alexei Starovoitov

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=20191102220025.2475981-2-ast@kernel.org \
    --to=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.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 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.