[PATCH bpf-next v3 3/4] selftests/bpf: Fix unaligned access failure in fexit_bpf2bpf

From: Tiezhu Yang

Date: Sun Nov 15 2026 - 22:59:27 EST


When executing "./test_progs -t fexit_bpf2bpf", there are 5 subtest
failures on platforms where CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
is not set, as the BPF verifier enforces strict alignment checks:

; tcp->check++; @ test_pkt_access.c:98
226: (69) r1 = *(u16 *)(r2 +16)
misaligned packet access off 2+(0x0; 0x3f)+16 size 2
...
libbpf: prog 'test_pkt_access': failed to load: -EACCES
libbpf: failed to load object './test_pkt_access.bpf.o'

Rather than skipping the tests or setting BPF_F_ANY_ALIGNMENT to
bypass the alignment check, fix this at the source by applying a
bitmask 'off &= ~1' right after the initial range check.

This explicitly communicates the 2-byte alignment state to pass
the verifier and run the full fexit_bpf2bpf test matrix.

Before:
$ sudo ./test_progs -t fexit_bpf2bpf | tail -1
Summary: 0/10 PASSED, 1 SKIPPED, 1/5 FAILED

After:
$ sudo ./test_progs -t fexit_bpf2bpf | tail -1
Summary: 1/15 PASSED, 1 SKIPPED, 0/0 FAILED

Fixes: 6dc03dc71387 ("selftests/bpf: Add test for freplace program with write access")
Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
---
tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c | 1 +
tools/testing/selftests/bpf/progs/test_pkt_access.c | 1 +
2 files changed, 2 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c b/tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c
index f4bbf87b82dd..ed06345825e2 100644
--- a/tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c
+++ b/tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c
@@ -173,6 +173,7 @@ int new_test_pkt_write_access_subprog(struct __sk_buff *skb, __u32 off)
if (off > sizeof(struct ethhdr) + sizeof(struct ipv6hdr))
return -1;

+ off &= ~1;
tcp = data + off;
if (tcp + 1 > data_end)
return -1;
diff --git a/tools/testing/selftests/bpf/progs/test_pkt_access.c b/tools/testing/selftests/bpf/progs/test_pkt_access.c
index bce7173152c6..21f28a762d60 100644
--- a/tools/testing/selftests/bpf/progs/test_pkt_access.c
+++ b/tools/testing/selftests/bpf/progs/test_pkt_access.c
@@ -91,6 +91,7 @@ int test_pkt_write_access_subprog(struct __sk_buff *skb, __u32 off)
if (off > sizeof(struct ethhdr) + sizeof(struct ipv6hdr))
return -1;

+ off &= ~1;
tcp = data + off;
if (tcp + 1 > data_end)
return -1;
--
2.42.0