Thread (45 messages) 45 messages, 6 authors, 2019-11-08

Re: [PATCH v2 bpf-next 02/17] bpf: Add bpf_arch_text_poke() helper

From: Alexei Starovoitov <hidden>
Date: 2019-11-07 17:50:53
Also in: bpf

On Thu, Nov 7, 2019 at 9:20 AM Song Liu [off-list ref] wrote:

quoted
On Nov 6, 2019, at 9:46 PM, Alexei Starovoitov [off-list ref] wrote:

Add bpf_arch_text_poke() helper that is used by BPF trampoline logic to patch
nops/calls in kernel text into calls into BPF trampoline and to patch
calls/nops inside BPF programs too.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
arch/x86/net/bpf_jit_comp.c | 51 +++++++++++++++++++++++++++++++++++++
include/linux/bpf.h         |  8 ++++++
kernel/bpf/core.c           |  6 +++++
3 files changed, 65 insertions(+)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 0399b1f83c23..8631d3bd637f 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -9,9 +9,11 @@
#include <linux/filter.h>
#include <linux/if_vlan.h>
#include <linux/bpf.h>
+#include <linux/memory.h>
#include <asm/extable.h>
#include <asm/set_memory.h>
#include <asm/nospec-branch.h>
+#include <asm/text-patching.h>

static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
{
@@ -487,6 +489,55 @@ static int emit_call(u8 **pprog, void *func, void *ip)
      return 0;
}

+int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
+                    void *old_addr, void *new_addr)
+{
+     u8 old_insn[NOP_ATOMIC5] = {};
+     u8 new_insn[NOP_ATOMIC5] = {};
+     u8 *prog;
+     int ret;
+
+     if (!is_kernel_text((long)ip))
+             /* BPF trampoline in modules is not supported */
+             return -EINVAL;
+
+     if (old_addr) {
+             prog = old_insn;
+             ret = emit_call(&prog, old_addr, (void *)ip);
+             if (ret)
+                     return ret;
+     }
+     if (old_addr) {
                ^ should be new_addr?
quoted
+             prog = new_insn;
+             ret = emit_call(&prog, old_addr, (void *)ip);
                                        ^^^ and here?
quoted
+             if (ret)
+                     return ret;
+     }
+     ret = -EBUSY;
+     mutex_lock(&text_mutex);
+     switch (t) {
+     case BPF_MOD_NOP_TO_CALL:
+             if (memcmp(ip, ideal_nops, 5))
Maybe use X86_CALL_SIZE instead of 5? And the five more "5" below?
ohh. yes. of course. I had it fixed.
NOP_ATOMIC5 above is incorrect as well. I had it fixed too.
Looks like I've lost another squash commit last night.
Sorry about that.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help