public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC/PATCH bpf-next 00/20] bpf: Add multi uprobe link
@ 2023-04-24 16:04 Jiri Olsa
  2023-04-24 16:04 ` [RFC/PATCH bpf-next 01/20] " Jiri Olsa
                   ` (20 more replies)
  0 siblings, 21 replies; 52+ messages in thread
From: Jiri Olsa @ 2023-04-24 16:04 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Viktor Malik, Daniel Xu, bpf, Martin KaFai Lau, Song Liu,
	Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev,
	Hao Luo, Arnaldo Carvalho de Melo

hi,
this patchset is adding support to attach multiple uprobes and usdt probes
through new uprobe_multi link.

The current uprobe is attached through the perf event and attaching many
uprobes takes a lot of time because of that.

The main reason is that we need to install perf event for each probed function
and profile shows perf event installation (perf_install_in_context) as culprit.

The new uprobe_multi link just creates raw uprobes and attaches the bpf
program to them without perf event being involved.

In addition to being faster we also save file descriptors. For the current
uprobe attach we use extra perf event fd for each probed function. The new
link just need one fd that covers all the functions we are attaching to.

By dropping perf we lose the ability to attach uprobe to specific pid.
We can workaround that by having pid check directly in the bpf program,
but we might need to check for another solution if that will turn out
to be a problem.


Attaching current bpftrace to 1000 uprobes:

  # BPFTRACE_MAX_PROBES=100000 perf stat -e cycles,instructions \
    ./bpftrace -e 'uprobe:./uprobe_multi:uprobe_multi_func_* { }, i:ms:1 { exit(); }' 
    ...

     126,666,390,509      cycles                                                                
      29,973,207,307      instructions                     #    0.24  insn per cycle            

        85.284833554 seconds time elapsed


Same bpftrace setup with uprobe_multi support:

  # perf stat -e cycles,instructions \
    ./bpftrace -e 'uprobe:./uprobe_multi:uprobe_multi_func_* { }, i:ms:1 { exit(); }' 
    ...

       6,818,470,649      cycles                                                                
      13,275,510,122      instructions                     #    1.95  insn per cycle            

         1.943269451 seconds time elapsed


I'm sending this as RFC because of:
  - I added/exported some new elf_* helper functions in libbpf,
    and I'm not sure that's the best/right way of doing this
  - I'm not completely sure about the usdt integration in bpf_program__attach_usdt,
    I was trying to detect uprobe_multi kernel support first, but ended up with
    just new field for struct bpf_usdt_opts
  - I plan to add more tests for usdt probes defined with refctr


Also available at:
  https://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
  uprobe_multi

There are PRs for tetragon [1] and bpftrace [2] support.

thanks,
jirka


[1] https://github.com/cilium/tetragon/pull/936
[2] https://github.com/olsajiri/bpftrace/tree/uprobe_multi

Cc: Viktor Malik <viktor.malik@gmail.com>
Cc: Daniel Xu <dxu@dxuuu.xyz>
---
Jiri Olsa (20):
      bpf: Add multi uprobe link
      bpf: Add cookies support for uprobe_multi link
      bpf: Add bpf_get_func_ip helper support for uprobe link
      libbpf: Update uapi bpf.h tools header
      libbpf: Add uprobe_multi attach type and link names
      libbpf: Factor elf_for_each_symbol function
      libbpf: Add elf_find_multi_func_offset function
      libbpf: Add elf_find_patern_func_offset function
      libbpf: Add bpf_link_create support for multi uprobes
      libbpf: Add bpf_program__attach_uprobe_multi_opts function
      libbpf: Add support for uprobe.multi/uprobe.multi program sections
      libbpf: Add uprobe multi link support to bpf_program__attach_usdt
      selftests/bpf: Add uprobe_multi skel test
      selftests/bpf: Add uprobe_multi api test
      selftests/bpf: Add uprobe_multi link test
      selftests/bpf: Add uprobe_multi test program
      selftests/bpf: Add uprobe_multi bench test
      selftests/bpf: Add usdt_multi test program
      selftests/bpf: Add usdt_multi bench test
      selftests/bpf: Add uprobe_multi cookie test

 include/linux/trace_events.h                               |   6 +
 include/uapi/linux/bpf.h                                   |  15 +++
 kernel/bpf/syscall.c                                       |  18 ++-
 kernel/trace/bpf_trace.c                                   | 310 +++++++++++++++++++++++++++++++++++++++++++-
 tools/include/uapi/linux/bpf.h                             |  15 +++
 tools/lib/bpf/bpf.c                                        |  10 ++
 tools/lib/bpf/bpf.h                                        |  10 +-
 tools/lib/bpf/libbpf.c                                     | 653 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
 tools/lib/bpf/libbpf.h                                     |  43 ++++++
 tools/lib/bpf/libbpf.map                                   |   3 +
 tools/lib/bpf/libbpf_internal.h                            |   2 +-
 tools/lib/bpf/usdt.c                                       | 127 +++++++++++++-----
 tools/testing/selftests/bpf/Makefile                       |  10 ++
 tools/testing/selftests/bpf/prog_tests/bpf_cookie.c        |  78 +++++++++++
 tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c | 286 ++++++++++++++++++++++++++++++++++++++++
 tools/testing/selftests/bpf/progs/uprobe_multi.c           |  72 +++++++++++
 tools/testing/selftests/bpf/progs/uprobe_multi_usdt.c      |  16 +++
 tools/testing/selftests/bpf/uprobe_multi.c                 |  53 ++++++++
 tools/testing/selftests/bpf/usdt_multi.c                   |  23 ++++
 19 files changed, 1634 insertions(+), 116 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi.c
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_usdt.c
 create mode 100644 tools/testing/selftests/bpf/uprobe_multi.c
 create mode 100644 tools/testing/selftests/bpf/usdt_multi.c

