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

Unified · Split

4 files
  1. include/uapi/linux/bpf.h +7 −0
  2. kernel/bpf/helpers.c +3 −0
  3. kernel/trace/bpf_trace.c +19 −0
  4. tools/include/uapi/linux/bpf.h +7 −0
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
kernel/bpf/helpers.c+3−0
@@ -1327,6 +1327,7 @@ const struct bpf_func_proto bpf_probe_read_user_proto __weak; · 1326 unmodified lines
1327const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;1327const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
1328const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;1328const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
1329const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;1329const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
1330const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
1330 1331
1331const struct bpf_func_proto *1332const struct bpf_func_proto *
1332bpf_base_func_proto(enum bpf_func_id func_id)1333bpf_base_func_proto(enum bpf_func_id func_id)
@@ -1424,6 +1425,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) · 91 unmodified lines
1424 return &bpf_snprintf_btf_proto;1425 return &bpf_snprintf_btf_proto;
1425 case BPF_FUNC_snprintf:1426 case BPF_FUNC_snprintf:
1426 return &bpf_snprintf_proto;1427 return &bpf_snprintf_proto;
1428 case BPF_FUNC_task_pt_regs:
1429 return &bpf_task_pt_regs_proto;
1427 default:1430 default:
1428 return NULL;1431 return NULL;
1429 }1432 }
kernel/trace/bpf_trace.c+19−0
@@ -723,6 +723,23 @@ const struct bpf_func_proto bpf_get_current_task_btf_proto = { · 722 unmodified lines
723 .ret_btf_id = &btf_task_struct_ids[0],723 .ret_btf_id = &btf_task_struct_ids[0],
724};724};
725 725
726BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
727{
728 return (unsigned long) task_pt_regs(task);
729}
730
731BTF_ID_LIST(bpf_task_pt_regs_ids)
732BTF_ID(struct, pt_regs)
733
734const struct bpf_func_proto bpf_task_pt_regs_proto = {
735 .func = bpf_task_pt_regs,
736 .gpl_only = true,
737 .arg1_type = ARG_PTR_TO_BTF_ID,
738 .arg1_btf_id = &btf_task_struct_ids[0],
739 .ret_type = RET_PTR_TO_BTF_ID,
740 .ret_btf_id = &bpf_task_pt_regs_ids[0],
741};
742
726BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)743BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
727{744{
728 struct bpf_array *array = container_of(map, struct bpf_array, map);745 struct bpf_array *array = container_of(map, struct bpf_array, map);
@@ -1032,6 +1049,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 303 unmodified lines
1032 return &bpf_get_current_task_proto;1049 return &bpf_get_current_task_proto;
1033 case BPF_FUNC_get_current_task_btf:1050 case BPF_FUNC_get_current_task_btf:
1034 return &bpf_get_current_task_btf_proto;1051 return &bpf_get_current_task_btf_proto;
1052 case BPF_FUNC_task_pt_regs:
1053 return &bpf_task_pt_regs_proto;
1035 case BPF_FUNC_get_current_uid_gid:1054 case BPF_FUNC_get_current_uid_gid:
1036 return &bpf_get_current_uid_gid_proto;1055 return &bpf_get_current_uid_gid_proto;
1037 case BPF_FUNC_get_current_comm:1056 case BPF_FUNC_get_current_comm:
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