Skip to content

Commit

Permalink
bpf: Use bytes instead of pages for bpf_jit_[charge|uncharge]_modmem
Browse files Browse the repository at this point in the history
This enables sub-page memory charge and allocation.

Signed-off-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220204185742.271030-3-song@kernel.org
  • Loading branch information
Song Liu authored and Alexei Starovoitov committed Feb 8, 2022
1 parent fac54e2 commit 3486bed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions include/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,8 @@ void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
void bpf_image_ksym_del(struct bpf_ksym *ksym);
void bpf_ksym_add(struct bpf_ksym *ksym);
void bpf_ksym_del(struct bpf_ksym *ksym);
int bpf_jit_charge_modmem(u32 pages);
void bpf_jit_uncharge_modmem(u32 pages);
int bpf_jit_charge_modmem(u32 size);
void bpf_jit_uncharge_modmem(u32 size);
bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
#else
static inline int bpf_trampoline_link_prog(struct bpf_prog *prog,
Expand Down
17 changes: 8 additions & 9 deletions kernel/bpf/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -833,22 +833,21 @@ static int __init bpf_jit_charge_init(void)
}
pure_initcall(bpf_jit_charge_init);

int bpf_jit_charge_modmem(u32 pages)
int bpf_jit_charge_modmem(u32 size)
{
if (atomic_long_add_return(pages, &bpf_jit_current) >
(bpf_jit_limit >> PAGE_SHIFT)) {
if (atomic_long_add_return(size, &bpf_jit_current) > bpf_jit_limit) {
if (!bpf_capable()) {
atomic_long_sub(pages, &bpf_jit_current);
atomic_long_sub(size, &bpf_jit_current);
return -EPERM;
}
}

return 0;
}

void bpf_jit_uncharge_modmem(u32 pages)
void bpf_jit_uncharge_modmem(u32 size)
{
atomic_long_sub(pages, &bpf_jit_current);
atomic_long_sub(size, &bpf_jit_current);
}

void *__weak bpf_jit_alloc_exec(unsigned long size)
Expand Down Expand Up @@ -879,11 +878,11 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
pages = size / PAGE_SIZE;

if (bpf_jit_charge_modmem(pages))
if (bpf_jit_charge_modmem(size))
return NULL;
hdr = bpf_jit_alloc_exec(size);
if (!hdr) {
bpf_jit_uncharge_modmem(pages);
bpf_jit_uncharge_modmem(size);
return NULL;
}

Expand All @@ -906,7 +905,7 @@ void bpf_jit_binary_free(struct bpf_binary_header *hdr)
u32 pages = hdr->pages;

bpf_jit_free_exec(hdr);
bpf_jit_uncharge_modmem(pages);
bpf_jit_uncharge_modmem(pages << PAGE_SHIFT);
}

/* This symbol is only overridden by archs that have different
Expand Down
6 changes: 3 additions & 3 deletions kernel/bpf/trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static void __bpf_tramp_image_put_deferred(struct work_struct *work)
im = container_of(work, struct bpf_tramp_image, work);
bpf_image_ksym_del(&im->ksym);
bpf_jit_free_exec(im->image);
bpf_jit_uncharge_modmem(1);
bpf_jit_uncharge_modmem(PAGE_SIZE);
percpu_ref_exit(&im->pcref);
kfree_rcu(im, rcu);
}
Expand Down Expand Up @@ -310,7 +310,7 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx)
if (!im)
goto out;

err = bpf_jit_charge_modmem(1);
err = bpf_jit_charge_modmem(PAGE_SIZE);
if (err)
goto out_free_im;

Expand All @@ -332,7 +332,7 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx)
out_free_image:
bpf_jit_free_exec(im->image);
out_uncharge:
bpf_jit_uncharge_modmem(1);
bpf_jit_uncharge_modmem(PAGE_SIZE);
out_free_im:
kfree(im);
out:
Expand Down

0 comments on commit 3486bed

Please sign in to comment.