Skip to content

Commit dc002bb

Browse files
Ard Biesheuvelborkmann
authored andcommitted
bpf: add __weak hook for allocating executable memory
By default, BPF uses module_alloc() to allocate executable memory, but this is not necessary on all arches and potentially undesirable on some of them. So break out the module_alloc() and module_memfree() calls into __weak functions to allow them to be overridden in arch code. Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 2a95471 commit dc002bb

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

kernel/bpf/core.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,16 @@ static void bpf_jit_uncharge_modmem(u32 pages)
623623
atomic_long_sub(pages, &bpf_jit_current);
624624
}
625625

626+
void *__weak bpf_jit_alloc_exec(unsigned long size)
627+
{
628+
return module_alloc(size);
629+
}
630+
631+
void __weak bpf_jit_free_exec(void *addr)
632+
{
633+
module_memfree(addr);
634+
}
635+
626636
struct bpf_binary_header *
627637
bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
628638
unsigned int alignment,
@@ -640,7 +650,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
640650

641651
if (bpf_jit_charge_modmem(pages))
642652
return NULL;
643-
hdr = module_alloc(size);
653+
hdr = bpf_jit_alloc_exec(size);
644654
if (!hdr) {
645655
bpf_jit_uncharge_modmem(pages);
646656
return NULL;
@@ -664,7 +674,7 @@ void bpf_jit_binary_free(struct bpf_binary_header *hdr)
664674
{
665675
u32 pages = hdr->pages;
666676

667-
module_memfree(hdr);
677+
bpf_jit_free_exec(hdr);
668678
bpf_jit_uncharge_modmem(pages);
669679
}
670680

0 commit comments

Comments
 (0)