Skip to content

Commit 5da5b66

Browse files
committed
Reapply "LoongArch: BPF: Sign extend kfunc call arguments"
stable inclusion from stable-v6.6.120 category: bugfix With prev commit, it can build successfully now. This reverts commit 1d1623f. Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent 5506c40 commit 5da5b66

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

arch/loongarch/net/bpf_jit.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,22 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
834834
if (ret < 0)
835835
return ret;
836836

837+
if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
838+
const struct btf_func_model *m;
839+
int i;
840+
841+
m = bpf_jit_find_kfunc_model(ctx->prog, insn);
842+
if (!m)
843+
return -EINVAL;
844+
845+
for (i = 0; i < m->nr_args; i++) {
846+
u8 reg = regmap[BPF_REG_1 + i];
847+
bool sign = m->arg_flags[i] & BTF_FMODEL_SIGNED_ARG;
848+
849+
emit_abi_ext(ctx, reg, m->arg_size[i], sign);
850+
}
851+
}
852+
837853
move_addr(ctx, t1, func_addr);
838854
emit_insn(ctx, jirl, LOONGARCH_GPR_RA, t1, 0);
839855

arch/loongarch/net/bpf_jit.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,32 @@ static inline void emit_sext_32(struct jit_ctx *ctx, enum loongarch_gpr reg, boo
8787
emit_insn(ctx, addiw, reg, reg, 0);
8888
}
8989

90+
/* Emit proper extension according to ABI requirements.
91+
* Note that it requires a value of size `size` already resides in register `reg`.
92+
*/
93+
static inline void emit_abi_ext(struct jit_ctx *ctx, int reg, u8 size, bool sign)
94+
{
95+
/* ABI requires unsigned char/short to be zero-extended */
96+
if (!sign && (size == 1 || size == 2))
97+
return;
98+
99+
switch (size) {
100+
case 1:
101+
emit_insn(ctx, extwb, reg, reg);
102+
break;
103+
case 2:
104+
emit_insn(ctx, extwh, reg, reg);
105+
break;
106+
case 4:
107+
emit_insn(ctx, addiw, reg, reg, 0);
108+
break;
109+
case 8:
110+
break;
111+
default:
112+
pr_warn("bpf_jit: invalid size %d for extension\n", size);
113+
}
114+
}
115+
90116
static inline void move_addr(struct jit_ctx *ctx, enum loongarch_gpr rd, u64 addr)
91117
{
92118
u64 imm_11_0, imm_31_12, imm_51_32, imm_63_52;

0 commit comments

Comments
 (0)