• Kumar Kartikeya Dwivedi's avatar
    bpf: Wire up freeing of referenced kptr · 14a324f6
    Kumar Kartikeya Dwivedi authored
    A destructor kfunc can be defined as void func(type *), where type may
    be void or any other pointer type as per convenience.
    
    In this patch, we ensure that the type is sane and capture the function
    pointer into off_desc of ptr_off_tab for the specific pointer offset,
    with the invariant that the dtor pointer is always set when 'kptr_ref'
    tag is applied to the pointer's pointee type, which is indicated by the
    flag BPF_MAP_VALUE_OFF_F_REF.
    
    Note that only BTF IDs whose destructor kfunc is registered, thus become
    the allowed BTF IDs for embedding as referenced kptr. Hence it serves
    the purpose of finding dtor kfunc BTF ID, as well acting as a check
    against the whitelist of allowed BTF IDs for this purpose.
    
    Finally, wire up the actual freeing of the referenced pointer if any at
    all available offsets, so that no references are leaked after the BPF
    map goes away and the BPF program previously moved the ownership a
    referenced pointer into it.
    
    The behavior is similar to BPF timers, where bpf_map_{update,delete}_elem
    will free any existing referenced kptr. The same case is with LRU map's
    bpf_lru_push_free/htab_lru_push_free functions, which are extended to
    reset unreferenced and free referenced kptr.
    
    Note that unlike BPF timers, kptr is not reset or freed when map uref
    drops to zero.
    Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220424214901.2743946-8-memxor@gmail.com
    14a324f6
arraymap.c 35.8 KB