selftests/bpf: veristat: Minimize map size during verification
Subsystems (3)
Emil Tsalapatis
reviewerPATCH v3 1/1 selftests/bpf: veristat: Minimize map size during verification
The veristat tool verifies that BPF objects along with their maps pass verification for a given kernel version. To do so, veristat loads the objects and maps into the kernel in order to pass them through the verifier.
Currently, veristat sizes the maps according to the max_entries field provided by the program author. Depending on the map type this field may be irrelevant to the verification process. However, loading a large map can fail because of -ENOMEM errors.
This is a problem when the map is supposed to run on large machines, but veristat tests it machines with significantly less RAM (e.g., CI). In that case veristat fails even if the program verifies.
Expand veristat to resize maps whose max_entries are not relevant to verification. Set the max_entries value as low as possible to avoid -ENOMEM errors.
CHANGELOG
=========
v2->v3
- Do not resize HASH_OF_MAPS because it can be declaratively populated from BPF at compile time (Sashiko)
v1->v2
- Removed unnecessary ARENA, LPM_TRIE, and RHASH handling (Sashiko)
--- tools/testing/selftests/bpf/veristat.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
tools/testing/selftests/bpf/veristat.c +23 -0
@@ -1248,6 +1248,29 @@ static void fixup_obj_maps(struct bpf_object *obj)/* fix up map size, if necessary */ switch (bpf_map__type (map)) { +/* +* if the verifier doesn't use max_entries +* then set to 1 to avoid -ENOMEM +*/ +case BPF_MAP_TYPE_HASH : +case BPF_MAP_TYPE_PERCPU_HASH : +case BPF_MAP_TYPE_LRU_HASH : +case BPF_MAP_TYPE_LRU_PERCPU_HASH : +case BPF_MAP_TYPE_SOCKHASH : +case BPF_MAP_TYPE_DEVMAP_HASH : +case BPF_MAP_TYPE_QUEUE : +case BPF_MAP_TYPE_STACK : +case BPF_MAP_TYPE_BLOOM_FILTER : +case BPF_MAP_TYPE_STACK_TRACE : +bpf_map__set_max_entries (map,1 ); +break ; + +/* ringbufs must be page-aligned */ +case BPF_MAP_TYPE_RINGBUF : +case BPF_MAP_TYPE_USER_RINGBUF : +bpf_map__set_max_entries (map,sysconf (_SC_PAGESIZE)); +break ; +case BPF_MAP_TYPE_SK_STORAGE :case BPF_MAP_TYPE_TASK_STORAGE :case BPF_MAP_TYPE_INODE_STORAGE :
Signature · 1 line
2.54.0
Quoted 19 lines · Emil Tsalapatis
The veristat tool verifies that BPF objects along with their maps pass verification for a given kernel version. To do so, veristat loads the objects and maps into the kernel in order to pass them through the verifier.
Currently, veristat sizes the maps according to the
max_entriesfield provided by the program author. Depending on the map type this field may be irrelevant to the verification process. However, loading a large map can fail because of-ENOMEMerrors.This is a problem when the map is supposed to run on large machines, but veristat tests it machines with significantly less RAM (e.g., CI). In that case veristat fails even if the program verifies.
Expand veristat to resize maps whose
max_entriesare not relevant to verification. Set themax_entriesvalue as low as possible to avoid-ENOMEMerrors.
Quoted 6 lines
Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Emil Tsalapatis <[email protected]>CHANGELOG
=========
for individual patches we normally don't commit changelog, so next time put it under --- below so it will be stripped out during applying (I cleaned this up manually)
Quoted 17 lines
v2->v3- Do not resize
HASH_OF_MAPSbecause it can be declaratively populated from BPF at compile time (Sashiko)
v1->v2- Removed unnecessary ARENA,
LPM_TRIE, and RHASH handling (Sashiko)--- tools/testing/selftests/bpf/veristat.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)tools/testing/selftests/bpf/veristat.c +0
-0
tools/testing/selftests/bpf/veristat.c static void fixup_obj_maps(struct bpf_object *obj) · line 1255
Quoted 8 lines
tools/testing/selftests/bpf/veristat.c +5
-0@@ -1248,6 +1248,29 @@ static void fixup_obj_maps(struct bpf_object *obj)/* fix up map size, if necessary */ switch (bpf_map__type (map)) { +/* +* if the verifier doesn't use max_entries +* then set to 1 to avoid -ENOMEM +*/ +case BPF_MAP_TYPE_HASH :
I'm wondering if we should add ARRAY here as that one is actually with high likelihood will be large, though there are some corner cases where size might matter (e.g., existing code omits error checking knowing that verifier will prove it can never fail; resizing to 1 might pessimize such check, probably, if index is larger than 1). Can you actually try the latter scenario with some small program, I'm curious how the verifier will behave?
anyways, applied to bpf-next, thanks!
Quoted 24 lines
tools/testing/selftests/bpf/veristat.c +18
-1+case BPF_MAP_TYPE_PERCPU_HASH : +case BPF_MAP_TYPE_LRU_HASH : +case BPF_MAP_TYPE_LRU_PERCPU_HASH : +case BPF_MAP_TYPE_SOCKHASH : +case BPF_MAP_TYPE_DEVMAP_HASH : +case BPF_MAP_TYPE_QUEUE : +case BPF_MAP_TYPE_STACK : +case BPF_MAP_TYPE_BLOOM_FILTER : +case BPF_MAP_TYPE_STACK_TRACE : +bpf_map__set_max_entries (map,1 ); +break ; + +/* ringbufs must be page-aligned */ +case BPF_MAP_TYPE_RINGBUF : +case BPF_MAP_TYPE_USER_RINGBUF : +bpf_map__set_max_entries (map,sysconf (_SC_PAGESIZE)); +break ; +case BPF_MAP_TYPE_SK_STORAGE :case BPF_MAP_TYPE_TASK_STORAGE :case BPF_MAP_TYPE_INODE_STORAGE : --2.54.0
Eduard Zingerman
maintainerOn Fri, 2026-07-10 at 14:44 -0700, Andrii Nakryiko wrote:
[...]
Quoted 18 lines
tools/testing/selftests/bpf/veristat.c static void fixup_obj_maps(struct bpf_object *obj) · line 1255
Quoted 9 lines
tools/testing/selftests/bpf/veristat.c +5
-0@@ -1248,6 +1248,29 @@ static void fixup_obj_maps(struct bpf_object *obj)/* fix up map size, if necessary */ switch (bpf_map__type (map)) { +/* +* if the verifier doesn't use max_entries +* then set to 1 to avoid -ENOMEM +*/ +case BPF_MAP_TYPE_HASH :I'm wondering if we should add ARRAY here as that one is actually with high likelihood will be large, though there are some corner cases where size might matter (e.g., existing code omits error checking knowing that verifier will prove it can never fail; resizing to 1 might pessimize such check, probably, if index is larger than 1). Can you actually try the latter scenario with some small program, I'm curious how the verifier will behave?
For array maps there are several complications: first is the constant maps handling and second is null check elision. The latter is somewhat nasty, see check_helper_call():
case RET_PTR_TO_MAP_VALUE:
...
if (func_id == BPF_FUNC_map_lookup_elem &&
can_elide_value_nullness(meta.map.ptr) && <-------- true for array maps
meta.const_map_key >= 0 &&
meta.const_map_key < meta.map.ptr->max_entries) <-- checks max_entries
ret_flag &= ~PTR_MAYBE_NULL;[...]
Quoted 24 lines · Eduard Zingerman
On Fri, 2026-07-10 at 14:44 -0700, Andrii Nakryiko wrote:
[...]
Quoted 18 lines
tools/testing/selftests/bpf/veristat.c static void fixup_obj_maps(struct bpf_object *obj) · line 1255
Quoted 9 lines
tools/testing/selftests/bpf/veristat.c +5
-0@@ -1248,6 +1248,29 @@ static void fixup_obj_maps(struct bpf_object *obj)/* fix up map size, if necessary */ switch (bpf_map__type (map)) { +/* +* if the verifier doesn't use max_entries +* then set to 1 to avoid -ENOMEM +*/ +case BPF_MAP_TYPE_HASH :I'm wondering if we should add ARRAY here as that one is actually with high likelihood will be large, though there are some corner cases where size might matter (e.g., existing code omits error checking knowing that verifier will prove it can never fail; resizing to 1 might pessimize such check, probably, if index is larger than 1). Can you actually try the latter scenario with some small program, I'm curious how the verifier will behave?
Quoted 4 lines
For array maps there are several complications: first is the constant maps handling and second is null check elision. The latter is somewhat nasty, see
check_helper_call():
yeah, I was thinking about that bit. Worst case, (valid) BPF code can have, say, 8-element array and have all the keys within [0, 7] range, by *decreasing* map to less than 8 elements we actually force NULL check.
Anyways, ARRAY, I believe, is one of the maps that caused this feature, so I think maybe we should still do a dirt-but-practical approach, and just cap ARRAY size if it exceeds some modest size (e.g., 128K, or something).
Oh well, capping non-ARRAYs is useful anyways, so let's keep it as is for now.
Quoted 9 lines
case RET_PTR_TO_MAP_VALUE: ... if (func_id == BPF_FUNC_map_lookup_elem && can_elide_value_nullness(meta.map.ptr) && <-------- true for array maps meta.const_map_key >= 0 && meta.const_map_key < meta.map.ptr->max_entries) <-- checks max_entries ret_flag &= ~PTR_MAYBE_NULL;[...]
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 11 lines · On Wed, 8 Jul 2026 15:32:39 -0400 you wrote:
The veristat tool verifies that BPF objects along with their maps pass verification for a given kernel version. To do so, veristat loads the objects and maps into the kernel in order to pass them through the verifier.
Currently, veristat sizes the maps according to the
max_entriesfield provided by the program author. Depending on the map type this field may be irrelevant to the verification process. However, loading a large map can fail because of-ENOMEMerrors.[...]
Here is the summary with links:
- [bpf-next,v3] selftests/bpf: veristat: Minimize map size during verification
https://git.kernel.org/bpf/bpf-next/c/e821c2238758
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html