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

selftests/bpf: Add uprobe session recursive test

Adding uprobe session test that verifies the cookie value is stored
properly when single uprobe-ed function is executed recursively.

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-9-jolsa@kernel.org

authored by

Jiri Olsa and committed by
Andrii Nakryiko
8bcb9c62 f6b45e35

+101
+57
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
··· 10 10 #include "uprobe_multi_pid_filter.skel.h" 11 11 #include "uprobe_multi_session.skel.h" 12 12 #include "uprobe_multi_session_cookie.skel.h" 13 + #include "uprobe_multi_session_recursive.skel.h" 13 14 #include "bpf/libbpf_internal.h" 14 15 #include "testing_helpers.h" 15 16 #include "../sdt.h" ··· 35 34 noinline void usdt_trigger(void) 36 35 { 37 36 STAP_PROBE(test, pid_filter_usdt); 37 + } 38 + 39 + noinline void uprobe_session_recursive(int i) 40 + { 41 + if (i) 42 + uprobe_session_recursive(i - 1); 38 43 } 39 44 40 45 struct child { ··· 1098 1091 uprobe_multi_session_cookie__destroy(skel); 1099 1092 } 1100 1093 1094 + static void test_session_recursive_skel_api(void) 1095 + { 1096 + struct uprobe_multi_session_recursive *skel = NULL; 1097 + int i, err; 1098 + 1099 + skel = uprobe_multi_session_recursive__open_and_load(); 1100 + if (!ASSERT_OK_PTR(skel, "uprobe_multi_session_recursive__open_and_load")) 1101 + goto cleanup; 1102 + 1103 + skel->bss->pid = getpid(); 1104 + 1105 + err = uprobe_multi_session_recursive__attach(skel); 1106 + if (!ASSERT_OK(err, "uprobe_multi_session_recursive__attach")) 1107 + goto cleanup; 1108 + 1109 + for (i = 0; i < ARRAY_SIZE(skel->bss->test_uprobe_cookie_entry); i++) 1110 + skel->bss->test_uprobe_cookie_entry[i] = i + 1; 1111 + 1112 + uprobe_session_recursive(5); 1113 + 1114 + /* 1115 + * entry uprobe: 1116 + * uprobe_session_recursive(5) { *cookie = 1, return 0 1117 + * uprobe_session_recursive(4) { *cookie = 2, return 1 1118 + * uprobe_session_recursive(3) { *cookie = 3, return 0 1119 + * uprobe_session_recursive(2) { *cookie = 4, return 1 1120 + * uprobe_session_recursive(1) { *cookie = 5, return 0 1121 + * uprobe_session_recursive(0) { *cookie = 6, return 1 1122 + * return uprobe: 1123 + * } i = 0 not executed 1124 + * } i = 1 test_uprobe_cookie_return[0] = 5 1125 + * } i = 2 not executed 1126 + * } i = 3 test_uprobe_cookie_return[1] = 3 1127 + * } i = 4 not executed 1128 + * } i = 5 test_uprobe_cookie_return[2] = 1 1129 + */ 1130 + 1131 + ASSERT_EQ(skel->bss->idx_entry, 6, "idx_entry"); 1132 + ASSERT_EQ(skel->bss->idx_return, 3, "idx_return"); 1133 + 1134 + ASSERT_EQ(skel->bss->test_uprobe_cookie_return[0], 5, "test_uprobe_cookie_return[0]"); 1135 + ASSERT_EQ(skel->bss->test_uprobe_cookie_return[1], 3, "test_uprobe_cookie_return[1]"); 1136 + ASSERT_EQ(skel->bss->test_uprobe_cookie_return[2], 1, "test_uprobe_cookie_return[2]"); 1137 + 1138 + cleanup: 1139 + uprobe_multi_session_recursive__destroy(skel); 1140 + } 1141 + 1101 1142 static void test_bench_attach_uprobe(void) 1102 1143 { 1103 1144 long attach_start_ns = 0, attach_end_ns = 0; ··· 1246 1191 test_session_skel_api(); 1247 1192 if (test__start_subtest("session_cookie")) 1248 1193 test_session_cookie_skel_api(); 1194 + if (test__start_subtest("session_cookie_recursive")) 1195 + test_session_recursive_skel_api(); 1249 1196 }
+44
tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.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 + #include "bpf_misc.h" 8 + 9 + char _license[] SEC("license") = "GPL"; 10 + 11 + int pid = 0; 12 + 13 + int idx_entry = 0; 14 + int idx_return = 0; 15 + 16 + __u64 test_uprobe_cookie_entry[6]; 17 + __u64 test_uprobe_cookie_return[3]; 18 + 19 + static int check_cookie(void) 20 + { 21 + __u64 *cookie = bpf_session_cookie(); 22 + 23 + if (bpf_session_is_return()) { 24 + if (idx_return >= ARRAY_SIZE(test_uprobe_cookie_return)) 25 + return 1; 26 + test_uprobe_cookie_return[idx_return++] = *cookie; 27 + return 0; 28 + } 29 + 30 + if (idx_entry >= ARRAY_SIZE(test_uprobe_cookie_entry)) 31 + return 1; 32 + *cookie = test_uprobe_cookie_entry[idx_entry]; 33 + return idx_entry++ % 2; 34 + } 35 + 36 + 37 + SEC("uprobe.session//proc/self/exe:uprobe_session_recursive") 38 + int uprobe_recursive(struct pt_regs *ctx) 39 + { 40 + if (bpf_get_current_pid_tgid() >> 32 != pid) 41 + return 1; 42 + 43 + return check_cookie(); 44 + }