-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why does linux profiling add NOP instruction before a function? #113
Comments
Thank you for the feedback, I will try to make the page clearer.
The reason to have the NOP instructions is so you can replace them. When they are added your assembly might look something like:
The NOPs are ignored by the CPU. So if we never touch them, it is as if they were never there. But when you attach a fentry/fexit program the following happens:
We replaced the NOPs, and will call into
Yes, correct. This flag controls if the NOPs are added or not. Without the NOPs, program types that depend on trampolines will not work.
A kprobe actually does modify the CPU instructions at runtime as well, just different. Lets take our example:
A kprobe replaces an actual instruction part of the function, not a NOP like so:
When this INT3 instruction (on x86) gets called, it triggers a CPU interrupt. So the CPU jumps to an interrupt service routine. Which copies all of the CPU state into memory (pt_regs). It then calls your kprobe program. It then copies back most of the state into the CPU. The kprobe system remembered which instruction it replaced and will execute that instruction now. It then jumps back the next instruction in the program we originally came from. The copying of all of the CPU state back and forth is why kprobes are more expensive. The tramlines only add the overhead of a function call and its glue logic which is way less. |
I find the explanation regarding the eBPF trampolines (https://docs.ebpf.io/linux/concepts/trampolines/) a bit confusing because it is not stated why the NOP instructions are there in the first place. Why does profiling require to have additional NOP instructions before the begnining of all kernel functions.
To be able to use fentry/fexit programs, is it required to compile the kernel using the
CONFIG_FUNCTION_TRACER
flag?Since kprobe does not modify the kernel source code at runtime, but solely when the eBPF program is loaded into the kernel, then why is it considered to result in more overhead than fentry/fexit?
The text was updated successfully, but these errors were encountered: