On Tue, Sep 26, 2023 at 12:00:20PM -0700, Song Liu wrote: SNIP > @@ -2665,25 +2672,61 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image > if (flags & BPF_TRAMP_F_SKIP_FRAME) > /* skip our return address and return to parent */ > EMIT4(0x48, 0x83, 0xC4, 8); /* add rsp, 8 */ > - emit_return(&prog, prog); > + emit_return(&prog, image + (prog - (u8 *)rw_image)); > /* Make sure the trampoline generation logic doesn't overflow */ > - if (WARN_ON_ONCE(prog > (u8 *)image_end - BPF_INSN_SAFETY)) { > + if (WARN_ON_ONCE(prog > (u8 *)rw_image_end - BPF_INSN_SAFETY)) { > ret = -EFAULT; > goto cleanup; > } > - ret = prog - (u8 *)image + BPF_INSN_SAFETY; > + ret = prog - (u8 *)rw_image + BPF_INSN_SAFETY; > > cleanup: > kfree(branches); > return ret; > } > > +void *arch_alloc_bpf_trampoline(int size) > +{ > + return bpf_prog_pack_alloc(size, jit_fill_hole); > +} > + > +void arch_free_bpf_trampoline(void *image, int size) > +{ > + bpf_prog_pack_free(image, size); > +} > + > +void arch_protect_bpf_trampoline(void *image, int size) > +{ > +} > + > +void arch_unprotect_bpf_trampoline(void *image, int size) > +{ > +} seems bit confusing having empty non weak functions to overload the weak versions IIUC would maybe some other way fit better than weak functions in here? like having arch specific macro to use bpf_prog_pack_alloc for trampoline allocation feel free to disregard if you have already investigated this ;-) jirka > + > int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end, > const struct btf_func_model *m, u32 flags, > struct bpf_tramp_links *tlinks, > void *func_addr) > { > - return __arch_prepare_bpf_trampoline(im, image, image_end, m, flags, tlinks, func_addr); > + void *rw_image, *tmp; > + int ret; > + u32 size = image_end - image; > + > + rw_image = bpf_jit_alloc_exec(size); > + if (!rw_image) > + return -ENOMEM; > + > + ret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m, > + flags, tlinks, func_addr); > + if (ret < 0) > + goto out; > + > + tmp = bpf_arch_text_copy(image, rw_image, size); > + if (IS_ERR(tmp)) > + ret = PTR_ERR(tmp); > +out: > + bpf_jit_free_exec(rw_image); > + return ret; > } > > int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, > @@ -2701,8 +2744,8 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, > if (!image) > return -ENOMEM; > > - ret = __arch_prepare_bpf_trampoline(&im, image, image + PAGE_SIZE, m, flags, > - tlinks, func_addr); > + ret = __arch_prepare_bpf_trampoline(&im, image, image + PAGE_SIZE, image, > + m, flags, tlinks, func_addr); > bpf_jit_free_exec(image); > return ret; > } > -- > 2.34.1 > >