Skip to content

Commit

Permalink
selftests/bpf: extend multi-uprobe tests with USDTs
Browse files Browse the repository at this point in the history
Validate libbpf's USDT-over-multi-uprobe logic by adding USDTs to
existing multi-uprobe tests. This checks correct libbpf fallback to
singular uprobes (when run on older kernels with buggy PID filtering).
We reuse already established child process and child thread testing
infrastructure, so additions are minimal. These test fail on either
older kernels or older version of libbpf that doesn't detect PID
filtering problems.

Acked-by: Jiri Olsa <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alexei Starovoitov <[email protected]>
  • Loading branch information
anakryiko authored and Alexei Starovoitov committed May 25, 2024
1 parent 7034242 commit 198034a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
25 changes: 25 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "uprobe_multi_usdt.skel.h"
#include "bpf/libbpf_internal.h"
#include "testing_helpers.h"
#include "../sdt.h"

static char test_data[] = "test_data";

Expand All @@ -26,6 +27,11 @@ noinline void uprobe_multi_func_3(void)
asm volatile ("");
}

noinline void usdt_trigger(void)
{
STAP_PROBE(test, pid_filter_usdt);
}

struct child {
int go[2];
int c2p[2]; /* child -> parent channel */
Expand Down Expand Up @@ -90,6 +96,7 @@ static struct child *spawn_child(void)
uprobe_multi_func_1();
uprobe_multi_func_2();
uprobe_multi_func_3();
usdt_trigger();

exit(errno);
}
Expand Down Expand Up @@ -117,6 +124,7 @@ static void *child_thread(void *ctx)
uprobe_multi_func_1();
uprobe_multi_func_2();
uprobe_multi_func_3();
usdt_trigger();

err = 0;
pthread_exit(&err);
Expand Down Expand Up @@ -182,6 +190,7 @@ static void uprobe_multi_test_run(struct uprobe_multi *skel, struct child *child
uprobe_multi_func_1();
uprobe_multi_func_2();
uprobe_multi_func_3();
usdt_trigger();
}

if (child)
Expand Down Expand Up @@ -269,8 +278,24 @@ __test_attach_api(const char *binary, const char *pattern, struct bpf_uprobe_mul
if (!ASSERT_OK_PTR(skel->links.uprobe_extra, "bpf_program__attach_uprobe_multi"))
goto cleanup;

/* Attach (uprobe-backed) USDTs */
skel->links.usdt_pid = bpf_program__attach_usdt(skel->progs.usdt_pid, pid, binary,
"test", "pid_filter_usdt", NULL);
if (!ASSERT_OK_PTR(skel->links.usdt_pid, "attach_usdt_pid"))
goto cleanup;

skel->links.usdt_extra = bpf_program__attach_usdt(skel->progs.usdt_extra, -1, binary,
"test", "pid_filter_usdt", NULL);
if (!ASSERT_OK_PTR(skel->links.usdt_extra, "attach_usdt_extra"))
goto cleanup;

uprobe_multi_test_run(skel, child);

ASSERT_FALSE(skel->bss->bad_pid_seen_usdt, "bad_pid_seen_usdt");
if (child) {
ASSERT_EQ(skel->bss->child_pid_usdt, child->pid, "usdt_multi_child_pid");
ASSERT_EQ(skel->bss->child_tid_usdt, child->tid, "usdt_multi_child_tid");
}
cleanup:
uprobe_multi__destroy(skel);
}
Expand Down
33 changes: 31 additions & 2 deletions tools/testing/selftests/bpf/progs/uprobe_multi.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <stdbool.h>
#include <bpf/usdt.bpf.h>

char _license[] SEC("license") = "GPL";

Expand All @@ -23,9 +23,12 @@ __u64 uprobe_multi_sleep_result = 0;
int pid = 0;
int child_pid = 0;
int child_tid = 0;
int child_pid_usdt = 0;
int child_tid_usdt = 0;

int expect_pid = 0;
bool bad_pid_seen = false;
bool bad_pid_seen_usdt = false;

bool test_cookie = false;
void *user_ptr = 0;
Expand Down Expand Up @@ -112,3 +115,29 @@ int uprobe_extra(struct pt_regs *ctx)
/* we need this one just to mix PID-filtered and global uprobes */
return 0;
}

SEC("usdt")
int usdt_pid(struct pt_regs *ctx)
{
__u64 cur_pid_tgid = bpf_get_current_pid_tgid();
__u32 cur_pid;

cur_pid = cur_pid_tgid >> 32;
if (pid && cur_pid != pid)
return 0;

if (expect_pid && cur_pid != expect_pid)
bad_pid_seen_usdt = true;

child_pid_usdt = cur_pid_tgid >> 32;
child_tid_usdt = (__u32)cur_pid_tgid;

return 0;
}

SEC("usdt")
int usdt_extra(struct pt_regs *ctx)
{
/* we need this one just to mix PID-filtered and global USDT probes */
return 0;
}

0 comments on commit 198034a

Please sign in to comment.