On Fri, Nov 4, 2022 at 1:09 AM Kumar Kartikeya Dwivedi <memxor@xxxxxxxxx> wrote: > > On Fri, Nov 04, 2022 at 08:07:25AM IST, Dave Marchevsky wrote: > > On 11/3/22 3:10 PM, Kumar Kartikeya Dwivedi wrote: > > > Introduce type safe memory allocator bpf_obj_new for BPF programs. The > > > kernel side kfunc is named bpf_obj_new_impl, as passing hidden arguments > > > to kfuncs still requires having them in prototype, unlike BPF helpers > > > which always take 5 arguments and have them checked using bpf_func_proto > > > in verifier, ignoring unset argument types. > > > > > > Introduce __ign suffix to ignore a specific kfunc argument during type > > > checks, then use this to introduce support for passing type metadata to > > > the bpf_obj_new_impl kfunc. > > > > > > The user passes BTF ID of the type it wants to allocates in program BTF, > > > the verifier then rewrites the first argument as the size of this type, > > > after performing some sanity checks (to ensure it exists and it is a > > > struct type). > > > > > > The second argument is also fixed up and passed by the verifier. This is > > > the btf_struct_meta for the type being allocated. It would be needed > > > mostly for the offset array which is required for zero initializing > > > special fields while leaving the rest of storage in unitialized state. > > > > > > It would also be needed in the next patch to perform proper destruction > > > of the object's special fields. > > > > > > A convenience macro is included in the bpf_experimental.h header to hide > > > over the ugly details of the implementation, leading to user code > > > looking similar to a language level extension which allocates and > > > constructs fields of a user type. > > > > > > struct bar { > > > struct bpf_list_node node; > > > }; > > > > > > struct foo { > > > struct bpf_spin_lock lock; > > > struct bpf_list_head head __contains(bar, node); > > > }; > > > > > > void prog(void) { > > > struct foo *f; > > > > > > f = bpf_obj_new(typeof(*f)); > > > if (!f) > > > return; > > > ... > > > } > > > > > > A key piece of this story is still missing, i.e. the free function, > > > which will come in the next patch. > > > > > > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@xxxxxxxxx> > > > --- > > > > [...] > > > > > diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h > > > new file mode 100644 > > > index 000000000000..1d3451084a68 > > > --- /dev/null > > > +++ b/tools/testing/selftests/bpf/bpf_experimental.h > > > > Maybe bpf_experimental.h should go in libbpf as part of this series? If > > including vmlinux.h is an issue - nothing in libbpf currently includes it - you > > could rely on the BPF program including it, with a comment similar to "Note > > that bpf programs need to include..." in lib/bpf/bpf_helpers.h . > > > > I don't have a problem with that, but I would like to also know Andrii's opinion > on this, since it won't work properly if people don't keep libbpf and kernel > version in sync. let's do the directory bikeshedding later. bpf_experimental.h can be moved to libbpf dir in the follow up.