On Wed, Aug 9, 2023 at 4:44 AM Kumar Kartikeya Dwivedi <memxor@xxxxxxxxx> wrote: > > Add support to libbpf to append exception callbacks when loading a > program. The exception callback is found by discovering the declaration > tag 'exception_callback:<value>' and finding the callback in the value > of the tag. > > The process is done in two steps. First, for each main program, the > bpf_object__sanitize_and_load_btf function finds and marks its > corresponding exception callback as defined by the declaration tag on > it. Second, bpf_object__reloc_code is modified to append the indicated > exception callback at the end of the instruction iteration (since > exception callback will never be appended in that loop, as it is not > directly referenced). > > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@xxxxxxxxx> > --- Just two point before you send next version: a) it seems like this appending of exception callback logically fits bpf_object__relocate() step, where other subprogs are appended. Any reason we can't do it there? b) all the callbacks are static functions, right? Which means in the case of static linking, we can have multiple subprogs with the same name. So this whole look up by name thing doesn't guarantee unique match. At the very least libbpf should check that the match is unique and error out otherwise. Ideally though, would be great if something like this would be supported instead (but I know it's way more complex, Alexei already mentioned that in person and on the list): try (my_callback) { ... code that throws ... } try (my_other_callback) { ... some other code that throws ... } This try() macro can be implemented in a form similar to bpf_for() by using fancy for() loop. It would look and feel way more like try/catch. > tools/lib/bpf/libbpf.c | 166 +++++++++++++++++++++++++++++++++++------ > 1 file changed, 142 insertions(+), 24 deletions(-) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index 17883f5a44b9..7c607bac8204 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -432,9 +432,11 @@ struct bpf_program { > int fd; > bool autoload; > bool autoattach; > + bool sym_global; > bool mark_btf_static; > enum bpf_prog_type type; > enum bpf_attach_type expected_attach_type; > + int exception_cb_idx; > > int prog_ifindex; > __u32 attach_btf_obj_fd; [...]