diff mbox series

[bpf,5/5] selftests/bpf: extend multi-uprobe tests with USDTs

Message ID 20240520234720.1748918-6-andrii@kernel.org (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Fix BPF multi-uprobe PID filtering logic | expand

Checks

Context Check Description
bpf/vmtest-bpf-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-VM_Test-15 fail Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-23 fail Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-22 fail Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
bpf/vmtest-bpf-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-31 fail Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-32 fail Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-VM_Test-38 fail Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-39 fail Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-40 fail Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-PR fail PR summary
bpf/vmtest-bpf-VM_Test-7 fail Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-8 fail Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-14 fail Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 12 maintainers not CCed: song@kernel.org john.fastabend@gmail.com martin.lau@linux.dev kpsingh@kernel.org yonghong.song@linux.dev sdf@google.com haoluo@google.com linux-kselftest@vger.kernel.org mykolal@fb.com shuah@kernel.org eddyz87@gmail.com jolsa@kernel.org
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning WARNING: line length of 89 exceeds 80 columns WARNING: line length of 90 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns WARNING: line length of 93 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Andrii Nakryiko May 20, 2024, 11:47 p.m. UTC
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.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 .../bpf/prog_tests/uprobe_multi_test.c        | 22 +++++++++++++
 .../selftests/bpf/progs/uprobe_multi.c        | 33 +++++++++++++++++--
 2 files changed, 53 insertions(+), 2 deletions(-)

Comments

Andrii Nakryiko May 21, 2024, 4:54 a.m. UTC | #1
On Mon, May 20, 2024 at 4:47 PM Andrii Nakryiko <andrii@kernel.org> wrote:
>
> 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.
>
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> ---
>  .../bpf/prog_tests/uprobe_multi_test.c        | 22 +++++++++++++
>  .../selftests/bpf/progs/uprobe_multi.c        | 33 +++++++++++++++++--
>  2 files changed, 53 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> index 677232d31432..85d46e568e90 100644
> --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> @@ -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";
>
> @@ -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 */
> @@ -269,8 +275,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);
>  }
> diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi.c b/tools/testing/selftests/bpf/progs/uprobe_multi.c
> index 86a7ff5d3726..44190efcdba2 100644
> --- a/tools/testing/selftests/bpf/progs/uprobe_multi.c
> +++ b/tools/testing/selftests/bpf/progs/uprobe_multi.c
> @@ -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";
>
> @@ -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;
> @@ -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;
> +}
> --
> 2.43.0
>

I lost the following during the final rebase before submitting,
sigh... With the piece below tests are passing again:

diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
index 85d46e568e90..bf6ca8e3eb13 100644
--- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
@@ -96,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);
        }
@@ -123,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);
@@ -188,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)


I'll wait till tomorrow for any feedback and will post v2.

I'm also curious about logistics? Do we want to get everything through
the bpf tree? Or bpf-next? Or split somehow? Thoughts?

I think the fix in patch #1 is important enough to backport to stable
kernels (multi-uprobes went into upstream v6.6 kernel, FYI).
Alexei Starovoitov May 21, 2024, 5:05 a.m. UTC | #2
On Mon, May 20, 2024 at 9:54 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Mon, May 20, 2024 at 4:47 PM Andrii Nakryiko <andrii@kernel.org> wrote:
> >
> > 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.
> >
> > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> > ---
> >  .../bpf/prog_tests/uprobe_multi_test.c        | 22 +++++++++++++
> >  .../selftests/bpf/progs/uprobe_multi.c        | 33 +++++++++++++++++--
> >  2 files changed, 53 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > index 677232d31432..85d46e568e90 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > @@ -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";
> >
> > @@ -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 */
> > @@ -269,8 +275,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);
> >  }
> > diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi.c b/tools/testing/selftests/bpf/progs/uprobe_multi.c
> > index 86a7ff5d3726..44190efcdba2 100644
> > --- a/tools/testing/selftests/bpf/progs/uprobe_multi.c
> > +++ b/tools/testing/selftests/bpf/progs/uprobe_multi.c
> > @@ -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";
> >
> > @@ -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;
> > @@ -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;
> > +}
> > --
> > 2.43.0
> >
>
> I lost the following during the final rebase before submitting,
> sigh... With the piece below tests are passing again:
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> index 85d46e568e90..bf6ca8e3eb13 100644
> --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> @@ -96,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);
>         }
> @@ -123,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);
> @@ -188,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)
>
>
> I'll wait till tomorrow for any feedback and will post v2.

pls wait for review from Jiri.

> I'm also curious about logistics? Do we want to get everything through
> the bpf tree? Or bpf-next? Or split somehow? Thoughts?

