From 3344c3595c0b4cfdcaeb278d4b18712159d7174a Mon Sep 17 00:00:00 2001 From: Mao Minkai Date: Wed, 31 Aug 2022 15:47:15 +0800 Subject: [PATCH] sw64: bpf: fix insn_offset Sunway inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I5PNHA -------------------------------- Since ctx->idx is the index of the next jited instruction, value of insn_offset should be set before build_insn(). Allocate 1 more entry for insn_offset[], and give epilogue_offset to it, so the correct jump offset can be calculated if the last instruction is BPF_JMP. Signed-off-by: Mao Minkai Signed-off-by: Gu Zitao --- arch/sw_64/net/bpf_jit_comp.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/sw_64/net/bpf_jit_comp.c b/arch/sw_64/net/bpf_jit_comp.c index 98ddb60200c8..10fc58eb4d37 100644 --- a/arch/sw_64/net/bpf_jit_comp.c +++ b/arch/sw_64/net/bpf_jit_comp.c @@ -516,8 +516,8 @@ static void jit_fill_hole(void *area, unsigned int size) static int bpf2sw64_offset(int bpf_idx, s32 off, const struct jit_ctx *ctx) { - int from = ctx->insn_offset[bpf_idx]; - int to = ctx->insn_offset[bpf_idx + off]; + int from = ctx->insn_offset[bpf_idx + 1]; + int to = ctx->insn_offset[bpf_idx + 1 + off]; if (ctx->image == NULL) return 0; @@ -1226,15 +1226,15 @@ static int build_body(struct jit_ctx *ctx) const struct bpf_insn *insn = &prog->insnsi[i]; int ret; + if (ctx->image == NULL) + ctx->insn_offset[i] = ctx->idx; ret = build_insn(insn, ctx); if (ret < 0) return ret; - if (ctx->image == NULL) - ctx->insn_offset[i] = ctx->idx; while (ret > 0) { i++; if (ctx->image == NULL) - ctx->insn_offset[i] = ctx->idx; + ctx->insn_offset[i] = ctx->insn_offset[i - 1]; ret--; } } @@ -1305,7 +1305,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) memset(&ctx, 0, sizeof(ctx)); ctx.prog = prog; - ctx.insn_offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL); + ctx.insn_offset = kcalloc(prog->len + 1, sizeof(int), GFP_KERNEL); if (ctx.insn_offset == NULL) { prog = orig_prog; goto out_off; @@ -1321,7 +1321,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) goto out_off; } - ctx.epilogue_offset = ctx.idx; + ctx.insn_offset[prog->len] = ctx.epilogue_offset = ctx.idx; build_epilogue(&ctx); /* Now we know the actual image size. */ -- GitLab