bpf: emulate push insns for uprobe on x86
Subsystems (4)
PATCH 1/1 bpf: emulate push insns for uprobe on x86
Uprobe is a tracing mechanism for userspace programs. Typical uprobe will incur overhead of two traps. First trap is caused by replaced trap insn, and the second trap is to execute the original displaced insn in user space.
To reduce the overhead, kernel provides hooks for architectures to emulate the original insn and skip the second trap. In x86, emulation
is done for certain branch insns.
This patch extends the emulation to "push <reg>" insns. These insns are typical in the beginning of the function. For example, bcc
in https://github.com/iovisor/bcc repo provides tools to measure funclantency, detect memleak, etc. The tools will place uprobes in the beginning of function and possibly uretprobes at the end of function. This patch is able to reduce the trap overhead for uprobe from 2 to 1.
Without this patch, uretprobe will typically incur three traps. With this patch, if the function starts with "push" insn, the number of traps can be reduced from 3 to 2.
An experiment was conducted on two local VMs, fedora 26 64-bit VM and 32-bit VM, both 4 processors and 4GB memory, booted with latest net-next (and this patch). The host is MacBook with intel i7 processor.
The test program looks like
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
static void test() __attribute__((noinline));
void test() {}
int main() {
struct timeval start, end; gettimeofday(&start, NULL);
for (int i = 0; i < 1000000; i++) {
test();
}
gettimeofday(&end, NULL);
printf("%ld\n", ((end.tv_sec * 1000000 + end.tv_usec)
- (start.tv_sec * 1000000 + start.tv_usec)));
return 0;}
The program is compiled without optimization, and the first insn for function "test" is "push %rbp". The host is relatively idle.
Before the test run, the uprobe is inserted as below for uprobe:
echo 'p <binary>:<test_func_offset>' > /sys/kernel/debug/tracing/uprobe_events echo 1 > /sys/kernel/debug/tracing/events/uprobes/enable
and for uretprobe:
echo 'r <binary>:<test_func_offset>' > /sys/kernel/debug/tracing/uprobe_events echo 1 > /sys/kernel/debug/tracing/events/uprobes/enable
Unit: microsecond(usec) per loop iteration
x86_64 W/ this patch W/O this patch uprobe 1.55 3.1 uretprobe 2.0 3.6
x86_32 W/ this patch W/O this patch uprobe 1.41 3.5 uretprobe 1.75 4.0
You can see that this patch significantly reduced the overhead, 50% for uprobe and 44% for uretprobe on x86_64, and even more on x86_32.
--- arch/x86/include/asm/uprobes.h | 10 ++++ arch/x86/kernel/uprobes.c | 115 +++++++++++++++++++++++++++++++++++------ 2 files changed, 109 insertions(+), 16 deletions(-)
arch/x86/include/asm/uprobes.h +10 -0
@@ -33,6 +33,11 @@ typedef u8 uprobe_opcode_t;#define UPROBE_SWBP_INSN 0xcc#define UPROBE_SWBP_INSN_SIZE 1 +enum uprobe_insn_t { +UPROBE_BRANCH_INSN =0 , +UPROBE_PUSH_INSN =1 , +}; +struct uprobe_xol_ops ;struct arch_uprobe {
@@ -42,6 +47,7 @@ struct arch_uprobe { };const struct uprobe_xol_ops *ops; +enum uprobe_insn_t insn_class; union { struct {
@@ -53,6 +59,10 @@ struct arch_uprobe {u8 fixups;u8 ilen; } defparam; +struct { +u8 rex_prefix; +u8 opc1; + } push; }; };
arch/x86/kernel/uprobes.c +99 -16
161 lines not on this page · raw message
Signature · 1 line
2.9.5
David Miller
maintainerFrom: Yonghong Song <[email protected]>
Date: Wed, 8 Nov 2017 13:37:12 -0800
Quoted 5 lines
Uprobe is a tracing mechanism for userspace programs. Typical uprobe will incur overhead of two traps. First trap is caused by replaced trap insn, and the second trap is to execute the original displaced insn in user space.
...
I don't understand how this is bpf related, and if it is you don't explain it well in the commit message.
Thank you.
Quoted 8 lines · David Miller
From: Yonghong Song <[email protected]>
Date: Wed, 8 Nov 2017 13:37:12 -0800Quoted 5 lines
Uprobe is a tracing mechanism for userspace programs. Typical uprobe will incur overhead of two traps. First trap is caused by replaced trap insn, and the second trap is to execute the original displaced insn in user space.
Quoted 4 lines
...
I don't understand how this is bpf related, and if it is you don't explain it well in the commit message.
Right. This is not related to bpf. Will remove the "bpf" from the subject line in the next revision.
Quoted 3 lines
Thank you.
Quoted 9 lines · Yonghong Song
Quoted 8 lines · David Miller
From: Yonghong Song <[email protected]>
Date: Wed, 8 Nov 2017 13:37:12 -0800Quoted 5 lines
Uprobe is a tracing mechanism for userspace programs. Typical uprobe will incur overhead of two traps. First trap is caused by replaced trap insn, and the second trap is to execute the original displaced insn in user space.
Quoted 7 lines
Quoted 4 lines
...
I don't understand how this is bpf related, and if it is you don't explain it well in the commit message.
Right. This is not related to bpf. Will remove the "bpf" from the subject line in the next revision.
The proper subject is something like:
[PATCH] uprobes/x86: .......
which you can figure out by looking at the subsystem prefixes via
git log arch/x86/kernel/uprobes.c
Note, that it says [PATCH} and nothing else. That patch is a nice performance improvement, but certainly not x86/urgent material. x86/urgent is for bug and regression fixes.
Thanks,
tglx
Quoted 15 lines · Thomas Gleixner
Quoted 14 lines · Yonghong Song
Quoted 12 lines · David Miller
From: Yonghong Song <[email protected]>
Date: Wed, 8 Nov 2017 13:37:12 -0800Quoted 5 lines
Uprobe is a tracing mechanism for userspace programs. Typical uprobe will incur overhead of two traps. First trap is caused by replaced trap insn, and the second trap is to execute the original displaced insn in user space.
...
I don't understand how this is bpf related, and if it is you don't explain it well in the commit message.
Quoted 6 lines
Quoted 2 lines
Right. This is not related to bpf. Will remove the "bpf" from the subject line in the next revision.
The proper subject is something like:
[PATCH] uprobes/x86: .......
Thanks, Thomas,
I will fix the subject etc. Previously, I added x86/urgent as the branch
I did my test on top of it (similar to net-next). I will add that information in the comments and re-submit.
Quoted 13 lines
which you can figure out by looking at the subsystem prefixes via
git log
arch/x86/kernel/uprobes.cNote, that it says [PATCH} and nothing else. That patch is a nice performance improvement, but certainly not x86/urgent material. x86/urgent is for bug and regression fixes.
Thanks,
tglx