bpf: Add a few bpf mem allocator functions
authorMartin KaFai Lau <martin.lau@kernel.org>
Wed, 22 Mar 2023 21:52:42 +0000 (14:52 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 26 Mar 2023 02:52:51 +0000 (19:52 -0700)
commite65a5c6edbc6ca4853e6076bd81db1a410592a09
tree48c2cb3a361df556443d7e73d74a45c1afdbc31d
parente99360762a9cbd93bf1d352e90df5df78daa2f90
bpf: Add a few bpf mem allocator functions

This patch adds a few bpf mem allocator functions which will
be used in the bpf_local_storage in a later patch.

bpf_mem_cache_alloc_flags(..., gfp_t flags) is added. When the
flags == GFP_KERNEL, it will fallback to __alloc(..., GFP_KERNEL).
bpf_local_storage knows its running context is sleepable (GFP_KERNEL)
and provides a better guarantee on memory allocation.

bpf_local_storage has some uncommon cases that its selem
cannot be reused immediately. It handles its own
rcu_head and goes through a rcu_trace gp and then free it.
bpf_mem_cache_raw_free() is added for direct free purpose
without leaking the LLIST_NODE_SZ internal knowledge.
During free time, the 'struct bpf_mem_alloc *ma' is no longer
available. However, the caller should know if it is
percpu memory or not and it can call different raw_free functions.
bpf_local_storage does not support percpu value, so only
the non-percpu 'bpf_mem_cache_raw_free()' is added in
this patch.

Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://lore.kernel.org/r/20230322215246.1675516-2-martin.lau@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf_mem_alloc.h
kernel/bpf/memalloc.c