Re: [PATCH] LoongArch: BPF: Sign extend struct ops return values properly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2025-08-20 18:39, Hengqi Chen wrote:

The ns_bpf_qdisc selftest triggers a kernel panic:

[...]
+/*
+ * Sign-extend the register if necessary
+ */
+static int sign_extend(struct jit_ctx *ctx, int r, u8 size)
+{
+	switch (size) {
+	case 1:
+		emit_insn(ctx, sllid, r, r, 56);
+		emit_insn(ctx, sraid, r, r, 56);
+		return 0;
+	case 2:
+		emit_insn(ctx, sllid, r, r, 48);
+		emit_insn(ctx, sraid, r, r, 48);
+		return 0;
Hi, Hengqi,

For sign-extend char or short, we can use `ext.w.b` or `ext.w.h`.


+	case 4:
+		emit_insn(ctx, addiw, r, r, 0);
+		return 0;
+	case 8:
+		return 0;
+	default:
+		return -1;
+	}
+}
+
  static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
  					 const struct btf_func_model *m, struct bpf_tramp_links *tlinks,
  					 void *func_addr, u32 flags)
@@ -1602,8 +1628,8 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
  	}
for (i = 0; i < fentry->nr_links; i++) {
-		ret = invoke_bpf_prog(ctx, fentry->links[i], args_off, retval_off,
-				      run_ctx_off, flags & BPF_TRAMP_F_RET_FENTRY_RET);
+		ret = invoke_bpf_prog(ctx, fentry->links[i], m, args_off, retval_off,
+			      run_ctx_off, flags & BPF_TRAMP_F_RET_FENTRY_RET);
  		if (ret)
  			return ret;
  	}
@@ -1612,7 +1638,7 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
  		if (!branches)
  			return -ENOMEM;
- invoke_bpf_mod_ret(ctx, fmod_ret, args_off, retval_off, run_ctx_off, branches);
+		invoke_bpf_mod_ret(ctx, fmod_ret, m, args_off, retval_off, run_ctx_off, branches);
  	}
if (flags & BPF_TRAMP_F_CALL_ORIG) {
@@ -1638,7 +1664,8 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
  	}
for (i = 0; i < fexit->nr_links; i++) {
-		ret = invoke_bpf_prog(ctx, fexit->links[i], args_off, retval_off, run_ctx_off, false);
+		ret = invoke_bpf_prog(ctx, fexit->links[i], m, args_off,
+				      retval_off, run_ctx_off, false);
  		if (ret)
  			goto out;
  	}
@@ -1657,6 +1684,12 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
  	if (save_ret) {
  		emit_insn(ctx, ldd, LOONGARCH_GPR_A0, LOONGARCH_GPR_FP, -retval_off);
  		emit_insn(ctx, ldd, regmap[BPF_REG_0], LOONGARCH_GPR_FP, -(retval_off - 8));
+		if (is_struct_ops) {
+			move_reg(ctx, LOONGARCH_GPR_A0, regmap[BPF_REG_0]);
+			ret = sign_extend(ctx, LOONGARCH_GPR_A0, m->ret_size);
+			if (ret)
+				goto out;
+		}
  	}
emit_insn(ctx, ldd, LOONGARCH_GPR_S1, LOONGARCH_GPR_FP, -sreg_off);





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux