Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add uprobe session cookie test

Adding uprobe session test that verifies the cookie value
get properly propagated from entry to return program.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20241108134544.480660-8-jolsa@kernel.org

authored by

Jiri Olsa and committed by
Andrii Nakryiko
f6b45e35 4856ecb1

+79
+31
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
··· 9 9 #include "uprobe_multi_consumers.skel.h" 10 10 #include "uprobe_multi_pid_filter.skel.h" 11 11 #include "uprobe_multi_session.skel.h" 12 + #include "uprobe_multi_session_cookie.skel.h" 12 13 #include "bpf/libbpf_internal.h" 13 14 #include "testing_helpers.h" 14 15 #include "../sdt.h" ··· 1063 1062 uprobe_multi_session__destroy(skel); 1064 1063 } 1065 1064 1065 + static void test_session_cookie_skel_api(void) 1066 + { 1067 + struct uprobe_multi_session_cookie *skel = NULL; 1068 + int err; 1069 + 1070 + skel = uprobe_multi_session_cookie__open_and_load(); 1071 + if (!ASSERT_OK_PTR(skel, "uprobe_multi_session_cookie__open_and_load")) 1072 + goto cleanup; 1073 + 1074 + skel->bss->pid = getpid(); 1075 + 1076 + err = uprobe_multi_session_cookie__attach(skel); 1077 + if (!ASSERT_OK(err, "uprobe_multi_session_cookie__attach")) 1078 + goto cleanup; 1079 + 1080 + /* trigger all probes */ 1081 + uprobe_multi_func_1(); 1082 + uprobe_multi_func_2(); 1083 + uprobe_multi_func_3(); 1084 + 1085 + ASSERT_EQ(skel->bss->test_uprobe_1_result, 1, "test_uprobe_1_result"); 1086 + ASSERT_EQ(skel->bss->test_uprobe_2_result, 2, "test_uprobe_2_result"); 1087 + ASSERT_EQ(skel->bss->test_uprobe_3_result, 3, "test_uprobe_3_result"); 1088 + 1089 + cleanup: 1090 + uprobe_multi_session_cookie__destroy(skel); 1091 + } 1092 + 1066 1093 static void test_bench_attach_uprobe(void) 1067 1094 { 1068 1095 long attach_start_ns = 0, attach_end_ns = 0; ··· 1189 1160 test_pid_filter_process(true); 1190 1161 if (test__start_subtest("session")) 1191 1162 test_session_skel_api(); 1163 + if (test__start_subtest("session_cookie")) 1164 + test_session_cookie_skel_api(); 1192 1165 }
+48
tools/testing/selftests/bpf/progs/uprobe_multi_session_cookie.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include <linux/bpf.h> 3 + #include <bpf/bpf_helpers.h> 4 + #include <bpf/bpf_tracing.h> 5 + #include <stdbool.h> 6 + #include "bpf_kfuncs.h" 7 + 8 + char _license[] SEC("license") = "GPL"; 9 + 10 + int pid = 0; 11 + 12 + __u64 test_uprobe_1_result = 0; 13 + __u64 test_uprobe_2_result = 0; 14 + __u64 test_uprobe_3_result = 0; 15 + 16 + static int check_cookie(__u64 val, __u64 *result) 17 + { 18 + __u64 *cookie; 19 + 20 + if (bpf_get_current_pid_tgid() >> 32 != pid) 21 + return 1; 22 + 23 + cookie = bpf_session_cookie(); 24 + 25 + if (bpf_session_is_return()) 26 + *result = *cookie == val ? val : 0; 27 + else 28 + *cookie = val; 29 + return 0; 30 + } 31 + 32 + SEC("uprobe.session//proc/self/exe:uprobe_multi_func_1") 33 + int uprobe_1(struct pt_regs *ctx) 34 + { 35 + return check_cookie(1, &test_uprobe_1_result); 36 + } 37 + 38 + SEC("uprobe.session//proc/self/exe:uprobe_multi_func_2") 39 + int uprobe_2(struct pt_regs *ctx) 40 + { 41 + return check_cookie(2, &test_uprobe_2_result); 42 + } 43 + 44 + SEC("uprobe.session//proc/self/exe:uprobe_multi_func_3") 45 + int uprobe_3(struct pt_regs *ctx) 46 + { 47 + return check_cookie(3, &test_uprobe_3_result); 48 + }