Skip to content

Commit 20e490a

Browse files
puranjaymohanpalmer-dabbelt
authored andcommitted
bpf: make bpf_prog_pack allocator portable
The bpf_prog_pack allocator currently uses module_alloc() and module_memfree() to allocate and free memory. This is not portable because different architectures use different methods for allocating memory for BPF programs. Like ARM64 and riscv use vmalloc()/vfree(). Use bpf_jit_alloc_exec() and bpf_jit_free_exec() for memory management in bpf_prog_pack allocator. Other architectures can override these with their implementation and will be able to use bpf_prog_pack directly. On architectures that don't override bpf_jit_alloc/free_exec() this is basically a NOP. Signed-off-by: Puranjay Mohan <puranjay12@gmail.com> Acked-by: Song Liu <song@kernel.org> Acked-by: Björn Töpel <bjorn@kernel.org> Tested-by: Björn Töpel <bjorn@rivosinc.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/r/20230831131229.497941-2-puranjay12@gmail.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 9e3b47a commit 20e490a

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

kernel/bpf/core.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_ins
870870
GFP_KERNEL);
871871
if (!pack)
872872
return NULL;
873-
pack->ptr = module_alloc(BPF_PROG_PACK_SIZE);
873+
pack->ptr = bpf_jit_alloc_exec(BPF_PROG_PACK_SIZE);
874874
if (!pack->ptr) {
875875
kfree(pack);
876876
return NULL;
@@ -894,7 +894,7 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns)
894894
mutex_lock(&pack_mutex);
895895
if (size > BPF_PROG_PACK_SIZE) {
896896
size = round_up(size, PAGE_SIZE);
897-
ptr = module_alloc(size);
897+
ptr = bpf_jit_alloc_exec(size);
898898
if (ptr) {
899899
bpf_fill_ill_insns(ptr, size);
900900
set_vm_flush_reset_perms(ptr);
@@ -932,7 +932,7 @@ void bpf_prog_pack_free(struct bpf_binary_header *hdr)
932932

933933
mutex_lock(&pack_mutex);
934934
if (hdr->size > BPF_PROG_PACK_SIZE) {
935-
module_memfree(hdr);
935+
bpf_jit_free_exec(hdr);
936936
goto out;
937937
}
938938

@@ -956,7 +956,7 @@ void bpf_prog_pack_free(struct bpf_binary_header *hdr)
956956
if (bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
957957
BPF_PROG_CHUNK_COUNT, 0) == 0) {
958958
list_del(&pack->list);
959-
module_memfree(pack->ptr);
959+
bpf_jit_free_exec(pack->ptr);
960960
kfree(pack);
961961
}
962962
out:

0 commit comments

Comments
 (0)