Re: [PATCH bpf-next v1 3/7] bpf: Add bpf_dynptr_from_mem, bpf_malloc, bpf_free

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Apr 8, 2022 at 6:12 PM Alexei Starovoitov
<alexei.starovoitov@xxxxxxxxx> wrote:
>
> On Fri, Apr 08, 2022 at 04:37:02PM -0700, Joanne Koong wrote:
> > > > > > + *
> > > > > > + * void bpf_free(struct bpf_dynptr *ptr)
> > > > >
> > > > > thinking about the next patch set that will add storing this malloc
> > > > > dynptr into the map, bpf_free() will be a lie, right? As it will only
> > > > > decrement a refcnt, not necessarily free it, right? So maybe just
> > > > > generic bpf_dynptr_put() or bpf_malloc_put() or something like that is
> > > > > a bit more "truthful"?
> > > > I like the simplicity of bpf_free(), but I can see how that might be
> > > > confusing. What are your thoughts on "bpf_dynptr_free()"? Since when
> > > > we get into dynptrs that are stored in maps vs. dynptrs stored
> > > > locally, calling bpf_dynptr_free() frees (invalidates) your local
> > > > dynptr even if it doesn't free the underlying memory if it still has
> > > > valid refcounts on it? To me, "malloc" and "_free" go more intuitively
> > > > together as a pair.
> > >
> > > Sounds good to me (though let's use _dynptr() as a suffix
> > > consistently). I also just realized that maybe we should call
> > > bpf_malloc() a bpf_malloc_dynptr() instead. I can see how we might
> > > want to enable plain bpf_malloc() with statically known size (similar
> > > to statically known bpf_ringbuf_reserve()) for temporary local malloc
> > > with direct memory access? So bpf_malloc_dynptr() would be a
> > > dynptr-enabled counterpart to fixed-sized bpf_malloc()? And then
> > > bpf_free() will work with direct pointer returned from bpf_malloc(),
> > > while bpf_free_dynptr() will work with dynptr returned from
> > > bpf_malloc_dynptr().
> > I see! What is the advantage of a plain bpf_malloc()? Is it that it's
> > a more ergonomic API (you get back a direct pointer to the data
> > instead of getting back a dynptr and then having to call
> > bpf_dynptr_data to get direct access) and you don't have to allocate
> > extra bytes for refcounting?
> >
> > I will rename this to bpf_malloc_dynptr() and bpf_free_dynptr().
>
> Let's make it consistent with kptr. Those helpers will be:
> bpf_kptr_alloc(btf_id, flags, &ptr)
> bpf_kptr_get
> bpf_kptr_put
>
> bpf_dynptr_alloc(byte_size, flags, &dynptr);

I don't have strong feelings about this naming, but
bpf_ringbuf_reserve_dynptr() is a bit of counter-example with a
convention of using "_dynptr" suffix for variations of API that
*produce* dynptrs as an output. bpf_dynptr_alloc() sounds like we are
allocating struct bpf_dynptr itself, not a memory to which bpf_dynptr
points. But I'm don't have perfect naming scheme.

> bpf_dynptr_put(dynptr);
> would fit the best.
>
> Output arg being first doesn't match anything we had.
> let's keep it last.

yep, agree

>
> zero-alloc or plain kmalloc can be indicated by the flag.
> kzalloc() in the kernel is just static inline that adds __GFP_ZERO to flags.
> We don't need bpf_dynptr_alloc and bpf_dynptr_zalloc as two helpers.
> The latter can be a static inline helper in a bpf program.

yeah, sure, my point was that zero-initialization is a better default

>
> Similar to Andrii's concern I feel that bpf_dynptr_free() would be misleading.



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux