bpf: Add __bpf_kfunc_{start,end}_defs macros
Subsystems (16)
PATCH v2 1/2 bpf: Add __bpf_kfunc_{start,end}_defs macros
BPF kfuncs are meant to be called from BPF programs. Accordingly, most kfuncs are not called from anywhere in the kernel, which the -Wmissing-prototypes warning is unhappy about. We've peppered __diag_ignore_all("-Wmissing-prototypes", ... everywhere kfuncs are defined in the codebase to suppress this warning.
This patch adds two macros meant to bound one or many kfunc definitions. All existing kfunc definitions which use these __diag calls to suppress -Wmissing-prototypes are migrated to use the newly-introduced macros. A new __diag_ignore_all - for "-Wmissing-declarations" - is added to the __bpf_kfunc_start_defs macro based on feedback from Andrii on an earlier version of this patch [0] and another recent mailing list thread [1].
In the future we might need to ignore different warnings or do other kfunc-specific things. This change will make it easier to make such modifications for all kfunc defs.
[0]: https://lore.kernel.org/bpf/CAEf4BzaE5dRWtK6RPLnjTW-MW9sx9K3Fn6uwqCTChK2Dcb1Xig@mail.gmail.com/
[1]: https://lore.kernel.org/bpf/ZT+2qCc%2FaXep0%2FLf@krava/
---
v1 -> v2: https://lore.kernel.org/bpf/[email protected]/
* Update Documentation/bpf/kfuncs.rst to use new macros (Yafang, Andrii)
* Update recently-added open-coded {task,cgroup} iters to use new macros (Yafang, Andrii)
* Add Andrii ack
Documentation/bpf/kfuncs.rst | 6 ++---- include/linux/btf.h | 9 +++++++++ kernel/bpf/bpf_iter.c | 6 ++---- kernel/bpf/cgroup_iter.c | 6 ++---- kernel/bpf/cpumask.c | 6 ++---- kernel/bpf/helpers.c | 6 ++---- kernel/bpf/map_iter.c | 6 ++---- kernel/bpf/task_iter.c | 18 ++++++------------ kernel/trace/bpf_trace.c | 6 ++---- net/bpf/test_run.c | 7 +++---- net/core/filter.c | 13 ++++--------- net/core/xdp.c | 6 ++---- net/ipv4/fou_bpf.c | 6 ++---- net/netfilter/nf_conntrack_bpf.c | 6 ++---- net/netfilter/nf_nat_bpf.c | 6 ++---- net/xfrm/xfrm_interface_bpf.c | 6 ++---- 16 files changed, 46 insertions(+), 73 deletions(-)
Documentation/bpf/kfuncs.rst +2 -4
@@ -37,16 +37,14 @@ prototype in a header for the wrapper kfunc. An example is given below:: /* Disables missing prototype warnings */ - __diag_push(); - __diag_ignore_all("-Wmissing-prototypes", - "Global kfuncs as their definitions will be in BTF"); + __bpf_kfunc_start_defs(); __bpf_kfunc struct task_struct *bpf_find_get_task_by_vpid(pid_t nr) { return find_get_task_by_vpid(nr); } - __diag_pop(); + __bpf_kfunc_end_defs(); A wrapper kfunc is often needed when we need to annotate parameters of the kfunc. Otherwise one may directly make the kfunc visible to the BPF program by
include/linux/btf.h +9 -0
@@ -84,6 +84,15 @@*/ #define __bpf_kfunc __used noinline +#define __bpf_kfunc_start_defs () \ + __diag_push(); \ + __diag_ignore_all("-Wmissing-declarations", \ + "Global kfuncs as their definitions will be in BTF");\ + __diag_ignore_all("-Wmissing-prototypes", \ + "Global kfuncs as their definitions will be in BTF") + +#define __bpf_kfunc_end_defs () __diag_pop() +/* * Return the name of the passed struct, if exists, or halt the build if for * example the structure gets renamed. In this way, developers have to revisit
kernel/bpf/bpf_iter.c +2 -4
@@ -782,9 +782,7 @@ struct bpf_iter_num_kern {int end;/* final value, exclusive */ }__aligned(8) ; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) {
@@ -843,4 +841,4 @@ __bpf_kfunc void bpf_iter_num_destroy(struct bpf_iter_num *it) s->cur = s->end =0 ; } -__diag_pop (); +__bpf_kfunc_end_defs ();
kernel/bpf/cgroup_iter.c +2 -4
@@ -305,9 +305,7 @@ struct bpf_iter_css_kern {unsigned int flags; } __attribute__((aligned(8))); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it,struct cgroup_subsys_state *start,unsigned int flags)
@@ -358,4 +356,4 @@ __bpf_kfunc void bpf_iter_css_destroy(struct bpf_iter_css *it) { } -__diag_pop (); +__bpf_kfunc_end_defs ();
kernel/bpf/cpumask.c +2 -4
@@ -34,9 +34,7 @@ static bool cpu_valid(u32 cpu)return cpu < nr_cpu_ids; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global kfuncs as their definitions will be in BTF" ); +__bpf_kfunc_start_defs ();/** * bpf_cpumask_create() - Create a mutable BPF cpumask.
@@ -407,7 +405,7 @@ __bpf_kfunc u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,return cpumask_any_and_distribute (src1, src2); } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (cpumask_kfunc_btf_ids) BTF_ID_FLAGS(func, bpf_cpumask_create, KF_ACQUIRE | KF_RET_NULL)
kernel/bpf/helpers.c +2 -4
@@ -1886,9 +1886,7 @@ void bpf_rb_root_free(const struct btf_field *field, void *rb_root, } } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc void *bpf_obj_new_impl(u64 local_type_id__k, void *meta__ign) {
@@ -2505,7 +2503,7 @@ __bpf_kfunc void bpf_throw(u64 cookie)WARN (1 ,"A call to BPF exception callback should never return\n" ); } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (generic_btf_ids) #ifdef CONFIG_KEXEC_CORE
kernel/bpf/map_iter.c +2 -4
@@ -193,9 +193,7 @@ static int __init bpf_map_iter_init(void)late_initcall (bpf_map_iter_init); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc s64 bpf_map_sum_elem_count(const struct bpf_map *map) {
@@ -213,7 +211,7 @@ __bpf_kfunc s64 bpf_map_sum_elem_count(const struct bpf_map *map)return ret; } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (bpf_map_iter_kfunc_ids)BTF_ID_FLAGS (func, bpf_map_sum_elem_count, KF_TRUSTED_ARGS)
kernel/bpf/task_iter.c +6 -12
@@ -824,9 +824,7 @@ struct bpf_iter_task_vma_kern {struct bpf_iter_task_vma_kern_data *data; } __attribute__((aligned(8))); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,struct task_struct *task,u64 addr)
@@ -892,7 +890,7 @@ __bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) } } -__diag_pop (); +__bpf_kfunc_end_defs ();struct bpf_iter_css_task {__u64 __opaque[1 ];
@@ -902,9 +900,7 @@ struct bpf_iter_css_task_kern {struct css_task_iter *css_it; } __attribute__((aligned(8))); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_css_task_new(struct bpf_iter_css_task *it,struct cgroup_subsys_state *css,unsigned int flags)
@@ -950,7 +946,7 @@ __bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it)bpf_mem_free (&bpf_global_ma, kit->css_it); } -__diag_pop (); +__bpf_kfunc_end_defs ();struct bpf_iter_task {__u64 __opaque[3 ];
@@ -971,9 +967,7 @@ enum {BPF_TASK_ITER_PROC_THREADS }; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,struct task_struct *task__nullable,unsigned int flags)
@@ -1043,7 +1037,7 @@ __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it) { } -__diag_pop (); +__bpf_kfunc_end_defs (); DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
kernel/trace/bpf_trace.c +2 -4
@@ -1252,9 +1252,7 @@ static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = { };#ifdef CONFIG_KEYS -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"kfuncs which will be used in BPF programs" ); +__bpf_kfunc_start_defs ();/** * bpf_lookup_user_key - lookup a key by its serial
@@ -1404,7 +1402,7 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr, }#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */ -__diag_pop (); +__bpf_kfunc_end_defs (); BTF_SET8_START(key_sig_kfunc_set) BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
net/bpf/test_run.c +3 -4
@@ -503,9 +503,8 @@ static int bpf_test_finish(const union bpf_attr *kattr,* architecture dependent calling conventions. 7+ can be supported in the * future. */ -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); + __bpf_kfunc int bpf_fentry_test1(int a) {return a +1 ;
@@ -605,7 +604,7 @@ __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p) { } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (bpf_test_modify_return_ids)BTF_ID_FLAGS (func, bpf_modify_return_test)
net/core/filter.c +4 -9
@@ -11767,9 +11767,7 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)return func; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_dynptr_from_skb(struct sk_buff *skb, u64 flags,struct bpf_dynptr_kern *ptr__uninit) {
@@ -11816,7 +11814,7 @@ __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,return 0 ; } -__diag_pop (); +__bpf_kfunc_end_defs ();int bpf_dynptr_from_skb_rdonly (struct sk_buff *skb,u64 flags,struct bpf_dynptr_kern *ptr__uninit)
@@ -11879,10 +11877,7 @@ static int __init bpf_kfunc_init(void) }late_initcall (bpf_kfunc_init); -/* Disables missing prototype warnings */ -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs ();/* bpf_sock_destroy: Destroy the given socket with ECONNABORTED error code. *
@@ -11916,7 +11911,7 @@ __bpf_kfunc int bpf_sock_destroy(struct sock_common *sock)return sk->sk_prot->diag_destroy (sk,ECONNABORTED ); } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (bpf_sk_iter_kfunc_ids)BTF_ID_FLAGS (func, bpf_sock_destroy, KF_TRUSTED_ARGS)
net/core/xdp.c +2 -4
@@ -696,9 +696,7 @@ struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf)return nxdpf; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs ();/** * bpf_xdp_metadata_rx_timestamp - Read XDP frame RX timestamp.
@@ -738,7 +736,7 @@ __bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32 *hash,return -EOPNOTSUPP ; } -__diag_pop (); +__bpf_kfunc_end_defs (); BTF_SET8_START(xdp_metadata_kfunc_ids)#define XDP_METADATA_KFUNC (_, __, name, ___) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
net/ipv4/fou_bpf.c +2 -4
@@ -22,9 +22,7 @@ enum bpf_fou_encap_type { FOU_BPF_ENCAP_GUE, }; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in BTF" ); +__bpf_kfunc_start_defs ();/* bpf_skb_set_fou_encap - Set FOU encap parameters *
@@ -100,7 +98,7 @@ __bpf_kfunc int bpf_skb_get_fou_encap(struct __sk_buff *skb_ctx,return 0 ; } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (fou_kfunc_set)BTF_ID_FLAGS (func, bpf_skb_set_fou_encap)
net/netfilter/nf_conntrack_bpf.c +2 -4
@@ -230,9 +230,7 @@ static int _nf_conntrack_btf_struct_access(struct bpf_verifier_log *log,return 0 ; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in nf_conntrack BTF" ); +__bpf_kfunc_start_defs ();/* bpf_xdp_ct_alloc - Allocate a new CT entry *
@@ -467,7 +465,7 @@ __bpf_kfunc int bpf_ct_change_status(struct nf_conn *nfct, u32 status)return nf_ct_change_status_common (nfct, status); } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (nf_ct_kfunc_set) BTF_ID_FLAGS(func, bpf_xdp_ct_alloc, KF_ACQUIRE | KF_RET_NULL)
net/netfilter/nf_nat_bpf.c +2 -4
@@ -12,9 +12,7 @@#include <net/netfilter/nf_conntrack_core.h> #include <net/netfilter/nf_nat.h> -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in nf_nat BTF" ); +__bpf_kfunc_start_defs ();/* bpf_ct_set_nat_info - Set source or destination nat address *
@@ -54,7 +52,7 @@ __bpf_kfunc int bpf_ct_set_nat_info(struct nf_conn___init *nfct,return nf_nat_setup_info (ct, &range, manip) ==NF_DROP ? -ENOMEM :0 ; } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (nf_nat_kfunc_set)BTF_ID_FLAGS (func, bpf_ct_set_nat_info, KF_TRUSTED_ARGS)
net/xfrm/xfrm_interface_bpf.c +2 -4
@@ -27,9 +27,7 @@ struct bpf_xfrm_info {int link; }; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in xfrm_interface BTF" ); +__bpf_kfunc_start_defs ();/* bpf_skb_get_xfrm_info - Get XFRM metadata *
@@ -93,7 +91,7 @@ __bpf_kfunc int bpf_skb_set_xfrm_info(struct __sk_buff *skb_ctx, const struct bpreturn 0 ; } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (xfrm_ifc_kfunc_set)BTF_ID_FLAGS (func, bpf_skb_get_xfrm_info)
Signature · 1 line
2.34.1
PATCH v2 2/2 bpf: Add __bpf_hook_{start,end} macros
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.
---
This patch was added in v2 in response to convo on v1's thread.
include/linux/btf.h | 2 ++ kernel/cgroup/rstat.c | 9 +++------ net/socket.c | 8 ++------ tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 6 ++---- 4 files changed, 9 insertions(+), 16 deletions(-)
include/linux/btf.h +2 -0
@@ -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
kernel/cgroup/rstat.c +3 -6
@@ -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)
net/socket.c +2 -6
@@ -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) {
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +2 -4
@@ -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 intbpf_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 , },
Signature · 1 line
2.34.1
Re: [PATCH v2 bpf-next 2/2] bpf: Add __bpf_hook_{start,end} macros
Quoted 12 lines · Dave Marchevsky
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_testmodhook points as well.
Quoted 2 lines
Signed-off-by Dave Marchevsky <[email protected]>Cc Yafang Shao <[email protected]>
jirka
Quoted 105 lines
---
This patch was added in v2 in response to convo on v1's thread.
include/linux/btf.h | 2 ++ kernel/cgroup/rstat.c | 9 +++------ net/socket.c | 8 ++------ tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 6 ++---- 4 files changed, 9 insertions(+), 16 deletions(-)include/linux/btf.h +2
-0@@ -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 kernel/cgroup/rstat.c +3
-6@@ -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)net/socket.c +2
-6@@ -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) {tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +2
-4@@ -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 intbpf_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 , },Signature · 1 line
2.34.1
Yafang Shao
Acked-byRe: [PATCH v2 bpf-next 2/2] bpf: Add __bpf_hook_{start,end} macros
Quoted 14 lines · Jiri Olsa
Quoted 12 lines · Dave Marchevsky
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_testmodhook points as well.
Quoted 4 lines
Quoted 2 lines
Signed-off-by Dave Marchevsky <[email protected]>Cc Yafang Shao <[email protected]>Acked-by Jiri Olsa <[email protected]>
Quoted 108 lines
jirka
Quoted 105 lines
---
This patch was added in v2 in response to convo on v1's thread.
include/linux/btf.h | 2 ++ kernel/cgroup/rstat.c | 9 +++------ net/socket.c | 8 ++------ tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 6 ++---- 4 files changed, 9 insertions(+), 16 deletions(-)include/linux/btf.h +2
-0@@ -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 kernel/cgroup/rstat.c +3
-6@@ -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)net/socket.c +2
-6@@ -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) {tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +2
-5@@ -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 intbpf_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 , }, --2.34.1
Signature · 2 lines
Regards Yafang
Quoted 17 lines · Dave Marchevsky
BPF kfuncs are meant to be called from BPF programs. Accordingly, most kfuncs are not called from anywhere in the kernel, which the -Wmissing-prototypes warning is unhappy about. We've peppered __diag_ignore_all("-Wmissing-prototypes", ... everywhere kfuncs are defined in the codebase to suppress this warning.
This patch adds two macros meant to bound one or many kfunc definitions. All existing kfunc definitions which use these
__diagcalls to suppress -Wmissing-prototypes are migrated to use the newly-introduced macros. A new__diag_ignore_all- for "-Wmissing-declarations" - is added to the__bpf_kfunc_start_defsmacro based on feedback from Andrii on an earlier version of this patch [0] and another recent mailing list thread [1].In the future we might need to ignore different warnings or do other kfunc-specific things. This change will make it easier to make such modifications for all kfunc defs.
Quoted 7 lines
[0]: https://lore.kernel.org/bpf/CAEf4BzaE5dRWtK6RPLnjTW-MW9sx9K3Fn6uwqCTChK2Dcb1Xig@mail.gmail.com/
[1]: https://lore.kernel.org/bpf/ZT+2qCc%2FaXep0%2FLf@krava/Signed-off-by Dave Marchevsky <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Acked-by Andrii Nakryiko <[email protected]>Cc Jiri Olsa <[email protected]>
jirka
Quoted 465 lines
---
v1 -> v2: https://lore.kernel.org/bpf/[email protected]/
* UpdateDocumentation/bpf/kfuncs.rstto use new macros (Yafang, Andrii)
* Update recently-added open-coded {task,cgroup} iters to use new macros (Yafang, Andrii)
* Add Andrii ackDocumentation/bpf/kfuncs.rst | 6 ++---- include/linux/btf.h | 9 +++++++++ kernel/bpf/bpf_iter.c | 6 ++---- kernel/bpf/cgroup_iter.c | 6 ++---- kernel/bpf/cpumask.c | 6 ++---- kernel/bpf/helpers.c | 6 ++---- kernel/bpf/map_iter.c | 6 ++---- kernel/bpf/task_iter.c | 18 ++++++------------ kernel/trace/bpf_trace.c | 6 ++---- net/bpf/test_run.c | 7 +++---- net/core/filter.c | 13 ++++--------- net/core/xdp.c | 6 ++---- net/ipv4/fou_bpf.c | 6 ++---- net/netfilter/nf_conntrack_bpf.c | 6 ++---- net/netfilter/nf_nat_bpf.c | 6 ++---- net/xfrm/xfrm_interface_bpf.c | 6 ++---- 16 files changed, 46 insertions(+), 73 deletions(-)Documentation/bpf/kfuncs.rst +2
-4@@ -37,16 +37,14 @@ prototype in a header for the wrapper kfunc. An example is given below:: /* Disables missing prototype warnings */ - __diag_push(); - __diag_ignore_all("-Wmissing-prototypes", - "Global kfuncs as their definitions will be in BTF"); + __bpf_kfunc_start_defs(); __bpf_kfunc struct task_struct *bpf_find_get_task_by_vpid(pid_t nr) { return find_get_task_by_vpid(nr); } - __diag_pop(); + __bpf_kfunc_end_defs(); A wrapper kfunc is often needed when we need to annotate parameters of the kfunc. Otherwise one may directly make the kfunc visible to the BPF program byinclude/linux/btf.h +9
-0@@ -84,6 +84,15 @@*/ #define __bpf_kfunc __used noinline +#define __bpf_kfunc_start_defs () \ + __diag_push(); \ + __diag_ignore_all("-Wmissing-declarations", \ + "Global kfuncs as their definitions will be in BTF");\ + __diag_ignore_all("-Wmissing-prototypes", \ + "Global kfuncs as their definitions will be in BTF") + +#define __bpf_kfunc_end_defs () __diag_pop() +/* * Return the name of the passed struct, if exists, or halt the build if for * example the structure gets renamed. In this way, developers have to revisit kernel/bpf/bpf_iter.c +2
-4@@ -782,9 +782,7 @@ struct bpf_iter_num_kern {int end;/* final value, exclusive */ }__aligned(8) ; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) {@@ -843,4 +841,4 @@ __bpf_kfunc void bpf_iter_num_destroy(struct bpf_iter_num *it) s->cur = s->end =0 ; } -__diag_pop (); +__bpf_kfunc_end_defs ();kernel/bpf/cgroup_iter.c +2
-4@@ -305,9 +305,7 @@ struct bpf_iter_css_kern {unsigned int flags; } __attribute__((aligned(8))); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it,struct cgroup_subsys_state *start,unsigned int flags)@@ -358,4 +356,4 @@ __bpf_kfunc void bpf_iter_css_destroy(struct bpf_iter_css *it) { } -__diag_pop (); +__bpf_kfunc_end_defs ();kernel/bpf/cpumask.c +2
-4@@ -34,9 +34,7 @@ static bool cpu_valid(u32 cpu)return cpu < nr_cpu_ids; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global kfuncs as their definitions will be in BTF" ); +__bpf_kfunc_start_defs ();/** * bpf_cpumask_create() - Create a mutable BPF cpumask. @@ -407,7 +405,7 @@ __bpf_kfunc u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,return cpumask_any_and_distribute (src1, src2); } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (cpumask_kfunc_btf_ids) BTF_ID_FLAGS(func, bpf_cpumask_create, KF_ACQUIRE | KF_RET_NULL)kernel/bpf/helpers.c +2
-4@@ -1886,9 +1886,7 @@ void bpf_rb_root_free(const struct btf_field *field, void *rb_root, } } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc void *bpf_obj_new_impl(u64 local_type_id__k, void *meta__ign) {@@ -2505,7 +2503,7 @@ __bpf_kfunc void bpf_throw(u64 cookie)WARN (1 ,"A call to BPF exception callback should never return\n" ); } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (generic_btf_ids) #ifdef CONFIG_KEXEC_COREkernel/bpf/map_iter.c +2
-4@@ -193,9 +193,7 @@ static int __init bpf_map_iter_init(void)late_initcall (bpf_map_iter_init); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc s64 bpf_map_sum_elem_count(const struct bpf_map *map) {@@ -213,7 +211,7 @@ __bpf_kfunc s64 bpf_map_sum_elem_count(const struct bpf_map *map)return ret; } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (bpf_map_iter_kfunc_ids)BTF_ID_FLAGS (func, bpf_map_sum_elem_count, KF_TRUSTED_ARGS)kernel/bpf/task_iter.c +6
-12@@ -824,9 +824,7 @@ struct bpf_iter_task_vma_kern {struct bpf_iter_task_vma_kern_data *data; } __attribute__((aligned(8))); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,struct task_struct *task,u64 addr)@@ -892,7 +890,7 @@ __bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) } } -__diag_pop (); +__bpf_kfunc_end_defs ();struct bpf_iter_css_task {__u64 __opaque[1 ];@@ -902,9 +900,7 @@ struct bpf_iter_css_task_kern {struct css_task_iter *css_it; } __attribute__((aligned(8))); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_css_task_new(struct bpf_iter_css_task *it,struct cgroup_subsys_state *css,unsigned int flags)@@ -950,7 +946,7 @@ __bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it)bpf_mem_free (&bpf_global_ma, kit->css_it); } -__diag_pop (); +__bpf_kfunc_end_defs ();struct bpf_iter_task {__u64 __opaque[3 ];@@ -971,9 +967,7 @@ enum {BPF_TASK_ITER_PROC_THREADS }; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,struct task_struct *task__nullable,unsigned int flags)@@ -1043,7 +1037,7 @@ __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it) { } -__diag_pop (); +__bpf_kfunc_end_defs (); DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);kernel/trace/bpf_trace.c +2
-4@@ -1252,9 +1252,7 @@ static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = { };#ifdef CONFIG_KEYS -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"kfuncs which will be used in BPF programs" ); +__bpf_kfunc_start_defs ();/** * bpf_lookup_user_key - lookup a key by its serial @@ -1404,7 +1402,7 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr, }#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */ -__diag_pop (); +__bpf_kfunc_end_defs (); BTF_SET8_START(key_sig_kfunc_set) BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)net/bpf/test_run.c +3
-4@@ -503,9 +503,8 @@ static int bpf_test_finish(const union bpf_attr *kattr,* architecture dependent calling conventions. 7+ can be supported in the * future. */ -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); + __bpf_kfunc int bpf_fentry_test1(int a) {return a +1 ;@@ -605,7 +604,7 @@ __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p) { } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (bpf_test_modify_return_ids)BTF_ID_FLAGS (func, bpf_modify_return_test)net/core/filter.c +4
-9@@ -11767,9 +11767,7 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)return func; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_dynptr_from_skb(struct sk_buff *skb, u64 flags,struct bpf_dynptr_kern *ptr__uninit) {@@ -11816,7 +11814,7 @@ __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,return 0 ; } -__diag_pop (); +__bpf_kfunc_end_defs ();int bpf_dynptr_from_skb_rdonly (struct sk_buff *skb,u64 flags,struct bpf_dynptr_kern *ptr__uninit)@@ -11879,10 +11877,7 @@ static int __init bpf_kfunc_init(void) }late_initcall (bpf_kfunc_init); -/* Disables missing prototype warnings */ -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs ();/* bpf_sock_destroy: Destroy the given socket with ECONNABORTED error code. * @@ -11916,7 +11911,7 @@ __bpf_kfunc int bpf_sock_destroy(struct sock_common *sock)return sk->sk_prot->diag_destroy (sk,ECONNABORTED ); } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (bpf_sk_iter_kfunc_ids)BTF_ID_FLAGS (func, bpf_sock_destroy, KF_TRUSTED_ARGS)net/core/xdp.c +2
-4@@ -696,9 +696,7 @@ struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf)return nxdpf; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs ();/** * bpf_xdp_metadata_rx_timestamp - Read XDP frame RX timestamp. @@ -738,7 +736,7 @@ __bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32 *hash,return -EOPNOTSUPP ; } -__diag_pop (); +__bpf_kfunc_end_defs (); BTF_SET8_START(xdp_metadata_kfunc_ids)#define XDP_METADATA_KFUNC (_, __, name, ___) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)net/ipv4/fou_bpf.c +2
-4@@ -22,9 +22,7 @@ enum bpf_fou_encap_type { FOU_BPF_ENCAP_GUE, }; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in BTF" ); +__bpf_kfunc_start_defs ();/* bpf_skb_set_fou_encap - Set FOU encap parameters * @@ -100,7 +98,7 @@ __bpf_kfunc int bpf_skb_get_fou_encap(struct __sk_buff *skb_ctx,return 0 ; } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (fou_kfunc_set)BTF_ID_FLAGS (func, bpf_skb_set_fou_encap)net/netfilter/nf_conntrack_bpf.c +2
-4@@ -230,9 +230,7 @@ static int _nf_conntrack_btf_struct_access(struct bpf_verifier_log *log,return 0 ; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in nf_conntrack BTF" ); +__bpf_kfunc_start_defs ();/* bpf_xdp_ct_alloc - Allocate a new CT entry * @@ -467,7 +465,7 @@ __bpf_kfunc int bpf_ct_change_status(struct nf_conn *nfct, u32 status)return nf_ct_change_status_common (nfct, status); } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (nf_ct_kfunc_set) BTF_ID_FLAGS(func, bpf_xdp_ct_alloc, KF_ACQUIRE | KF_RET_NULL)net/netfilter/nf_nat_bpf.c +2
-4@@ -12,9 +12,7 @@#include <net/netfilter/nf_conntrack_core.h> #include <net/netfilter/nf_nat.h> -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in nf_nat BTF" ); +__bpf_kfunc_start_defs ();/* bpf_ct_set_nat_info - Set source or destination nat address * @@ -54,7 +52,7 @@ __bpf_kfunc int bpf_ct_set_nat_info(struct nf_conn___init *nfct,return nf_nat_setup_info (ct, &range, manip) ==NF_DROP ? -ENOMEM :0 ; } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (nf_nat_kfunc_set)BTF_ID_FLAGS (func, bpf_ct_set_nat_info, KF_TRUSTED_ARGS)net/xfrm/xfrm_interface_bpf.c +2
-4@@ -27,9 +27,7 @@ struct bpf_xfrm_info {int link; }; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in xfrm_interface BTF" ); +__bpf_kfunc_start_defs ();/* bpf_skb_get_xfrm_info - Get XFRM metadata * @@ -93,7 +91,7 @@ __bpf_kfunc int bpf_skb_set_xfrm_info(struct __sk_buff *skb_ctx, const struct bpreturn 0 ; } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (xfrm_ifc_kfunc_set)BTF_ID_FLAGS (func, bpf_skb_get_xfrm_info)Signature · 1 line
2.34.1
Quoted 17 lines · Dave Marchevsky
BPF kfuncs are meant to be called from BPF programs. Accordingly, most kfuncs are not called from anywhere in the kernel, which the -Wmissing-prototypes warning is unhappy about. We've peppered __diag_ignore_all("-Wmissing-prototypes", ... everywhere kfuncs are defined in the codebase to suppress this warning.
This patch adds two macros meant to bound one or many kfunc definitions. All existing kfunc definitions which use these
__diagcalls to suppress -Wmissing-prototypes are migrated to use the newly-introduced macros. A new__diag_ignore_all- for "-Wmissing-declarations" - is added to the__bpf_kfunc_start_defsmacro based on feedback from Andrii on an earlier version of this patch [0] and another recent mailing list thread [1].In the future we might need to ignore different warnings or do other kfunc-specific things. This change will make it easier to make such modifications for all kfunc defs.
Quoted 7 lines
[0]: https://lore.kernel.org/bpf/CAEf4BzaE5dRWtK6RPLnjTW-MW9sx9K3Fn6uwqCTChK2Dcb1Xig@mail.gmail.com/
[1]: https://lore.kernel.org/bpf/ZT+2qCc%2FaXep0%2FLf@krava/Signed-off-by Dave Marchevsky <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Acked-by Andrii Nakryiko <[email protected]>Cc Jiri Olsa <[email protected]>
Thanks for taking care of this!
Quoted 18 lines · Dave Marchevsky
BPF kfuncs are meant to be called from BPF programs. Accordingly, most kfuncs are not called from anywhere in the kernel, which the -Wmissing-prototypes warning is unhappy about. We've peppered __diag_ignore_all("-Wmissing-prototypes", ... everywhere kfuncs are defined in the codebase to suppress this warning.
This patch adds two macros meant to bound one or many kfunc definitions. All existing kfunc definitions which use these
__diagcalls to suppress -Wmissing-prototypes are migrated to use the newly-introduced macros. A new__diag_ignore_all- for "-Wmissing-declarations" - is added to the__bpf_kfunc_start_defsmacro based on feedback from Andrii on an earlier version of this patch [0] and another recent mailing list thread [1].In the future we might need to ignore different warnings or do other kfunc-specific things. This change will make it easier to make such modifications for all kfunc defs.
Quoted 7 lines
[0]: https://lore.kernel.org/bpf/CAEf4BzaE5dRWtK6RPLnjTW-MW9sx9K3Fn6uwqCTChK2Dcb1Xig@mail.gmail.com/
[1]: https://lore.kernel.org/bpf/ZT+2qCc%2FaXep0%2FLf@krava/Signed-off-by Dave Marchevsky <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Acked-by Andrii Nakryiko <[email protected]>Cc Jiri Olsa <[email protected]>
Quoted 465 lines
---
v1 -> v2: https://lore.kernel.org/bpf/[email protected]/
* UpdateDocumentation/bpf/kfuncs.rstto use new macros (Yafang, Andrii)
* Update recently-added open-coded {task,cgroup} iters to use new macros (Yafang, Andrii)
* Add Andrii ackDocumentation/bpf/kfuncs.rst | 6 ++---- include/linux/btf.h | 9 +++++++++ kernel/bpf/bpf_iter.c | 6 ++---- kernel/bpf/cgroup_iter.c | 6 ++---- kernel/bpf/cpumask.c | 6 ++---- kernel/bpf/helpers.c | 6 ++---- kernel/bpf/map_iter.c | 6 ++---- kernel/bpf/task_iter.c | 18 ++++++------------ kernel/trace/bpf_trace.c | 6 ++---- net/bpf/test_run.c | 7 +++---- net/core/filter.c | 13 ++++--------- net/core/xdp.c | 6 ++---- net/ipv4/fou_bpf.c | 6 ++---- net/netfilter/nf_conntrack_bpf.c | 6 ++---- net/netfilter/nf_nat_bpf.c | 6 ++---- net/xfrm/xfrm_interface_bpf.c | 6 ++---- 16 files changed, 46 insertions(+), 73 deletions(-)Documentation/bpf/kfuncs.rst +2
-4@@ -37,16 +37,14 @@ prototype in a header for the wrapper kfunc. An example is given below:: /* Disables missing prototype warnings */ - __diag_push(); - __diag_ignore_all("-Wmissing-prototypes", - "Global kfuncs as their definitions will be in BTF"); + __bpf_kfunc_start_defs(); __bpf_kfunc struct task_struct *bpf_find_get_task_by_vpid(pid_t nr) { return find_get_task_by_vpid(nr); } - __diag_pop(); + __bpf_kfunc_end_defs(); A wrapper kfunc is often needed when we need to annotate parameters of the kfunc. Otherwise one may directly make the kfunc visible to the BPF program byinclude/linux/btf.h +9
-0@@ -84,6 +84,15 @@*/ #define __bpf_kfunc __used noinline +#define __bpf_kfunc_start_defs () \ + __diag_push(); \ + __diag_ignore_all("-Wmissing-declarations", \ + "Global kfuncs as their definitions will be in BTF");\ + __diag_ignore_all("-Wmissing-prototypes", \ + "Global kfuncs as their definitions will be in BTF") + +#define __bpf_kfunc_end_defs () __diag_pop() +/* * Return the name of the passed struct, if exists, or halt the build if for * example the structure gets renamed. In this way, developers have to revisit kernel/bpf/bpf_iter.c +2
-4@@ -782,9 +782,7 @@ struct bpf_iter_num_kern {int end;/* final value, exclusive */ }__aligned(8) ; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) {@@ -843,4 +841,4 @@ __bpf_kfunc void bpf_iter_num_destroy(struct bpf_iter_num *it) s->cur = s->end =0 ; } -__diag_pop (); +__bpf_kfunc_end_defs ();kernel/bpf/cgroup_iter.c +2
-4@@ -305,9 +305,7 @@ struct bpf_iter_css_kern {unsigned int flags; } __attribute__((aligned(8))); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it,struct cgroup_subsys_state *start,unsigned int flags)@@ -358,4 +356,4 @@ __bpf_kfunc void bpf_iter_css_destroy(struct bpf_iter_css *it) { } -__diag_pop (); +__bpf_kfunc_end_defs ();kernel/bpf/cpumask.c +2
-4@@ -34,9 +34,7 @@ static bool cpu_valid(u32 cpu)return cpu < nr_cpu_ids; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global kfuncs as their definitions will be in BTF" ); +__bpf_kfunc_start_defs ();/** * bpf_cpumask_create() - Create a mutable BPF cpumask. @@ -407,7 +405,7 @@ __bpf_kfunc u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,return cpumask_any_and_distribute (src1, src2); } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (cpumask_kfunc_btf_ids) BTF_ID_FLAGS(func, bpf_cpumask_create, KF_ACQUIRE | KF_RET_NULL)kernel/bpf/helpers.c +2
-4@@ -1886,9 +1886,7 @@ void bpf_rb_root_free(const struct btf_field *field, void *rb_root, } } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc void *bpf_obj_new_impl(u64 local_type_id__k, void *meta__ign) {@@ -2505,7 +2503,7 @@ __bpf_kfunc void bpf_throw(u64 cookie)WARN (1 ,"A call to BPF exception callback should never return\n" ); } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (generic_btf_ids) #ifdef CONFIG_KEXEC_COREkernel/bpf/map_iter.c +2
-4@@ -193,9 +193,7 @@ static int __init bpf_map_iter_init(void)late_initcall (bpf_map_iter_init); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc s64 bpf_map_sum_elem_count(const struct bpf_map *map) {@@ -213,7 +211,7 @@ __bpf_kfunc s64 bpf_map_sum_elem_count(const struct bpf_map *map)return ret; } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (bpf_map_iter_kfunc_ids)BTF_ID_FLAGS (func, bpf_map_sum_elem_count, KF_TRUSTED_ARGS)kernel/bpf/task_iter.c +6
-12@@ -824,9 +824,7 @@ struct bpf_iter_task_vma_kern {struct bpf_iter_task_vma_kern_data *data; } __attribute__((aligned(8))); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,struct task_struct *task,u64 addr)@@ -892,7 +890,7 @@ __bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) } } -__diag_pop (); +__bpf_kfunc_end_defs ();struct bpf_iter_css_task {__u64 __opaque[1 ];@@ -902,9 +900,7 @@ struct bpf_iter_css_task_kern {struct css_task_iter *css_it; } __attribute__((aligned(8))); -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_css_task_new(struct bpf_iter_css_task *it,struct cgroup_subsys_state *css,unsigned int flags)@@ -950,7 +946,7 @@ __bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it)bpf_mem_free (&bpf_global_ma, kit->css_it); } -__diag_pop (); +__bpf_kfunc_end_defs ();struct bpf_iter_task {__u64 __opaque[3 ];@@ -971,9 +967,7 @@ enum {BPF_TASK_ITER_PROC_THREADS }; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,struct task_struct *task__nullable,unsigned int flags)@@ -1043,7 +1037,7 @@ __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it) { } -__diag_pop (); +__bpf_kfunc_end_defs (); DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);kernel/trace/bpf_trace.c +2
-4@@ -1252,9 +1252,7 @@ static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = { };#ifdef CONFIG_KEYS -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"kfuncs which will be used in BPF programs" ); +__bpf_kfunc_start_defs ();/** * bpf_lookup_user_key - lookup a key by its serial @@ -1404,7 +1402,7 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr, }#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */ -__diag_pop (); +__bpf_kfunc_end_defs (); BTF_SET8_START(key_sig_kfunc_set) BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)net/bpf/test_run.c +3
-4@@ -503,9 +503,8 @@ static int bpf_test_finish(const union bpf_attr *kattr,* architecture dependent calling conventions. 7+ can be supported in the * future. */ -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); + __bpf_kfunc int bpf_fentry_test1(int a) {return a +1 ;@@ -605,7 +604,7 @@ __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p) { } -__diag_pop (); +__bpf_kfunc_end_defs ();BTF_SET8_START (bpf_test_modify_return_ids)BTF_ID_FLAGS (func, bpf_modify_return_test)net/core/filter.c +4
-9@@ -11767,9 +11767,7 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)return func; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs (); __bpf_kfunc int bpf_dynptr_from_skb(struct sk_buff *skb, u64 flags,struct bpf_dynptr_kern *ptr__uninit) {@@ -11816,7 +11814,7 @@ __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,return 0 ; } -__diag_pop (); +__bpf_kfunc_end_defs ();int bpf_dynptr_from_skb_rdonly (struct sk_buff *skb,u64 flags,struct bpf_dynptr_kern *ptr__uninit)@@ -11879,10 +11877,7 @@ static int __init bpf_kfunc_init(void) }late_initcall (bpf_kfunc_init); -/* Disables missing prototype warnings */ -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs ();/* bpf_sock_destroy: Destroy the given socket with ECONNABORTED error code. * @@ -11916,7 +11911,7 @@ __bpf_kfunc int bpf_sock_destroy(struct sock_common *sock)return sk->sk_prot->diag_destroy (sk,ECONNABORTED ); } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (bpf_sk_iter_kfunc_ids)BTF_ID_FLAGS (func, bpf_sock_destroy, KF_TRUSTED_ARGS)net/core/xdp.c +2
-4@@ -696,9 +696,7 @@ struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf)return nxdpf; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in vmlinux BTF" ); +__bpf_kfunc_start_defs ();/** * bpf_xdp_metadata_rx_timestamp - Read XDP frame RX timestamp. @@ -738,7 +736,7 @@ __bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32 *hash,return -EOPNOTSUPP ; } -__diag_pop (); +__bpf_kfunc_end_defs (); BTF_SET8_START(xdp_metadata_kfunc_ids)#define XDP_METADATA_KFUNC (_, __, name, ___) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)net/ipv4/fou_bpf.c +2
-4@@ -22,9 +22,7 @@ enum bpf_fou_encap_type { FOU_BPF_ENCAP_GUE, }; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in BTF" ); +__bpf_kfunc_start_defs ();/* bpf_skb_set_fou_encap - Set FOU encap parameters * @@ -100,7 +98,7 @@ __bpf_kfunc int bpf_skb_get_fou_encap(struct __sk_buff *skb_ctx,return 0 ; } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (fou_kfunc_set)BTF_ID_FLAGS (func, bpf_skb_set_fou_encap)net/netfilter/nf_conntrack_bpf.c +2
-4@@ -230,9 +230,7 @@ static int _nf_conntrack_btf_struct_access(struct bpf_verifier_log *log,return 0 ; } -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in nf_conntrack BTF" ); +__bpf_kfunc_start_defs ();/* bpf_xdp_ct_alloc - Allocate a new CT entry * @@ -467,7 +465,7 @@ __bpf_kfunc int bpf_ct_change_status(struct nf_conn *nfct, u32 status)return nf_ct_change_status_common (nfct, status); } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (nf_ct_kfunc_set) BTF_ID_FLAGS(func, bpf_xdp_ct_alloc, KF_ACQUIRE | KF_RET_NULL)net/netfilter/nf_nat_bpf.c +2
-4@@ -12,9 +12,7 @@#include <net/netfilter/nf_conntrack_core.h> #include <net/netfilter/nf_nat.h> -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in nf_nat BTF" ); +__bpf_kfunc_start_defs ();/* bpf_ct_set_nat_info - Set source or destination nat address * @@ -54,7 +52,7 @@ __bpf_kfunc int bpf_ct_set_nat_info(struct nf_conn___init *nfct,return nf_nat_setup_info (ct, &range, manip) ==NF_DROP ? -ENOMEM :0 ; } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (nf_nat_kfunc_set)BTF_ID_FLAGS (func, bpf_ct_set_nat_info, KF_TRUSTED_ARGS)net/xfrm/xfrm_interface_bpf.c +2
-5@@ -27,9 +27,7 @@ struct bpf_xfrm_info {int link; }; -__diag_push (); -__diag_ignore_all ("-Wmissing-prototypes" , -"Global functions as their definitions will be in xfrm_interface BTF" ); +__bpf_kfunc_start_defs ();/* bpf_skb_get_xfrm_info - Get XFRM metadata * @@ -93,7 +91,7 @@ __bpf_kfunc int bpf_skb_set_xfrm_info(struct __sk_buff *skb_ctx, const struct bpreturn 0 ; } -__diag_pop() +__bpf_kfunc_end_defs ();BTF_SET8_START (xfrm_ifc_kfunc_set)BTF_ID_FLAGS (func, bpf_skb_get_xfrm_info) --2.34.1
Signature · 2 lines
Regards Yafang
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 6 lines · On Tue, 31 Oct 2023 14:56:24 -0700 you wrote:
BPF kfuncs are meant to be called from BPF programs. Accordingly, most kfuncs are not called from anywhere in the kernel, which the -Wmissing-prototypes warning is unhappy about. We've peppered __diag_ignore_all("-Wmissing-prototypes", ... everywhere kfuncs are defined in the codebase to suppress this warning.
Quoted 8 lines
This patch adds two macros meant to bound one or many kfunc definitions. All existing kfunc definitions which use these
__diagcalls to suppress -Wmissing-prototypes are migrated to use the newly-introduced macros. A new__diag_ignore_all- for "-Wmissing-declarations" - is added to the__bpf_kfunc_start_defsmacro based on feedback from Andrii on an earlier version of this patch [0] and another recent mailing list thread [1].[...]
Here is the summary with links:
- [v2,bpf-next,1/2] bpf: Add __bpf_kfunc_{start,end}_defs macros
https://git.kernel.org/bpf/bpf/c/391145ba2acc - [v2,bpf-next,2/2] bpf: Add __bpf_hook_{start,end} macros https://git.kernel.org/bpf/bpf/c/15fb6f2b6c4c
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html