bpf: cleanup BTF-enabled raw_tp
Subsystems (8)
Alexei Starovoitov
maintainerv2 cover bpf: cleanup BTF-enabled raw_tp
v1->v2: addressed Andrii's feedback
When BTF-enabled raw_tp were introduced the plan was to follow up with BTF-enabled kprobe and kretprobe reusing PROG_RAW_TRACEPOINT and PROG_KPROBE types. But k[ret]probe expect pt_regs while BTF-enabled program ctx will be the same as raw_tp. kretprobe is indistinguishable from kprobe while BTF-enabled kretprobe will have access to retval while kprobe will not. Hence PROG_KPROBE type is not reusable and reusing PROG_RAW_TRACEPOINT no longer fits well.
Hence introduce 'umbrella' prog type BPF_PROG_TYPE_TRACING that will cover different BTF-enabled tracing attach points. The changes make libbpf side cleaner as well. check_attach_btf_id() is cleaner too.
Alexei Starovoitov (2):
bpf: replace prog_raw_tp+btf_id with prog_tracing
libbpf: add support for prog_tracing
include/linux/bpf.h | 5 +++ include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 2 + kernel/bpf/syscall.c | 6 +-- kernel/bpf/verifier.c | 34 ++++++++++----- kernel/trace/bpf_trace.c | 44 +++++++++++++++---- tools/include/uapi/linux/bpf.h | 2 + tools/lib/bpf/bpf.c | 8 ++-- tools/lib/bpf/bpf.h | 5 ++- tools/lib/bpf/libbpf.c | 79 ++++++++++++++++++++++++---------- tools/lib/bpf/libbpf.h | 2 + tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/libbpf_probes.c | 1 + 13 files changed, 142 insertions(+), 49 deletions(-)
Signature · 1 line
2.17.1
Alexei Starovoitov
maintainerPATCH v2 1/2 bpf: replace prog_raw_tp+btf_id with prog_tracing
The bpf program type raw_tp together with 'expected_attach_type' was the most appropriate api to indicate BTF-enabled raw_tp programs. But during development it became apparent that 'expected_attach_type' cannot be used and new 'attach_btf_id' field had to be introduced. Which means that the information is duplicated in two fields where one of them is ignored.
Clean it up by introducing new program type where both 'expected_attach_type' and 'attach_btf_id' fields have specific meaning.
In the future 'expected_attach_type' will be extended
with other attach points that have similar semantics to raw_tp. This patch is replacing BTF-enabled BPF_PROG_TYPE_RAW_TRACEPOINT with prog_type = BPF_RPOG_TYPE_TRACINGexpected_attach_type = BPF_TRACE_RAW_TPattach_btf_id = btf_id of raw tracepoint inside the kernel Future patches will addexpected_attach_type = BPF_TRACE_FENTRY or BPF_TRACE_FEXIT where programs have the same input context and the same helpers, but different attach points.
--- include/linux/bpf.h | 5 +++++ include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 2 ++ kernel/bpf/syscall.c | 6 +++--- kernel/bpf/verifier.c | 34 +++++++++++++++++++++--------- kernel/trace/bpf_trace.c | 44 ++++++++++++++++++++++++++++++++------- 6 files changed, 71 insertions(+), 21 deletions(-)
include/linux/bpf.h +5 -0
@@ -373,6 +373,11 @@ enum bpf_cgroup_storage_type {#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX +/* The longest tracepoint has 12 args. +* See include/trace/bpf_probe.h +*/ +#define MAX_BPF_FUNC_ARGS 12 +struct bpf_prog_stats {u64 cnt;u64 nsecs;
include/linux/bpf_types.h +1 -0
@@ -26,6 +26,7 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_TRACEPOINT, tracepoint) BPF_PROG_TYPE(BPF_PROG_TYPE_PERF_EVENT, perf_event) BPF_PROG_TYPE(BPF_PROG_TYPE_RAW_TRACEPOINT, raw_tracepoint) BPF_PROG_TYPE(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, raw_tracepoint_writable) +BPF_PROG_TYPE(BPF_PROG_TYPE_TRACING, tracing)#endif #ifdef CONFIG_CGROUP_BPF BPF_PROG_TYPE (BPF_PROG_TYPE_CGROUP_DEVICE , cg_dev)
include/uapi/linux/bpf.h +2 -0
@@ -173,6 +173,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_CGROUP_SYSCTL ,BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE ,BPF_PROG_TYPE_CGROUP_SOCKOPT , + BPF_PROG_TYPE_TRACING, }; enum bpf_attach_type {
@@ -199,6 +200,7 @@ enum bpf_attach_type {BPF_CGROUP_UDP6_RECVMSG ,BPF_CGROUP_GETSOCKOPT ,BPF_CGROUP_SETSOCKOPT , +BPF_TRACE_RAW_TP , __MAX_BPF_ATTACH_TYPE };
kernel/bpf/syscall.c +3 -3
@@ -1571,7 +1571,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type, u32 btf_id) {switch (prog_type) { -case BPF_PROG_TYPE_RAW_TRACEPOINT : +case BPF_PROG_TYPE_TRACING :if (btf_id >BTF_MAX_TYPE )return -EINVAL ;break ;
@@ -1833,13 +1833,13 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)return PTR_ERR (prog);if (prog->type !=BPF_PROG_TYPE_RAW_TRACEPOINT && + prog->type !=BPF_PROG_TYPE_TRACING && prog->type !=BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE ) { err = -EINVAL ; goto out_put_prog; } -if (prog->type ==BPF_PROG_TYPE_RAW_TRACEPOINT && - prog->aux->attach_btf_id) { +if (prog->type ==BPF_PROG_TYPE_TRACING ) {if (attr->raw_tracepoint.name) {/* raw_tp name should not be specified in raw_tp * programs that were verified via in-kernel BTF info
kernel/bpf/verifier.c +24 -10
@@ -9381,24 +9381,36 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) {struct bpf_prog *prog = env->prog;u32 btf_id = prog->aux->attach_btf_id; +const char prefix[] ="btf_trace_" ;const struct btf_type *t;const char *tname; -if (prog->type ==BPF_PROG_TYPE_RAW_TRACEPOINT && btf_id) { -const char prefix[] ="btf_trace_" ; +if (prog->type !=BPF_PROG_TYPE_TRACING ) +return 0 ; - t =btf_type_by_id (btf_vmlinux, btf_id); -if (!t) { -verbose (env,"attach_btf_id %u is invalid\n" , btf_id); -return -EINVAL ; - } +if (!btf_id) { +verbose (env,"Tracing programs must provide btf_id\n" ); +return -EINVAL ; + } + t =btf_type_by_id (btf_vmlinux, btf_id); +if (!t) { +verbose (env,"attach_btf_id %u is invalid\n" , btf_id); +return -EINVAL ; + } + tname =btf_name_by_offset (btf_vmlinux, t->name_off); +if (!tname) { +verbose (env,"attach_btf_id %u doesn't have a name\n" , btf_id); +return -EINVAL ; + } + +switch (prog->expected_attach_type) { +case BPF_TRACE_RAW_TP :if (!btf_type_is_typedef (t)) {verbose (env,"attach_btf_id %u is not a typedef\n" , btf_id);return -EINVAL ; } - tname =btf_name_by_offset (btf_vmlinux, t->name_off); -if (!tname ||strncmp (prefix, tname,sizeof (prefix) -1 )) { +if (strncmp (prefix, tname,sizeof (prefix) -1 )) {verbose (env,"attach_btf_id %u points to wrong type name %s\n" , btf_id, tname);return -EINVAL ;
@@ -9419,8 +9431,10 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) prog->aux->attach_func_name = tname; prog->aux->attach_func_proto = t; prog->aux->attach_btf_trace = true; +return 0 ; +default : +return -EINVAL ; } -return 0 ; } int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
kernel/trace/bpf_trace.c +36 -8
72 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v2 bpf-next 1/2] bpf: replace prog_raw_tp+btf_id with prog_tracing
Quoted 11 lines · Alexei Starovoitov
The bpf program type
raw_tptogether with 'expected_attach_type' was the most appropriate api to indicate BTF-enabledraw_tpprograms. But during development it became apparent that 'expected_attach_type' cannot be used and new 'attach_btf_id' field had to be introduced. Which means that the information is duplicated in two fields where one of them is ignored.
Clean it up by introducing new program type where both 'expected_attach_type' and 'attach_btf_id' fields have specific meaning.
In the future 'expected_attach_type' will be extended
with other attach points that have similar semantics toraw_tp.
Quoted 8 lines
This patch is replacing BTF-enabled
BPF_PROG_TYPE_RAW_TRACEPOINTwithprog_type=BPF_RPOG_TYPE_TRACINGexpected_attach_type=BPF_TRACE_RAW_TPattach_btf_id=btf_idof raw tracepoint inside the kernel Future patches will addexpected_attach_type=BPF_TRACE_FENTRYorBPF_TRACE_FEXITwhere programs have the same input context and the same helpers, but different attach points.
Daniel Borkmann
maintainerRe: [PATCH v2 bpf-next 1/2] bpf: replace prog_raw_tp+btf_id with prog_tracing
In reply to Alexei StarovoitovQuoted 9 lines · Alexei Starovoitov
The bpf program type
raw_tptogether with 'expected_attach_type' was the most appropriate api to indicate BTF-enabledraw_tpprograms. But during development it became apparent that 'expected_attach_type' cannot be used and new 'attach_btf_id' field had to be introduced. Which means that the information is duplicated in two fields where one of them is ignored.
Clean it up by introducing new program type where both 'expected_attach_type' and 'attach_btf_id' fields have specific meaning.
Hm, just for my understanding, the expected_attach_type is unused for tracing so far. Are you aware of anyone (bcc / bpftrace / etc) leaving uninitialized garbage in there? Just seems confusing that we have all the different tracing prog types and now adding yet another one as BPF_RPOG_TYPE_TRACING which will act as umbrella one and again have different attach types some of which probably resemble existing tracing prog types again (kprobes / kretprobes for example). Sounds like this new type would implicitly deprecate all the existing types (sort of as we're replacing them with new sub-types)?
True that k[ret]probe expects pt_regs whereas BTF enabled program context will be the same as raw_tp as well, but couldn't this logic be hidden in the kernel e.g. via attach_btf_id as well since this is an opt-in? Could the fentry/fexit be described through attach_btf_id as well?
Quoted 11 lines
In the future '
expected_attach_type' will be extended
with other attach points that have similar semantics toraw_tp. This patch is replacing BTF-enabledBPF_PROG_TYPE_RAW_TRACEPOINTwithprog_type=BPF_RPOG_TYPE_TRACINGexpected_attach_type=BPF_TRACE_RAW_TPattach_btf_id=btf_idof raw tracepoint inside the kernel Future patches will addexpected_attach_type=BPF_TRACE_FENTRYorBPF_TRACE_FEXITwhere programs have the same input context and the same helpers, but different attach points.
Quoted 2 lines
Signed-off-by Alexei Starovoitov <[email protected]>Acked-by Andrii Nakryiko <[email protected]>
Thanks,
Daniel
Re: [PATCH v2 bpf-next 1/2] bpf: replace prog_raw_tp+btf_id with prog_tracing
Quoted 11 lines · Daniel Borkmann
Quoted 9 lines · Alexei Starovoitov
The bpf program type
raw_tptogether with 'expected_attach_type' was the most appropriate api to indicate BTF-enabledraw_tpprograms. But during development it became apparent that 'expected_attach_type' cannot be used and new 'attach_btf_id' field had to be introduced. Which means that the information is duplicated in two fields where one of them is ignored.
Clean it up by introducing new program type where both 'expected_attach_type' and 'attach_btf_id' fields have specific meaning.
Quoted 3 lines
Hm, just for my understanding, the
expected_attach_typeis unused for tracing so far. Are you aware of anyone (bcc / bpftrace / etc) leaving uninitialized garbage in there?
I'm not aware, but the risk is there. Better safe than sorry. If we need to revert in the future that would be massive. I'm already worried about new CHECK_ATTR check in raw_tp_open. Equally unlikely user space breakage, but that one is easy to revert whereas what you're proposing would mean revert everything.
Quoted 7 lines
Just seems confusing that we have all
the different tracing prog types and now adding yet another one asBPF_RPOG_TYPE_TRACINGwhich will act as umbrella one and again have different attach types some of which probably resemble existing tracing prog types again (kprobes / kretprobes for example). Sounds like this new type would implicitly deprecate all the existing types (sort of as we're replacing them with new sub-types)?
All existing once are still supported and may grow its own helpers and what not. Having new prog type makes things grow independently much easier.
I was thinking to call it BPF_PROG_TYPE_BTF_ENABLED or BPF_PROG_TYPE_GENERIC, since I suspect upcoming lsm and others will fit right in,
but I think it's cleaner to define categories of bpf programs now instead of specific purpose types like we had in the past before BTF.
Quoted 4 lines
True that k[ret]probe expects
pt_regswhereas BTF enabled program context will be the same asraw_tpas well, but couldn't this logic be hidden in the kernel e.g. viaattach_btf_idas well since this is an opt-in? Could the fentry/fexit be described throughattach_btf_idas well?
That's what I tried first, but the code grows too ugly. Also for attaching fentry/fexit I'm adding new bpf_trace_open command similar to bpf_raw_tp_open, since existing kprobe attach style doesn't work at all. imo the code is much cleaner now.
I guess it's hard to judge, since you haven't seen my first version hacking into existing types that I quickly discarded due to messy and hard to read code logic.
Daniel Borkmann
maintainerRe: [PATCH v2 bpf-next 1/2] bpf: replace prog_raw_tp+btf_id with prog_tracing
Quoted 16 lines · Alexei Starovoitov
Quoted 11 lines · Daniel Borkmann
Quoted 9 lines · Alexei Starovoitov
The bpf program type
raw_tptogether with 'expected_attach_type' was the most appropriate api to indicate BTF-enabledraw_tpprograms. But during development it became apparent that 'expected_attach_type' cannot be used and new 'attach_btf_id' field had to be introduced. Which means that the information is duplicated in two fields where one of them is ignored.
Clean it up by introducing new program type where both 'expected_attach_type' and 'attach_btf_id' fields have specific meaning.Quoted 3 lines
Hm, just for my understanding, the
expected_attach_typeis unused for tracing so far. Are you aware of anyone (bcc / bpftrace / etc) leaving uninitialized garbage in there?
Quoted 5 lines
I'm not aware, but the risk is there. Better safe than sorry. If we need to revert in the future that would be massive. I'm already worried about new
CHECK_ATTRcheck inraw_tp_open. Equally unlikely user space breakage, but that one is easy to revert whereas what you're proposing would mean revert everything.
Hmm, yeah, it's unfortunate that expected_attach_type is not enforced as
0. We sort of implicitly do for older kernels where expected_attach_type is not known to it, but there is still non-zero risk given there seems plenty of stuff in the wild on BPF tracing and not all might rely on libbpf. Perhaps we should probably enforce it for all new program types, so we can reuse it in future.
Quoted 8 lines
Quoted 7 lines
Just seems confusing that we have all
the different tracing prog types and now adding yet another one asBPF_RPOG_TYPE_TRACINGwhich will act as umbrella one and again have different attach types some of which probably resemble existing tracing prog types again (kprobes / kretprobes for example). Sounds like this new type would implicitly deprecate all the existing types (sort of as we're replacing them with new sub-types)?
Quoted 6 lines
All existing once are still supported and may grow its own helpers and what not. Having new prog type makes things grow independently much easier.
I was thinking to call itBPF_PROG_TYPE_BTF_ENABLEDorBPF_PROG_TYPE_GENERIC, since I suspect upcoming lsm and others will fit right in,
but I think it's cleaner to define categories of bpf programs now instead of specific purpose types like we had in the past before BTF.
Yes, otherwise we likely would have BPF_PROG_TYPE_GENERIC as last one and would keep defining sub-types. ;)
Quoted 9 lines
Quoted 4 lines
True that k[ret]probe expects
pt_regswhereas BTF enabled program context will be the same asraw_tpas well, but couldn't this logic be hidden in the kernel e.g. viaattach_btf_idas well since this is an opt-in? Could the fentry/fexit be described throughattach_btf_idas well?That's what I tried first, but the code grows too ugly. Also for attaching fentry/fexit I'm adding new
bpf_trace_opencommand similar tobpf_raw_tp_open, since existing kprobe attach style doesn't work at all. imo the code is much cleaner now.
Ok, fair enough that the fentry/fexit approach doesn't have too much in common after all with plain kprobes. Applied then, thanks!
Alexei Starovoitov
maintainerPATCH v2 2/2 libbpf: add support for prog_tracing
In reply to Alexei StarovoitovCleanup libbpf from expected_attach_type == attach_btf_id hack and introduce BPF_PROG_TYPE_TRACING.
--- tools/include/uapi/linux/bpf.h | 2 + tools/lib/bpf/bpf.c | 8 ++-- tools/lib/bpf/bpf.h | 5 ++- tools/lib/bpf/libbpf.c | 79 ++++++++++++++++++++++++---------- tools/lib/bpf/libbpf.h | 2 + tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/libbpf_probes.c | 1 + 7 files changed, 71 insertions(+), 28 deletions(-)
tools/include/uapi/linux/bpf.h +2 -0
@@ -173,6 +173,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_CGROUP_SYSCTL ,BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE ,BPF_PROG_TYPE_CGROUP_SOCKOPT , + BPF_PROG_TYPE_TRACING, }; enum bpf_attach_type {
@@ -199,6 +200,7 @@ enum bpf_attach_type {BPF_CGROUP_UDP6_RECVMSG ,BPF_CGROUP_GETSOCKOPT ,BPF_CGROUP_SETSOCKOPT , +BPF_TRACE_RAW_TP , __MAX_BPF_ATTACH_TYPE };
tools/lib/bpf/bpf.c +4 -4
@@ -228,9 +228,10 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,memset (&attr,0 ,sizeof (attr)); attr.prog_type = load_attr->prog_type; attr.expected_attach_type = load_attr->expected_attach_type; -if (attr.prog_type ==BPF_PROG_TYPE_RAW_TRACEPOINT ) -/* expected_attach_type is ignored for tracing progs */ - attr.attach_btf_id = attr.expected_attach_type; +if (attr.prog_type ==BPF_PROG_TYPE_TRACING ) + attr.attach_btf_id = load_attr->attach_btf_id; +else + attr.prog_ifindex = load_attr->prog_ifindex; attr.insn_cnt = (__u32 )load_attr->insns_cnt; attr.insns =ptr_to_u64 (load_attr->insns); attr.license =ptr_to_u64 (load_attr->license);
@@ -245,7 +246,6 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, } attr.kern_version = load_attr->kern_version; - attr.prog_ifindex = load_attr->prog_ifindex; attr.prog_btf_fd = load_attr->prog_btf_fd; attr.func_info_rec_size = load_attr->func_info_rec_size; attr.func_info_cnt = load_attr->func_info_cnt;
tools/lib/bpf/bpf.h +4 -1
@@ -78,7 +78,10 @@ struct bpf_load_program_attr {size_t insns_cnt;const char *license;__u32 kern_version; -__u32 prog_ifindex; +union { +__u32 prog_ifindex; +__u32 attach_btf_id; + };__u32 prog_btf_fd;__u32 func_info_rec_size;const void *func_info;
tools/lib/bpf/libbpf.c +56 -23
139 lines not on this page · raw message
tools/lib/bpf/libbpf.h +2 -0
@@ -307,6 +307,7 @@ LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog); LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog); LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog); LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog); +LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog); LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog); LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
@@ -326,6 +327,7 @@ LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog); +LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);/* * No need for __attribute__((packed)), all members of 'bpf_map_def'
tools/lib/bpf/libbpf.map +2 -0
@@ -197,4 +197,6 @@ LIBBPF_0.0.6 { bpf_object__open_mem; bpf_program__get_expected_attach_type; bpf_program__get_type; + bpf_program__is_tracing; + bpf_program__set_tracing; } LIBBPF_0.0.5;
tools/lib/bpf/libbpf_probes.c +1 -0
@@ -102,6 +102,7 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,case BPF_PROG_TYPE_FLOW_DISSECTOR :case BPF_PROG_TYPE_CGROUP_SYSCTL :case BPF_PROG_TYPE_CGROUP_SOCKOPT : +case BPF_PROG_TYPE_TRACING :default :break ; }
Signature · 1 line
2.17.1
Re: [PATCH v2 bpf-next 2/2] libbpf: add support for prog_tracing
Quoted 2 lines · Alexei Starovoitov
Cleanup libbpf from
expected_attach_type==attach_btf_idhack and introduceBPF_PROG_TYPE_TRACING.