Skip to contentSkip to conversation

selftests/bpf: veristat: Minimize map size during verification

PATCH RESEND Superseded Emil Tsalapatis 2 participants 2 messages Jul 7

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

PATCH RESEND v2 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
=========

v1->v2

- Removed unnecessary ARENA, LPM_TRIE, and RHASH handling (Sashiko)

---

Resending because the changelog's formatting was messing with the patch's actual formatting.

 tools/testing/selftests/bpf/veristat.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
tools/testing/selftests/bpf/veristat.c +24 -0
index a7db6f04f7e1..a2a724c3893d 100644
@@ -1248,6 +1248,30 @@ 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_HASH_OF_MAPS:
+		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
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] veristat overrides BPF_MAP_TYPE_HASH_OF_MAPS max_entries to 1, causing map preparation to fail…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] veristat overrides BPF_MAP_TYPE_HASH_OF_MAPS max_entries to 1, causing map preparation to fail for declaratively initialized maps --

--- Patch [1]: [PATCH RESEND bpf-next v2] selftests/bpf: veristat: Minimize map size during verification --- commit be52ac6965be2ec63632786bb06d60b4e6152bc6
Author: Emil Tsalapatis <[email protected]>

selftests/bpf: veristat: Minimize map size during verification

This commit updates veristat to resize BPF maps where max_entries is irrelevant to the verification process. By shrinking these maps to the minimum size, it avoids -ENOMEM errors on memory-constrained systems.

Quoted 6 lines
tools/testing/selftests/bpf/veristat.c +0 -0
@@ -1248,6 +1248,30 @@ static void fixup_obj_maps(struct bpf_object *obj)
 
 		/* fix up map size, if necessary */

tools/testing/selftests/bpf/veristat.c static void fixup_obj_maps(struct bpf_object *obj) · line 1259

Quoted 10 lines
tools/testing/selftests/bpf/veristat.c +9 -0
 		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_HASH_OF_MAPS:

[Severity: Medium]
Will this cause map preparation to fail with -E2BIG if there are multiple inner maps declaratively initialized?

In fixup_obj_maps(), if the outer map's max_entries is forcibly overridden to 1, when libbpf later attempts to populate the slots via bpf_map_update_elem() during bpf_object__prepare(), the first insertion succeeds, but subsequent insertions fail because the hash map's capacity is constrained to 1.

Does this mean tests that declaratively initialize multiple inner maps (such as test_btf_map_in_map.bpf.o) will fail to load with invalid map FDs, subsequently causing the verifier to reject the programs?

Quoted 1 line
tools/testing/selftests/bpf/veristat.c +1 -0
+		case BPF_MAP_TYPE_SOCKHASH:

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1