Skip to contentSkip to conversation

bpf: suppress -Wcast-function-type warning

PATCH In review Arnd Bergmann 3 participants 3 messages Oct 26, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: suppress -Wcast-function-type warning

From: Arnd Bergmann <[email protected]>

Building with -Wextra shows lots of warnings in the bpf code such as

kernel/bpf/verifier.c: In function ‘jit_subprogs’:
include/linux/filter.h:345:4: warning: cast between incompatible function types from ‘unsigned int (*)(const void *, const struct bpf_insn *)’ to ‘u64 (*)(u64, u64, u64, u64, u64)’ {aka ‘long long unsigned int (*)(long long unsigned int, long long unsigned int, long long unsigned int, long long unsigned int, long long unsigned int)’} [-Wcast-function-type]
345 | ((u64 (*)(u64, u64, u64, u64, u64))(x))

      |    ^
kernel/bpf/verifier.c:10706:16: note: in expansion of macro ‘BPF_CAST_CALL’
10706 |    insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
      |                ^~~~~~~~~~~~~

This appears to be intentional, so change the cast in a way that suppresses the warning.

---
 include/linux/filter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/filter.h +1 -1
index 1b62397bd124..20ba04583eaa 100644
@@ -342,7 +342,7 @@ static inline bool insn_is_zext(const struct bpf_insn *insn)
 /* Function call */
 
 #define BPF_CAST_CALL(x)					\
-		((u64 (*)(u64, u64, u64, u64, u64))(x))
+		((u64 (*)(u64, u64, u64, u64, u64))(uintptr_t)(x))
 
 #define BPF_EMIT_CALL(FUNC)					\
 	((struct bpf_insn) {					\
Signature · 1 line
2.27.0
#1 lore raw

From: Arnd Bergmann

Quoted 13 lines

Sent: 26 October 2020 21:03

From: Arnd Bergmann <[email protected]>

Building with -Wextra shows lots of warnings in the bpf code such as

kernel/bpf/verifier.c: In function ‘jit_subprogs’:
include/linux/filter.h:345:4: warning: cast between incompatible function types from ‘unsigned int (*)(const void *, const struct bpf_insn *)’ to ‘u64 (*)(u64, u64, u64, u64, u64)’ {aka ‘long long unsigned int (*)(long long unsigned int, long long unsigned int, long long unsigned int, long long unsigned int, long long unsigned int)’} [-Wcast-function-type]
345 | ((u64 (*)(u64, u64, u64, u64, u64))(x))

Quoted 7 lines
      |    ^
kernel/bpf/verifier.c:10706:16: note: in expansion of macro ‘BPF_CAST_CALL’
10706 |    insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
      |                ^~~~~~~~~~~~~

This appears to be intentional, so change the cast in a way that suppresses the warning.

It is also utterly horrid.
If the value is ever cast back then there is a lot of scope
for it going badly wrong somewhere.
It certainly isn't valid to use the target type to call the original function.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

Acked-by on v1 1/1
#2 lore raw
In reply to Arnd Bergmann
Quoted 14 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Building with -Wextra shows lots of warnings in the bpf code such as

kernel/bpf/verifier.c: In function ‘jit_subprogs’:
include/linux/filter.h:345:4: warning: cast between incompatible function types from ‘unsigned int (*)(const void *, const struct bpf_insn *)’ to ‘u64 (*)(u64, u64, u64, u64, u64)’ {aka ‘long long unsigned int (*)(long long unsigned int, long long unsigned int, long long unsigned int, long long unsigned int, long long unsigned int)’} [-Wcast-function-type]
345 | ((u64 (*)(u64, u64, u64, u64, u64))(x))

      |    ^
kernel/bpf/verifier.c:10706:16: note: in expansion of macro ‘BPF_CAST_CALL’
10706 |    insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
      |                ^~~~~~~~~~~~~
Quoted 5 lines

This appears to be intentional, so change the cast in a way that suppresses the warning.

---

LGTM.

Quoted 19 lines
 include/linux/filter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/filter.h +1 -2
index 1b62397bd124..20ba04583eaa 100644
@@ -342,7 +342,7 @@ static inline bool insn_is_zext(const struct bpf_insn *insn)
 /* Function call */
 
 #define BPF_CAST_CALL(x)                                       \
-               ((u64 (*)(u64, u64, u64, u64, u64))(x))
+               ((u64 (*)(u64, u64, u64, u64, u64))(uintptr_t)(x))
 
 #define BPF_EMIT_CALL(FUNC)                                    \
        ((struct bpf_insn) {                                    \
--

2.27.0