Skip to content

Commit e2c95a6

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf, ppc64: generalize fetching subprog into bpf_jit_get_func_addr
Make fetching of the BPF call address from ppc64 JIT generic. ppc64 was using a slightly different variant rather than through the insns' imm field encoding as the target address would not fit into that space. Therefore, the target subprog number was encoded into the insns' offset and fetched through fp->aux->func[off]->bpf_func instead. Given there are other JITs with this issue and the mechanism of fetching the address is JIT-generic, move it into the core as a helper instead. On the JIT side, we get information on whether the retrieved address is a fixed one, that is, not changing through JIT passes, or a dynamic one. For the former, JITs can optimize their imm emission because this doesn't change jump offsets throughout JIT process. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Sandipan Das <sandipan@linux.ibm.com> Tested-by: Sandipan Das <sandipan@linux.ibm.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 6950012 commit e2c95a6

3 files changed

Lines changed: 76 additions & 19 deletions

File tree

‎arch/powerpc/net/bpf_jit_comp64.c‎

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,33 @@ static void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
166166
PPC_BLR();
167167
}
168168

169-
static void bpf_jit_emit_func_call(u32 *image, struct codegen_context *ctx, u64 func)
169+
static void bpf_jit_emit_func_call_hlp(u32 *image, struct codegen_context *ctx,
170+
u64 func)
171+
{
172+
#ifdef PPC64_ELF_ABI_v1
173+
/* func points to the function descriptor */
174+
PPC_LI64(b2p[TMP_REG_2], func);
175+
/* Load actual entry point from function descriptor */
176+
PPC_BPF_LL(b2p[TMP_REG_1], b2p[TMP_REG_2], 0);
177+
/* ... and move it to LR */
178+
PPC_MTLR(b2p[TMP_REG_1]);
179+
/*
180+
* Load TOC from function descriptor at offset 8.
181+
* We can clobber r2 since we get called through a
182+
* function pointer (so caller will save/restore r2)
183+
* and since we don't use a TOC ourself.
184+
*/
185+
PPC_BPF_LL(2, b2p[TMP_REG_2], 8);
186+
#else
187+
/* We can clobber r12 */
188+
PPC_FUNC_ADDR(12, func);
189+
PPC_MTLR(12);
190+
#endif
191+
PPC_BLRL();
192+
}
193+
194+
static void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx,
195+
u64 func)
170196
{
171197
unsigned int i, ctx_idx = ctx->idx;
172198

@@ -273,7 +299,7 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
273299
{
274300
const struct bpf_insn *insn = fp->insnsi;
275301
int flen = fp->len;
276-
int i;
302+
int i, ret;
277303

278304
/* Start of epilogue code - will only be valid 2nd pass onwards */
279305
u32 exit_addr = addrs[flen];
@@ -284,8 +310,9 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
284310
u32 src_reg = b2p[insn[i].src_reg];
285311
s16 off = insn[i].off;
286312
s32 imm = insn[i].imm;
313+
bool func_addr_fixed;
314+
u64 func_addr;
287315
u64 imm64;
288-
u8 *func;
289316
u32 true_cond;
290317
u32 tmp_idx;
291318

@@ -711,23 +738,15 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
711738
case BPF_JMP | BPF_CALL:
712739
ctx->seen |= SEEN_FUNC;
713740

714-
/* bpf function call */
715-
if (insn[i].src_reg == BPF_PSEUDO_CALL)
716-
if (!extra_pass)
717-
func = NULL;
718-
else if (fp->aux->func && off < fp->aux->func_cnt)
719-
/* use the subprog id from the off
720-
* field to lookup the callee address
721-
*/
722-
func = (u8 *) fp->aux->func[off]->bpf_func;
723-
else
724-
return -EINVAL;
725-
/* kernel helper call */
726-
else
727-
func = (u8 *) __bpf_call_base + imm;
728-
729-
bpf_jit_emit_func_call(image, ctx, (u64)func);
741+
ret = bpf_jit_get_func_addr(fp, &insn[i], extra_pass,
742+
&func_addr, &func_addr_fixed);
743+
if (ret < 0)
744+
return ret;
730745

746+
if (func_addr_fixed)
747+
bpf_jit_emit_func_call_hlp(image, ctx, func_addr);
748+
else
749+
bpf_jit_emit_func_call_rel(image, ctx, func_addr);
731750
/* move return value from r3 to BPF_REG_0 */
732751
PPC_MR(b2p[BPF_REG_0], 3);
733752
break;

‎include/linux/filter.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,10 @@ void bpf_jit_binary_free(struct bpf_binary_header *hdr);
866866

867867
void bpf_jit_free(struct bpf_prog *fp);
868868

869+
int bpf_jit_get_func_addr(const struct bpf_prog *prog,
870+
const struct bpf_insn *insn, bool extra_pass,
871+
u64 *func_addr, bool *func_addr_fixed);
872+
869873
struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *fp);
870874
void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);
871875

‎kernel/bpf/core.c‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,40 @@ void __weak bpf_jit_free(struct bpf_prog *fp)
672672
bpf_prog_unlock_free(fp);
673673
}
674674

675+
int bpf_jit_get_func_addr(const struct bpf_prog *prog,
676+
const struct bpf_insn *insn, bool extra_pass,
677+
u64 *func_addr, bool *func_addr_fixed)
678+
{
679+
s16 off = insn->off;
680+
s32 imm = insn->imm;
681+
u8 *addr;
682+
683+
*func_addr_fixed = insn->src_reg != BPF_PSEUDO_CALL;
684+
if (!*func_addr_fixed) {
685+
/* Place-holder address till the last pass has collected
686+
* all addresses for JITed subprograms in which case we
687+
* can pick them up from prog->aux.
688+
*/
689+
if (!extra_pass)
690+
addr = NULL;
691+
else if (prog->aux->func &&
692+
off >= 0 && off < prog->aux->func_cnt)
693+
addr = (u8 *)prog->aux->func[off]->bpf_func;
694+
else
695+
return -EINVAL;
696+
} else {
697+
/* Address of a BPF helper call. Since part of the core
698+
* kernel, it's always at a fixed location. __bpf_call_base
699+
* and the helper with imm relative to it are both in core
700+
* kernel.
701+
*/
702+
addr = (u8 *)__bpf_call_base + imm;
703+
}
704+
705+
*func_addr = (unsigned long)addr;
706+
return 0;
707+
}
708+
675709
static int bpf_jit_blind_insn(const struct bpf_insn *from,
676710
const struct bpf_insn *aux,
677711
struct bpf_insn *to_buff)

0 commit comments

Comments
 (0)