• Yonghong Song's avatar
    bpf: Add alloc/xchg/direct_access support for local percpu kptr · 36d8bdf7
    Yonghong Song authored
    Add two new kfunc's, bpf_percpu_obj_new_impl() and
    bpf_percpu_obj_drop_impl(), to allocate a percpu obj.
    Two functions are very similar to bpf_obj_new_impl()
    and bpf_obj_drop_impl(). The major difference is related
    to percpu handling.
    
        bpf_rcu_read_lock()
        struct val_t __percpu_kptr *v = map_val->percpu_data;
        ...
        bpf_rcu_read_unlock()
    
    For a percpu data map_val like above 'v', the reg->type
    is set as
    	PTR_TO_BTF_ID | MEM_PERCPU | MEM_RCU
    if inside rcu critical section.
    
    MEM_RCU marking here is similar to NON_OWN_REF as 'v'
    is not a owning reference. But NON_OWN_REF is
    trusted and typically inside the spinlock while
    MEM_RCU is under rcu read lock. RCU is preferred here
    since percpu data structures mean potential concurrent
    access into its contents.
    
    Also, bpf_percpu_obj_new_impl() is restricted such that
    no pointers or special fields are allowed. Therefore,
    the bpf_list_head and bpf_rb_root will not be supported
    in this patch set to avoid potential memory leak issue
    due to racing between bpf_obj_free_fields() and another
    bpf_kptr_xchg() moving an allocated object to
    bpf_list_head and bpf_rb_root.
    Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/r/20230827152744.1996739-1-yonghong.song@linux.devSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    36d8bdf7
helpers.c 66.8 KB