Skip to contentSkip to conversation

bpf: remove trunctaiton warning in BPF_LD_IMM64_RAW

PATCH In review Ben Dooks 3 participants 3 messages Mar 14, 2024

Subsystems (2)
lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

PATCH 1/1 bpf: remove trunctaiton warning in BPF_LD_IMM64_RAW

When building W=1, the following warningds get generated by lib/test_bpf.c due to the BPF_LD_IMM64_RAW() truncating a 64bit to 32bit value without a specific mask to do so.

lib/test_bpf.c:6441:25: warning: cast truncates bits from constant value (ffffffff0000 becomes ffff0000) lib/test_bpf.c:6442:25: warning: cast truncates bits from constant value (ffffffff0000 becomes ffff0000) lib/test_bpf.c:6473:25: warning: cast truncates bits from constant value (123456789abcdef becomes 89abcdef) lib/test_bpf.c:6489:25: warning: cast truncates bits from constant value (123456789abcdef becomes 89abcdef) lib/test_bpf.c:6490:25: warning: cast truncates bits from constant value (123456780a0c0e0 becomes 80a0c0e0) lib/test_bpf.c:6599:25: warning: cast truncates bits from constant value (123456789abcdef becomes 89abcdef) lib/test_bpf.c:6637:25: warning: cast truncates bits from constant value (ffffffff0000 becomes ffff0000)

(more warnings truncated)

---
 include/linux/filter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/filter.h +1 -1
index c99bc3df2d28..c1ff6511f365 100644
@@ -238,7 +238,7 @@ static inline bool insn_is_zext(const struct bpf_insn *insn)
 		.dst_reg = DST,					\
 		.src_reg = SRC,					\
 		.off   = 0,					\
-		.imm   = (__u32) (IMM) }),			\
+		.imm   = (__u32) ((IMM) & (__u64)0xffffffff) }), \
 	((struct bpf_insn) {					\
 		.code  = 0, /* zero is reserved opcode */	\
 		.dst_reg = 0,					\
Signature · 1 line
2.37.2.352.g3c44437643
Acked-by on v1 1/1
#1 lore raw
Quoted 12 lines · Ben Dooks

When building W=1, the following warningds get generated by lib/test_bpf.c due to the BPF_LD_IMM64_RAW() truncating a 64bit to 32bit value without a specific mask to do so.

lib/test_bpf.c:6441:25: warning: cast truncates bits from constant value (ffffffff0000 becomes ffff0000) lib/test_bpf.c:6442:25: warning: cast truncates bits from constant value (ffffffff0000 becomes ffff0000) lib/test_bpf.c:6473:25: warning: cast truncates bits from constant value (123456789abcdef becomes 89abcdef) lib/test_bpf.c:6489:25: warning: cast truncates bits from constant value (123456789abcdef becomes 89abcdef) lib/test_bpf.c:6490:25: warning: cast truncates bits from constant value (123456780a0c0e0 becomes 80a0c0e0) lib/test_bpf.c:6599:25: warning: cast truncates bits from constant value (123456789abcdef becomes 89abcdef) lib/test_bpf.c:6637:25: warning: cast truncates bits from constant value (ffffffff0000 becomes ffff0000)

Quoted 3 lines

(more warnings truncated)

I wonder how many other places (in the kernel) we have where we silently cast u64 to u32.. Surprised that's the only warning you get :-)

#2 lore raw
In reply to Ben Dooks
Quoted 25 lines · Ben Dooks

When building W=1, the following warningds get generated by lib/test_bpf.c due to the BPF_LD_IMM64_RAW() truncating a 64bit to 32bit value without a specific mask to do so.

lib/test_bpf.c:6441:25: warning: cast truncates bits from constant value (ffffffff0000 becomes ffff0000) lib/test_bpf.c:6442:25: warning: cast truncates bits from constant value (ffffffff0000 becomes ffff0000) lib/test_bpf.c:6473:25: warning: cast truncates bits from constant value (123456789abcdef becomes 89abcdef) lib/test_bpf.c:6489:25: warning: cast truncates bits from constant value (123456789abcdef becomes 89abcdef) lib/test_bpf.c:6490:25: warning: cast truncates bits from constant value (123456780a0c0e0 becomes 80a0c0e0) lib/test_bpf.c:6599:25: warning: cast truncates bits from constant value (123456789abcdef becomes 89abcdef) lib/test_bpf.c:6637:25: warning: cast truncates bits from constant value (ffffffff0000 becomes ffff0000)

(more warnings truncated)

---
 include/linux/filter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/filter.h +0 -0
index c99bc3df2d28..c1ff6511f365 100644

include/linux/filter.h static inline bool insn_is_zext(const struct bpf_insn *insn) · line 241

Quoted 5 lines
include/linux/filter.h +1 -1
@@ -238,7 +238,7 @@ static inline bool insn_is_zext(const struct bpf_insn *insn)
                .dst_reg = DST,                                 \
                .src_reg = SRC,                                 \
                .off   = 0,                                     \
-               .imm   = (__u32) (IMM) }),                      \
+               .imm   = (__u32) ((IMM) & (__u64)0xffffffff) }), \

What compiler is this?
I don't think it's a good idea to uglify this macro just to shut up a compiler.
As Stan said there are plenty of other places where such _explicit_ u32 cast is used.
We're not going to mess with all of them.

pw-bot: cr