Skip to content

Commit ef07860

Browse files
liu-song-6Alexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Select proper size for bpf_prog_pack
Using HPAGE_PMD_SIZE as the size for bpf_prog_pack is not ideal in some cases. Specifically, for NUMA systems, __vmalloc_node_range requires PMD_SIZE * num_online_nodes() to allocate huge pages. Also, if the system does not support huge pages (i.e., with cmdline option nohugevmalloc), it is better to use PAGE_SIZE packs. Add logic to select proper size for bpf_prog_pack. This solution is not ideal, as it makes assumption about the behavior of module_alloc and __vmalloc_node_range. However, it appears to be the easiest solution as it doesn't require changes in module_alloc and vmalloc code. Fixes: 5763105 ("bpf: Introduce bpf_prog_pack allocator") Signed-off-by: Song Liu <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 46e9244 commit ef07860

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

kernel/bpf/core.c

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <linux/extable.h>
3434
#include <linux/log2.h>
3535
#include <linux/bpf_verifier.h>
36+
#include <linux/nodemask.h>
3637

3738
#include <asm/barrier.h>
3839
#include <asm/unaligned.h>
@@ -815,46 +816,66 @@ int bpf_jit_add_poke_descriptor(struct bpf_prog *prog,
815816
* allocator. The prog_pack allocator uses HPAGE_PMD_SIZE page (2MB on x86)
816817
* to host BPF programs.
817818
*/
818-
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
819-
#define BPF_PROG_PACK_SIZE HPAGE_PMD_SIZE
820-
#else
821-
#define BPF_PROG_PACK_SIZE PAGE_SIZE
822-
#endif
823819
#define BPF_PROG_CHUNK_SHIFT 6
824820
#define BPF_PROG_CHUNK_SIZE (1 << BPF_PROG_CHUNK_SHIFT)
825821
#define BPF_PROG_CHUNK_MASK (~(BPF_PROG_CHUNK_SIZE - 1))
826-
#define BPF_PROG_CHUNK_COUNT (BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE)
827822

828823
struct bpf_prog_pack {
829824
struct list_head list;
830825
void *ptr;
831826
unsigned long bitmap[];
832827
};
833828

834-
#define BPF_PROG_MAX_PACK_PROG_SIZE BPF_PROG_PACK_SIZE
835829
#define BPF_PROG_SIZE_TO_NBITS(size) (round_up(size, BPF_PROG_CHUNK_SIZE) / BPF_PROG_CHUNK_SIZE)
836830

831+
static size_t bpf_prog_pack_size = -1;
832+
833+
static int bpf_prog_chunk_count(void)
834+
{
835+
WARN_ON_ONCE(bpf_prog_pack_size == -1);
836+
return bpf_prog_pack_size / BPF_PROG_CHUNK_SIZE;
837+
}
838+
837839
static DEFINE_MUTEX(pack_mutex);
838840
static LIST_HEAD(pack_list);
839841

842+
static size_t select_bpf_prog_pack_size(void)
843+
{
844+
size_t size;
845+
void *ptr;
846+
847+
size = PMD_SIZE * num_online_nodes();
848+
ptr = module_alloc(size);
849+
850+
/* Test whether we can get huge pages. If not just use PAGE_SIZE
851+
* packs.
852+
*/
853+
if (!ptr || !is_vm_area_hugepages(ptr))
854+
size = PAGE_SIZE;
855+
856+
vfree(ptr);
857+
return size;
858+
}
859+
840860
static struct bpf_prog_pack *alloc_new_pack(void)
841861
{
842862
struct bpf_prog_pack *pack;
843863

844-
pack = kzalloc(sizeof(*pack) + BITS_TO_BYTES(BPF_PROG_CHUNK_COUNT), GFP_KERNEL);
864+
pack = kzalloc(struct_size(pack, bitmap, BITS_TO_LONGS(bpf_prog_chunk_count())),
865+
GFP_KERNEL);
845866
if (!pack)
846867
return NULL;
847-
pack->ptr = module_alloc(BPF_PROG_PACK_SIZE);
868+
pack->ptr = module_alloc(bpf_prog_pack_size);
848869
if (!pack->ptr) {
849870
kfree(pack);
850871
return NULL;
851872
}
852-
bitmap_zero(pack->bitmap, BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE);
873+
bitmap_zero(pack->bitmap, bpf_prog_pack_size / BPF_PROG_CHUNK_SIZE);
853874
list_add_tail(&pack->list, &pack_list);
854875

855876
set_vm_flush_reset_perms(pack->ptr);
856-
set_memory_ro((unsigned long)pack->ptr, BPF_PROG_PACK_SIZE / PAGE_SIZE);
857-
set_memory_x((unsigned long)pack->ptr, BPF_PROG_PACK_SIZE / PAGE_SIZE);
877+
set_memory_ro((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
878+
set_memory_x((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
858879
return pack;
859880
}
860881

@@ -865,21 +886,24 @@ static void *bpf_prog_pack_alloc(u32 size)
865886
unsigned long pos;
866887
void *ptr = NULL;
867888

868-
if (size > BPF_PROG_MAX_PACK_PROG_SIZE) {
889+
mutex_lock(&pack_mutex);
890+
if (bpf_prog_pack_size == -1)
891+
bpf_prog_pack_size = select_bpf_prog_pack_size();
892+
893+
if (size > bpf_prog_pack_size) {
869894
size = round_up(size, PAGE_SIZE);
870895
ptr = module_alloc(size);
871896
if (ptr) {
872897
set_vm_flush_reset_perms(ptr);
873898
set_memory_ro((unsigned long)ptr, size / PAGE_SIZE);
874899
set_memory_x((unsigned long)ptr, size / PAGE_SIZE);
875900
}
876-
return ptr;
901+
goto out;
877902
}
878-
mutex_lock(&pack_mutex);
879903
list_for_each_entry(pack, &pack_list, list) {
880-
pos = bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
904+
pos = bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0,
881905
nbits, 0);
882-
if (pos < BPF_PROG_CHUNK_COUNT)
906+
if (pos < bpf_prog_chunk_count())
883907
goto found_free_area;
884908
}
885909

@@ -905,13 +929,13 @@ static void bpf_prog_pack_free(struct bpf_binary_header *hdr)
905929
unsigned long pos;
906930
void *pack_ptr;
907931

908-
if (hdr->size > BPF_PROG_MAX_PACK_PROG_SIZE) {
932+
mutex_lock(&pack_mutex);
933+
if (hdr->size > bpf_prog_pack_size) {
909934
module_memfree(hdr);
910-
return;
935+
goto out;
911936
}
912937

913-
pack_ptr = (void *)((unsigned long)hdr & ~(BPF_PROG_PACK_SIZE - 1));
914-
mutex_lock(&pack_mutex);
938+
pack_ptr = (void *)((unsigned long)hdr & ~(bpf_prog_pack_size - 1));
915939

916940
list_for_each_entry(tmp, &pack_list, list) {
917941
if (tmp->ptr == pack_ptr) {
@@ -927,8 +951,8 @@ static void bpf_prog_pack_free(struct bpf_binary_header *hdr)
927951
pos = ((unsigned long)hdr - (unsigned long)pack_ptr) >> BPF_PROG_CHUNK_SHIFT;
928952

929953
bitmap_clear(pack->bitmap, pos, nbits);
930-
if (bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
931-
BPF_PROG_CHUNK_COUNT, 0) == 0) {
954+
if (bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0,
955+
bpf_prog_chunk_count(), 0) == 0) {
932956
list_del(&pack->list);
933957
module_memfree(pack->ptr);
934958
kfree(pack);

0 commit comments

Comments
 (0)