Skip to content

Commit 5c04433

Browse files
liu-song-6Martin KaFai Lau
authored andcommitted
bpf: Charge modmem for struct_ops trampoline
Current code charges modmem for regular trampoline, but not for struct_ops trampoline. Add bpf_jit_[charge|uncharge]_modmem() to struct_ops so the trampoline is charged in both cases. Signed-off-by: Song Liu <song@kernel.org> Link: https://lore.kernel.org/r/20230914222542.2986059-1-song@kernel.org Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent 971f7c3 commit 5c04433

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

kernel/bpf/bpf_struct_ops.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,10 @@ static void __bpf_struct_ops_map_free(struct bpf_map *map)
615615
if (st_map->links)
616616
bpf_struct_ops_map_put_progs(st_map);
617617
bpf_map_area_free(st_map->links);
618-
bpf_jit_free_exec(st_map->image);
618+
if (st_map->image) {
619+
bpf_jit_free_exec(st_map->image);
620+
bpf_jit_uncharge_modmem(PAGE_SIZE);
621+
}
619622
bpf_map_area_free(st_map->uvalue);
620623
bpf_map_area_free(st_map);
621624
}
@@ -657,6 +660,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
657660
struct bpf_struct_ops_map *st_map;
658661
const struct btf_type *t, *vt;
659662
struct bpf_map *map;
663+
int ret;
660664

661665
st_ops = bpf_struct_ops_find_value(attr->btf_vmlinux_value_type_id);
662666
if (!st_ops)
@@ -681,12 +685,27 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
681685
st_map->st_ops = st_ops;
682686
map = &st_map->map;
683687

688+
ret = bpf_jit_charge_modmem(PAGE_SIZE);
689+
if (ret) {
690+
__bpf_struct_ops_map_free(map);
691+
return ERR_PTR(ret);
692+
}
693+
694+
st_map->image = bpf_jit_alloc_exec(PAGE_SIZE);
695+
if (!st_map->image) {
696+
/* __bpf_struct_ops_map_free() uses st_map->image as flag
697+
* for "charged or not". In this case, we need to unchange
698+
* here.
699+
*/
700+
bpf_jit_uncharge_modmem(PAGE_SIZE);
701+
__bpf_struct_ops_map_free(map);
702+
return ERR_PTR(-ENOMEM);
703+
}
684704
st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
685705
st_map->links =
686706
bpf_map_area_alloc(btf_type_vlen(t) * sizeof(struct bpf_links *),
687707
NUMA_NO_NODE);
688-
st_map->image = bpf_jit_alloc_exec(PAGE_SIZE);
689-
if (!st_map->uvalue || !st_map->links || !st_map->image) {
708+
if (!st_map->uvalue || !st_map->links) {
690709
__bpf_struct_ops_map_free(map);
691710
return ERR_PTR(-ENOMEM);
692711
}
@@ -907,4 +926,3 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
907926
kfree(link);
908927
return err;
909928
}
910-

0 commit comments

Comments
 (0)