Skip to contentSkip to conversation

bpf: Emit struct bpf_xdp_sock type in vmlinux BTF

PATCH Applied Amery Hung 3 participants 5 messages Sep 25, 2025

Subsystems (6)
lore
Share & tools
lore

Amery Hung

author
#0 lore raw

PATCH 1/2 bpf: Emit struct bpf_xdp_sock type in vmlinux BTF

Similar to other BPF UAPI struct, force emit BTF of struct bpf_xdp_sock so that it is defined in vmlinux.h.

In a later patch, a selftest will use vmlinux.h to get the definition of struct bpf_xdp_sock instead of bpf.h.

---
 net/core/filter.c | 2 ++
 1 file changed, 2 insertions(+)
net/core/filter.c +2 -0
index b20d59bb19b8..2af0a5f1d748 100644
@@ -7439,6 +7439,8 @@ u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
 				      offsetof(struct xdp_sock, FIELD)); \
 	} while (0)
 
+	BTF_TYPE_EMIT(struct bpf_xdp_sock);
+
 	switch (si->off) {
 	case offsetof(struct bpf_xdp_sock, queue_id):
 		BPF_XDP_SOCK_GET(queue_id);
Signature · 1 line
2.47.3
author
#1 lore raw

PATCH 2/2 selftests/bpf: Test changing packet data from global functions with a kfunc

The verifier should invalidate all packet pointers after a packet data changing kfunc is called. So, similar to commit 3f23ee5590d9 ("selftests/bpf: test for changing packet data from global functions"), test changing packet data from global functions to make sure packet pointers are indeed invalidated.

---
 .../selftests/bpf/progs/verifier_sock.c       | 30 ++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/verifier_sock.c +29 -1
index bf88c644eb30..0bed5715c9e1 100644
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Converted from tools/testing/selftests/bpf/verifier/sock.c */
 
-#include <linux/bpf.h>
+#include "vmlinux.h"
 #include <bpf/bpf_helpers.h>
 #include "bpf_misc.h"
 
@@ -1068,6 +1068,34 @@ int invalidate_pkt_pointers_from_global_func(struct __sk_buff *sk)
 	return TCX_PASS;
 }
 
+__noinline
+long xdp_pull_data2(struct xdp_md *x, __u32 len)
+{
+	return bpf_xdp_pull_data(x, len);
+}
+
+__noinline
+long xdp_pull_data1(struct xdp_md *x, __u32 len)
+{
+	return xdp_pull_data2(x, len);
+}
+
+/* global function calls bpf_xdp_pull_data(), which invalidates packet
+ * pointers established before global function call.
+ */
+SEC("xdp")
+__failure __msg("invalid mem access")
+int invalidate_xdp_pkt_pointers_from_global_func(struct xdp_md *x)
+{
+	int *p = (void *)(long)x->data;
+
+	if ((void *)(p + 1) > (void *)(long)x->data_end)
+		return TCX_DROP;
+	xdp_pull_data1(x, 0);
+	*p = 42; /* this is unsafe */
+	return TCX_PASS;
+}
+
 __noinline
 int tail_call(struct __sk_buff *sk)
 {
Signature · 1 line
2.47.3
reviewer Applied
#2 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: Test changing packet data from global functions with a kfunc

Quoted 5 lines · Amery Hung

The verifier should invalidate all packet pointers after a packet data changing kfunc is called. So, similar to commit 3f23ee5590d9 ("selftests/bpf: test for changing packet data from global functions"), test changing packet data from global functions to make sure packet pointers are indeed invalidated.

Applied. Thanks.

tools/testing/selftests/bpf/progs/verifier_sock.c int invalidate_pkt_pointers_from_global_func(struct __sk_buff *sk) · line 1074

Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_sock.c +4 -0
+__noinline
+long xdp_pull_data2(struct xdp_md *x, __u32 len)
+{
+	return bpf_xdp_pull_data(x, len);

This tested the mark_subprog_changes_pkt_data() in visit_insn().

afaik, it does not test the clear_all_pkt_pointers() in check_"k"func_call(). Unlike the existing "changes_data" helpers, it is the first kfunc doing it. Although we know that it should work after fixing the xdp_native.bpf.c :), it is still good to have a regression test for it. Probably another xdp prog in verifier_sock.c that does bpf_xdp_pull_data() in the main prog. Please follow up.

Quoted 12 lines
tools/testing/selftests/bpf/progs/verifier_sock.c +12 -0
+}
+
+__noinline
+long xdp_pull_data1(struct xdp_md *x, __u32 len)
+{
+	return xdp_pull_data2(x, len);
+}
+
+/* global function calls bpf_xdp_pull_data(), which invalidates packet
+ * pointers established before global function call.
+ */
+SEC("xdp")

tools/testing/selftests/bpf/progs/verifier_sock.c int invalidate_pkt_pointers_from_global_func(struct __sk_buff *sk) · line 1096

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_sock.c +10 -0
+__failure __msg("invalid mem access")
+int invalidate_xdp_pkt_pointers_from_global_func(struct xdp_md *x)
+{
+	int *p = (void *)(long)x->data;
+
+	if ((void *)(p + 1) > (void *)(long)x->data_end)
+		return TCX_DROP;
+	xdp_pull_data1(x, 0);
+	*p = 42; /* this is unsafe */
+	return TCX_PASS;

I fixed this to XDP_PASS as we discussed offline.

Quoted 5 lines
tools/testing/selftests/bpf/progs/verifier_sock.c +2 -0
+}
+
  __noinline
  int tail_call(struct __sk_buff *sk)
  {
author
#3 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: Test changing packet data from global functions with a kfunc

Quoted 17 lines · Martin KaFai Lau
Quoted 5 lines · Amery Hung

The verifier should invalidate all packet pointers after a packet data changing kfunc is called. So, similar to commit 3f23ee5590d9 ("selftests/bpf: test for changing packet data from global functions"), test changing packet data from global functions to make sure packet pointers are indeed invalidated.

Applied. Thanks.

tools/testing/selftests/bpf/progs/verifier_sock.c int invalidate_pkt_pointers_from_global_func(struct __sk_buff *sk) · line 1074

Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_sock.c +4 -0
+__noinline
+long xdp_pull_data2(struct xdp_md *x, __u32 len)
+{
+     return bpf_xdp_pull_data(x, len);

This tested the mark_subprog_changes_pkt_data() in visit_insn().

Quoted 6 lines

afaik, it does not test the clear_all_pkt_pointers() in check_"k"func_call(). Unlike the existing "changes_data" helpers, it is the first kfunc doing it. Although we know that it should work after fixing the xdp_native.bpf.c :), it is still good to have a regression test for it. Probably another xdp prog in verifier_sock.c that does bpf_xdp_pull_data() in the main prog. Please follow up.

I will add another test in verifier_sock.c.

Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/bpf/progs/verifier_sock.c +16 -0
+}
+
+__noinline
+long xdp_pull_data1(struct xdp_md *x, __u32 len)
+{
+     return xdp_pull_data2(x, len);
+}
+
+/* global function calls bpf_xdp_pull_data(), which invalidates packet
+ * pointers established before global function call.
+ */
+SEC("xdp")
+__failure __msg("invalid mem access")
+int invalidate_xdp_pkt_pointers_from_global_func(struct xdp_md *x)
+{
+     int *p = (void *)(long)x->data;
Quoted 9 lines

tools/testing/selftests/bpf/progs/verifier_sock.c int invalidate_pkt_pointers_from_global_func(struct __sk_buff *sk) · line 1096

Quoted 6 lines
tools/testing/selftests/bpf/progs/verifier_sock.c +6 -0
+
+     if ((void *)(p + 1) > (void *)(long)x->data_end)
+             return TCX_DROP;
+     xdp_pull_data1(x, 0);
+     *p = 42; /* this is unsafe */
+     return TCX_PASS;

I fixed this to XDP_PASS as we discussed offline.

Thank you!

Quoted 6 lines
Quoted 5 lines
tools/testing/selftests/bpf/progs/verifier_sock.c +2 -0
+}
+
  __noinline
  int tail_call(struct __sk_buff *sk)
  {
#4 lore raw
In reply to Amery Hung
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Quoted 9 lines · On Thu, 25 Sep 2025 10:00:12 -0700 you wrote:

Similar to other BPF UAPI struct, force emit BTF of struct bpf_xdp_sock so that it is defined in vmlinux.h.

In a later patch, a selftest will use vmlinux.h to get the definition of struct bpf_xdp_sock instead of bpf.h.

[...]

Here is the summary with links:
- [bpf-next,1/2] bpf: Emit struct bpf_xdp_sock type in vmlinux BTF

    https://git.kernel.org/bpf/bpf-next/c/bc8712f2b525
  - [bpf-next,2/2] selftests/bpf: Test changing packet data from global functions with a kfunc
    https://git.kernel.org/bpf/bpf-next/c/9b5c111c3cd6

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html