Skip to content

Commit 8c2e043

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: check if bpf_fastcall is recognized for kfuncs
Use kfunc_bpf_cast_to_kern_ctx() and kfunc_bpf_rdonly_cast() to verify that bpf_fastcall pattern is recognized for kfunc calls. Acked-by: Yonghong Song <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f406026 commit 8c2e043

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_core_read.h>
56
#include "../../../include/linux/filter.h"
67
#include "bpf_misc.h"
8+
#include <stdbool.h>
9+
#include "bpf_kfuncs.h"
710

811
SEC("raw_tp")
912
__arch_x86_64
@@ -842,4 +845,56 @@ __naked int bpf_fastcall_max_stack_fail(void)
842845
);
843846
}
844847

848+
SEC("cgroup/getsockname_unix")
849+
__xlated("0: r2 = 1")
850+
/* bpf_cast_to_kern_ctx is replaced by a single assignment */
851+
__xlated("1: r0 = r1")
852+
__xlated("2: r0 = r2")
853+
__xlated("3: exit")
854+
__success
855+
__naked void kfunc_bpf_cast_to_kern_ctx(void)
856+
{
857+
asm volatile (
858+
"r2 = 1;"
859+
"*(u64 *)(r10 - 32) = r2;"
860+
"call %[bpf_cast_to_kern_ctx];"
861+
"r2 = *(u64 *)(r10 - 32);"
862+
"r0 = r2;"
863+
"exit;"
864+
:
865+
: __imm(bpf_cast_to_kern_ctx)
866+
: __clobber_all);
867+
}
868+
869+
SEC("raw_tp")
870+
__xlated("3: r3 = 1")
871+
/* bpf_rdonly_cast is replaced by a single assignment */
872+
__xlated("4: r0 = r1")
873+
__xlated("5: r0 = r3")
874+
void kfunc_bpf_rdonly_cast(void)
875+
{
876+
asm volatile (
877+
"r2 = %[btf_id];"
878+
"r3 = 1;"
879+
"*(u64 *)(r10 - 32) = r3;"
880+
"call %[bpf_rdonly_cast];"
881+
"r3 = *(u64 *)(r10 - 32);"
882+
"r0 = r3;"
883+
:
884+
: __imm(bpf_rdonly_cast),
885+
[btf_id]"r"(bpf_core_type_id_kernel(union bpf_attr))
886+
: __clobber_common);
887+
}
888+
889+
/* BTF FUNC records are not generated for kfuncs referenced
890+
* from inline assembly. These records are necessary for
891+
* libbpf to link the program. The function below is a hack
892+
* to ensure that BTF FUNC records are generated.
893+
*/
894+
void kfunc_root(void)
895+
{
896+
bpf_cast_to_kern_ctx(0);
897+
bpf_rdonly_cast(0, 0);
898+
}
899+
845900
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)