Skip to content

RFC bpf: Speed up trampoline attach

Jiri Olsa bpf netdev oe-kbuild-all 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest16 patches 30 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/16ftrace: Add check_direct_entry functionkernel/trace/ftrace.c+32 -230 replies
2/16ftrace: Add adjust_direct_size functionkernel/trace/ftrace.c+23 -160 replies
3/16ftrace: Add get/put_direct_func functionkernel/trace/ftrace.c+30 -140 replies
4/16ftrace: Add ftrace_set_filter_ips functioninclude/linux/ftrace.h kernel/trace/ftrace.c+59 -00 replies
5/16ftrace: Add register_ftrace_direct_ips functioninclude/linux/ftrace.h kernel/trace/ftrace.c+77 -00 replies
6/16ftrace: Add unregister_ftrace_direct_ips functioninclude/linux/ftrace.h kernel/trace/ftrace.c+53 -00 replies
7/16kallsyms: Use rb tree for kallsyms name searchkernel/kallsyms.c+86 -96 replies
8/16bpf: Use delayed link free in bpf_link_putkernel/bpf/syscall.c+2 -62 replies
9/16bpf: Add BPF_TRAMPOLINE_BATCH_ATTACH supportinclude/linux/bpf.h include/uapi/linux/bpf.h kernel/bpf/syscall.c +1 more+164 -155 replies
10/16bpf: Add BPF_TRAMPOLINE_BATCH_DETACH supportinclude/linux/bpf.h include/uapi/linux/bpf.h kernel/bpf/syscall.c +1 more+46 -130 replies
11/16bpf: Sync uapi bpf.h to toolstools/include/uapi/linux/bpf.h+8 -00 replies
12/16bpf: Move synchronize_rcu_mult for batch processing (NOT TO BE MERGED)kernel/bpf/syscall.c kernel/bpf/trampoline.c+5 -10 replies
13/16libbpf: Add trampoline batch attach supporttools/lib/bpf/bpf.c tools/lib/bpf/bpf.h tools/lib/bpf/libbpf.c +2 more+93 -24 replies
14/16libbpf: Add trampoline batch detach supporttools/lib/bpf/bpf.c tools/lib/bpf/bpf.h tools/lib/bpf/libbpf.c +1 more+66 -20 replies
15/16selftests/bpf: Add trampoline batch testtools/testing/selftests/bpf/prog_tests/trampoline_batch.c tools/testing/selftests/bpf/progs/trampoline_batch_test.c+120 -00 replies
16/16selftests/bpf: Add attach batch test (NOT TO BE MERGED)net/bpf/test_run.c tools/testing/selftests/bpf/prog_tests/attach_test.c tools/testing/selftests/bpf/progs/attach_test.c+144 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC bpf-next 00/16] bpf: Speed up trampoline attach

hi,
this patchset tries to speed up the attach time for trampolines and make bpftrace faster for wildcard use cases like:

  # bpftrace -ve "kfunc:__x64_sys_s* { printf("test\n"); }"

Profiles show mostly ftrace backend, because we add trampoline functions one by one and ftrace direct function registering is quite expensive. Thus main change in this patchset is to allow batch attach and use just single ftrace call to attach or detach multiple ips/trampolines.

This patchset also contains other speedup changes that showed up in profiles:

- delayed link free
to bypass detach cycles completely

- kallsyms rbtree search
change linear search to rb tree search

For clean attach workload I added also new attach selftest, which is not meant to be merged but is used to show profile results.

Following numbers show speedup after applying specific change on top of the previous (and including the previous changes).

profiled with: 'perf stat -r 5 -e cycles:k,cycles:u ...'

For bpftrace:

  # bpftrace -ve "kfunc:__x64_sys_s* { printf("test\n"); } i:ms:10 { printf("exit\n"); exit();}"

- base

      3,290,457,628      cycles:k         ( +-  0.27% )
        933,581,973      cycles:u         ( +-  0.20% )
      50.25 +- 4.79 seconds time elapsed  ( +-  9.53% )

+ delayed link free

      2,535,458,767      cycles:k         ( +-  0.55% )
        940,046,382      cycles:u         ( +-  0.27% )
      33.60 +- 3.27 seconds time elapsed  ( +-  9.73% )

+ kallsym rbtree search

      2,199,433,771      cycles:k         ( +-  0.55% )
        936,105,469      cycles:u         ( +-  0.37% )
      26.48 +- 3.57 seconds time elapsed  ( +- 13.49% )

