On 11/7/19 9:50 AM, Alexei Starovoitov wrote: > On Thu, Nov 7, 2019 at 9:20 AM Song Liu <songliubraving@xxxxxx> wrote: >> >> >> >>> On Nov 6, 2019, at 9:46 PM, Alexei Starovoitov <ast@xxxxxxxxxx> 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@xxxxxxxxxx> >>> --- >>> 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? >>> + prog = new_insn; >>> + ret = emit_call(&prog, old_addr, (void *)ip); >> ^^^ and here? >>> + 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. Argh. Now I see what happened. I squashed it into patch 3 instead of patch 2. sorry.