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

bpf: simplify tnum output if a fully known constant

Emit tnum representation as just a constant if all bits are known.
Use decimal-vs-hex logic to determine exact format of emitted
constant value, just like it's done for register range values.
For that move tnum_strn() to kernel/bpf/log.c to reuse decimal-vs-hex
determination logic and constants.

Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20231202175705.885270-12-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
81eff2e3 5c19e1d0

+17 -10
+13
kernel/bpf/log.c
··· 539 539 verbose(env, "%#llx", num); 540 540 } 541 541 542 + int tnum_strn(char *str, size_t size, struct tnum a) 543 + { 544 + /* print as a constant, if tnum is fully known */ 545 + if (a.mask == 0) { 546 + if (is_unum_decimal(a.value)) 547 + return snprintf(str, size, "%llu", a.value); 548 + else 549 + return snprintf(str, size, "%#llx", a.value); 550 + } 551 + return snprintf(str, size, "(%#llx; %#llx)", a.value, a.mask); 552 + } 553 + EXPORT_SYMBOL_GPL(tnum_strn); 554 + 542 555 static void print_scalar_ranges(struct bpf_verifier_env *env, 543 556 const struct bpf_reg_state *reg, 544 557 const char **sep)
-6
kernel/bpf/tnum.c
··· 172 172 return a.value == b.value; 173 173 } 174 174 175 - int tnum_strn(char *str, size_t size, struct tnum a) 176 - { 177 - return snprintf(str, size, "(%#llx; %#llx)", a.value, a.mask); 178 - } 179 - EXPORT_SYMBOL_GPL(tnum_strn); 180 - 181 175 int tnum_sbin(char *str, size_t size, struct tnum a) 182 176 { 183 177 size_t n;
+1 -1
tools/testing/selftests/bpf/progs/verifier_direct_packet_access.c
··· 411 411 412 412 SEC("tc") 413 413 __description("direct packet access: test17 (pruning, alignment)") 414 - __failure __msg("misaligned packet access off 2+(0x0; 0x0)+15+-4 size 4") 414 + __failure __msg("misaligned packet access off 2+0+15+-4 size 4") 415 415 __flag(BPF_F_STRICT_ALIGNMENT) 416 416 __naked void packet_access_test17_pruning_alignment(void) 417 417 {
+1 -1
tools/testing/selftests/bpf/progs/verifier_int_ptr.c
··· 67 67 68 68 SEC("cgroup/sysctl") 69 69 __description("ARG_PTR_TO_LONG misaligned") 70 - __failure __msg("misaligned stack access off (0x0; 0x0)+-20+0 size 8") 70 + __failure __msg("misaligned stack access off 0+-20+0 size 8") 71 71 __naked void arg_ptr_to_long_misaligned(void) 72 72 { 73 73 asm volatile (" \
+2 -2
tools/testing/selftests/bpf/progs/verifier_stack_ptr.c
··· 37 37 38 38 SEC("socket") 39 39 __description("PTR_TO_STACK store/load - bad alignment on off") 40 - __failure __msg("misaligned stack access off (0x0; 0x0)+-8+2 size 8") 40 + __failure __msg("misaligned stack access off 0+-8+2 size 8") 41 41 __failure_unpriv 42 42 __naked void load_bad_alignment_on_off(void) 43 43 { ··· 53 53 54 54 SEC("socket") 55 55 __description("PTR_TO_STACK store/load - bad alignment on reg") 56 - __failure __msg("misaligned stack access off (0x0; 0x0)+-10+8 size 8") 56 + __failure __msg("misaligned stack access off 0+-10+8 size 8") 57 57 __failure_unpriv 58 58 __naked void load_bad_alignment_on_reg(void) 59 59 {