Skip to content

Commit ed2d9e1

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
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: Song Liu <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 3486bed commit ed2d9e1

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

include/linux/filter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ struct sock_fprog_kern {
548548
#define BPF_IMAGE_ALIGNMENT 8
549549

550550
struct bpf_binary_header {
551-
u32 pages;
551+
u32 size;
552552
u8 image[] __aligned(BPF_IMAGE_ALIGNMENT);
553553
};
554554

@@ -886,8 +886,8 @@ static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
886886
static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
887887
{
888888
set_vm_flush_reset_perms(hdr);
889-
set_memory_ro((unsigned long)hdr, hdr->pages);
890-
set_memory_x((unsigned long)hdr, hdr->pages);
889+
set_memory_ro((unsigned long)hdr, hdr->size >> PAGE_SHIFT);
890+
set_memory_x((unsigned long)hdr, hdr->size >> PAGE_SHIFT);
891891
}
892892

893893
static inline struct bpf_binary_header *

kernel/bpf/core.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ bpf_prog_ksym_set_addr(struct bpf_prog *prog)
543543
WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
544544

545545
prog->aux->ksym.start = (unsigned long) prog->bpf_func;
546-
prog->aux->ksym.end = addr + hdr->pages * PAGE_SIZE;
546+
prog->aux->ksym.end = addr + hdr->size;
547547
}
548548

549549
static void
@@ -866,7 +866,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
866866
bpf_jit_fill_hole_t bpf_fill_ill_insns)
867867
{
868868
struct bpf_binary_header *hdr;
869-
u32 size, hole, start, pages;
869+
u32 size, hole, start;
870870

871871
WARN_ON_ONCE(!is_power_of_2(alignment) ||
872872
alignment > BPF_IMAGE_ALIGNMENT);
@@ -876,7 +876,6 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
876876
* random section of illegal instructions.
877877
*/
878878
size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
879-
pages = size / PAGE_SIZE;
880879

881880
if (bpf_jit_charge_modmem(size))
882881
return NULL;
@@ -889,7 +888,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
889888
/* Fill space with illegal/arch-dep instructions. */
890889
bpf_fill_ill_insns(hdr, size);
891890

892-
hdr->pages = pages;
891+
hdr->size = size;
893892
hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
894893
PAGE_SIZE - sizeof(*hdr));
895894
start = (get_random_int() % hole) & ~(alignment - 1);
@@ -902,10 +901,10 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
902901

903902
void bpf_jit_binary_free(struct bpf_binary_header *hdr)
904903
{
905-
u32 pages = hdr->pages;
904+
u32 size = hdr->size;
906905

907906
bpf_jit_free_exec(hdr);
908-
bpf_jit_uncharge_modmem(pages << PAGE_SHIFT);
907+
bpf_jit_uncharge_modmem(size);
909908
}
910909

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

0 commit comments

Comments
 (0)