Skip to content

Commit ef4b775

Browse files
liu-song-6Nobody
authored andcommitted
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]>
1 parent 0974b5e commit ef4b775

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

kernel/bpf/core.c

Lines changed: 51 additions & 24 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>
@@ -814,46 +815,69 @@ int bpf_jit_add_poke_descriptor(struct bpf_prog *prog,
814815
* allocator. The prog_pack allocator uses HPAGE_PMD_SIZE page (2MB on x86)
815816
* to host BPF programs.
816817
*/
817-
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
818-
#define BPF_PROG_PACK_SIZE HPAGE_PMD_SIZE
819-
#else
820-
#define BPF_PROG_PACK_SIZE PAGE_SIZE
821-
#endif
822818
#define BPF_PROG_CHUNK_SHIFT 6
823819
#define BPF_PROG_CHUNK_SIZE (1 << BPF_PROG_CHUNK_SHIFT)
824820
#define BPF_PROG_CHUNK_MASK (~(BPF_PROG_CHUNK_SIZE - 1))
825-
#define BPF_PROG_CHUNK_COUNT (BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE)
826821

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

833-
#define BPF_PROG_MAX_PACK_PROG_SIZE BPF_PROG_PACK_SIZE
834828
#define BPF_PROG_SIZE_TO_NBITS(size) (round_up(size, BPF_PROG_CHUNK_SIZE) / BPF_PROG_CHUNK_SIZE)
835829

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

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

843-
pack = kzalloc(sizeof(*pack) + BITS_TO_BYTES(BPF_PROG_CHUNK_COUNT), GFP_KERNEL);
844-
if (!pack)
864+
ptr = module_alloc(bpf_prog_pack_size);
865+
if (!ptr)
845866
return NULL;
846-
pack->ptr = module_alloc(BPF_PROG_PACK_SIZE);
847-
if (!pack->ptr) {
848-
kfree(pack);
867+
868+
pack = kzalloc(struct_size(pack, bitmap, BITS_TO_LONGS(bpf_prog_chunk_count())),
869+
GFP_KERNEL);
870+
if (!pack) {
871+
vfree(ptr);
849872
return NULL;
850873
}
851-
bitmap_zero(pack->bitmap, BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE);
874+
pack->ptr = ptr;
875+
bitmap_zero(pack->bitmap, bpf_prog_pack_size / BPF_PROG_CHUNK_SIZE);
852876
list_add_tail(&pack->list, &pack_list);
853877

854878
set_vm_flush_reset_perms(pack->ptr);
855-
set_memory_ro((unsigned long)pack->ptr, BPF_PROG_PACK_SIZE / PAGE_SIZE);
856-
set_memory_x((unsigned long)pack->ptr, BPF_PROG_PACK_SIZE / PAGE_SIZE);
879+
set_memory_ro((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
880+
set_memory_x((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
857881
return pack;
858882
}
859883

@@ -864,21 +888,24 @@ static void *bpf_prog_pack_alloc(u32 size)
864888
unsigned long pos;
865889
void *ptr = NULL;
866890

867-
if (size > BPF_PROG_MAX_PACK_PROG_SIZE) {
891+
mutex_lock(&pack_mutex);
892+
if (bpf_prog_pack_size == -1)
893+
bpf_prog_pack_size = select_bpf_prog_pack_size();
894+
895+
if (size > bpf_prog_pack_size) {
868896
size = round_up(size, PAGE_SIZE);
869897
ptr = module_alloc(size);
870898
if (ptr) {
871899
set_vm_flush_reset_perms(ptr);
872900
set_memory_ro((unsigned long)ptr, size / PAGE_SIZE);
873901
set_memory_x((unsigned long)ptr, size / PAGE_SIZE);
874902
}
875-
return ptr;
903+
goto out;
876904
}
877-
mutex_lock(&pack_mutex);
878905
list_for_each_entry(pack, &pack_list, list) {
879-
pos = bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
906+
pos = bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0,
880907
nbits, 0);
881-
if (pos < BPF_PROG_CHUNK_COUNT)
908+
if (pos < bpf_prog_chunk_count())
882909
goto found_free_area;
883910
}
884911

@@ -904,12 +931,12 @@ static void bpf_prog_pack_free(struct bpf_binary_header *hdr)
904931
unsigned long pos;
905932
void *pack_ptr;
906933

907-
if (hdr->size > BPF_PROG_MAX_PACK_PROG_SIZE) {
934+
if (hdr->size > bpf_prog_pack_size) {
908935
module_memfree(hdr);
909936
return;
910937
}
911938

912-
pack_ptr = (void *)((unsigned long)hdr & ~(BPF_PROG_PACK_SIZE - 1));
939+
pack_ptr = (void *)((unsigned long)hdr & ~(bpf_prog_pack_size - 1));
913940
mutex_lock(&pack_mutex);
914941

915942
list_for_each_entry(tmp, &pack_list, list) {
@@ -926,8 +953,8 @@ static void bpf_prog_pack_free(struct bpf_binary_header *hdr)
926953
pos = ((unsigned long)hdr - (unsigned long)pack_ptr) >> BPF_PROG_CHUNK_SHIFT;
927954

928955
bitmap_clear(pack->bitmap, pos, nbits);
929-
if (bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
930-
BPF_PROG_CHUNK_COUNT, 0) == 0) {
956+
if (bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0,
957+
bpf_prog_chunk_count(), 0) == 0) {
931958
list_del(&pack->list);
932959
module_memfree(pack->ptr);
933960
kfree(pack);

0 commit comments

Comments
 (0)