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
48714871 * Return
48724872 * Value specified by user at BPF link creation/attachment time
48734873 * 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.
48744880 */
48754881 #define __BPF_FUNC_MAPPER(FN) \
48764882 FN(unspec), \
@@ -5048,6 +5054,7 @@ union bpf_attr { · 171 unmodified lines
50485054 FN(timer_cancel), \
50495055 FN(get_func_ip), \
50505056 FN(get_attach_cookie), \
5057+ FN(task_pt_regs), \
50515058 /* */
50525059
50535060 /* integer value in 'imm' field of BPF_CALL instruction selects which helper