Skip to contentSkip to conversation

bpf: Add explicit cast to 'void *' for __BPF_DISPATCHER_UPDATE()

PATCH Applied Nathan Chancellor 4 participants 4 messages Nov 7, 2022 – Nov 8, 2022

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

PATCH 1/1 bpf: Add explicit cast to 'void *' for __BPF_DISPATCHER_UPDATE()

When building with clang:

  kernel/bpf/dispatcher.c:126:33: error: pointer type mismatch ('void *' and 'unsigned int (*)(const void *, const struct bpf_insn *, bpf_func_t)' (aka 'unsigned int (*)(const void *, const struct bpf_insn *, unsigned int (*)(const void *, const struct bpf_insn *))')) [-Werror,-Wpointer-type-mismatch]
          __BPF_DISPATCHER_UPDATE(d, new ?: &bpf_dispatcher_nop_func);
                                     ~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~
  ./include/linux/bpf.h:1045:54: note: expanded from macro '__BPF_DISPATCHER_UPDATE'
          __static_call_update((_d)->sc_key, (_d)->sc_tramp, (_new))
                                                              ^~~~
  1 error generated.

The warning is pointing out that the type of new ('void *') and &bpf_dispatcher_nop_func are not compatible, which could have side effects coming out of a conditional operator due to promotion rules.

Add the explicit cast to 'void *' to make it clear that this is expected, as __BPF_DISPATCHER_UPDATE() expands to a call to __static_call_update(), which expects a 'void *' as its final argument.

Fixes c86df29d11df ("bpf: Convert BPF_DISPATCHER to use static_call() (not ftrace)")
---
 kernel/bpf/dispatcher.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/dispatcher.c +1 -1
index 7dfb8d0d5202..c19719f48ce0 100644
@@ -123,7 +123,7 @@ static void bpf_dispatcher_update(struct bpf_dispatcher *d, int prev_num_progs)
 			return;
 	}
 
-	__BPF_DISPATCHER_UPDATE(d, new ?: &bpf_dispatcher_nop_func);
+	__BPF_DISPATCHER_UPDATE(d, new ?: (void *)&bpf_dispatcher_nop_func);
 
 	if (new)
 		d->image_off = noff;

base-commit: c86df29d11dfba27c0a1f5039cd6fe387fbf4239

Signature · 1 line
2.38.1
Acked-by on v1 1/1
#1 lore raw
Quoted 18 lines · Nathan Chancellor

When building with clang:

  kernel/bpf/dispatcher.c:126:33: error: pointer type mismatch ('void *' and 'unsigned int (*)(const void *, const struct bpf_insn *, bpf_func_t)' (aka 'unsigned int (*)(const void *, const struct bpf_insn *, unsigned int (*)(const void *, const struct bpf_insn *))')) [-Werror,-Wpointer-type-mismatch]
          __BPF_DISPATCHER_UPDATE(d, new ?: &bpf_dispatcher_nop_func);
                                     ~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~
  ./include/linux/bpf.h:1045:54: note: expanded from macro '__BPF_DISPATCHER_UPDATE'
          __static_call_update((_d)->sc_key, (_d)->sc_tramp, (_new))
                                                              ^~~~
  1 error generated.

The warning is pointing out that the type of new ('void *') and &bpf_dispatcher_nop_func are not compatible, which could have side effects coming out of a conditional operator due to promotion rules.

Add the explicit cast to 'void *' to make it clear that this is expected, as __BPF_DISPATCHER_UPDATE() expands to a call to __static_call_update(), which expects a 'void *' as its final argument.

Quoted 5 lines
Fixes c86df29d11df ("bpf: Convert BPF_DISPATCHER to use static_call() (not ftrace)")
Acked-by on v1 1/1
#2 lore raw
In reply to Nathan Chancellor
Quoted 18 lines · Nathan Chancellor

When building with clang:

   kernel/bpf/dispatcher.c:126:33: error: pointer type mismatch ('void *' and 'unsigned int (*)(const void *, const struct bpf_insn *, bpf_func_t)' (aka 'unsigned int (*)(const void *, const struct bpf_insn *, unsigned int (*)(const void *, const struct bpf_insn *))')) [-Werror,-Wpointer-type-mismatch]
           __BPF_DISPATCHER_UPDATE(d, new ?: &bpf_dispatcher_nop_func);
                                      ~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~
   ./include/linux/bpf.h:1045:54: note: expanded from macro '__BPF_DISPATCHER_UPDATE'
           __static_call_update((_d)->sc_key, (_d)->sc_tramp, (_new))
                                                               ^~~~
   1 error generated.

The warning is pointing out that the type of new ('void *') and &bpf_dispatcher_nop_func are not compatible, which could have side effects coming out of a conditional operator due to promotion rules.

Add the explicit cast to 'void *' to make it clear that this is expected, as __BPF_DISPATCHER_UPDATE() expands to a call to __static_call_update(), which expects a 'void *' as its final argument.

Quoted 5 lines
Fixes c86df29d11df ("bpf: Convert BPF_DISPATCHER to use static_call() (not ftrace)")
#3 lore raw
In reply to Nathan Chancellor
This patch was applied to bpf/bpf.git (master) by Martin KaFai Lau <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Martin KaFai Lau <[email protected]>:

Quoted 11 lines · On Mon, 7 Nov 2022 10:07:11 -0700 you wrote:

When building with clang:

  kernel/bpf/dispatcher.c:126:33: error: pointer type mismatch ('void *' and 'unsigned int (*)(const void *, const struct bpf_insn *, bpf_func_t)' (aka 'unsigned int (*)(const void *, const struct bpf_insn *, unsigned int (*)(const void *, const struct bpf_insn *))')) [-Werror,-Wpointer-type-mismatch]
          __BPF_DISPATCHER_UPDATE(d, new ?: &bpf_dispatcher_nop_func);
                                     ~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~
  ./include/linux/bpf.h:1045:54: note: expanded from macro '__BPF_DISPATCHER_UPDATE'
          __static_call_update((_d)->sc_key, (_d)->sc_tramp, (_new))
                                                              ^~~~
  1 error generated.

[...]

Here is the summary with links:
- [bpf] bpf: Add explicit cast to 'void *' for __BPF_DISPATCHER_UPDATE()

    https://git.kernel.org/bpf/bpf/c/a679120edfcf

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html