History

bpf: Add bpf_task_pt_regs() helper

The motivation behind this helper is to access userspace pt_regs in a kprobe handler. uprobe's ctx is the userspace pt_regs. kprobe's ctx is the kernelspace pt_regs. bpf_task_pt_regs() allows accessing userspace pt_regs in a kprobe handler. The final case (kernelspace pt_regs in uprobe) is pretty rare (usermode helper) so I think that can be solved later if necessary. More concretely, this helper is useful in doing BPF-based DWARF stack unwinding. Currently the kernel can only do framepointer based stack unwinds for userspace code. This is because the DWARF state machines are too fragile to be computed in kernelspace [0]. The idea behind DWARF-based stack unwinds w/ BPF is to copy a chunk of the userspace stack (while in prog context) and send it up to userspace for unwinding (probably with libunwind) [1]. This would effectively enable profiling applications with -fomit-frame-pointer using kprobes and uprobes. [0]: https://lkml.org/lkml/2012/2/10/356 [1]: https://github.com/danobi/bpf-dwarf-walk

Daniel Xu committed 5 years ago

Commit dd6e10f1 parent a396eda4 files · +36 −0Browse files

Showing the changes to tools/include/uapi/linux (1 of 4 files). Show all

Unified · Split

tools/include/uapi/linux/bpf.h+7−0
@@ -4871,6 +4871,12 @@ union bpf_attr { · 4870 unmodified lines
4871 * Return4871 * Return
4872 * Value specified by user at BPF link creation/attachment time4872 * Value specified by user at BPF link creation/attachment time
4873 * or 0, if it was not specified.4873 * or 0, if it was not specified.
4874 *
4875 * long bpf_task_pt_regs(struct task_struct *task)
4876 * Description
4877 * Get the struct pt_regs associated with **task**.
4878 * Return
4879 * A pointer to struct pt_regs.
4874 */4880 */
4875#define __BPF_FUNC_MAPPER(FN) \4881#define __BPF_FUNC_MAPPER(FN) \
4876 FN(unspec), \4882 FN(unspec), \
@@ -5048,6 +5054,7 @@ union bpf_attr { · 171 unmodified lines
5048 FN(timer_cancel), \5054 FN(timer_cancel), \
5049 FN(get_func_ip), \5055 FN(get_func_ip), \
5050 FN(get_attach_cookie), \5056 FN(get_attach_cookie), \
5057 FN(task_pt_regs), \
5051 /* */5058 /* */
5052 5059
5053/* integer value in 'imm' field of BPF_CALL instruction selects which helper5060/* integer value in 'imm' field of BPF_CALL instruction selects which helper