Skip to content

RFC uprobes: Add support to optimize usdt probes on x86_64

Jiri Olsa bpf linux-trace-kernel lkml 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 RFC Latest23 patches 13 repliesIn review
  2. v2 RFC18 patches 14 repliesSuperseded
  3. v113 patches 49 repliesSuperseded

Changes in v3 from the cover letter

- I tried to have just single syscall for both entry and return uprobe, but it turned out to be slower than having two separated syscalls, probably due to extra save/restore processing we have to do for argument reg, I see differences like:

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/23uprobes: Rename arch_uretprobe_trampoline functionarch/x86/kernel/uprobes.c include/linux/uprobes.h kernel/events/uprobes.c+4 -40 replies
2/23uprobes: Make copy_from_page globalinclude/linux/uprobes.h kernel/events/uprobes.c+9 -80 replies
3/23uprobes: Move ref_ctr_offset update out of uprobe_write_opcodekernel/events/uprobes.c+32 -180 replies
4/23uprobes: Add uprobe_write functioninclude/linux/uprobes.h kernel/events/uprobes.c+14 -30 replies
5/23uprobes: Add nbytes argument to uprobe_write_opcodeinclude/linux/uprobes.h kernel/events/uprobes.c+9 -90 replies
6/23uprobes: Add orig argument to uprobe_write and uprobe_write_opcodearch/arm/probes/uprobes/core.c include/linux/uprobes.h kernel/events/uprobes.c+14 -152 replies
7/23uprobes: Remove breakpoint in unapply_uprobe under mmap_write_lockkernel/events/uprobes.c+2 -20 replies
8/23uprobes/x86: Add uprobe syscall to speed up uprobearch/x86/entry/syscalls/syscall_64.tbl arch/x86/kernel/uprobes.c include/linux/syscalls.h +3 more+161 -02 replies
9/23uprobes/x86: Add mapping for optimized uprobe trampolinesarch/x86/kernel/uprobes.c include/linux/uprobes.h kernel/events/uprobes.c +1 more+140 -00 replies
10/23uprobes/x86: Add support to emulate nop5 instructionarch/x86/kernel/uprobes.c+16 -05 replies
11/23uprobes/x86: Add support to optimize uprobesarch/x86/include/asm/uprobes.h arch/x86/kernel/uprobes.c include/linux/uprobes.h +1 more+290 -80 replies
12/23selftests/bpf: Use 5-byte nop for x86 usdt probestools/testing/selftests/bpf/sdt.h+8 -10 replies
13/23selftests/bpf: Reorg the uprobe_syscall test functiontools/testing/selftests/bpf/prog_tests/uprobe_syscall.c+12 -220 replies
14/23selftests/bpf: Rename uprobe_syscall_executed prog to test_uretprobe_multitools/testing/selftests/bpf/prog_tests/uprobe_syscall.c tools/testing/selftests/bpf/progs/uprobe_syscall_executed.c+6 -60 replies
15/23selftests/bpf: Add uprobe/usdt syscall teststools/testing/selftests/bpf/prog_tests/uprobe_syscall.c tools/testing/selftests/bpf/progs/uprobe_syscall_executed.c+294 -00 replies
16/23selftests/bpf: Add hit/attach/detach race optimized uprobe testtools/testing/selftests/bpf/prog_tests/uprobe_syscall.c+74 -00 replies
17/23selftests/bpf: Add uprobe syscall sigill signal testtools/testing/selftests/bpf/prog_tests/uprobe_syscall.c+36 -00 replies
18/23selftests/bpf: Add optimized usdt variant for basic usdt testtools/testing/selftests/bpf/prog_tests/usdt.c+25 -130 replies
19/23selftests/bpf: Add uprobe_regs_equal testtools/testing/selftests/bpf/prog_tests/uprobe_syscall.c tools/testing/selftests/bpf/progs/uprobe_syscall.c+44 -170 replies
20/23selftests/bpf: Change test_uretprobe_regs_change for uprobe and uretprobetools/testing/selftests/bpf/prog_tests/uprobe_syscall.c tools/testing/selftests/bpf/test_kmods/bpf_testmod.c+16 -60 replies
21/23selftests/bpf: Add 5-byte nop uprobe trigger benchtools/testing/selftests/bpf/bench.c tools/testing/selftests/bpf/benchs/bench_trigger.c tools/testing/selftests/bpf/benchs/run_bench_uprobes.sh+55 -10 replies
22/23seccomp: passthrough uprobe systemcall without filteringkernel/seccomp.c+25 -70 replies
23/23selftests/seccomp: validate uprobe syscall passes through seccomptools/testing/selftests/seccomp/seccomp_bpf.c+86 -210 replies

Reviews trailers per patch

3 of 23 patches have a Reviewed-by or Acked-by.

PatchAndrii Nakryiko
1/23A-b
2/23A-b
3/23
4/23
5/23A-b
6/23
7/23
8/23
9/23
10/23
11/23
12/23
13/23
14/23
15/23
16/23
17/23
18/23
19/23
20/23
21/23
22/23
23/23

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH RFCv3 00/23] uprobes: Add support to optimize usdt probes on x86_64

hi,
this patchset adds support to optimize usdt probes on top of 5-byte nop instruction.

The generic approach (optimize all uprobes) is hard due to emulating possible multiple original instructions and its related issues. The usdt case, which stores 5-byte nop seems much easier, so starting with that.

The basic idea is to replace breakpoint exception with syscall which is faster on x86_64. For more details please see changelog of patch 8.

The run_bench_uprobes.sh benchmark triggers uprobe (on top of different original instructions) in a loop and counts how many of those happened per second (the unit below is million loops).

There's big speed up if you consider current usdt implementation (uprobe-nop) compared to proposed usdt (uprobe-nop5):

current:

        usermode-count :  152.604 ± 0.044M/s
        syscall-count  :   13.359 ± 0.042M/s
-->     uprobe-nop     :    3.229 ± 0.002M/s
        uprobe-push    :    3.086 ± 0.004M/s
        uprobe-ret     :    1.114 ± 0.004M/s
        uprobe-nop5    :    1.121 ± 0.005M/s
        uretprobe-nop  :    2.145 ± 0.002M/s
        uretprobe-push :    2.070 ± 0.001M/s
        uretprobe-ret  :    0.931 ± 0.001M/s
        uretprobe-nop5 :    0.957 ± 0.001M/s

after the change:

        usermode-count :  152.448 ± 0.244M/s
        syscall-count  :   14.321 ± 0.059M/s
        uprobe-nop     :    3.148 ± 0.007M/s
        uprobe-push    :    2.976 ± 0.004M/s
        uprobe-ret     :    1.068 ± 0.003M/s
-->     uprobe-nop5    :    7.038 ± 0.007M/s
        uretprobe-nop  :    2.109 ± 0.004M/s
        uretprobe-push :    2.035 ± 0.001M/s
        uretprobe-ret  :    0.908 ± 0.001M/s
        uretprobe-nop5 :    3.377 ± 0.009M/s

I see bit more speed up on Intel (above) compared to AMD. The big nop5 speed up is partly due to emulating nop5 and partly due to optimization.

The key speed up we do this for is the USDT switch from nop to nop5:

        uprobe-nop     :    3.148 ± 0.007M/s
        uprobe-nop5    :    7.038 ± 0.007M/s

rfc v3 changes:
- I tried to have just single syscall for both entry and return uprobe, but it turned out to be slower than having two separated syscalls, probably due to extra save/restore processing we have to do for argument reg, I see differences like:

    2 syscalls:      uprobe-nop5    :    7.038 ± 0.007M/s
    1 syscall:       uprobe-nop5    :    6.943 ± 0.003M/s

- use instructions (nop5/int3/call) to determine the state of the uprobe update in the process
- removed endbr instruction from uprobe trampoline
- seccomp changes

pending todo (or follow ups):
- shadow stack fails for uprobe session setup, will fix it in next version
- use PROCMAP_QUERY in tests
- alloc 'struct uprobes_state' for mm_struct only when needed [Andrii]

thanks,
jirka

---
Jiri Olsa (23):
uprobes: Rename arch_uretprobe_trampoline function
uprobes: Make copy_from_page global
uprobes: Move ref_ctr_offset update out of uprobe_write_opcode
uprobes: Add uprobe_write function
uprobes: Add nbytes argument to uprobe_write_opcode
uprobes: Add orig argument to uprobe_write and uprobe_write_opcode
uprobes: Remove breakpoint in unapply_uprobe under mmap_write_lock uprobes/x86: Add uprobe syscall to speed up uprobe uprobes/x86: Add mapping for optimized uprobe trampolines uprobes/x86: Add support to emulate nop5 instruction uprobes/x86: Add support to optimize uprobes
selftests/bpf: Use 5-byte nop for x86 usdt probes selftests/bpf: Reorg the uprobe_syscall test function selftests/bpf: Rename uprobe_syscall_executed prog to test_uretprobe_multi selftests/bpf: Add uprobe/usdt syscall tests
selftests/bpf: Add hit/attach/detach race optimized uprobe test selftests/bpf: Add uprobe syscall sigill signal test selftests/bpf: Add optimized usdt variant for basic usdt test selftests/bpf: Add uprobe_regs_equal test
selftests/bpf: Change test_uretprobe_regs_change for uprobe and uretprobe selftests/bpf: Add 5-byte nop uprobe trigger bench
seccomp: passthrough uprobe systemcall without filtering selftests/seccomp: validate uprobe syscall passes through seccomp

 arch/arm/probes/uprobes/core.c                              |   2 +-
 arch/x86/entry/syscalls/syscall_64.tbl                      |   1 +
 arch/x86/include/asm/uprobes.h                              |   7 ++
 arch/x86/kernel/uprobes.c                                   | 540 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/syscalls.h                                    |   2 +
 include/linux/uprobes.h                                     |  19 +++-
 kernel/events/uprobes.c                                     | 141 +++++++++++++++++-------
 kernel/fork.c                                               |   1 +
 kernel/seccomp.c                                            |  32 ++++--
 kernel/sys_ni.c                                             |   1 +
 tools/testing/selftests/bpf/bench.c                         |  12 +++
 tools/testing/selftests/bpf/benchs/bench_trigger.c          |  42 ++++++++
 tools/testing/selftests/bpf/benchs/run_bench_uprobes.sh     |   2 +-
 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c     | 453 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 tools/testing/selftests/bpf/prog_tests/usdt.c               |  38 ++++---
 tools/testing/selftests/bpf/progs/uprobe_syscall.c          |   4 +-
 tools/testing/selftests/bpf/progs/uprobe_syscall_executed.c |  41 ++++++-
 tools/testing/selftests/bpf/sdt.h                           |   9 +-
 tools/testing/selftests/bpf/test_kmods/bpf_testmod.c        |  11 +-
 tools/testing/selftests/seccomp/seccomp_bpf.c               | 107 ++++++++++++++----
 20 files changed, 1338 insertions(+), 127 deletions(-)