On Mon, May 9, 2022 at 3:44 PM Joanne Koong <joannelkoong@xxxxxxxxx> wrote: > > This patch adds the bulk of the verifier work for supporting dynamic > pointers (dynptrs) in bpf. This patch implements malloc-type dynptrs > through 2 new APIs (bpf_dynptr_alloc and bpf_dynptr_put) that can be > called by a bpf program. Malloc-type dynptrs are dynptrs that dynamically > allocate memory on behalf of the program. > > A bpf_dynptr is opaque to the bpf program. It is a 16-byte structure > defined internally as: > > struct bpf_dynptr_kern { > void *data; > u32 size; > u32 offset; > } __aligned(8); > > The upper 8 bits of *size* is reserved (it contains extra metadata about > read-only status and dynptr type); consequently, a dynptr only supports > memory less than 16 MB. > > The 2 new APIs for malloc-type dynptrs are: > > long bpf_dynptr_alloc(u32 size, u64 flags, struct bpf_dynptr *ptr); > void bpf_dynptr_put(struct bpf_dynptr *ptr); > > Please note that there *must* be a corresponding bpf_dynptr_put for > every bpf_dynptr_alloc (even if the alloc fails). This is enforced > by the verifier. > > In the verifier, dynptr state information will be tracked in stack > slots. When the program passes in an uninitialized dynptr > (ARG_PTR_TO_DYNPTR | MEM_UNINIT), the stack slots corresponding > to the frame pointer where the dynptr resides at are marked STACK_DYNPTR. > > For helper functions that take in initialized dynptrs (eg > bpf_dynptr_read + bpf_dynptr_write which are added later in this > patchset), the verifier enforces that the dynptr has been initialized > properly by checking that their corresponding stack slots have been marked > as STACK_DYNPTR. Dynptr release functions (eg bpf_dynptr_put) will clear > the stack slots. The verifier enforces at program exit that there are no > referenced dynptrs that haven't been released. > > Signed-off-by: Joanne Koong <joannelkoong@xxxxxxxxx> > --- > include/linux/bpf.h | 62 ++++++++- > include/linux/bpf_verifier.h | 21 +++ > include/uapi/linux/bpf.h | 30 +++++ > kernel/bpf/helpers.c | 75 +++++++++++ > kernel/bpf/verifier.c | 228 ++++++++++++++++++++++++++++++++- > scripts/bpf_doc.py | 2 + > tools/include/uapi/linux/bpf.h | 30 +++++ > 7 files changed, 445 insertions(+), 3 deletions(-) > Apart from what Daniel and Alexei are discussing, LGTM Acked-by: Andrii Nakryiko <andrii@xxxxxxxxxx> [...]