bpf: Add __bpf_hook_{start,end} macros
authorDave Marchevsky <davemarchevsky@fb.com>
Tue, 31 Oct 2023 21:56:25 +0000 (14:56 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 2 Nov 2023 05:33:53 +0000 (22:33 -0700)
Not all uses of __diag_ignore_all(...) in BPF-related code in order to
suppress warnings are wrapping kfunc definitions. Some "hook point"
definitions - small functions meant to be used as attach points for
fentry and similar BPF progs - need to suppress -Wmissing-declarations.

We could use __bpf_kfunc_{start,end}_defs added in the previous patch in
such cases, but this might be confusing to someone unfamiliar with BPF
internals. Instead, this patch adds __bpf_hook_{start,end} macros,
currently having the same effect as __bpf_kfunc_{start,end}_defs, then
uses them to suppress warnings for two hook points in the kernel itself
and some bpf_testmod hook points as well.

Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>
Cc: Yafang Shao <laoar.shao@gmail.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Yafang Shao <laoar.shao@gmail.com>
Link: https://lore.kernel.org/r/20231031215625.2343848-2-davemarchevsky@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/btf.h
kernel/cgroup/rstat.c
net/socket.c
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

index dc5ce962f600c365cbed8d9923bbe6c07ca4920b..59d404e22814e885ae8429a361f2f43bed3b8f30 100644 (file)
@@ -92,6 +92,8 @@
                          "Global kfuncs as their definitions will be in BTF")
 
 #define __bpf_kfunc_end_defs() __diag_pop()
+#define __bpf_hook_start() __bpf_kfunc_start_defs()
+#define __bpf_hook_end() __bpf_kfunc_end_defs()
 
 /*
  * Return the name of the passed struct, if exists, or halt the build if for
index d80d7a6081412994582e2a3686442226b582cd50..c0adb7254b45aef82756c523cee94651e0dc81c6 100644 (file)
@@ -156,19 +156,16 @@ static struct cgroup *cgroup_rstat_cpu_pop_updated(struct cgroup *pos,
  * optimize away the callsite. Therefore, __weak is needed to ensure that the
  * call is still emitted, by telling the compiler that we don't know what the
  * function might eventually be.
- *
- * __diag_* below are needed to dismiss the missing prototype warning.
  */
-__diag_push();
-__diag_ignore_all("-Wmissing-prototypes",
-                 "kfuncs which will be used in BPF programs");
+
+__bpf_hook_start();
 
 __weak noinline void bpf_rstat_flush(struct cgroup *cgrp,
                                     struct cgroup *parent, int cpu)
 {
 }
 
-__diag_pop();
+__bpf_hook_end();
 
 /* see cgroup_rstat_flush() */
 static void cgroup_rstat_flush_locked(struct cgroup *cgrp)
index 0d1c4e78fc7f8c7fde79cb57a6c98a12487b24cc..3379c64217a4c1e166f3fc0ef5053c152b2eb80e 100644 (file)
@@ -1685,20 +1685,16 @@ struct file *__sys_socket_file(int family, int type, int protocol)
  *     Therefore, __weak is needed to ensure that the call is still
  *     emitted, by telling the compiler that we don't know what the
  *     function might eventually be.
- *
- *     __diag_* below are needed to dismiss the missing prototype warning.
  */
 
-__diag_push();
-__diag_ignore_all("-Wmissing-prototypes",
-                 "A fmod_ret entry point for BPF programs");
+__bpf_hook_start();
 
 __weak noinline int update_socket_protocol(int family, int type, int protocol)
 {
        return protocol;
 }
 
-__diag_pop();
+__bpf_hook_end();
 
 int __sys_socket(int family, int type, int protocol)
 {
index a5e246f7b202748b142e0b53b6b21c6f3fe220e5..91907b321f913408ba4ecd31cf679ea07f672e4e 100644 (file)
@@ -39,9 +39,7 @@ struct bpf_testmod_struct_arg_4 {
        int b;
 };
 
-__diag_push();
-__diag_ignore_all("-Wmissing-prototypes",
-                 "Global functions as their definitions will be in bpf_testmod.ko BTF");
+__bpf_hook_start();
 
 noinline int
 bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a, int b, int c) {
@@ -335,7 +333,7 @@ noinline int bpf_fentry_shadow_test(int a)
 }
 EXPORT_SYMBOL_GPL(bpf_fentry_shadow_test);
 
-__diag_pop();
+__bpf_hook_end();
 
 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
        .attr = { .name = "bpf_testmod", .mode = 0666, },