I think the whole thing through bpf tree makes it the easiest for everyone.
Jiri Olsa May 21, 2024, 10:04 a.m. UTC | #3
On Mon, May 20, 2024 at 09:54:40PM -0700, Andrii Nakryiko wrote:
> On Mon, May 20, 2024 at 4:47 PM Andrii Nakryiko <andrii@kernel.org> wrote:
> >
> > 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.
> >
> > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> > ---
> >  .../bpf/prog_tests/uprobe_multi_test.c        | 22 +++++++++++++
> >  .../selftests/bpf/progs/uprobe_multi.c        | 33 +++++++++++++++++--
> >  2 files changed, 53 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > index 677232d31432..85d46e568e90 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > @@ -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";
> >
> > @@ -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 */
> > @@ -269,8 +275,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);
> >  }
> > diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi.c b/tools/testing/selftests/bpf/progs/uprobe_multi.c
> > index 86a7ff5d3726..44190efcdba2 100644
> > --- a/tools/testing/selftests/bpf/progs/uprobe_multi.c
> > +++ b/tools/testing/selftests/bpf/progs/uprobe_multi.c
> > @@ -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";
> >
> > @@ -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;
> > @@ -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;
> > +}
> > --
> > 2.43.0
> >
> 
> I lost the following during the final rebase before submitting,
> sigh... With the piece below tests are passing again:
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> index 85d46e568e90..bf6ca8e3eb13 100644
> --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> @@ -96,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);
>         }
> @@ -123,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);
> @@ -188,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)
> 
> 
> I'll wait till tomorrow for any feedback and will post v2.

tests are passing for me with the changes above

> 
> I'm also curious about logistics? Do we want to get everything through
> the bpf tree? Or bpf-next? Or split somehow? Thoughts?
> 
> I think the fix in patch #1 is important enough to backport to stable
> kernels (multi-uprobes went into upstream v6.6 kernel, FYI).

agreed, perhaps also the patch #3 for libbpf detection?

jirka
Andrii Nakryiko May 21, 2024, 4:13 p.m. UTC | #4
On Tue, May 21, 2024 at 3:04 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Mon, May 20, 2024 at 09:54:40PM -0700, Andrii Nakryiko wrote:
> > On Mon, May 20, 2024 at 4:47 PM Andrii Nakryiko <andrii@kernel.org> wrote:
> > >
> > > 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.
> > >
> > > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> > > ---
> > >  .../bpf/prog_tests/uprobe_multi_test.c        | 22 +++++++++++++
> > >  .../selftests/bpf/progs/uprobe_multi.c        | 33 +++++++++++++++++--
> > >  2 files changed, 53 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > > index 677232d31432..85d46e568e90 100644
> > > --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > > +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > > @@ -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";
> > >
> > > @@ -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 */
> > > @@ -269,8 +275,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);
> > >  }
> > > diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi.c b/tools/testing/selftests/bpf/progs/uprobe_multi.c
> > > index 86a7ff5d3726..44190efcdba2 100644
> > > --- a/tools/testing/selftests/bpf/progs/uprobe_multi.c
> > > +++ b/tools/testing/selftests/bpf/progs/uprobe_multi.c
> > > @@ -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";
> > >
> > > @@ -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;
> > > @@ -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;
> > > +}
> > > --
> > > 2.43.0
> > >
> >
> > I lost the following during the final rebase before submitting,
> > sigh... With the piece below tests are passing again:
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > index 85d46e568e90..bf6ca8e3eb13 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
> > @@ -96,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);
> >         }
> > @@ -123,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);
> > @@ -188,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)
> >
> >
> > I'll wait till tomorrow for any feedback and will post v2.
>
> tests are passing for me with the changes above
>
> >
> > I'm also curious about logistics? Do we want to get everything through
> > the bpf tree? Or bpf-next? Or split somehow? Thoughts?
> >
> > I think the fix in patch #1 is important enough to backport to stable
> > kernels (multi-uprobes went into upstream v6.6 kernel, FYI).
>
> agreed, perhaps also the patch #3 for libbpf detection?

no one should be building libbpf from kernel sources, Github is the
authoritative source code for packaging. So I think it's not necessary
to backport libbpf. Tests will keep passing with just patch #1.

>
> jirka
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
index 677232d31432..85d46e568e90 100644
--- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
@@ -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";
 
@@ -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 */
@@ -269,8 +275,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);
 }
diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi.c b/tools/testing/selftests/bpf/progs/uprobe_multi.c
index 86a7ff5d3726..44190efcdba2 100644
--- a/tools/testing/selftests/bpf/progs/uprobe_multi.c
+++ b/tools/testing/selftests/bpf/progs/uprobe_multi.c
@@ -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";
 
@@ -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;
@@ -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;
+}