libbpf: move arena variables out of the zero page
Subsystems (5)
Emil Tsalapatis
reviewercover libbpf: move arena variables out of the zero page
Modify libbpf to place arena globals in a small offset inside the arena mapping instead of at the very beginning. This allows programs to leave the "zero page" of the arena unmapped, so that NULL arena pointer dereferences trigger a page fault and associated backtrace in BPF streams. In contrast, the current policy of placing global data in the zero pages means that NULL dereferences silently corrupt global data, e.g, arena qspinlock state. This makes arena bugs more difficult to debug.
The patchset adds code to libbpf to move global arena data 16 pages into the arena mapping. If this move is impossible, libbpf tries progressively smaller increments, and finally defaults to 0 if there is not enough space in the arena. At load time, libbpf adjusts each symbol's location within the arena by that offset. The patchset also adds padding to the BPF skeleton struct arena datasec to ensure the arena's fields are pointing in the right locations within the mapping.
Emil Tsalapatis (4):
selftests/bpf: explicitly account for globals in verifier_arena_large
libbpf: add stub for offset-related skeleton padding
libbpf: offset global arena data into the arena if possible selftests/bpf: add tests for the arena offset of globals
tools/bpf/bpftool/gen.c | 23 ++++++- tools/lib/bpf/libbpf.c | 36 ++++++++++- tools/lib/bpf/libbpf.h | 9 +++ tools/lib/bpf/libbpf.map | 1 + .../selftests/bpf/prog_tests/verifier.c | 6 ++ .../bpf/progs/verifier_arena_globals1.c | 60 ++++++++++++++++++ .../bpf/progs/verifier_arena_globals2.c | 49 +++++++++++++++ .../bpf/progs/verifier_arena_globals3.c | 61 +++++++++++++++++++ .../bpf/progs/verifier_arena_large.c | 25 ++++++-- 9 files changed, 261 insertions(+), 9 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_globals1.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_globals2.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_globals3.c
Signature · 1 line
2.49.0
Emil Tsalapatis
reviewerPATCH 1/4 selftests/bpf: explicitly account for globals in verifier_arena_large
The big_alloc1 test in verifier_arena_large assumes that the arena base and the first page allocated by bpf_arena_alloc_pages are identical. This is not the case, because the first page in the arena is populated by global arena data. The test still passes because the code makes the tacit assumption that the first page is on offset PAGE_SIZE instead of 0.
Make this distinction explicit in the code, and adjust the page offsets requested during the test to count from the beginning of the arena instead of using the address of the first allocated page.
--- .../selftests/bpf/progs/verifier_arena_large.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/progs/verifier_arena_large.c +11 -4
@@ -23,18 +23,25 @@ int big_alloc1(void *ctx) {#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) volatile char __arena *page1, *page2, *no_page, *page3; - void __arena *base; +u64 base; - page1 = base =bpf_arena_alloc_pages (&arena,NULL ,1 ,NUMA_NO_NODE ,0 ); + base = (u64 )arena_base (&arena); + + page1 =bpf_arena_alloc_pages (&arena,NULL ,1 ,NUMA_NO_NODE ,0 );if (!page1)return 1 ; + +/* Account for global arena data. */ +if ((u64 )page1 != base +PAGE_SIZE ) +return 15 ; + *page1 =1 ; - page2 =bpf_arena_alloc_pages (&arena, base +ARENA_SIZE -PAGE_SIZE *2 , + page2 = bpf_arena_alloc_pages(&arena, (void __arena *)(ARENA_SIZE - PAGE_SIZE),1 ,NUMA_NO_NODE ,0 );if (!page2)return 2 ; *page2 =2 ; - no_page =bpf_arena_alloc_pages (&arena, base +ARENA_SIZE -PAGE_SIZE , + no_page = bpf_arena_alloc_pages(&arena, (void __arena *)ARENA_SIZE,1 ,NUMA_NO_NODE ,0 );if (no_page)return 3 ;
Signature · 1 line
2.49.0
Emil Tsalapatis
reviewerPATCH 2/4 libbpf: add stub for offset-related skeleton padding
In reply to Emil TsalapatisAdd a stub function for reporting in which offset within a mapping libbpf places the map's data. This will be used in a subsequent patch to support offsetting arena variables within the mapped region.
Adjust skeleton generation to account for the new arena memory layout by adding padding corresponding to the offset into the arena map. Add a libbbpf API function to get the data offset within the map's mapping during skeleton generation.
--- tools/bpf/bpftool/gen.c | 23 +++++++++++++++++++++-- tools/lib/bpf/libbpf.c | 10 ++++++++++ tools/lib/bpf/libbpf.h | 9 +++++++++ tools/lib/bpf/libbpf.map | 1 + 4 files changed, 41 insertions(+), 2 deletions(-)
tools/bpf/bpftool/gen.c +21 -2
@@ -148,7 +148,8 @@ static int codegen_datasec_def(struct bpf_object *obj,struct btf *btf, struct btf_dump *d, const struct btf_type *sec, - const char *obj_name) + const char *obj_name, + int var_off) { const char *sec_name = btf__name_by_offset(btf, sec->name_off);const struct btf_var_secinfo *sec_var =btf_var_secinfos (sec);
@@ -163,6 +164,17 @@ static int codegen_datasec_def(struct bpf_object *obj, strip_mods = true;printf (" struct %s__%s {\n" , obj_name, sec_ident); + +/* +* Arena variables may be placed in an offset within the section. +* Represent this in the skeleton using a padding struct. +*/ +if (var_off >0 ) { +printf ("\t\tchar __pad%d[%d];\n" , + pad_cnt, var_off); + pad_cnt++; + } +for (i =0 ; i < vlen; i++, sec_var++) {const struct btf_type *var =btf__type_by_id (btf, sec_var->type);const char *var_name =btf__name_by_offset (btf, var->name_off);
@@ -279,6 +291,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)struct bpf_map *map;const struct btf_type *sec;char map_ident[256 ]; +int var_off;int err =0 ; d =btf_dump__new (btf, codegen_btf_dump_printf,NULL ,NULL );
@@ -303,7 +316,13 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)printf (" struct %s__%s {\n" , obj_name, map_ident);printf (" } *%s;\n" , map_ident); }else { - err =codegen_datasec_def (obj, btf, d, sec, obj_name); + var_off =bpf_map__data_offset (map); +if (var_off <0 ) { +p_err ("bpf_map__data_offset called on unmapped map\n" ); + err = var_off; + goto out; + } + err =codegen_datasec_def (obj, btf, d, sec, obj_name, var_off);if (err) goto out; }
tools/lib/bpf/libbpf.c +10 -0
@@ -10552,6 +10552,16 @@ const char *bpf_map__name(const struct bpf_map *map)return map->name; } +int bpf_map__data_offset (const struct bpf_map *map) +{ +if (!map->mmaped) +return -EINVAL ; + +/* No offsetting for now. */ +return 0 ; +} + +enum bpf_map_type bpf_map__type (const struct bpf_map *map) {return map->def.type;
tools/lib/bpf/libbpf.h +9 -0
@@ -1314,6 +1314,15 @@ LIBBPF_API int bpf_map__set_exclusive_program(struct bpf_map *map, struct bpf_pr*/ LIBBPF_API struct bpf_program *bpf_map__exclusive_program(struct bpf_map *map); +/* +* @brief **bpf_map__data_offset** returns the offset of the map's data +* within the address mapping. +* @param BPF map whose variable offset we are looking into. +* @return the offset >= 0 of the map's contents within its mapping; negative +* error code, otherwise. +*/ +LIBBPF_API int bpf_map__data_offset(const struct bpf_map *map); +struct bpf_xdp_set_link_opts {size_t sz;int old_fd;
tools/lib/bpf/libbpf.map +1 -0
@@ -451,4 +451,5 @@ LIBBPF_1.7.0 { global: bpf_map__set_exclusive_program; bpf_map__exclusive_program; + bpf_map__data_offset; } LIBBPF_1.6.0;
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH 2/4] libbpf: add stub for offset-related skeleton padding
Can bpf_map__data_offset() be called here on an arena map that doesn't have its mmaped field set yet?
Quoted 4 lines
tools/bpf/bpftool/gen.c +0
-0
[ ... ]
Quoted 18 lines
tools/bpf/bpftool/gen.c +0
-0@@ -269,7 +269,8 @@ static int codegen_datasec_def(struct bpf_object *obj,static bool is_mmapable_map (const struct bpf_map *map,char *buf,size_t sz) {size_t tmp_sz;if (bpf_map__type (map) ==BPF_MAP_TYPE_ARENA &&bpf_map__initial_value (map, &tmp_sz)) {snprintf (buf, sz,"arena" );return true; }if (!bpf_map__is_internal (map) || !(bpf_map__map_flags (map) &BPF_F_MMAPABLE ))return false;if (!get_map_ident (map, buf, sz))return false;return true; }
[ ... ]
Quoted 8 lines
tools/bpf/bpftool/gen.c +1
-0@@ -287,6 +291,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)struct bpf_map *map;const struct btf_type *sec;char map_ident[256 ]; +int var_off;int err =0 ; d =btf_dump__new (btf, codegen_btf_dump_printf,NULL ,NULL );
[ ... ]
Quoted 5 lines
tools/bpf/bpftool/gen.c +0
-1@@ -303,7 +316,13 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)printf (" struct %s__%s {\n" , obj_name, map_ident);printf (" } *%s;\n" , map_ident); }else { - err =codegen_datasec_def (obj, btf, d, sec, obj_name);
tools/bpf/bpftool/gen.c static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) · line 328
Quoted 10 lines
tools/bpf/bpftool/gen.c +7
-0+ var_off =bpf_map__data_offset (map); +if (var_off <0 ) { +p_err ("bpf_map__data_offset called on unmapped map\n" ); + err = var_off; + goto out; + } + err =codegen_datasec_def (obj, btf, d, sec, obj_name, var_off);if (err) goto out; }
Can bpf_map__data_offset() be called here on an arena map that doesn't have its mmaped field set yet?
During skeleton generation in do_skeleton(), the code calls bpf_object__open_mem() but never calls bpf_object__load(). Looking at is_mmapable_map() above, arena maps return true and will reach this code path. However, arena maps only get their mmaped field set during bpf_object__load() in libbpf.c around line 5615, not during bpf_object__open().
This means bpf_map__data_offset() will be called on arena maps with map->mmaped == NULL, causing it to return -EINVAL, which would fail skeleton generation for any program with arena maps.
Quoted 6 lines
tools/lib/bpf/libbpf.c +0
-0@@ -10552,6 +10552,16 @@ const char *bpf_map__name(const struct bpf_map *map)return map->name;
tools/lib/bpf/libbpf.c const char *bpf_map__name(const struct bpf_map *map) · line 10562
Quoted 10 lines
tools/lib/bpf/libbpf.c +8
-0} +int bpf_map__data_offset (const struct bpf_map *map) +{ +if (!map->mmaped) +return -EINVAL ; + +/* No offsetting for now. */ +return 0 ; +}
^^^^
The check for map->mmaped here causes the problem described above. Since this function is called during skeleton generation (before load), the mmaped field won't be set for arena maps yet. Should this check be removed, or should the skeleton generation code path be changed?
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/19448868610
Emil Tsalapatis
reviewerRe: [PATCH 2/4] libbpf: add stub for offset-related skeleton padding
Quoted 66 lines · On Mon, Nov 17, 2025 at 7:18 PM <[email protected]> wrote:
Quoted 5 lines
tools/bpf/bpftool/gen.c +0
-0[ ... ]
Quoted 18 lines
tools/bpf/bpftool/gen.c +0
-0@@ -269,7 +269,8 @@ static int codegen_datasec_def(struct bpf_object *obj,static bool is_mmapable_map (const struct bpf_map *map,char *buf,size_t sz) {size_t tmp_sz;if (bpf_map__type (map) ==BPF_MAP_TYPE_ARENA &&bpf_map__initial_value (map, &tmp_sz)) {snprintf (buf, sz,"arena" );return true; }if (!bpf_map__is_internal (map) || !(bpf_map__map_flags (map) &BPF_F_MMAPABLE ))return false;if (!get_map_ident (map, buf, sz))return false;return true; }[ ... ]
Quoted 8 lines
tools/bpf/bpftool/gen.c +1
-0@@ -287,6 +291,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)struct bpf_map *map;const struct btf_type *sec;char map_ident[256 ]; +int var_off;int err =0 ; d =btf_dump__new (btf, codegen_btf_dump_printf,NULL ,NULL );[ ... ]
Quoted 5 lines
tools/bpf/bpftool/gen.c +0
-1@@ -303,7 +316,13 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)printf (" struct %s__%s {\n" , obj_name, map_ident);printf (" } *%s;\n" , map_ident); }else { - err =codegen_datasec_def (obj, btf, d, sec, obj_name);tools/bpf/bpftool/gen.c static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) · line 328
Quoted 10 lines
tools/bpf/bpftool/gen.c +7
-0+ var_off =bpf_map__data_offset (map); +if (var_off <0 ) { +p_err ("bpf_map__data_offset called on unmapped map\n" ); + err = var_off; + goto out; + } + err =codegen_datasec_def (obj, btf, d, sec, obj_name, var_off);if (err) goto out; }Can
bpf_map__data_offset()be called here on an arena map that doesn't have its mmaped field set yet?During skeleton generation in
do_skeleton(), the code callsbpf_object__open_mem()but never callsbpf_object__load(). Looking atis_mmapable_map()above, arena maps return true and will reach this code path. However, arena maps only get their mmaped field set duringbpf_object__load()inlibbpf.caround line 5615, not duringbpf_object__open().
Quoted 4 lines
This means
bpf_map__data_offset()will be called on arena maps withmap->mmaped== NULL, causing it to return-EINVAL, which would fail skeleton generation for any program with arena maps.
This analysis seems incorrect. bpf_object__open_mem -> bpf_object_open -> bpf_object__init_maps -> bpf_object__init_user_btf_maps -> init_arena_map_data happens at opening time, and this call chain properly populates the ->mmaped field of the arena map.
Quoted 18 lines
Quoted 6 lines
tools/lib/bpf/libbpf.c +0
-0@@ -10552,6 +10552,16 @@ const char *bpf_map__name(const struct bpf_map *map)return map->name;tools/lib/bpf/libbpf.c const char *bpf_map__name(const struct bpf_map *map) · line 10562
Quoted 10 lines
tools/lib/bpf/libbpf.c +8
-0} +int bpf_map__data_offset (const struct bpf_map *map) +{ +if (!map->mmaped) +return -EINVAL ; + +/* No offsetting for now. */ +return 0 ; +}^^^^
Quoted 5 lines
The check for
map->mmapedhere causes the problem described above. Since this function is called during skeleton generation (before load), the mmaped field won't be set for arena maps yet. Should this check be removed, or should the skeleton generation code path be changed?
See above.
Quoted 8 lines
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/19448868610
Emil Tsalapatis
reviewerPATCH 3/4 libbpf: offset global arena data into the arena if possible
In reply to Emil TsalapatisCurrently, libbpf places global arena data at the very beginning of the arena mapping. Stray NULL dereferences into the arena then find valid data and lead to silent corruption instead of causing an arena page fault. The data is placed in the mapping at load time, preventing us from reserving the region using bpf_arena_reserve_pages().
Adjust the arena logic to attempt placing the data from an offset within the arena (currently 16 pages in) instead of the very beginning. If placing the data at an offset would lead to an allocation failure due to global data being as large as the entire arena, progressively reduce the offset down to 0 until placement succeeds.
Adjust existing arena tests in the same commit to account for the new global data offset. New tests that explicitly consider the new feature are introduced in the next patch.
--- tools/lib/bpf/libbpf.c | 30 +++++++++++++++---- .../bpf/progs/verifier_arena_large.c | 14 +++++++-- 2 files changed, 37 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf.c +25 -5
@@ -757,6 +757,7 @@ struct bpf_object {int arena_map_idx;void *arena_data;size_t arena_data_sz; +__u32 arena_data_off;void *jumptables_data;size_t jumptables_data_sz;
@@ -2991,10 +2992,14 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, void *data, size_t data_sz) {const long page_sz =sysconf (_SC_PAGE_SIZE); +const size_t data_alloc_sz =roundup (data_sz, page_sz); +/* default offset into the arena, may be resized */ +const long max_off_pages =16 ;size_t mmap_sz; +long off_pages; mmap_sz =bpf_map_mmap_sz (map); -if (roundup (data_sz, page_sz) > mmap_sz) { +if (data_alloc_sz > mmap_sz) {pr_warn ("elf: sec '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n" , sec_name, mmap_sz, data_sz);return -E2BIG ;
@@ -3006,6 +3011,17 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,memcpy (obj->arena_data, data, data_sz); obj->arena_data_sz = data_sz; +/* +* find the largest offset for global arena variables +* where they still fit in the arena +*/ +for (off_pages = max_off_pages; off_pages >0 ; off_pages >>=1 ) { +if (off_pages * page_sz + data_alloc_sz <= mmap_sz) +break ; + } + + obj->arena_data_off = off_pages * page_sz; +/* make bpf_map__init_value() work for ARENA maps */ map->mmaped = obj->arena_data;
@@ -4663,7 +4679,7 @@ static int bpf_program__record_reloc(struct bpf_program *prog, reloc_desc->type =RELO_DATA ; reloc_desc->insn_idx = insn_idx; reloc_desc->map_idx = obj->arena_map_idx; - reloc_desc->sym_off = sym->st_value; + reloc_desc->sym_off = sym->st_value + obj->arena_data_off; map = &obj->maps[obj->arena_map_idx]; pr_debug("prog '%s': found arena map %d (%s, sec %d, off %zu) for insn %u\n",
@@ -5624,7 +5640,8 @@ bpf_object__create_maps(struct bpf_object *obj)return err; }if (obj->arena_data) { -memcpy (map->mmaped, obj->arena_data, obj->arena_data_sz); +memcpy (map->mmaped + obj->arena_data_off, obj->arena_data, + obj->arena_data_sz);zfree (&obj->arena_data); } }
@@ -10557,8 +10574,11 @@ int bpf_map__data_offset(const struct bpf_map *map)if (!map->mmaped)return -EINVAL ; -/* No offsetting for now. */ -return 0 ; +/* Only arenas have offsetting. */ +if (map->def.type !=BPF_MAP_TYPE_ARENA ) +return 0 ; + +return map->obj->arena_data_off; }
tools/testing/selftests/bpf/progs/verifier_arena_large.c +12 -2
@@ -10,6 +10,7 @@#include "bpf_arena_common.h" #define ARENA_SIZE (1ull << 32) +#define GLOBAL_PGOFF (16)struct { __uint(type, BPF_MAP_TYPE_ARENA);
@@ -31,8 +32,7 @@ int big_alloc1(void *ctx)if (!page1)return 1 ; -/* Account for global arena data. */ -if ((u64 )page1 != base +PAGE_SIZE ) +if ((u64 )page1 != base)return 15 ; *page1 =1 ;
@@ -216,6 +216,16 @@ int big_alloc2(void *ctx) __u8 __arena *pg;int i, err; +/* +* The global data is placed in a page with global offset 16. +* This test is about page allocation contiguity, so avoid +* accounting for the stray allocation by also allocating +* all pages before it. We never use the page range, so leak it. +*/ + pg =bpf_arena_alloc_pages (&arena,NULL ,GLOBAL_PGOFF ,NUMA_NO_NODE ,0 ); +if (!pg) +return 10 ; + base =bpf_arena_alloc_pages (&arena,NULL ,1 ,NUMA_NO_NODE ,0 );if (!base)return 1 ;
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH 3/4] libbpf: offset global arena data into the arena if possible
Can the multiplication overflow __u32 on systems with large page sizes? On a system configured with 1GB huge pages as the default, 16 pages * 1GB = 16GB, which exceeds 2^32. The result would be…
Quoted 12 lines
tools/lib/bpf/libbpf.c +1
-0@@ -757,6 +757,7 @@ struct bpf_object {int arena_map_idx;void *arena_data;size_t arena_data_sz; +__u32 arena_data_off;void *jumptables_data;size_t jumptables_data_sz;
[ ... ]
Quoted 21 lines
tools/lib/bpf/libbpf.c +6
-1@@ -2991,10 +2992,14 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, void *data, size_t data_sz) {const long page_sz =sysconf (_SC_PAGE_SIZE); +const size_t data_alloc_sz =roundup (data_sz, page_sz); +/* default offset into the arena, may be resized */ +const long max_off_pages =16 ;size_t mmap_sz; +long off_pages; mmap_sz =bpf_map_mmap_sz (map); -if (roundup (data_sz, page_sz) > mmap_sz) { +if (data_alloc_sz > mmap_sz) {pr_warn ("elf: sec '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n" , sec_name, mmap_sz, data_sz);return -E2BIG ;@@ -3006,6 +3011,17 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,memcpy (obj->arena_data, data, data_sz); obj->arena_data_sz = data_sz; +/*
tools/lib/bpf/libbpf.c static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, · line 3024
Quoted 10 lines
tools/lib/bpf/libbpf.c +9
-0+* find the largest offset for global arena variables +* where they still fit in the arena +*/ +for (off_pages = max_off_pages; off_pages >0 ; off_pages >>=1 ) { +if (off_pages * page_sz + data_alloc_sz <= mmap_sz) +break ; + } + + obj->arena_data_off = off_pages * page_sz; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Can the multiplication overflow __u32 on systems with large page sizes? On a system configured with 1GB huge pages as the default, 16 pages * 1GB = 16GB, which exceeds 2^32. The result would be truncated when stored in arena_data_off (declared as __u32), causing the offset calculation to wrap around.
While this configuration is rare, the truncation would cause arena data to be placed at an incorrect offset, breaking the intended behavior. The three uses of arena_data_off (in init_arena_map_data, bpf_program__record_reloc, and bpf_object__create_maps) would all use the truncated value consistently, so if truncation occurs to 0, the code would fall back to the original behavior of placing data at offset 0.
Should arena_data_off be changed to size_t or __u64 to handle larger page sizes, or is __u32 considered sufficient for expected arena usage?
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/19448868610
Emil Tsalapatis
reviewerRe: [PATCH 3/4] libbpf: offset global arena data into the arena if possible
Quoted 61 lines · On Mon, Nov 17, 2025 at 7:26 PM <[email protected]> wrote:
Quoted 13 lines
tools/lib/bpf/libbpf.c +1
-0@@ -757,6 +757,7 @@ struct bpf_object {int arena_map_idx;void *arena_data;size_t arena_data_sz; +__u32 arena_data_off;void *jumptables_data;size_t jumptables_data_sz;[ ... ]
Quoted 21 lines
tools/lib/bpf/libbpf.c +6
-1@@ -2991,10 +2992,14 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, void *data, size_t data_sz) {const long page_sz =sysconf (_SC_PAGE_SIZE); +const size_t data_alloc_sz =roundup (data_sz, page_sz); +/* default offset into the arena, may be resized */ +const long max_off_pages =16 ;size_t mmap_sz; +long off_pages; mmap_sz =bpf_map_mmap_sz (map); -if (roundup (data_sz, page_sz) > mmap_sz) { +if (data_alloc_sz > mmap_sz) {pr_warn ("elf: sec '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n" , sec_name, mmap_sz, data_sz);return -E2BIG ;@@ -3006,6 +3011,17 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,memcpy (obj->arena_data, data, data_sz); obj->arena_data_sz = data_sz; +/* tools/lib/bpf/libbpf.c static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, · line 3024
Quoted 10 lines
tools/lib/bpf/libbpf.c +9
-0+* find the largest offset for global arena variables +* where they still fit in the arena +*/ +for (off_pages = max_off_pages; off_pages >0 ; off_pages >>=1 ) { +if (off_pages * page_sz + data_alloc_sz <= mmap_sz) +break ; + } + + obj->arena_data_off = off_pages * page_sz; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Can the multiplication overflow
__u32on systems with large page sizes? On a system configured with 1GB huge pages as the default, 16 pages * 1GB = 16GB, which exceeds 2^32. The result would be truncated when stored inarena_data_off(declared as__u32), causing the offset calculation to wrap around.While this configuration is rare, the truncation would cause arena data to be placed at an incorrect offset, breaking the intended behavior. The three uses of
arena_data_off(ininit_arena_map_data,bpf_program__record_reloc, andbpf_object__create_maps) would all use the truncated value consistently, so if truncation occurs to 0, the code would fall back to the original behavior of placing data at offset 0.
Quoted 3 lines
Should
arena_data_offbe changed tosize_tor__u64to handle larger page sizes, or is__u32considered sufficient for expected arena usage?
The off_pages * page_sz product is bound by the loop right above it. If it were > 4 GiB, then so would the mmap_sz, and arena map creation would fail.
Quoted 8 lines
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/19448868610
Emil Tsalapatis
reviewerPATCH 4/4 selftests/bpf: add tests for the arena offset of globals
In reply to Emil TsalapatisAdd tests for the new libbpf globals arena offset logic. The tests cover all three cases: The globals being small enough to be placed at the maximum possible offset, being as large as the arena itself and being placed at the very beginning, and requiring an intermediate offset into the arena.
--- .../selftests/bpf/prog_tests/verifier.c | 6 ++ .../bpf/progs/verifier_arena_globals1.c | 60 ++++++++++++++++++ .../bpf/progs/verifier_arena_globals2.c | 49 +++++++++++++++ .../bpf/progs/verifier_arena_globals3.c | 61 +++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_globals1.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_globals2.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_globals3.c
tools/testing/selftests/bpf/prog_tests/verifier.c +6 -0
@@ -6,6 +6,9 @@#include "verifier_and.skel.h" #include "verifier_arena.skel.h" #include "verifier_arena_large.skel.h" +#include "verifier_arena_globals1.skel.h" +#include "verifier_arena_globals2.skel.h" +#include "verifier_arena_globals3.skel.h" #include "verifier_array_access.skel.h" #include "verifier_async_cb_context.skel.h" #include "verifier_basic_stack.skel.h"
@@ -147,6 +150,9 @@ static void run_tests_aux(const char *skel_name,void test_verifier_and (void ) {RUN (verifier_and); }void test_verifier_arena (void ) {RUN (verifier_arena); }void test_verifier_arena_large (void ) {RUN (verifier_arena_large); } +void test_verifier_arena_globals1 (void ) {RUN (verifier_arena_globals1); } +void test_verifier_arena_globals2 (void ) {RUN (verifier_arena_globals2); } +void test_verifier_arena_globals3 (void ) {RUN (verifier_arena_globals3); }void test_verifier_basic_stack (void ) {RUN (verifier_basic_stack); }void test_verifier_bitfield_write (void ) {RUN (verifier_bitfield_write); }void test_verifier_bounds (void ) {RUN (verifier_bounds); }
tools/testing/selftests/bpf/progs/verifier_arena_globals1.c new file +60 -0
60 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_arena_globals2.c new file +49 -0
49 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_arena_globals3.c new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
2.49.0