bpf: Add bpf_task_pt_regs() helper
Subsystems (7)
v2 cover 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
Changes from v1:
- Conwolidate BTF_ID decls for task_struct
- Enable bpf_get_current_task_btf() for all prog types
- Enable bpf_task_pt_regs() for all prog types
- Use ASSERT_* macros instead of CHECK
Daniel Xu (5):
bpf: Add BTF_ID_LIST_GLOBAL_SINGLE macro
bpf: Consolidate task_struct BTF_ID declarations
bpf: Extend bpf_base_func_proto helpers with
bpf_get_current_task_btf()
bpf: Add bpf_task_pt_regs() helper
bpf: selftests: Add bpf_task_pt_regs() selftest
include/linux/btf_ids.h | 5 ++ include/uapi/linux/bpf.h | 7 +++ kernel/bpf/bpf_task_storage.c | 6 +-- kernel/bpf/helpers.c | 6 +++ kernel/bpf/stackmap.c | 4 +- kernel/bpf/task_iter.c | 11 ++--- kernel/trace/bpf_trace.c | 25 ++++++++-- tools/include/uapi/linux/bpf.h | 7 +++ .../selftests/bpf/prog_tests/task_pt_regs.c | 47 +++++++++++++++++++ .../selftests/bpf/progs/test_task_pt_regs.c | 29 ++++++++++++ 10 files changed, 131 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_pt_regs.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_pt_regs.c
Signature · 1 line
2.33.0
PATCH v2 1/5 bpf: Add BTF_ID_LIST_GLOBAL_SINGLE macro
Same as BTF_ID_LIST_SINGLE macro except defines a global ID.
--- include/linux/btf_ids.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/btf_ids.h +3 -0
@@ -82,6 +82,9 @@ __BTF_ID_LIST(name, globl)#define BTF_ID_LIST_SINGLE (name, prefix, typename) \ BTF_ID_LIST(name) \ BTF_ID(prefix, typename) +#define BTF_ID_LIST_GLOBAL_SINGLE (name, prefix, typename) \ + BTF_ID_LIST_GLOBAL(name) \ + BTF_ID(prefix, typename)/* * The BTF_ID_UNUSED macro defines 4 zero bytes.
Signature · 1 line
2.33.0
PATCH v2 2/5 bpf: Consolidate task_struct BTF_ID declarations
In reply to Daniel XuNo need to have it defined 5 times. Once is enough.
--- include/linux/btf_ids.h | 2 ++ kernel/bpf/bpf_task_storage.c | 6 ++---- kernel/bpf/stackmap.c | 4 +--- kernel/bpf/task_iter.c | 11 +++++------ kernel/trace/bpf_trace.c | 4 ++-- 5 files changed, 12 insertions(+), 15 deletions(-)
include/linux/btf_ids.h +2 -0
@@ -188,4 +188,6 @@ MAX_BTF_SOCK_TYPE,extern u32 btf_sock_ids[];#endif +extern u32 btf_task_struct_ids[]; +#endif
kernel/bpf/bpf_task_storage.c +2 -4
@@ -317,15 +317,13 @@ const struct bpf_map_ops task_storage_map_ops = { .map_owner_storage_ptr = task_storage_ptr, }; -BTF_ID_LIST_SINGLE(bpf_task_storage_btf_ids, struct, task_struct) -const struct bpf_func_proto bpf_task_storage_get_proto = { .func = bpf_task_storage_get, .gpl_only = false, .ret_type =RET_PTR_TO_MAP_VALUE_OR_NULL , .arg1_type =ARG_CONST_MAP_PTR , .arg2_type =ARG_PTR_TO_BTF_ID , - .arg2_btf_id = &bpf_task_storage_btf_ids[0 ], + .arg2_btf_id = &btf_task_struct_ids[0 ], .arg3_type =ARG_PTR_TO_MAP_VALUE_OR_NULL , .arg4_type =ARG_ANYTHING , };
@@ -336,5 +334,5 @@ const struct bpf_func_proto bpf_task_storage_delete_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_CONST_MAP_PTR, .arg2_type = ARG_PTR_TO_BTF_ID, - .arg2_btf_id = &bpf_task_storage_btf_ids[0], + .arg2_btf_id = &btf_task_struct_ids[0], };
kernel/bpf/stackmap.c +1 -3
@@ -530,14 +530,12 @@ BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,return res; } -BTF_ID_LIST_SINGLE(bpf_get_task_stack_btf_ids, struct, task_struct) -const struct bpf_func_proto bpf_get_task_stack_proto = { .func = bpf_get_task_stack, .gpl_only = false, .ret_type =RET_INTEGER , .arg1_type =ARG_PTR_TO_BTF_ID , - .arg1_btf_id = &bpf_get_task_stack_btf_ids[0 ], + .arg1_btf_id = &btf_task_struct_ids[0 ], .arg2_type =ARG_PTR_TO_UNINIT_MEM , .arg3_type =ARG_CONST_SIZE_OR_ZERO , .arg4_type =ARG_ANYTHING ,
kernel/bpf/task_iter.c +5 -6
@@ -525,7 +525,6 @@ static const struct seq_operations task_vma_seq_ops = { }; BTF_ID_LIST(btf_task_file_ids) -BTF_ID(struct, task_struct) BTF_ID(struct, file) BTF_ID(struct, vm_area_struct)
@@ -591,19 +590,19 @@ static int __init task_iter_init(void) {int ret; - task_reg_info.ctx_arg_info[0 ].btf_id = btf_task_file_ids[0 ]; + task_reg_info.ctx_arg_info[0 ].btf_id = btf_task_struct_ids[0 ]; ret =bpf_iter_reg_target (&task_reg_info);if (ret)return ret; - task_file_reg_info.ctx_arg_info[0 ].btf_id = btf_task_file_ids[0 ]; - task_file_reg_info.ctx_arg_info[1 ].btf_id = btf_task_file_ids[1 ]; + task_file_reg_info.ctx_arg_info[0 ].btf_id = btf_task_struct_ids[0 ]; + task_file_reg_info.ctx_arg_info[1 ].btf_id = btf_task_file_ids[0 ]; ret =bpf_iter_reg_target (&task_file_reg_info);if (ret)return ret; - task_vma_reg_info.ctx_arg_info[0 ].btf_id = btf_task_file_ids[0 ]; - task_vma_reg_info.ctx_arg_info[1 ].btf_id = btf_task_file_ids[2 ]; + task_vma_reg_info.ctx_arg_info[0 ].btf_id = btf_task_struct_ids[0 ]; + task_vma_reg_info.ctx_arg_info[1 ].btf_id = btf_task_file_ids[1 ];return bpf_iter_reg_target (&task_vma_reg_info); }late_initcall (task_iter_init);
kernel/trace/bpf_trace.c +2 -2
@@ -714,13 +714,13 @@ BPF_CALL_0(bpf_get_current_task_btf)return (unsigned long ) current; } -BTF_ID_LIST_SINGLE(bpf_get_current_btf_ids, struct, task_struct) +BTF_ID_LIST_GLOBAL_SINGLE(btf_task_struct_ids, struct, task_struct)static const struct bpf_func_proto bpf_get_current_task_btf_proto = { .func = bpf_get_current_task_btf, .gpl_only = true, .ret_type =RET_PTR_TO_BTF_ID , - .ret_btf_id = &bpf_get_current_btf_ids[0 ], + .ret_btf_id = &btf_task_struct_ids[0 ], }; BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Signature · 1 line
2.33.0
PATCH v2 3/5 bpf: Extend bpf_base_func_proto helpers with bpf_get_current_task_btf()
In reply to Daniel Xubpf_get_current_task() is already supported so it's natural to also include the _btf() variant for btf-powered helpers.
This is required for non-tracing progs to use bpf_task_pt_regs() in the next commit.
--- kernel/bpf/helpers.c | 3 +++ kernel/trace/bpf_trace.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-)
kernel/bpf/helpers.c +3 -0
@@ -1322,6 +1322,7 @@ void bpf_timer_cancel_and_free(void *val) }const struct bpf_func_proto bpf_get_current_task_proto__weak ; +const struct bpf_func_proto bpf_get_current_task_btf_proto__weak ;const struct bpf_func_proto bpf_probe_read_user_proto__weak ;const struct bpf_func_proto bpf_probe_read_user_str_proto__weak ;const struct bpf_func_proto bpf_probe_read_kernel_proto__weak ;
@@ -1407,6 +1408,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)return bpf_get_trace_printk_proto ();case BPF_FUNC_get_current_task:return &bpf_get_current_task_proto; +case BPF_FUNC_get_current_task_btf: +return &bpf_get_current_task_btf_proto;case BPF_FUNC_probe_read_user:return &bpf_probe_read_user_proto;case BPF_FUNC_probe_read_kernel:
kernel/trace/bpf_trace.c +1 -1
@@ -716,7 +716,7 @@ BPF_CALL_0(bpf_get_current_task_btf) BTF_ID_LIST_GLOBAL_SINGLE(btf_task_struct_ids, struct, task_struct) -static const struct bpf_func_proto bpf_get_current_task_btf_proto = { +const struct bpf_func_proto bpf_get_current_task_btf_proto = { .func = bpf_get_current_task_btf, .gpl_only = true, .ret_type =RET_PTR_TO_BTF_ID ,
Signature · 1 line
2.33.0
PATCH v2 4/5 bpf: Add bpf_task_pt_regs() helper
In reply to Daniel XuThe 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
--- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/helpers.c | 3 +++ kernel/trace/bpf_trace.c | 19 +++++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 36 insertions(+)
include/uapi/linux/bpf.h +7 -0
@@ -4871,6 +4871,12 @@ union bpf_attr {* Return * Value specified by user at BPF link creation/attachment time * or 0, if it was not specified. +* +* long bpf_task_pt_regs(struct task_struct *task) +* Description +* Get the struct pt_regs associated with **task**. +* Return +* A pointer to struct pt_regs. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5048,6 +5054,7 @@ union bpf_attr {FN (timer_cancel), \FN (get_func_ip), \FN (get_attach_cookie), \ +FN (task_pt_regs), \/* */ /* 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;const struct bpf_func_proto bpf_probe_read_user_str_proto__weak ;const struct bpf_func_proto bpf_probe_read_kernel_proto__weak ;const struct bpf_func_proto bpf_probe_read_kernel_str_proto__weak ; +const struct bpf_func_proto bpf_task_pt_regs_proto__weak ;const struct bpf_func_proto *bpf_base_func_proto (enum bpf_func_id func_id)
@@ -1424,6 +1425,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_snprintf_btf_proto;case BPF_FUNC_snprintf:return &bpf_snprintf_proto; +case BPF_FUNC_task_pt_regs: +return &bpf_task_pt_regs_proto;default :return NULL ; }
kernel/trace/bpf_trace.c +19 -0
@@ -723,6 +723,23 @@ const struct bpf_func_proto bpf_get_current_task_btf_proto = { .ret_btf_id = &btf_task_struct_ids[0], }; +BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task) +{ +return (unsigned long )task_pt_regs (task); +} + +BTF_ID_LIST (bpf_task_pt_regs_ids) +BTF_ID(struct, pt_regs) + +const struct bpf_func_proto bpf_task_pt_regs_proto = { + .func = bpf_task_pt_regs, + .gpl_only = true, + .arg1_type =ARG_PTR_TO_BTF_ID , + .arg1_btf_id = &btf_task_struct_ids[0 ], + .ret_type =RET_PTR_TO_BTF_ID , + .ret_btf_id = &bpf_task_pt_regs_ids[0 ], +}; + BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) { 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)return &bpf_get_current_task_proto;case BPF_FUNC_get_current_task_btf:return &bpf_get_current_task_btf_proto; +case BPF_FUNC_task_pt_regs: +return &bpf_task_pt_regs_proto;case BPF_FUNC_get_current_uid_gid:return &bpf_get_current_uid_gid_proto;case BPF_FUNC_get_current_comm:
tools/include/uapi/linux/bpf.h +7 -0
@@ -4871,6 +4871,12 @@ union bpf_attr {* Return * Value specified by user at BPF link creation/attachment time * or 0, if it was not specified. +* +* long bpf_task_pt_regs(struct task_struct *task) +* Description +* Get the struct pt_regs associated with **task**. +* Return +* A pointer to struct pt_regs. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5048,6 +5054,7 @@ union bpf_attr {FN (timer_cancel), \FN (get_func_ip), \FN (get_attach_cookie), \ +FN (task_pt_regs), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.33.0
PATCH v2 5/5 bpf: selftests: Add bpf_task_pt_regs() selftest
In reply to Daniel XuThis test retrieves the uprobe's pt_regs in two different ways and compares the contents in an arch-agnostic way.
--- .../selftests/bpf/prog_tests/task_pt_regs.c | 47 +++++++++++++++++++ .../selftests/bpf/progs/test_task_pt_regs.c | 29 ++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_pt_regs.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_pt_regs.c
tools/testing/selftests/bpf/prog_tests/task_pt_regs.c new file +47 -0
@@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include <test_progs.h> +#include <linux/ptrace.h> +#include "test_task_pt_regs.skel.h" + +void test_task_pt_regs (void ) +{ +struct test_task_pt_regs *skel; +struct bpf_link *uprobe_link; +size_t uprobe_offset; +ssize_t base_addr; +bool match; + + base_addr =get_base_addr (); +if (!ASSERT_GT (base_addr,0 ,"get_base_addr" )) +return ; + uprobe_offset =get_uprobe_offset (&get_base_addr, base_addr); + + skel =test_task_pt_regs__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open" )) +return ; +if (!ASSERT_OK_PTR (skel->bss,"check_bss" )) + goto cleanup; + + uprobe_link =bpf_program__attach_uprobe (skel->progs.handle_uprobe, + false/* retprobe */ , +0 /* self pid */ , +"/proc/self/exe" , + uprobe_offset); +if (!ASSERT_OK_PTR (uprobe_link,"attach_uprobe" )) + goto cleanup; + skel->links.handle_uprobe = uprobe_link; + +/* trigger & validate uprobe */ +get_base_addr (); + +if (!ASSERT_EQ (skel->bss->uprobe_res,1 ,"check_uprobe_res" )) + goto cleanup; + + match = !memcmp (&skel->bss->current_regs, &skel->bss->ctx_regs, +sizeof (skel->bss->current_regs)); +ASSERT_TRUE (match,"check_regs_match" ); + +cleanup: +test_task_pt_regs__destroy (skel); +}
tools/testing/selftests/bpf/progs/test_task_pt_regs.c new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/ptrace.h> +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +struct pt_regs current_regs = {}; +struct pt_regs ctx_regs = {}; +int uprobe_res =0 ; + +SEC("uprobe/trigger_func") +int handle_uprobe (struct pt_regs *ctx) +{ +struct task_struct *current; +struct pt_regs *regs; + + current =bpf_get_current_task_btf (); + regs = (struct pt_regs *)bpf_task_pt_regs (current); +__builtin_memcpy (¤t_regs, regs,sizeof (*regs)); +__builtin_memcpy (&ctx_regs, ctx,sizeof (*ctx)); + +/* Prove that uprobe was run */ + uprobe_res =1 ; + +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.33.0