selftests/bpf: improve test coverage for kfunc call
Subsystems (4)
PATCH v2 1/1 selftests/bpf: improve test coverage for kfunc call
On powerpc, immediate load instructions are sign extended. In case of unsigned types, arguments should be explicitly zero-extended by the caller. For kfunc call, this needs to be handled in the JIT code. In bpf_kfunc_call_test4(), that tests for sign-extension of signed argument types in kfunc calls, add some additional failure checks. And add bpf_kfunc_call_test5() to test zero-extension of unsigned argument types in kfunc calls.
---
Changes in v2:
- Added asm version of the selftest for consistent testing across different BPF ISA versions.
- Added comments clearly stating the intent of the test cases.
- Updated sign-extension selftest to have additional failure checks.
.../selftests/bpf/prog_tests/kfunc_call.c | 2 + .../selftests/bpf/progs/kfunc_call_test.c | 98 +++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 54 +++++++++- .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 4 files changed, 154 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/kfunc_call.c +2 -0
@@ -74,6 +74,8 @@ static struct kfunc_test_params kfunc_tests[] = {TC_TEST (kfunc_call_test1,12 ),TC_TEST (kfunc_call_test2,3 ),TC_TEST (kfunc_call_test4,-1234 ), +TC_TEST (kfunc_call_test5,0 ), +TC_TEST (kfunc_call_test5_asm,0 ),TC_TEST (kfunc_call_test_ref_btf_id,0 ),TC_TEST (kfunc_call_test_get_mem,42 ), SYSCALL_TEST(kfunc_syscall_test, 0),
tools/testing/selftests/bpf/progs/kfunc_call_test.c +98 -0
106 lines not on this page · raw message
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +53 -1
71 lines not on this page · raw message
tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +1 -0
@@ -110,6 +110,7 @@ __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b, int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym;struct sock *bpf_kfunc_call_test3 (struct sock *sk) __ksym; long bpf_kfunc_call_test4(signed char a, short b, int c, long d) __ksym; +int bpf_kfunc_call_test5(__u8 a, __u16 b, __u32 c) __ksym; void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym; void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym;
Signature · 1 line
2.53.0
Quoted 9 lines · Hari Bathini
On powerpc, immediate load instructions are sign extended. In case of unsigned types, arguments should be explicitly zero-extended by the caller. For kfunc call, this needs to be handled in the JIT code. In
bpf_kfunc_call_test4(), that tests for sign-extension of signed argument types in kfunc calls, add some additional failure checks. And addbpf_kfunc_call_test5()to test zero-extension of unsigned argument types in kfunc calls.Signed-off-by Hari Bathini <[email protected]>
LGTM with a nit below.
Quoted 53 lines
---
Changes in v2:
- Added asm version of the selftest for consistent testing across different BPF ISA versions.
- Added comments clearly stating the intent of the test cases.
- Updated sign-extension selftest to have additional failure checks..../selftests/bpf/prog_tests/kfunc_call.c | 2 + .../selftests/bpf/progs/kfunc_call_test.c | 98 +++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 54 +++++++++- .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 4 files changed, 154 insertions(+), 1 deletion(-)tools/testing/selftests/bpf/prog_tests/kfunc_call.c +2
-0@@ -74,6 +74,8 @@ static struct kfunc_test_params kfunc_tests[] = {TC_TEST (kfunc_call_test1,12 ),TC_TEST (kfunc_call_test2,3 ),TC_TEST (kfunc_call_test4,-1234 ), +TC_TEST (kfunc_call_test5,0 ), +TC_TEST (kfunc_call_test5_asm,0 ),TC_TEST (kfunc_call_test_ref_btf_id,0 ),TC_TEST (kfunc_call_test_get_mem,42 ), SYSCALL_TEST(kfunc_syscall_test, 0),tools/testing/selftests/bpf/progs/kfunc_call_test.c +15
-0@@ -2,8 +2,106 @@/* Copyright (c) 2021 Facebook */ #include <vmlinux.h> #include <bpf/bpf_helpers.h> +#include "bpf_misc.h" #include "../test_kmods/bpf_testmod_kfunc.h" +SEC("tc") +int kfunc_call_test5 (struct __sk_buff *skb) +{ +struct bpf_sock *sk = skb->sk; +int ret; +u32 val32; +u16 val16; +u8 val8; + +if (!sk) +return -1 ; + + sk =bpf_sk_fullsock (sk); +if (!sk)
tools/testing/selftests/bpf/progs/kfunc_call_test.c line 31
Quoted 10 lines
tools/testing/selftests/bpf/progs/kfunc_call_test.c +10
-0+return -1 ; + +/* +* Test with constant values to verify zero-extension. +* ISA-dependent BPF asm: +* With ALU32: w1 = 0xFF; w2 = 0xFFFF; w3 = 0xFFFFffff +* Without ALU32: r1 = 0xFF; r2 = 0xFFFF; r3 = 0xFFFFffff +* Both zero-extend to 64-bit before the kfunc call. +*/ + ret =bpf_kfunc_call_test5 (0xFF ,0xFFFF ,0xFFFFffffULL );
Can we just use 0xFFFFffff instead of 0xFFFFffffULL?
Quoted 31 lines
tools/testing/selftests/bpf/progs/kfunc_call_test.c +31
-0+if (ret) +return ret; + + val32 =bpf_get_prandom_u32 (); + val16 = val32 &0xFFFF ; + val8 = val32 &0xFF ; + ret =bpf_kfunc_call_test5 (val8, val16, val32); +if (ret) +return ret; + +/* +* Test multiplication with different operand sizes: +* +* val8 * 0xFF: +* - Both operands promote to int (32-bit signed) +* - Result: 32-bit multiplication, truncated to u8, then zero-extended +* +* val16 * 0xFFFF: +* - Both operands promote to int (32-bit signed) +* - Result: 32-bit multiplication, truncated to u16, then zero-extended +* +* val32 * 0xFFFFffffULL: +* - val32 (u32) promotes to unsigned long long (due to ULL suffix) +* - Result: 64-bit unsigned multiplication, truncated to u32, then zero-extended +*/ + ret =bpf_kfunc_call_test5 (val8 *0xFF , val16 *0xFFFF , val32 *0xFFFFffffULL ); +if (ret) +return ret; + +return 0 ; +}
[...]
Quoted 11 lines · Yonghong Song
Quoted 8 lines · Hari Bathini
On powerpc, immediate load instructions are sign extended. In case of unsigned types, arguments should be explicitly zero-extended by the caller. For kfunc call, this needs to be handled in the JIT code. In
bpf_kfunc_call_test4(), that tests for sign-extension of signed argument types in kfunc calls, add some additional failure checks. And addbpf_kfunc_call_test5()to test zero-extension of unsigned argument types in kfunc calls.
Quoted 5 lines
Quoted 1 line
Signed-off-by Hari Bathini <[email protected]>LGTM with a nit below.
Acked-by Yonghong Song <[email protected]>
Thanks for the review, Yonghong.
Quoted 57 lines
Quoted 57 lines
---
Changes in v2:
- Added asm version of the selftest for consistent testing across different BPF ISA versions.
- Added comments clearly stating the intent of the test cases.
- Updated sign-extension selftest to have additional failure checks..../selftests/bpf/prog_tests/kfunc_call.c | 2 + .../selftests/bpf/progs/kfunc_call_test.c | 98 +++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 54 +++++++++- .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 +
4 files changed, 154 insertions(+), 1 deletion(-)tools/testing/selftests/bpf/prog_tests/kfunc_call.c +2
-0@@ -74,6 +74,8 @@ static struct kfunc_test_params kfunc_tests[] = { TC_TEST(kfunc_call_test1, 12), TC_TEST(kfunc_call_test2, 3), TC_TEST(kfunc_call_test4, -1234), + TC_TEST(kfunc_call_test5, 0), + TC_TEST(kfunc_call_test5_asm, 0), TC_TEST(kfunc_call_test_ref_btf_id, 0), TC_TEST(kfunc_call_test_get_mem, 42), SYSCALL_TEST(kfunc_syscall_test, 0),tools/testing/selftests/bpf/progs/kfunc_call_test.c +19
-0@@ -2,8 +2,106 @@ /* Copyright (c) 2021 Facebook */ #include <vmlinux.h> #include <bpf/bpf_helpers.h> +#include "bpf_misc.h" #include "../test_kmods/bpf_testmod_kfunc.h" +SEC("tc") +int kfunc_call_test5 (struct __sk_buff *skb) +{ + struct bpf_sock *sk = skb->sk; + int ret; + u32 val32; + u16 val16; + u8 val8; + + if (!sk) + return -1; + + sk = bpf_sk_fullsock(sk); + if (!sk) + return -1; + + /* +* Test with constant values to verify zero-extension.
Quoted 8 lines
tools/testing/selftests/bpf/progs/kfunc_call_test.c line 31
Quoted 6 lines
tools/testing/selftests/bpf/progs/kfunc_call_test.c +6
-0+* ISA-dependent BPF asm: +* With ALU32: w1 = 0xFF; w2 = 0xFFFF; w3 = 0xFFFFffff +* Without ALU32: r1 = 0xFF; r2 = 0xFFFF; r3 = 0xFFFFffff +* Both zero-extend to 64-bit before the kfunc call. +*/ + ret = bpf_kfunc_call_test5(0xFF, 0xFFFF, 0xFFFFffffULL);Can we just use
0xFFFFffffinstead of0xFFFFffffULL?
Alexei, can you confirm if I need to respin with this change?
- Hari
Quoted 91 lines · Hari Bathini
Quoted 11 lines · Yonghong Song
Quoted 8 lines · Hari Bathini
On powerpc, immediate load instructions are sign extended. In case of unsigned types, arguments should be explicitly zero-extended by the caller. For kfunc call, this needs to be handled in the JIT code. In
bpf_kfunc_call_test4(), that tests for sign-extension of signed argument types in kfunc calls, add some additional failure checks. And addbpf_kfunc_call_test5()to test zero-extension of unsigned argument types in kfunc calls.Quoted 5 lines
Quoted 1 line
Signed-off-by Hari Bathini <[email protected]>LGTM with a nit below.
Acked-by Yonghong Song <[email protected]>Thanks for the review, Yonghong.
Quoted 59 lines
Quoted 59 lines
---
Changes in v2:
- Added asm version of the selftest for consistent testing across different BPF ISA versions.
- Added comments clearly stating the intent of the test cases.
- Updated sign-extension selftest to have additional failure checks..../selftests/bpf/prog_tests/kfunc_call.c | 2 + .../selftests/bpf/progs/kfunc_call_test.c | 98 +++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 54 +++++++++- .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 4 files changed, 154 insertions(+), 1 deletion(-)(quoted diff) renamed from tools/testing/selftests/bpf/prog_tests/kfunc_call.c +0
-0
tools/testing/selftests/bpf/prog_tests/kfunc_call.cindexf79c8e53cb3e..62f3fb79f5d1100644tools/testing/selftests/bpf/prog_tests/kfunc_call.c +2
-0@@ -74,6 +74,8 @@ static struct kfunc_test_params kfunc_tests[] = {TC_TEST (kfunc_call_test1,12 ),TC_TEST (kfunc_call_test2,3 ),TC_TEST (kfunc_call_test4,-1234 ), +TC_TEST (kfunc_call_test5,0 ), +TC_TEST (kfunc_call_test5_asm,0 ),TC_TEST (kfunc_call_test_ref_btf_id,0 ),TC_TEST (kfunc_call_test_get_mem,42 ), SYSCALL_TEST(kfunc_syscall_test, 0),(quoted diff) renamed from tools/testing/selftests/bpf/progs/kfunc_call_test.c +0
-0
tools/testing/selftests/bpf/progs/kfunc_call_test.cindex8b86113a0126..5edc51564f71100644tools/testing/selftests/bpf/progs/kfunc_call_test.c +19
-0@@ -2,8 +2,106 @@/* Copyright (c) 2021 Facebook */ #include <vmlinux.h> #include <bpf/bpf_helpers.h> +#include "bpf_misc.h" #include "../test_kmods/bpf_testmod_kfunc.h" +SEC("tc") +int kfunc_call_test5 (struct __sk_buff *skb) +{ +struct bpf_sock *sk = skb->sk; +int ret; +u32 val32; +u16 val16; +u8 val8; + +if (!sk) +return -1 ; + + sk =bpf_sk_fullsock (sk); +if (!sk) +return -1 ; + +/* +* Test with constant values to verify zero-extension. Quoted 8 lines
tools/testing/selftests/bpf/progs/kfunc_call_test.c line 31
Quoted 6 lines
tools/testing/selftests/bpf/progs/kfunc_call_test.c +6
-0+* ISA-dependent BPF asm: +* With ALU32: w1 = 0xFF; w2 = 0xFFFF; w3 = 0xFFFFffff +* Without ALU32: r1 = 0xFF; r2 = 0xFFFF; r3 = 0xFFFFffff +* Both zero-extend to 64-bit before the kfunc call. +*/ + ret =bpf_kfunc_call_test5 (0xFF ,0xFFFF ,0xFFFFffffULL );Can we just use
0xFFFFffffinstead of0xFFFFffffULL?
Quoted 1 line
Alexei, can you confirm if I need to respin with this change?
I prefer the explicit type when the constant is on the boundary.
I was about to apply it, but it fails on s390. See CI:
Error: #160 kfunc_call
Error: #160/13 kfunc_call/kfunc_call_test5_asm
Error: #160/13 kfunc_call/kfunc_call_test5_asm verify_success:PASS:skel 0 nsec verify_success:PASS:bpf_object__find_program_by_name 0 nsec verify_success:PASS:kfunc_call_test5_asm 0 nsec verify_success:FAIL:retval unexpected retval: actual 6 != expected 0 Test Results:
bpftool: PASStest_progs: FAIL (returned 1)
It doesn't look to be endianness related (which is often the case for breaking s390).
In this case it could be an s390 JIT issue?
Ilya ?
pw-bot: cr
On 3/13/26 15:20, Alexei Starovoitov wrote:
[...]
Quoted 13 lines
I was about to apply it, but it fails on s390. See CI:
Error: #160
kfunc_call
Error: #160/13 kfunc_call/kfunc_call_test5_asm
Error: #160/13 kfunc_call/kfunc_call_test5_asm verify_success:PASS:skel 0 nsec verify_success:PASS:bpf_object__find_program_by_name 0 nsec verify_success:PASS:kfunc_call_test5_asm 0 nsec verify_success:FAIL:retval unexpected retval: actual 6 != expected 0 Test Results:
bpftool: PASStest_progs: FAIL (returned 1)
Quoted 7 lines
It doesn't look to be endianness related (which is often the case for breaking s390).
In this case it could be an s390 JIT issue?Ilya ?
pw-bot: cr
I implemented sign-extension required by the s390x ABI, but forgot about zero-extension.
I will send the fix soon, after ./test_progs completes.
Quoted 51 lines · Hari Bathini
On powerpc, immediate load instructions are sign extended. In case of unsigned types, arguments should be explicitly zero-extended by the caller. For kfunc call, this needs to be handled in the JIT code. In
bpf_kfunc_call_test4(), that tests for sign-extension of signed argument types in kfunc calls, add some additional failure checks. And addbpf_kfunc_call_test5()to test zero-extension of unsigned argument types in kfunc calls.Signed-off-by Hari Bathini <[email protected]>---
Changes in v2:
- Added asm version of the selftest for consistent testing across different BPF ISA versions.
- Added comments clearly stating the intent of the test cases.
- Updated sign-extension selftest to have additional failure checks..../selftests/bpf/prog_tests/kfunc_call.c | 2 + .../selftests/bpf/progs/kfunc_call_test.c | 98 +++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 54 +++++++++- .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 4 files changed, 154 insertions(+), 1 deletion(-)tools/testing/selftests/bpf/prog_tests/kfunc_call.c +2
-0@@ -74,6 +74,8 @@ static struct kfunc_test_params kfunc_tests[] = {TC_TEST (kfunc_call_test1,12 ),TC_TEST (kfunc_call_test2,3 ),TC_TEST (kfunc_call_test4,-1234 ), +TC_TEST (kfunc_call_test5,0 ), +TC_TEST (kfunc_call_test5_asm,0 ),TC_TEST (kfunc_call_test_ref_btf_id,0 ),TC_TEST (kfunc_call_test_get_mem,42 ), SYSCALL_TEST(kfunc_syscall_test, 0),tools/testing/selftests/bpf/progs/kfunc_call_test.c +4
-0@@ -2,8 +2,106 @@/* Copyright (c) 2021 Facebook */ #include <vmlinux.h> #include <bpf/bpf_helpers.h> +#include "bpf_misc.h" #include "../test_kmods/bpf_testmod_kfunc.h" +SEC("tc") +int kfunc_call_test5 (struct __sk_buff *skb) +{
tools/testing/selftests/bpf/progs/kfunc_call_test.c line 20
Quoted 10 lines
tools/testing/selftests/bpf/progs/kfunc_call_test.c +10
-0+struct bpf_sock *sk = skb->sk; +int ret; +u32 val32; +u16 val16; +u8 val8; + +if (!sk) +return -1 ; + + sk =bpf_sk_fullsock (sk);
It looks like this socket stuff is not really needed in this test.
Quoted 178 lines
tools/testing/selftests/bpf/progs/kfunc_call_test.c +84
-0+if (!sk) +return -1 ; + +/* +* Test with constant values to verify zero-extension. +* ISA-dependent BPF asm: +* With ALU32: w1 = 0xFF; w2 = 0xFFFF; w3 = 0xFFFFffff +* Without ALU32: r1 = 0xFF; r2 = 0xFFFF; r3 = 0xFFFFffff +* Both zero-extend to 64-bit before the kfunc call. +*/ + ret =bpf_kfunc_call_test5 (0xFF ,0xFFFF ,0xFFFFffffULL ); +if (ret) +return ret; + + val32 =bpf_get_prandom_u32 (); + val16 = val32 &0xFFFF ; + val8 = val32 &0xFF ; + ret =bpf_kfunc_call_test5 (val8, val16, val32); +if (ret) +return ret; + +/* +* Test multiplication with different operand sizes: +* +* val8 * 0xFF: +* - Both operands promote to int (32-bit signed) +* - Result: 32-bit multiplication, truncated to u8, then zero-extended +* +* val16 * 0xFFFF: +* - Both operands promote to int (32-bit signed) +* - Result: 32-bit multiplication, truncated to u16, then zero-extended +* +* val32 * 0xFFFFffffULL: +* - val32 (u32) promotes to unsigned long long (due to ULL suffix) +* - Result: 64-bit unsigned multiplication, truncated to u32, then zero-extended +*/ + ret =bpf_kfunc_call_test5 (val8 *0xFF , val16 *0xFFFF , val32 *0xFFFFffffULL ); +if (ret) +return ret; + +return 0 ; +} + +/* +* Assembly version testing the multiplication edge case explicitly. +* This ensures consistent testing across different ISA versions. +*/ +SEC("tc") +__naked int kfunc_call_test5_asm (void ) +{ + asmvolatile ( +/* Get a random u32 value */ +"call %[bpf_get_prandom_u32];" +"r6 = r0;" /* Save val32 in r6 */ + +/* Prepare first argument: val8 * 0xFF */ +"r1 = r6;" +"r1 &= 0xFF;" /* val8 = val32 & 0xFF */ +"r7 = 0xFF;" +"r1 *= r7;" /* 64-bit mult: r1 = r1 * r7 */ + +/* Prepare second argument: val16 * 0xFFFF */ +"r2 = r6;" +"r2 &= 0xFFFF;" /* val16 = val32 & 0xFFFF */ +"r7 = 0xFFFF;" +"r2 *= r7;" /* 64-bit mult: r2 = r2 * r7 */ + +/* Prepare third argument: val32 * 0xFFFFffff */ +"r3 = r6;" /* val32 */ +"r7 = 0xFFFFffff;" +"r3 *= r7;" /* 64-bit mult: r3 = r3 * r7 */ + +/* Call kfunc with multiplication results */ +"call bpf_kfunc_call_test5;" + +/* Check return value */ +"if r0 != 0 goto exit_%=;" +"r0 = 0;" +"exit_%=: exit;" + : + : __imm(bpf_get_prandom_u32) + : __clobber_all); +} + SEC("tc")int kfunc_call_test4 (struct __sk_buff *skb) {tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +53
-1@@ -760,12 +760,63 @@ __bpf_kfunc struct sock *bpf_kfunc_call_test3(struct sock *sk) __bpf_kfunc long noinline bpf_kfunc_call_test4(signed char a, short b, int c, long d) { -/* Provoke the compiler to assume that the caller has sign-extended a, +/* +* Make val as volatile to avoid compiler optimizations. +* Verify that negative signed values remain negative after +* sign-extension (JIT must sign-extend, not zero-extend). +*/ +volatile long val; + +/* val will be positive, if JIT does zero-extension instead of sign-extension */ + val = a; +if (val >=0 ) +return 1 ; + + val = b; +if (val >=0 ) +return 2 ; + + val = c; +if (val >=0 ) +return 3 ; + +/* +* Provoke the compiler to assume that the caller has sign-extended a, * b and c on platforms where this is required (e.g. s390x). */ return (long )a + (long )b + (long )c + d; } +__bpf_kfunc int bpf_kfunc_call_test5(u8 a, u16 b, u32 c) +{ +/* +* Make val as volatile to avoid compiler optimizations on the below checks +* In C, assigning u8/u16/u32 to long performs zero-extension. +*/ +volatile long val = a; + +/* Check zero-extension */ +if (val != (unsigned long )a) +return 1 ; +/* Check no sign-extension */ +if (val <0 ) +return 2 ; + + val = b; +if (val != (unsigned long )b) +return 3 ; +if (val <0 ) +return 4 ; + + val = c; +if (val != (unsigned long )c) +return 5 ; +if (val <0 ) +return 6 ; + +return 0 ; +} +static struct prog_test_ref_kfunc prog_test_struct = { .a =42 , .b =108 ,@@ -1228,6 +1279,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test1) BTF_ID_FLAGS(func, bpf_kfunc_call_test2) BTF_ID_FLAGS(func, bpf_kfunc_call_test3) BTF_ID_FLAGS(func, bpf_kfunc_call_test4) +BTF_ID_FLAGS(func, bpf_kfunc_call_test5) BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1) BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1)BTF_ID_FLAGS (func, bpf_kfunc_call_test_mem_len_fail2)tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +1
-0@@ -110,6 +110,7 @@ __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b, int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym;struct sock *bpf_kfunc_call_test3 (struct sock *sk) __ksym; long bpf_kfunc_call_test4(signed char a, short b, int c, long d) __ksym; +int bpf_kfunc_call_test5(__u8 a, __u16 b, __u32 c) __ksym; void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym; void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym;Signature · 1 line
2.53.0