2020-10-27 07:31:50

by Arnd Bergmann

[permalink] [raw]
Subject: [PATCH] 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.

Signed-off-by: Arnd Bergmann <[email protected]>
---
include/linux/filter.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/filter.h b/include/linux/filter.h
index 1b62397bd124..20ba04583eaa 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -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


2020-10-27 08:23:13

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH] bpf: suppress -Wcast-function-type warning

On Mon, Oct 26, 2020 at 2:03 PM Arnd Bergmann <[email protected]> wrote:
>
> 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.
>
> Signed-off-by: Arnd Bergmann <[email protected]>
> ---

LGTM.

Acked-by: Andrii Nakryiko <[email protected]>

> include/linux/filter.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/filter.h b/include/linux/filter.h
> index 1b62397bd124..20ba04583eaa 100644
> --- a/include/linux/filter.h
> +++ b/include/linux/filter.h
> @@ -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
>