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

bpf: Use size instead of pages in bpf_binary_header

This is necessary to charge sub page memory for the BPF program.
Signed-off-by: default avatarSong Liu <songliubraving@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220204185742.271030-4-song@kernel.org
parent 3486bedd
...@@ -548,7 +548,7 @@ struct sock_fprog_kern { ...@@ -548,7 +548,7 @@ struct sock_fprog_kern {
#define BPF_IMAGE_ALIGNMENT 8 #define BPF_IMAGE_ALIGNMENT 8
struct bpf_binary_header { struct bpf_binary_header {
u32 pages; u32 size;
u8 image[] __aligned(BPF_IMAGE_ALIGNMENT); u8 image[] __aligned(BPF_IMAGE_ALIGNMENT);
}; };
...@@ -886,8 +886,8 @@ static inline void bpf_prog_lock_ro(struct bpf_prog *fp) ...@@ -886,8 +886,8 @@ static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr) static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
{ {
set_vm_flush_reset_perms(hdr); set_vm_flush_reset_perms(hdr);
set_memory_ro((unsigned long)hdr, hdr->pages); set_memory_ro((unsigned long)hdr, hdr->size >> PAGE_SHIFT);
set_memory_x((unsigned long)hdr, hdr->pages); set_memory_x((unsigned long)hdr, hdr->size >> PAGE_SHIFT);
} }
static inline struct bpf_binary_header * static inline struct bpf_binary_header *
......
...@@ -543,7 +543,7 @@ bpf_prog_ksym_set_addr(struct bpf_prog *prog) ...@@ -543,7 +543,7 @@ bpf_prog_ksym_set_addr(struct bpf_prog *prog)
WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog)); WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
prog->aux->ksym.start = (unsigned long) prog->bpf_func; prog->aux->ksym.start = (unsigned long) prog->bpf_func;
prog->aux->ksym.end = addr + hdr->pages * PAGE_SIZE; prog->aux->ksym.end = addr + hdr->size;
} }
static void static void
...@@ -866,7 +866,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, ...@@ -866,7 +866,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
bpf_jit_fill_hole_t bpf_fill_ill_insns) bpf_jit_fill_hole_t bpf_fill_ill_insns)
{ {
struct bpf_binary_header *hdr; struct bpf_binary_header *hdr;
u32 size, hole, start, pages; u32 size, hole, start;
WARN_ON_ONCE(!is_power_of_2(alignment) || WARN_ON_ONCE(!is_power_of_2(alignment) ||
alignment > BPF_IMAGE_ALIGNMENT); alignment > BPF_IMAGE_ALIGNMENT);
...@@ -876,7 +876,6 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, ...@@ -876,7 +876,6 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
* random section of illegal instructions. * random section of illegal instructions.
*/ */
size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
pages = size / PAGE_SIZE;
if (bpf_jit_charge_modmem(size)) if (bpf_jit_charge_modmem(size))
return NULL; return NULL;
...@@ -889,7 +888,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, ...@@ -889,7 +888,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
/* Fill space with illegal/arch-dep instructions. */ /* Fill space with illegal/arch-dep instructions. */
bpf_fill_ill_insns(hdr, size); bpf_fill_ill_insns(hdr, size);
hdr->pages = pages; hdr->size = size;
hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
PAGE_SIZE - sizeof(*hdr)); PAGE_SIZE - sizeof(*hdr));
start = (get_random_int() % hole) & ~(alignment - 1); start = (get_random_int() % hole) & ~(alignment - 1);
...@@ -902,10 +901,10 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, ...@@ -902,10 +901,10 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
void bpf_jit_binary_free(struct bpf_binary_header *hdr) void bpf_jit_binary_free(struct bpf_binary_header *hdr)
{ {
u32 pages = hdr->pages; u32 size = hdr->size;
bpf_jit_free_exec(hdr); bpf_jit_free_exec(hdr);
bpf_jit_uncharge_modmem(pages << PAGE_SHIFT); bpf_jit_uncharge_modmem(size);
} }
/* This symbol is only overridden by archs that have different /* This symbol is only overridden by archs that have different
......
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