Skip to content

Commit 3486bed

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
bpf: Use bytes instead of pages for bpf_jit_[charge|uncharge]_modmem
This enables sub-page memory charge and allocation. Signed-off-by: Song Liu <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent fac54e2 commit 3486bed

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

include/linux/bpf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,8 @@ void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
846846
void bpf_image_ksym_del(struct bpf_ksym *ksym);
847847
void bpf_ksym_add(struct bpf_ksym *ksym);
848848
void bpf_ksym_del(struct bpf_ksym *ksym);
849-
int bpf_jit_charge_modmem(u32 pages);
850-
void bpf_jit_uncharge_modmem(u32 pages);
849+
int bpf_jit_charge_modmem(u32 size);
850+
void bpf_jit_uncharge_modmem(u32 size);
851851
bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
852852
#else
853853
static inline int bpf_trampoline_link_prog(struct bpf_prog *prog,

kernel/bpf/core.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -833,22 +833,21 @@ static int __init bpf_jit_charge_init(void)
833833
}
834834
pure_initcall(bpf_jit_charge_init);
835835

836-
int bpf_jit_charge_modmem(u32 pages)
836+
int bpf_jit_charge_modmem(u32 size)
837837
{
838-
if (atomic_long_add_return(pages, &bpf_jit_current) >
839-
(bpf_jit_limit >> PAGE_SHIFT)) {
838+
if (atomic_long_add_return(size, &bpf_jit_current) > bpf_jit_limit) {
840839
if (!bpf_capable()) {
841-
atomic_long_sub(pages, &bpf_jit_current);
840+
atomic_long_sub(size, &bpf_jit_current);
842841
return -EPERM;
843842
}
844843
}
845844

846845
return 0;
847846
}
848847

849-
void bpf_jit_uncharge_modmem(u32 pages)
848+
void bpf_jit_uncharge_modmem(u32 size)
850849
{
851-
atomic_long_sub(pages, &bpf_jit_current);
850+
atomic_long_sub(size, &bpf_jit_current);
852851
}
853852

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

882-
if (bpf_jit_charge_modmem(pages))
881+
if (bpf_jit_charge_modmem(size))
883882
return NULL;
884883
hdr = bpf_jit_alloc_exec(size);
885884
if (!hdr) {
886-
bpf_jit_uncharge_modmem(pages);
885+
bpf_jit_uncharge_modmem(size);
887886
return NULL;
888887
}
889888

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

908907
bpf_jit_free_exec(hdr);
909-
bpf_jit_uncharge_modmem(pages);
908+
bpf_jit_uncharge_modmem(pages << PAGE_SHIFT);
910909
}
911910

912911
/* This symbol is only overridden by archs that have different

kernel/bpf/trampoline.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static void __bpf_tramp_image_put_deferred(struct work_struct *work)
213213
im = container_of(work, struct bpf_tramp_image, work);
214214
bpf_image_ksym_del(&im->ksym);
215215
bpf_jit_free_exec(im->image);
216-
bpf_jit_uncharge_modmem(1);
216+
bpf_jit_uncharge_modmem(PAGE_SIZE);
217217
percpu_ref_exit(&im->pcref);
218218
kfree_rcu(im, rcu);
219219
}
@@ -310,7 +310,7 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx)
310310
if (!im)
311311
goto out;
312312

313-
err = bpf_jit_charge_modmem(1);
313+
err = bpf_jit_charge_modmem(PAGE_SIZE);
314314
if (err)
315315
goto out_free_im;
316316

@@ -332,7 +332,7 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx)
332332
out_free_image:
333333
bpf_jit_free_exec(im->image);
334334
out_uncharge:
335-
bpf_jit_uncharge_modmem(1);
335+
bpf_jit_uncharge_modmem(PAGE_SIZE);
336336
out_free_im:
337337
kfree(im);
338338
out:

0 commit comments

Comments
 (0)