selftests/bpf: Add missing kfunc declarations to fix build errors
Subsystems (3)
PATCH v2 1/1 selftests/bpf: Add missing kfunc declarations to fix build errors
A number of BPF selftests that utilize kernel functions (kfuncs) fail to build due to missing function prototypes. This results in compilation errors, as implicit function declarations are treated as errors:
error: call to undeclared function 'bpf_copy_from_user_task_str';
ISO C99 and later do not support implicit function declarations
Unlike BPF helpers, kfuncs are not automatically available to BPF programs and must be explicitly declared before use.
To resolve this, centralize all the necessary kfunc declarations into the bpf_kfuncs.h header file. This header is then included in all the test programs that were previously missing these declarations.
This approach also allows for the removal of redundant local extern declarations from individual source files (e.g., in irq.c), leading to cleaner and more maintainable code.
Change since v1:
- Add a kfunc declaration for __bpf_trap in bpf_kfuncs.h
--- tools/testing/selftests/bpf/bpf_kfuncs.h | 65 +++++++++++++++++++ .../selftests/bpf/progs/bpf_iter_tasks.c | 1 + .../bpf/progs/bpf_qdisc_fail__incompl_ops.c | 1 + .../selftests/bpf/progs/bpf_qdisc_fifo.c | 1 + .../selftests/bpf/progs/bpf_qdisc_fq.c | 1 + .../selftests/bpf/progs/cgroup_read_xattr.c | 1 + .../testing/selftests/bpf/progs/dmabuf_iter.c | 1 + .../selftests/bpf/progs/dynptr_success.c | 1 + tools/testing/selftests/bpf/progs/irq.c | 6 +- .../selftests/bpf/progs/linked_list_peek.c | 1 + .../selftests/bpf/progs/rbtree_search.c | 1 + .../selftests/bpf/progs/rcu_read_lock.c | 1 + .../selftests/bpf/progs/read_cgroupfs_xattr.c | 1 + .../selftests/bpf/progs/res_spin_lock.c | 1 + .../selftests/bpf/progs/res_spin_lock_fail.c | 1 + .../struct_ops_refcounted_fail__tail_call.c | 1 + .../selftests/bpf/progs/test_spin_lock_fail.c | 1 + .../selftests/bpf/progs/verifier_bpf_trap.c | 1 + 18 files changed, 82 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/bpf_kfuncs.h +65 -0
@@ -2,6 +2,7 @@#define __BPF_KFUNCS__struct bpf_sock_addr_kern ; +struct bpf_res_spin_lock ;/* Description * Initializes an skb-type dynptr
@@ -42,6 +43,28 @@ extern bool bpf_dynptr_is_null(const struct bpf_dynptr *ptr) __ksym __weak; extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak; extern __u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym __weak; extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym __weak; +extern int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, __u32 dst_off, struct bpf_dynptr *src_ptr, + __u32 src_off, __u32 size) __ksym __weak; +extern int bpf_probe_read_user_dynptr(struct bpf_dynptr *dptr, __u32 off, + __u32 size, const void *unsafe_ptr__ign) __ksym __weak; +extern int bpf_probe_read_kernel_dynptr(struct bpf_dynptr *dptr, __u32 off, + __u32 size, const void *unsafe_ptr__ign) __ksym __weak; +extern int bpf_probe_read_user_str_dynptr(struct bpf_dynptr *dptr, __u32 off, + __u32 size, const void *unsafe_ptr__ign) __ksym __weak; +extern int bpf_probe_read_kernel_str_dynptr(struct bpf_dynptr *dptr, __u32 off, + __u32 size, const void *unsafe_ptr__ign) __ksym __weak; +extern int bpf_copy_from_user_dynptr(struct bpf_dynptr *dptr, __u32 off, + __u32 size, const void *unsafe_ptr__ign) __ksym __weak; +extern int bpf_copy_from_user_str_dynptr(struct bpf_dynptr *dptr, __u32 off, + __u32 size, const void *unsafe_ptr__ign) __ksym __weak; +extern int bpf_copy_from_user_task_dynptr(struct bpf_dynptr *dptr, __u32 off, + __u32 size, const void *unsafe_ptr__ign, + struct task_struct *tsk) __ksym __weak; +extern int bpf_copy_from_user_task_str_dynptr(struct bpf_dynptr *dptr, __u32 off, + __u32 size, const void *unsafe_ptr__ign, + struct task_struct *tsk) __ksym __weak; +extern int bpf_copy_from_user_task_str(void *dst, __u32, const void *, + struct task_struct *, __u64) __ksym __weak;/* Description * Modify the address of a AF_UNIX sockaddr.
@@ -92,4 +115,46 @@ extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str, const struct bpf_dynptr *value_p, int flags) __ksym __weak; extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) __ksym __weak; +extern void bpf_local_irq_save(unsigned long *) __ksym __weak; +extern void bpf_local_irq_restore(unsigned long *) __ksym __weak; +extern int bpf_copy_from_user_str(void *dst, __u32 dst__sz, + const void *unsafe_ptr__ign, __u64 flags) __ksym __weak; +extern int bpf_res_spin_lock_irqsave(struct bpf_res_spin_lock *lock, + unsigned long *flags__irq_flag) __ksym __weak; +extern void bpf_res_spin_unlock_irqrestore(struct bpf_res_spin_lock *lock, + unsigned long *flags__irq_flag) __ksym __weak; +extern int bpf_res_spin_lock(struct bpf_res_spin_lock *lock) __ksym __weak; +extern void bpf_res_spin_unlock(struct bpf_res_spin_lock *lock) __ksym __weak; + +extern struct bpf_list_node *bpf_list_front (struct bpf_list_head *head) __ksym__weak ; +extern struct bpf_list_node *bpf_list_back (struct bpf_list_head *head) __ksym__weak ; + +struct bpf_sk_buff_ptr ; +struct sk_buff ; +struct Qdisc ; + +extern void bpf_qdisc_skb_drop(struct sk_buff *skb, + struct bpf_sk_buff_ptr *to_free_list) __ksym __weak; +extern void bpf_qdisc_bstats_update(struct Qdisc *sch, const struct sk_buff *skb) __ksym __weak; +extern void bpf_kfree_skb(struct sk_buff *skb) __ksym __weak; +extern __u32 bpf_skb_get_hash(struct sk_buff *) __ksym __weak; +extern void bpf_qdisc_watchdog_schedule(struct Qdisc *sch, __u64 expire, + __u64 delta_ns) __ksym __weak; + +extern struct cgroup *bpf_cgroup_from_id (__u64 cgid) __ksym__weak ; +extern void bpf_cgroup_release(struct cgroup *cgrp) __ksym __weak; +extern void bpf_rcu_read_lock(void) __ksym __weak; +extern void bpf_rcu_read_unlock(void) __ksym __weak; +extern struct cgroup *bpf_cgroup_ancestor (struct cgroup *cgrp,int level) __ksym__weak ; + + +extern struct bpf_rb_node *bpf_rbtree_root (struct bpf_rb_root *root) __ksym__weak ; +extern struct bpf_rb_node *bpf_rbtree_left (struct bpf_rb_root *root, +struct bpf_rb_node *node) __ksym__weak ; +extern struct bpf_rb_node *bpf_rbtree_right (struct bpf_rb_root *root, +struct bpf_rb_node *node) __ksym__weak ; + +extern void bpf_task_release(struct task_struct *p) __ksym __weak; +extern void __bpf_trap(void) __ksym __weak; +#endif
tools/testing/selftests/bpf/progs/bpf_iter_tasks.c +1 -0
@@ -3,6 +3,7 @@#include <vmlinux.h> #include <bpf/bpf_helpers.h> #include <bpf/bpf_tracing.h> +#include "bpf_kfuncs.h" char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/bpf_qdisc_fail__incompl_ops.c +1 -0
@@ -3,6 +3,7 @@#include <vmlinux.h> #include "bpf_experimental.h" #include "bpf_qdisc_common.h" +#include "bpf_kfuncs.h" char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c +1 -0
@@ -3,6 +3,7 @@#include <vmlinux.h> #include "bpf_experimental.h" #include "bpf_qdisc_common.h" +#include "bpf_kfuncs.h" char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/bpf_qdisc_fq.c +1 -0
@@ -37,6 +37,7 @@#include <bpf/bpf_helpers.h> #include "bpf_experimental.h" #include "bpf_qdisc_common.h" +#include "bpf_kfuncs.h" char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/cgroup_read_xattr.c +1 -0
@@ -7,6 +7,7 @@#include <bpf/bpf_core_read.h> #include "bpf_experimental.h" #include "bpf_misc.h" +#include "bpf_kfuncs.h" char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/dmabuf_iter.c +1 -0
@@ -3,6 +3,7 @@#include <vmlinux.h> #include <bpf/bpf_core_read.h> #include <bpf/bpf_helpers.h> +#include "bpf_experimental.h" /* From uapi/linux/dma-buf.h */ #define DMA_BUF_NAME_LEN 32
tools/testing/selftests/bpf/progs/dynptr_success.c +1 -0
@@ -8,6 +8,7 @@#include <bpf/bpf_tracing.h> #include "bpf_misc.h" #include "errno.h" +#include "bpf_kfuncs.h" char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/irq.c +1 -5
@@ -4,13 +4,9 @@#include <bpf/bpf_helpers.h> #include "bpf_misc.h" #include "bpf_experimental.h" +#include "bpf_kfuncs.h" unsigned long global_flags; - -extern void bpf_local_irq_save(unsigned long *) __weak __ksym; -extern void bpf_local_irq_restore(unsigned long *) __weak __ksym; -extern int bpf_copy_from_user_str(void *dst, u32 dst__sz, const void *unsafe_ptr__ign, u64 flags) __weak __ksym; - struct bpf_res_spin_lock lockA __hidden SEC(".data.A"); struct bpf_res_spin_lock lockB __hidden SEC(".data.B");
tools/testing/selftests/bpf/progs/linked_list_peek.c +1 -0
@@ -5,6 +5,7 @@#include <bpf/bpf_helpers.h> #include "bpf_misc.h" #include "bpf_experimental.h" +#include "bpf_kfuncs.h" struct node_data {struct bpf_list_node l;
tools/testing/selftests/bpf/progs/rbtree_search.c +1 -0
@@ -5,6 +5,7 @@#include <bpf/bpf_helpers.h> #include "bpf_misc.h" #include "bpf_experimental.h" +#include "bpf_kfuncs.h" struct node_data {struct bpf_refcount ref;
tools/testing/selftests/bpf/progs/rcu_read_lock.c +1 -0
@@ -6,6 +6,7 @@#include <bpf/bpf_tracing.h> #include "bpf_tracing_net.h" #include "bpf_misc.h" +#include "bpf_kfuncs.h" char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/read_cgroupfs_xattr.c +1 -0
@@ -6,6 +6,7 @@#include <bpf/bpf_helpers.h> #include <bpf/bpf_core_read.h> #include "bpf_experimental.h" +#include "bpf_kfuncs.h" char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/res_spin_lock.c +1 -0
@@ -4,6 +4,7 @@#include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> #include "bpf_misc.h" +#include "bpf_kfuncs.h" #define EDEADLK 35#define ETIMEDOUT 110
tools/testing/selftests/bpf/progs/res_spin_lock_fail.c +1 -0
@@ -6,6 +6,7 @@#include <bpf/bpf_core_read.h> #include "bpf_misc.h" #include "bpf_experimental.h" +#include "bpf_kfuncs.h" struct arr_elem {struct bpf_res_spin_lock lock;
tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__tail_call.c +1 -0
@@ -4,6 +4,7 @@#include <bpf/bpf_tracing.h> #include "../test_kmods/bpf_testmod.h" #include "bpf_misc.h" +#include "bpf_kfuncs.h" char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/test_spin_lock_fail.c +1 -0
@@ -3,6 +3,7 @@#include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> #include "bpf_experimental.h" +#include "bpf_kfuncs.h" struct foo {struct bpf_spin_lock lock;
tools/testing/selftests/bpf/progs/verifier_bpf_trap.c +1 -0
@@ -3,6 +3,7 @@#include <vmlinux.h> #include <bpf/bpf_helpers.h> #include "bpf_misc.h" +#include "bpf_kfuncs.h" #if __clang_major__ >=21 &&0 SEC ("socket" )
Signature · 1 line
2.43.0