From: "Björn Töpel" <bjorn.topel@gmail.com>
To: netdev@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net
Cc: "Björn Töpel" <bjorn.topel@intel.com>,
bpf@vger.kernel.org, magnus.karlsson@gmail.com,
magnus.karlsson@intel.com, jonathan.lemon@gmail.com
Subject: [RFC PATCH bpf-next 1/4] bpf: teach bpf_arch_text_poke() jumps
Date: Wed, 13 Nov 2019 21:47:34 +0100 [thread overview]
Message-ID: <20191113204737.31623-2-bjorn.topel@gmail.com> (raw)
In-Reply-To: <20191113204737.31623-1-bjorn.topel@gmail.com>
From: Björn Töpel <bjorn.topel@intel.com>
The BPF dispatcher, introduced in future commits, hijacks a trampoline
function. This commit teaches the text poker to emit jmpq in addtion
to callq.
Signed-off-by: Björn Töpel <bjorn.topel@intel.com>
---
arch/x86/net/bpf_jit_comp.c | 34 +++++++++++++++++++++++++++++-----
include/linux/bpf.h | 3 +++
2 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 79157d886a3e..28782a1c386e 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -481,7 +481,7 @@ static void emit_stx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
*pprog = prog;
}
-static int emit_call(u8 **pprog, void *func, void *ip)
+static int emit_call_jmp(u8 **pprog, void *func, void *ip, u8 insn)
{
u8 *prog = *pprog;
int cnt = 0;
@@ -492,17 +492,28 @@ static int emit_call(u8 **pprog, void *func, void *ip)
pr_err("Target call %p is out of range\n", func);
return -EINVAL;
}
- EMIT1_off32(0xE8, offset);
+ EMIT1_off32(insn, offset);
*pprog = prog;
return 0;
}
+static int emit_call(u8 **pprog, void *func, void *ip)
+{
+ return emit_call_jmp(pprog, func, ip, 0xE8);
+}
+
+/* Emits tail-call */
+static int emit_jmp(u8 **pprog, void *func, void *ip)
+{
+ return emit_call_jmp(pprog, func, ip, 0xE9);
+}
+
int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
void *old_addr, void *new_addr)
{
u8 old_insn[X86_CALL_SIZE] = {};
u8 new_insn[X86_CALL_SIZE] = {};
- u8 *prog;
+ u8 *prog, insn;
int ret;
if (!is_kernel_text((long)ip) &&
@@ -510,31 +521,44 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
/* BPF trampoline in modules is not supported */
return -EINVAL;
+ switch (t) {
+ case BPF_MOD_NOP_TO_CALL:
+ case BPF_MOD_CALL_TO_CALL:
+ case BPF_MOD_CALL_TO_NOP:
+ insn = 0xE8;
+ break;
+ default:
+ insn = 0xE9;
+ }
+
if (old_addr) {
prog = old_insn;
- ret = emit_call(&prog, old_addr, (void *)ip);
+ ret = emit_call_jmp(&prog, old_addr, (void *)ip, insn);
if (ret)
return ret;
}
if (new_addr) {
prog = new_insn;
- ret = emit_call(&prog, new_addr, (void *)ip);
+ ret = emit_call_jmp(&prog, new_addr, (void *)ip, insn);
if (ret)
return ret;
}
ret = -EBUSY;
mutex_lock(&text_mutex);
switch (t) {
+ case BPF_MOD_NOP_TO_JMP:
case BPF_MOD_NOP_TO_CALL:
if (memcmp(ip, ideal_nops[NOP_ATOMIC5], X86_CALL_SIZE))
goto out;
text_poke(ip, new_insn, X86_CALL_SIZE);
break;
+ case BPF_MOD_JMP_TO_JMP:
case BPF_MOD_CALL_TO_CALL:
if (memcmp(ip, old_insn, X86_CALL_SIZE))
goto out;
text_poke(ip, new_insn, X86_CALL_SIZE);
break;
+ case BPF_MOD_JMP_TO_NOP:
case BPF_MOD_CALL_TO_NOP:
if (memcmp(ip, old_insn, X86_CALL_SIZE))
goto out;
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 6a80af092048..38b0715050a9 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1270,6 +1270,9 @@ enum bpf_text_poke_type {
BPF_MOD_NOP_TO_CALL,
BPF_MOD_CALL_TO_CALL,
BPF_MOD_CALL_TO_NOP,
+ BPF_MOD_NOP_TO_JMP,
+ BPF_MOD_JMP_TO_JMP,
+ BPF_MOD_JMP_TO_NOP,
};
int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
void *addr1, void *addr2);
--
2.20.1
next prev parent reply other threads:[~2019-11-13 20:48 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-13 20:47 [RFC PATCH bpf-next 0/4] Introduce xdp_call.h and the BPF dispatcher Björn Töpel
2019-11-13 20:47 ` Björn Töpel [this message]
2019-11-13 20:47 ` [RFC PATCH bpf-next 2/4] bpf: introduce " Björn Töpel
2019-11-13 21:40 ` Edward Cree
2019-11-14 6:29 ` Björn Töpel
2019-11-14 10:18 ` Edward Cree
2019-11-14 11:21 ` Björn Töpel
2019-11-14 13:58 ` Peter Zijlstra
2019-11-14 12:31 ` Toke Høiland-Jørgensen
2019-11-14 13:03 ` Daniel Borkmann
2019-11-14 13:09 ` Toke Høiland-Jørgensen
2019-11-14 13:56 ` Björn Töpel
2019-11-14 14:55 ` Toke Høiland-Jørgensen
2019-11-14 15:03 ` Björn Töpel
2019-11-14 15:12 ` Toke Høiland-Jørgensen
2019-11-15 0:30 ` Alexei Starovoitov
2019-11-15 7:56 ` Björn Töpel
2019-11-15 21:58 ` Alexei Starovoitov
2019-11-18 10:03 ` Björn Töpel
2019-11-18 19:36 ` Andrii Nakryiko
2019-11-18 20:11 ` Björn Töpel
2019-11-13 20:47 ` [RFC PATCH bpf-next 3/4] xdp: introduce xdp_call Björn Töpel
2019-11-13 20:47 ` [RFC PATCH bpf-next 4/4] i40e: start using xdp_call.h Björn Töpel
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=20191113204737.31623-2-bjorn.topel@gmail.com \
--to=bjorn.topel@gmail.com \
--cc=ast@kernel.org \
--cc=bjorn.topel@intel.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=jonathan.lemon@gmail.com \
--cc=magnus.karlsson@gmail.com \
--cc=magnus.karlsson@intel.com \
--cc=netdev@vger.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