xref: /linux/include/linux/bpf_mem_alloc.h (revision c39aa3b2)
17c8199e2SAlexei Starovoitov /* SPDX-License-Identifier: GPL-2.0-only */
27c8199e2SAlexei Starovoitov /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
37c8199e2SAlexei Starovoitov #ifndef _BPF_MEM_ALLOC_H
47c8199e2SAlexei Starovoitov #define _BPF_MEM_ALLOC_H
57c8199e2SAlexei Starovoitov #include <linux/compiler_types.h>
69f2c6e96SAlexei Starovoitov #include <linux/workqueue.h>
77c8199e2SAlexei Starovoitov 
87c8199e2SAlexei Starovoitov struct bpf_mem_cache;
97c8199e2SAlexei Starovoitov struct bpf_mem_caches;
107c8199e2SAlexei Starovoitov 
117c8199e2SAlexei Starovoitov struct bpf_mem_alloc {
127c8199e2SAlexei Starovoitov 	struct bpf_mem_caches __percpu *caches;
137c8199e2SAlexei Starovoitov 	struct bpf_mem_cache __percpu *cache;
149fc8e802SYonghong Song 	struct obj_cgroup *objcg;
153f2189e4SHou Tao 	bool percpu;
169f2c6e96SAlexei Starovoitov 	struct work_struct work;
177c8199e2SAlexei Starovoitov };
187c8199e2SAlexei Starovoitov 
195d5de3a4SHou Tao /* 'size != 0' is for bpf_mem_alloc which manages fixed-size objects.
205d5de3a4SHou Tao  * Alloc and free are done with bpf_mem_cache_{alloc,free}().
215d5de3a4SHou Tao  *
225d5de3a4SHou Tao  * 'size = 0' is for bpf_mem_alloc which manages many fixed-size objects.
235d5de3a4SHou Tao  * Alloc and free are done with bpf_mem_{alloc,free}() and the size of
245d5de3a4SHou Tao  * the returned object is given by the size argument of bpf_mem_alloc().
25*c39aa3b2SYonghong Song  * If percpu equals true, error will be returned in order to avoid
26*c39aa3b2SYonghong Song  * large memory consumption and the below bpf_mem_alloc_percpu_unit_init()
27*c39aa3b2SYonghong Song  * should be used to do on-demand per-cpu allocation for each size.
285d5de3a4SHou Tao  */
294ab67149SAlexei Starovoitov int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size, bool percpu);
30*c39aa3b2SYonghong Song /* Initialize a non-fix-size percpu memory allocator */
31*c39aa3b2SYonghong Song int bpf_mem_alloc_percpu_init(struct bpf_mem_alloc *ma, struct obj_cgroup *objcg);
32*c39aa3b2SYonghong Song /* The percpu allocation with a specific unit size. */
33*c39aa3b2SYonghong Song int bpf_mem_alloc_percpu_unit_init(struct bpf_mem_alloc *ma, int size);
347c8199e2SAlexei Starovoitov void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma);
357c8199e2SAlexei Starovoitov 
367c8199e2SAlexei Starovoitov /* kmalloc/kfree equivalent: */
377c8199e2SAlexei Starovoitov void *bpf_mem_alloc(struct bpf_mem_alloc *ma, size_t size);
387c8199e2SAlexei Starovoitov void bpf_mem_free(struct bpf_mem_alloc *ma, void *ptr);
395af6807bSAlexei Starovoitov void bpf_mem_free_rcu(struct bpf_mem_alloc *ma, void *ptr);
407c8199e2SAlexei Starovoitov 
417c8199e2SAlexei Starovoitov /* kmem_cache_alloc/free equivalent: */
427c8199e2SAlexei Starovoitov void *bpf_mem_cache_alloc(struct bpf_mem_alloc *ma);
437c8199e2SAlexei Starovoitov void bpf_mem_cache_free(struct bpf_mem_alloc *ma, void *ptr);
445af6807bSAlexei Starovoitov void bpf_mem_cache_free_rcu(struct bpf_mem_alloc *ma, void *ptr);
45e65a5c6eSMartin KaFai Lau void bpf_mem_cache_raw_free(void *ptr);
46e65a5c6eSMartin KaFai Lau void *bpf_mem_cache_alloc_flags(struct bpf_mem_alloc *ma, gfp_t flags);
477c8199e2SAlexei Starovoitov 
487c8199e2SAlexei Starovoitov #endif /* _BPF_MEM_ALLOC_H */
49