From nobody Tue Apr 7 08:15:23 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31825C433FE for ; Thu, 13 Oct 2022 15:43:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230026AbiJMPn5 (ORCPT ); Thu, 13 Oct 2022 11:43:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230119AbiJMPnh (ORCPT ); Thu, 13 Oct 2022 11:43:37 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7ABC3144E07 for ; Thu, 13 Oct 2022 08:42:02 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2560C61864 for ; Thu, 13 Oct 2022 15:41:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67177C433D6; Thu, 13 Oct 2022 15:41:47 +0000 (UTC) From: Huacai Chen To: Huacai Chen Cc: loongarch@lists.linux.dev, Xuefeng Li , Tiezhu Yang , Guo Ren , Xuerui Wang , Jiaxun Yang , linux-kernel@vger.kernel.org, Huacai Chen Subject: [PATCH] LoongArch: BPF: Avoid declare variables in switch-case Date: Thu, 13 Oct 2022 23:40:00 +0800 Message-Id: <20221013154000.3462836-1-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Not all compilers support declare variables in switch-case, so move declarations to the beginning of a function. Otherwise we may get such build errors: arch/loongarch/net/bpf_jit.c: In function =E2=80=98emit_atomic=E2=80=99: arch/loongarch/net/bpf_jit.c:362:3: error: a label can only be part of a st= atement and a declaration is not a statement u8 r0 =3D regmap[BPF_REG_0]; ^~ arch/loongarch/net/bpf_jit.c: In function =E2=80=98build_insn=E2=80=99: arch/loongarch/net/bpf_jit.c:727:3: error: a label can only be part of a st= atement and a declaration is not a statement u8 t7 =3D -1; ^~ arch/loongarch/net/bpf_jit.c:778:3: error: a label can only be part of a st= atement and a declaration is not a statement int ret; ^~~ arch/loongarch/net/bpf_jit.c:779:3: error: expected expression before =E2= =80=98u64=E2=80=99 u64 func_addr; ^~~ arch/loongarch/net/bpf_jit.c:780:3: warning: ISO C90 forbids mixed declarat= ions and code [-Wdeclaration-after-statement] bool func_addr_fixed; ^~~~ arch/loongarch/net/bpf_jit.c:784:11: error: =E2=80=98func_addr=E2=80=99 und= eclared (first use in this function); did you mean =E2=80=98in_addr=E2=80= =99? &func_addr, &func_addr_fixed); ^~~~~~~~~ in_addr arch/loongarch/net/bpf_jit.c:784:11: note: each undeclared identifier is re= ported only once for each function it appears in arch/loongarch/net/bpf_jit.c:814:3: error: a label can only be part of a st= atement and a declaration is not a statement u64 imm64 =3D (u64)(insn + 1)->imm << 32 | (u32)insn->imm; ^~~ Signed-off-by: Huacai Chen --- arch/loongarch/net/bpf_jit.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c index 43f0a98efe38..2a9b590f47e6 100644 --- a/arch/loongarch/net/bpf_jit.c +++ b/arch/loongarch/net/bpf_jit.c @@ -279,6 +279,7 @@ static void emit_atomic(const struct bpf_insn *insn, st= ruct jit_ctx *ctx) const u8 t1 =3D LOONGARCH_GPR_T1; const u8 t2 =3D LOONGARCH_GPR_T2; const u8 t3 =3D LOONGARCH_GPR_T3; + const u8 r0 =3D regmap[BPF_REG_0]; const u8 src =3D regmap[insn->src_reg]; const u8 dst =3D regmap[insn->dst_reg]; const s16 off =3D insn->off; @@ -359,8 +360,6 @@ static void emit_atomic(const struct bpf_insn *insn, st= ruct jit_ctx *ctx) break; /* r0 =3D atomic_cmpxchg(dst + off, r0, src); */ case BPF_CMPXCHG: - u8 r0 =3D regmap[BPF_REG_0]; - move_reg(ctx, t2, r0); if (isdw) { emit_insn(ctx, lld, r0, t1, 0); @@ -390,8 +389,11 @@ static bool is_signed_bpf_cond(u8 cond) =20 static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bo= ol extra_pass) { - const bool is32 =3D BPF_CLASS(insn->code) =3D=3D BPF_ALU || - BPF_CLASS(insn->code) =3D=3D BPF_JMP32; + u8 t0 =3D -1; + u64 func_addr; + bool func_addr_fixed; + int i =3D insn - ctx->prog->insnsi; + int ret, jmp_offset; const u8 code =3D insn->code; const u8 cond =3D BPF_OP(code); const u8 t1 =3D LOONGARCH_GPR_T1; @@ -400,8 +402,8 @@ static int build_insn(const struct bpf_insn *insn, stru= ct jit_ctx *ctx, bool ext const u8 dst =3D regmap[insn->dst_reg]; const s16 off =3D insn->off; const s32 imm =3D insn->imm; - int jmp_offset; - int i =3D insn - ctx->prog->insnsi; + const u64 imm64 =3D (u64)(insn + 1)->imm << 32 | (u32)insn->imm; + const bool is32 =3D BPF_CLASS(insn->code) =3D=3D BPF_ALU || BPF_CLASS(ins= n->code) =3D=3D BPF_JMP32; =20 switch (code) { /* dst =3D src */ @@ -724,24 +726,23 @@ static int build_insn(const struct bpf_insn *insn, st= ruct jit_ctx *ctx, bool ext case BPF_JMP32 | BPF_JSGE | BPF_K: case BPF_JMP32 | BPF_JSLT | BPF_K: case BPF_JMP32 | BPF_JSLE | BPF_K: - u8 t7 =3D -1; jmp_offset =3D bpf2la_offset(i, off, ctx); if (imm) { move_imm(ctx, t1, imm, false); - t7 =3D t1; + t0 =3D t1; } else { /* If imm is 0, simply use zero register. */ - t7 =3D LOONGARCH_GPR_ZERO; + t0 =3D LOONGARCH_GPR_ZERO; } move_reg(ctx, t2, dst); if (is_signed_bpf_cond(BPF_OP(code))) { - emit_sext_32(ctx, t7, is32); + emit_sext_32(ctx, t0, is32); emit_sext_32(ctx, t2, is32); } else { - emit_zext_32(ctx, t7, is32); + emit_zext_32(ctx, t0, is32); emit_zext_32(ctx, t2, is32); } - if (emit_cond_jmp(ctx, cond, t2, t7, jmp_offset) < 0) + if (emit_cond_jmp(ctx, cond, t2, t0, jmp_offset) < 0) goto toofar; break; =20 @@ -775,10 +776,6 @@ static int build_insn(const struct bpf_insn *insn, str= uct jit_ctx *ctx, bool ext =20 /* function call */ case BPF_JMP | BPF_CALL: - int ret; - u64 func_addr; - bool func_addr_fixed; - mark_call(ctx); ret =3D bpf_jit_get_func_addr(ctx->prog, insn, extra_pass, &func_addr, &func_addr_fixed); @@ -811,8 +808,6 @@ static int build_insn(const struct bpf_insn *insn, stru= ct jit_ctx *ctx, bool ext =20 /* dst =3D imm64 */ case BPF_LD | BPF_IMM | BPF_DW: - u64 imm64 =3D (u64)(insn + 1)->imm << 32 | (u32)insn->imm; - move_imm(ctx, dst, imm64, is32); return 1; =20 --=20 2.31.1