+ batch support

      1,456,854,867      cycles:k         ( +-  0.57% )
        937,737,431      cycles:u         ( +-  0.13% )
      12.44 +- 2.98 seconds time elapsed  ( +- 23.95% )

+ rcu fix

      1,427,959,119      cycles:k         ( +-  0.87% )
        930,833,507      cycles:u         ( +-  0.23% )
      14.53 +- 3.51 seconds time elapsed  ( +- 24.14% )

For attach_test numbers do not show direct time speedup when using the batch support, but show big decrease in kernel cycles. It seems the time is spent in rcu waiting, which I tried to address in most likely wrong rcu fix:

  # ./test_progs -t attach_test

- base

      1,350,136,760      cycles:k         ( +-  0.07% )
         70,591,712      cycles:u         ( +-  0.26% )
      24.26 +- 2.82 seconds time elapsed  ( +- 11.62% )

+ delayed link free

        996,152,309      cycles:k         ( +-  0.37% )
         69,263,150      cycles:u         ( +-  0.50% )
      15.63 +- 1.80 seconds time elapsed  ( +- 11.51% )

+ kallsym rbtree search

        390,217,706      cycles:k         ( +-  0.66% )
         68,999,019      cycles:u         ( +-  0.46% )
      14.11 +- 2.11 seconds time elapsed  ( +- 14.98% )

+ batch support

         37,410,887      cycles:k         ( +-  0.98% )
         70,062,158      cycles:u         ( +-  0.39% )
      26.80 +- 4.10 seconds time elapsed  ( +- 15.31% )

+ rcu fix

         36,812,432      cycles:k         ( +-  2.52% )
         69,907,191      cycles:u         ( +-  0.38% )
      15.04 +- 2.94 seconds time elapsed  ( +- 19.54% )

I still need to go through the changes and double check them, also those ftrace changes are most likely wrong and most likely I broke few tests (hence it's RFC), but I wonder you guys would like this batch solution and if there are any thoughts on that.

Also available in
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
bpf/batch

thanks,
jirka

---
Jiri Olsa (16):
ftrace: Add check_direct_entry function
ftrace: Add adjust_direct_size function
ftrace: Add get/put_direct_func function
ftrace: Add ftrace_set_filter_ips function
ftrace: Add register_ftrace_direct_ips function
ftrace: Add unregister_ftrace_direct_ips function
kallsyms: Use rb tree for kallsyms name search
bpf: Use delayed link free in bpf_link_put
bpf: Add BPF_TRAMPOLINE_BATCH_ATTACH support
bpf: Add BPF_TRAMPOLINE_BATCH_DETACH support
bpf: Sync uapi bpf.h to tools
bpf: Move synchronize_rcu_mult for batch processing (NOT TO BE MERGED)
libbpf: Add trampoline batch attach support
libbpf: Add trampoline batch detach support
selftests/bpf: Add trampoline batch test
selftests/bpf: Add attach batch test (NOT TO BE MERGED)

 include/linux/bpf.h                                       |  18 +++++-
 include/linux/ftrace.h                                    |   7 +++
 include/uapi/linux/bpf.h                                  |   8 +++
 kernel/bpf/syscall.c                                      | 125 ++++++++++++++++++++++++++++++++++----
 kernel/bpf/trampoline.c                                   |  95 +++++++++++++++++++++++------
 kernel/kallsyms.c                                         |  95 ++++++++++++++++++++++++++---
 kernel/trace/ftrace.c                                     | 304 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
 net/bpf/test_run.c                                        |  55 +++++++++++++++++
 tools/include/uapi/linux/bpf.h                            |   8 +++
 tools/lib/bpf/bpf.c                                       |  24 ++++++++
 tools/lib/bpf/bpf.h                                       |   2 +
 tools/lib/bpf/libbpf.c                                    | 126 ++++++++++++++++++++++++++++++++++++++-
 tools/lib/bpf/libbpf.h                                    |   5 +-
 tools/lib/bpf/libbpf.map                                  |   2 +
 tools/testing/selftests/bpf/prog_tests/attach_test.c      |  27 +++++++++
 tools/testing/selftests/bpf/prog_tests/trampoline_batch.c |  45 ++++++++++++++
 tools/testing/selftests/bpf/progs/attach_test.c           |  62 +++++++++++++++++++
 tools/testing/selftests/bpf/progs/trampoline_batch_test.c |  75 +++++++++++++++++++++++
 18 files changed, 995 insertions(+), 88 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/attach_test.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/trampoline_batch.c
 create mode 100644 tools/testing/selftests/bpf/progs/attach_test.c
 create mode 100644 tools/testing/selftests/bpf/progs/trampoline_batch_test.c