Subject: [PATCH bpf-next 3/3] selftests/bpf: Add BPF_BRANCH_SNAPSHOT_F_COPY test

From: Leon Hwang <[email-masked]>
Date: Fri, 9 Jan 2026 23:34:20 +0800

Add test for BPF_BRANCH_SNAPSHOT_F_COPY flag by adding flag to the
callsite of bpf_get_branch_snapshot helper.

Signed-off-by: Leon Hwang <[email-masked]>
---
 .../bpf/prog_tests/get_branch_snapshot.c      | 26 ++++++++++++++++---
 .../selftests/bpf/progs/get_branch_snapshot.c |  3 ++-
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c b/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
index 0394a1156d99..6b8ab1655ab0 100644
--- a/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
+++ b/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
@@ -73,7 +73,7 @@ static void close_perf_events(void)
 	free(pfd_array);
 }
 
-void serial_test_get_branch_snapshot(void)
+static void test_branch_snapshot(int flags)
 {
 	struct get_branch_snapshot *skel = NULL;
 	int err;
@@ -89,8 +89,14 @@ void serial_test_get_branch_snapshot(void)
 		goto cleanup;
 	}
 
-	skel = get_branch_snapshot__open_and_load();
-	if (!ASSERT_OK_PTR(skel, "get_branch_snapshot__open_and_load"))
+	skel = get_branch_snapshot__open();
+	if (!ASSERT_OK_PTR(skel, "get_branch_snapshot__open"))
+		goto cleanup;
+
+	skel->rodata->flags = flags;
+
+	err = get_branch_snapshot__load(skel);
+	if (!ASSERT_OK(err, "get_branch_snapshot__load"))
 		goto cleanup;
 
 	err = kallsyms_find("bpf_testmod_loop_test", &skel->bss->address_low);
@@ -128,3 +134,17 @@ void serial_test_get_branch_snapshot(void)
 	get_branch_snapshot__destroy(skel);
 	close_perf_events();
 }
+
+void serial_test_get_branch_snapshot(void)
+{
+	test_branch_snapshot(0);
+}
+
+enum {
+	BPF_BRANCH_SNAPSHOT_F_COPY	= 1,	/* Copy branch snapshot from bpf_branch_snapshot. */
+};
+
+void serial_test_copy_branch_snapshot(void)
+{
+	test_branch_snapshot(BPF_BRANCH_SNAPSHOT_F_COPY);
+}
diff --git a/tools/testing/selftests/bpf/progs/get_branch_snapshot.c b/tools/testing/selftests/bpf/progs/get_branch_snapshot.c
index 511ac634eef0..47a1984bdf46 100644
--- a/tools/testing/selftests/bpf/progs/get_branch_snapshot.c
+++ b/tools/testing/selftests/bpf/progs/get_branch_snapshot.c
@@ -6,6 +6,7 @@
 
 char _license[] SEC("license") = "GPL";
 
+volatile const int flags = 0;
 __u64 test1_hits = 0;
 __u64 address_low = 0;
 __u64 address_high = 0;
@@ -25,7 +26,7 @@ int BPF_PROG(test1, int n, int ret)
 {
 	long i;
 
-	total_entries = bpf_get_branch_snapshot(entries, sizeof(entries), 0);
+	total_entries = bpf_get_branch_snapshot(entries, sizeof(entries), flags);
 	total_entries /= sizeof(struct perf_branch_entry);
 
 	for (i = 0; i < ENTRY_CNT; i++) {
-- 
2.52.0