Commit f08a1c65 authored by Song Liu's avatar Song Liu Committed by Alexei Starovoitov

bpf: Let bpf_prog_pack_free handle any pointer

Currently, bpf_prog_pack_free only can only free pointer to struct
bpf_binary_header, which is not flexible. Add a size argument to
bpf_prog_pack_free so that it can handle any pointer.
Signed-off-by: default avatarSong Liu <song@kernel.org>
Acked-by: default avatarIlya Leoshkevich <iii@linux.ibm.com>
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>  # on s390x
Reviewed-by: default avatarBjörn Töpel <bjorn@rivosinc.com>
Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/r/20231206224054.492250-2-song@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 7065eefb
...@@ -1067,7 +1067,7 @@ struct bpf_binary_header * ...@@ -1067,7 +1067,7 @@ struct bpf_binary_header *
bpf_jit_binary_pack_hdr(const struct bpf_prog *fp); bpf_jit_binary_pack_hdr(const struct bpf_prog *fp);
void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns); void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns);
void bpf_prog_pack_free(struct bpf_binary_header *hdr); void bpf_prog_pack_free(void *ptr, u32 size);
static inline bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp) static inline bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
{ {
......
...@@ -928,20 +928,20 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns) ...@@ -928,20 +928,20 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns)
return ptr; return ptr;
} }
void bpf_prog_pack_free(struct bpf_binary_header *hdr) void bpf_prog_pack_free(void *ptr, u32 size)
{ {
struct bpf_prog_pack *pack = NULL, *tmp; struct bpf_prog_pack *pack = NULL, *tmp;
unsigned int nbits; unsigned int nbits;
unsigned long pos; unsigned long pos;
mutex_lock(&pack_mutex); mutex_lock(&pack_mutex);
if (hdr->size > BPF_PROG_PACK_SIZE) { if (size > BPF_PROG_PACK_SIZE) {
bpf_jit_free_exec(hdr); bpf_jit_free_exec(ptr);
goto out; goto out;
} }
list_for_each_entry(tmp, &pack_list, list) { list_for_each_entry(tmp, &pack_list, list) {
if ((void *)hdr >= tmp->ptr && (tmp->ptr + BPF_PROG_PACK_SIZE) > (void *)hdr) { if (ptr >= tmp->ptr && (tmp->ptr + BPF_PROG_PACK_SIZE) > ptr) {
pack = tmp; pack = tmp;
break; break;
} }
...@@ -950,10 +950,10 @@ void bpf_prog_pack_free(struct bpf_binary_header *hdr) ...@@ -950,10 +950,10 @@ void bpf_prog_pack_free(struct bpf_binary_header *hdr)
if (WARN_ONCE(!pack, "bpf_prog_pack bug\n")) if (WARN_ONCE(!pack, "bpf_prog_pack bug\n"))
goto out; goto out;
nbits = BPF_PROG_SIZE_TO_NBITS(hdr->size); nbits = BPF_PROG_SIZE_TO_NBITS(size);
pos = ((unsigned long)hdr - (unsigned long)pack->ptr) >> BPF_PROG_CHUNK_SHIFT; pos = ((unsigned long)ptr - (unsigned long)pack->ptr) >> BPF_PROG_CHUNK_SHIFT;
WARN_ONCE(bpf_arch_text_invalidate(hdr, hdr->size), WARN_ONCE(bpf_arch_text_invalidate(ptr, size),
"bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n"); "bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n");
bitmap_clear(pack->bitmap, pos, nbits); bitmap_clear(pack->bitmap, pos, nbits);
...@@ -1100,8 +1100,7 @@ bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr, ...@@ -1100,8 +1100,7 @@ bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr,
*rw_header = kvmalloc(size, GFP_KERNEL); *rw_header = kvmalloc(size, GFP_KERNEL);
if (!*rw_header) { if (!*rw_header) {
bpf_arch_text_copy(&ro_header->size, &size, sizeof(size)); bpf_prog_pack_free(ro_header, size);
bpf_prog_pack_free(ro_header);
bpf_jit_uncharge_modmem(size); bpf_jit_uncharge_modmem(size);
return NULL; return NULL;
} }
...@@ -1132,7 +1131,7 @@ int bpf_jit_binary_pack_finalize(struct bpf_prog *prog, ...@@ -1132,7 +1131,7 @@ int bpf_jit_binary_pack_finalize(struct bpf_prog *prog,
kvfree(rw_header); kvfree(rw_header);
if (IS_ERR(ptr)) { if (IS_ERR(ptr)) {
bpf_prog_pack_free(ro_header); bpf_prog_pack_free(ro_header, ro_header->size);
return PTR_ERR(ptr); return PTR_ERR(ptr);
} }
return 0; return 0;
...@@ -1153,7 +1152,7 @@ void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header, ...@@ -1153,7 +1152,7 @@ void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header,
{ {
u32 size = ro_header->size; u32 size = ro_header->size;
bpf_prog_pack_free(ro_header); bpf_prog_pack_free(ro_header, size);
kvfree(rw_header); kvfree(rw_header);
bpf_jit_uncharge_modmem(size); bpf_jit_uncharge_modmem(size);
} }
......
...@@ -150,10 +150,7 @@ void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from, ...@@ -150,10 +150,7 @@ void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
goto out; goto out;
d->rw_image = bpf_jit_alloc_exec(PAGE_SIZE); d->rw_image = bpf_jit_alloc_exec(PAGE_SIZE);
if (!d->rw_image) { if (!d->rw_image) {
u32 size = PAGE_SIZE; bpf_prog_pack_free(d->image, PAGE_SIZE);
bpf_arch_text_copy(d->image, &size, sizeof(size));
bpf_prog_pack_free((struct bpf_binary_header *)d->image);
d->image = NULL; d->image = NULL;
goto out; goto out;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment