mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-04-29 12:28:27 +02:00
powerpc64/bpf: fix kfunc call support
[ Upstream commit01b6ac7272] Commit61688a82e0("powerpc/bpf: enable kfunc call") inadvertently enabled kfunc call support for 32-bit powerpc but that support will not be possible until ABI mismatch between 32-bit powerpc and eBPF is handled in 32-bit powerpc JIT code. Till then, advertise support only for 64-bit powerpc. Also, in powerpc ABI, caller needs to extend the arguments properly based on signedness. The JIT code is responsible for handling this explicitly for kfunc calls as verifier can't handle this for each architecture-specific ABI needs. But this was not taken care of while kfunc call support was enabled for powerpc. Fix it by handling this with bpf_jit_find_kfunc_model() and using zero_extend() & sign_extend() helper functions. Fixes:61688a82e0("powerpc/bpf: enable kfunc call") Cc: stable@vger.kernel.org Signed-off-by: Hari Bathini <hbathini@linux.ibm.com> Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com> Link: https://patch.msgid.link/20260303181031.390073-7-hbathini@linux.ibm.com Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
a87072464e
commit
c75bdffc07
@@ -362,7 +362,7 @@ void bpf_jit_free(struct bpf_prog *fp)
|
||||
|
||||
bool bpf_jit_supports_kfunc_call(void)
|
||||
{
|
||||
return true;
|
||||
return IS_ENABLED(CONFIG_PPC64);
|
||||
}
|
||||
|
||||
bool bpf_jit_supports_far_kfunc_call(void)
|
||||
|
||||
@@ -293,6 +293,83 @@ int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int zero_extend(u32 *image, struct codegen_context *ctx, u32 src_reg, u32 dst_reg, u32 size)
|
||||
{
|
||||
switch (size) {
|
||||
case 1:
|
||||
/* zero-extend 8 bits into 64 bits */
|
||||
EMIT(PPC_RAW_RLDICL(dst_reg, src_reg, 0, 56));
|
||||
return 0;
|
||||
case 2:
|
||||
/* zero-extend 16 bits into 64 bits */
|
||||
EMIT(PPC_RAW_RLDICL(dst_reg, src_reg, 0, 48));
|
||||
return 0;
|
||||
case 4:
|
||||
/* zero-extend 32 bits into 64 bits */
|
||||
EMIT(PPC_RAW_RLDICL(dst_reg, src_reg, 0, 32));
|
||||
fallthrough;
|
||||
case 8:
|
||||
/* Nothing to do */
|
||||
return 0;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int sign_extend(u32 *image, struct codegen_context *ctx, u32 src_reg, u32 dst_reg, u32 size)
|
||||
{
|
||||
switch (size) {
|
||||
case 1:
|
||||
/* sign-extend 8 bits into 64 bits */
|
||||
EMIT(PPC_RAW_EXTSB(dst_reg, src_reg));
|
||||
return 0;
|
||||
case 2:
|
||||
/* sign-extend 16 bits into 64 bits */
|
||||
EMIT(PPC_RAW_EXTSH(dst_reg, src_reg));
|
||||
return 0;
|
||||
case 4:
|
||||
/* sign-extend 32 bits into 64 bits */
|
||||
EMIT(PPC_RAW_EXTSW(dst_reg, src_reg));
|
||||
fallthrough;
|
||||
case 8:
|
||||
/* Nothing to do */
|
||||
return 0;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle powerpc ABI expectations from caller:
|
||||
* - Unsigned arguments are zero-extended.
|
||||
* - Signed arguments are sign-extended.
|
||||
*/
|
||||
static int prepare_for_kfunc_call(const struct bpf_prog *fp, u32 *image,
|
||||
struct codegen_context *ctx,
|
||||
const struct bpf_insn *insn)
|
||||
{
|
||||
const struct btf_func_model *m = bpf_jit_find_kfunc_model(fp, insn);
|
||||
int i;
|
||||
|
||||
if (!m)
|
||||
return -1;
|
||||
|
||||
for (i = 0; i < m->nr_args; i++) {
|
||||
/* Note that BPF ABI only allows up to 5 args for kfuncs */
|
||||
u32 reg = bpf_to_ppc(BPF_REG_1 + i), size = m->arg_size[i];
|
||||
|
||||
if (!(m->arg_flags[i] & BTF_FMODEL_SIGNED_ARG)) {
|
||||
if (zero_extend(image, ctx, reg, reg, size))
|
||||
return -1;
|
||||
} else {
|
||||
if (sign_extend(image, ctx, reg, reg, size))
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
|
||||
{
|
||||
/*
|
||||
@@ -678,14 +755,16 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
|
||||
/* special mov32 for zext */
|
||||
EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 0, 0, 31));
|
||||
break;
|
||||
} else if (off == 8) {
|
||||
EMIT(PPC_RAW_EXTSB(dst_reg, src_reg));
|
||||
} else if (off == 16) {
|
||||
EMIT(PPC_RAW_EXTSH(dst_reg, src_reg));
|
||||
} else if (off == 32) {
|
||||
EMIT(PPC_RAW_EXTSW(dst_reg, src_reg));
|
||||
} else if (dst_reg != src_reg)
|
||||
EMIT(PPC_RAW_MR(dst_reg, src_reg));
|
||||
}
|
||||
if (off == 0) {
|
||||
/* MOV */
|
||||
if (dst_reg != src_reg)
|
||||
EMIT(PPC_RAW_MR(dst_reg, src_reg));
|
||||
} else {
|
||||
/* MOVSX: dst = (s8,s16,s32)src (off = 8,16,32) */
|
||||
if (sign_extend(image, ctx, src_reg, dst_reg, off / 8))
|
||||
return -1;
|
||||
}
|
||||
goto bpf_alu32_trunc;
|
||||
case BPF_ALU | BPF_MOV | BPF_K: /* (u32) dst = imm */
|
||||
case BPF_ALU64 | BPF_MOV | BPF_K: /* dst = (s64) imm */
|
||||
@@ -1079,6 +1158,12 @@ emit_clear:
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
/* Take care of powerpc ABI requirements before kfunc call */
|
||||
if (insn[i].src_reg == BPF_PSEUDO_KFUNC_CALL) {
|
||||
if (prepare_for_kfunc_call(fp, image, ctx, &insn[i]))
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = bpf_jit_emit_func_call_rel(image, fimage, ctx, func_addr);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
Reference in New Issue
Block a user