bpf, mips: No need to use min() to get MAX_TAIL_CALL_CNT
authorTiezhu Yang <yangtiezhu@loongson.cn>
Mon, 29 Aug 2022 03:05:09 +0000 (11:05 +0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 29 Aug 2022 13:38:14 +0000 (15:38 +0200)
MAX_TAIL_CALL_CNT is 33, so min(MAX_TAIL_CALL_CNT, 0xffff) is always
MAX_TAIL_CALL_CNT, it is better to use MAX_TAIL_CALL_CNT directly.

At the same time, add BUILD_BUG_ON(MAX_TAIL_CALL_CNT > 0xffff) with a
comment on why the assertion is there.

Suggested-by: Daniel Borkmann <daniel@iogearbox.net>
Suggested-by: Johan Almbladh <johan.almbladh@anyfinetworks.com>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/1661742309-2320-1-git-send-email-yangtiezhu@loongson.cn
arch/mips/net/bpf_jit_comp32.c
arch/mips/net/bpf_jit_comp64.c

index 83c975d5cca27d6135c3fe0b44ab9ddda45c6449..ace5db3fbd171fc63421cc716c2f00d4967a8501 100644 (file)
@@ -1376,12 +1376,20 @@ void build_prologue(struct jit_context *ctx)
        const u8 *fp = bpf2mips32[BPF_REG_FP];
        int stack, saved, locals, reserved;
 
+       /*
+        * In the unlikely event that the TCC limit is raised to more
+        * than 16 bits, it is clamped to the maximum value allowed for
+        * the generated code (0xffff). It is better fail to compile
+        * instead of degrading gracefully.
+        */
+       BUILD_BUG_ON(MAX_TAIL_CALL_CNT > 0xffff);
+
        /*
         * The first two instructions initialize TCC in the reserved (for us)
         * 16-byte area in the parent's stack frame. On a tail call, the
         * calling function jumps into the prologue after these instructions.
         */
-       emit(ctx, ori, MIPS_R_T9, MIPS_R_ZERO, min(MAX_TAIL_CALL_CNT, 0xffff));
+       emit(ctx, ori, MIPS_R_T9, MIPS_R_ZERO, MAX_TAIL_CALL_CNT);
        emit(ctx, sw, MIPS_R_T9, 0, MIPS_R_SP);
 
        /*
index 6475828ffb36d5544e3457ed670a35b8e83734b9..0e7c1bdcf9148e07fced608a435bc8d3987169a0 100644 (file)
@@ -547,12 +547,20 @@ void build_prologue(struct jit_context *ctx)
        u8 zx = bpf2mips64[JIT_REG_ZX];
        int stack, saved, locals, reserved;
 
+       /*
+        * In the unlikely event that the TCC limit is raised to more
+        * than 16 bits, it is clamped to the maximum value allowed for
+        * the generated code (0xffff). It is better fail to compile
+        * instead of degrading gracefully.
+        */
+       BUILD_BUG_ON(MAX_TAIL_CALL_CNT > 0xffff);
+
        /*
         * The first instruction initializes the tail call count register.
         * On a tail call, the calling function jumps into the prologue
         * after this instruction.
         */
-       emit(ctx, ori, tc, MIPS_R_ZERO, min(MAX_TAIL_CALL_CNT, 0xffff));
+       emit(ctx, ori, tc, MIPS_R_ZERO, MAX_TAIL_CALL_CNT);
 
        /* === Entry-point for tail calls === */