Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf/tests: Add 32 bits only long conditional jump tests

Commit f1517eb790f9 ("bpf/tests: Expand branch conversion JIT test")
introduced "Long conditional jump tests" but due to those tests making
use of 64 bits DIV and MOD, they don't get jited on powerpc/32,
leading to the long conditional jump test being skiped for unrelated
reason.

Add 4 new tests that are restricted to 32 bits ALU so that the jump
tests can also be performed on platforms that do no support 64 bits
operations.

Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/609f87a2d84e032c8d9ccb9ba7aebef893698f1e.1736154762.git.christophe.leroy@csgroup.eu

authored by

Christophe Leroy and committed by
Daniel Borkmann
25326085 8c21f884

+58 -6
+58 -6
lib/test_bpf.c
··· 478 478 * to overflow the field size of the native instruction, triggering 479 479 * a branch conversion mechanism in some JITs. 480 480 */ 481 - static int __bpf_fill_max_jmp(struct bpf_test *self, int jmp, int imm) 481 + static int __bpf_fill_max_jmp(struct bpf_test *self, int jmp, int imm, bool alu32) 482 482 { 483 483 struct bpf_insn *insns; 484 484 int len = S16_MAX + 5; ··· 501 501 }; 502 502 int op = ops[(i >> 1) % ARRAY_SIZE(ops)]; 503 503 504 - if (i & 1) 504 + if ((i & 1) || alu32) 505 505 insns[i++] = BPF_ALU32_REG(op, R0, R1); 506 506 else 507 507 insns[i++] = BPF_ALU64_REG(op, R0, R1); ··· 516 516 } 517 517 518 518 /* Branch taken by runtime decision */ 519 + static int bpf_fill_max_jmp_taken_32(struct bpf_test *self) 520 + { 521 + return __bpf_fill_max_jmp(self, BPF_JEQ, 1, true); 522 + } 523 + 519 524 static int bpf_fill_max_jmp_taken(struct bpf_test *self) 520 525 { 521 - return __bpf_fill_max_jmp(self, BPF_JEQ, 1); 526 + return __bpf_fill_max_jmp(self, BPF_JEQ, 1, false); 522 527 } 523 528 524 529 /* Branch not taken by runtime decision */ 530 + static int bpf_fill_max_jmp_not_taken_32(struct bpf_test *self) 531 + { 532 + return __bpf_fill_max_jmp(self, BPF_JEQ, 0, true); 533 + } 534 + 525 535 static int bpf_fill_max_jmp_not_taken(struct bpf_test *self) 526 536 { 527 - return __bpf_fill_max_jmp(self, BPF_JEQ, 0); 537 + return __bpf_fill_max_jmp(self, BPF_JEQ, 0, false); 528 538 } 529 539 530 540 /* Branch always taken, known at JIT time */ 541 + static int bpf_fill_max_jmp_always_taken_32(struct bpf_test *self) 542 + { 543 + return __bpf_fill_max_jmp(self, BPF_JGE, 0, true); 544 + } 545 + 531 546 static int bpf_fill_max_jmp_always_taken(struct bpf_test *self) 532 547 { 533 - return __bpf_fill_max_jmp(self, BPF_JGE, 0); 548 + return __bpf_fill_max_jmp(self, BPF_JGE, 0, false); 534 549 } 535 550 536 551 /* Branch never taken, known at JIT time */ 552 + static int bpf_fill_max_jmp_never_taken_32(struct bpf_test *self) 553 + { 554 + return __bpf_fill_max_jmp(self, BPF_JLT, 0, true); 555 + } 556 + 537 557 static int bpf_fill_max_jmp_never_taken(struct bpf_test *self) 538 558 { 539 - return __bpf_fill_max_jmp(self, BPF_JLT, 0); 559 + return __bpf_fill_max_jmp(self, BPF_JLT, 0, false); 540 560 } 541 561 542 562 /* ALU result computation used in tests */ ··· 14253 14233 { { 0, 0 } }, 14254 14234 }, 14255 14235 /* Conditional branch conversions */ 14236 + { 14237 + "Long conditional jump: taken at runtime (32 bits)", 14238 + { }, 14239 + INTERNAL | FLAG_NO_DATA, 14240 + { }, 14241 + { { 0, 1 } }, 14242 + .fill_helper = bpf_fill_max_jmp_taken_32, 14243 + }, 14244 + { 14245 + "Long conditional jump: not taken at runtime (32 bits)", 14246 + { }, 14247 + INTERNAL | FLAG_NO_DATA, 14248 + { }, 14249 + { { 0, 2 } }, 14250 + .fill_helper = bpf_fill_max_jmp_not_taken_32, 14251 + }, 14252 + { 14253 + "Long conditional jump: always taken, known at JIT time (32 bits)", 14254 + { }, 14255 + INTERNAL | FLAG_NO_DATA, 14256 + { }, 14257 + { { 0, 1 } }, 14258 + .fill_helper = bpf_fill_max_jmp_always_taken_32, 14259 + }, 14260 + { 14261 + "Long conditional jump: never taken, known at JIT time (32 bits)", 14262 + { }, 14263 + INTERNAL | FLAG_NO_DATA, 14264 + { }, 14265 + { { 0, 2 } }, 14266 + .fill_helper = bpf_fill_max_jmp_never_taken_32, 14267 + }, 14256 14268 { 14257 14269 "Long conditional jump: taken at runtime", 14258 14270 { },