^ permalink raw reply	[flat|nested] 52+ messages in thread

end of thread, other threads:[~2023-04-28 21:19 UTC | newest]

Thread overview: 52+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-24 16:04 [RFC/PATCH bpf-next 00/20] bpf: Add multi uprobe link Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 01/20] " Jiri Olsa
2023-04-24 22:11   ` Alexei Starovoitov
2023-04-25  9:54     ` Jiri Olsa
2023-04-26 19:01       ` Andrii Nakryiko
2023-04-27 13:15         ` Jiri Olsa
2023-04-25 23:56   ` Yonghong Song
2023-04-26  7:37     ` Jiri Olsa
2023-04-26 19:00   ` Andrii Nakryiko
2023-04-27 13:14     ` Jiri Olsa
2023-04-26 19:17   ` Andrii Nakryiko
2023-04-27 13:15     ` Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 02/20] bpf: Add cookies support for uprobe_multi link Jiri Olsa
2023-04-26  0:03   ` Yonghong Song
2023-04-26 19:13   ` Andrii Nakryiko
2023-04-27 12:58     ` Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 03/20] bpf: Add bpf_get_func_ip helper support for uprobe link Jiri Olsa
2023-04-26 19:11   ` Andrii Nakryiko
2023-04-27 12:45     ` Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 04/20] libbpf: Update uapi bpf.h tools header Jiri Olsa
2023-04-26 19:14   ` Andrii Nakryiko
2023-04-27 12:58     ` Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 05/20] libbpf: Add uprobe_multi attach type and link names Jiri Olsa
2023-04-26 19:14   ` Andrii Nakryiko
2023-04-24 16:04 ` [RFC/PATCH bpf-next 06/20] libbpf: Factor elf_for_each_symbol function Jiri Olsa
2023-04-26 19:27   ` Andrii Nakryiko
2023-04-27 13:23     ` Jiri Olsa
2023-04-27 22:28       ` Andrii Nakryiko
2023-04-24 16:04 ` [RFC/PATCH bpf-next 07/20] libbpf: Add elf_find_multi_func_offset function Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 08/20] libbpf: Add elf_find_patern_func_offset function Jiri Olsa
2023-04-26 19:24   ` Andrii Nakryiko
2023-04-27 13:21     ` Jiri Olsa
2023-04-27 22:29       ` Andrii Nakryiko
2023-04-24 16:04 ` [RFC/PATCH bpf-next 09/20] libbpf: Add bpf_link_create support for multi uprobes Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 10/20] libbpf: Add bpf_program__attach_uprobe_multi_opts function Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 11/20] libbpf: Add support for uprobe.multi/uprobe.multi program sections Jiri Olsa
2023-04-26 19:31   ` Andrii Nakryiko
2023-04-24 16:04 ` [RFC/PATCH bpf-next 12/20] libbpf: Add uprobe multi link support to bpf_program__attach_usdt Jiri Olsa
2023-04-26 19:32   ` Andrii Nakryiko
2023-04-24 16:04 ` [RFC/PATCH bpf-next 13/20] selftests/bpf: Add uprobe_multi skel test Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 14/20] selftests/bpf: Add uprobe_multi api test Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 15/20] selftests/bpf: Add uprobe_multi link test Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 16/20] selftests/bpf: Add uprobe_multi test program Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 17/20] selftests/bpf: Add uprobe_multi bench test Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 18/20] selftests/bpf: Add usdt_multi test program Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 19/20] selftests/bpf: Add usdt_multi bench test Jiri Olsa
2023-04-24 16:04 ` [RFC/PATCH bpf-next 20/20] selftests/bpf: Add uprobe_multi cookie test Jiri Olsa
2023-04-26 19:09 ` [RFC/PATCH bpf-next 00/20] bpf: Add multi uprobe link Andrii Nakryiko
2023-04-27 12:44   ` Jiri Olsa
2023-04-27 22:24     ` Andrii Nakryiko
2023-04-28 10:55       ` Jiri Olsa
2023-04-28 21:19         ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox