All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next] selftests/bpf: veristat: Minimize map size during verification
@ 2026-07-06 22:50 Emil Tsalapatis
  2026-07-06 23:03 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Emil Tsalapatis @ 2026-07-06 22:50 UTC (permalink / raw)
  To: bpf; +Cc: ast, andrii, memxor, daniel, eddyz87, 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_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.

Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com>
---
 tools/testing/selftests/bpf/veristat.c | 46 ++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/tools/testing/selftests/bpf/veristat.c b/tools/testing/selftests/bpf/veristat.c
index a7db6f04f7e1..8d374e140653 100644
--- a/tools/testing/selftests/bpf/veristat.c
+++ b/tools/testing/selftests/bpf/veristat.c
@@ -37,6 +37,10 @@
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #endif
 
+#ifndef div_round_up
+#define div_round_up(a, b) (((a) + (b) - 1) / (b))
+#endif
+
 enum stat_id {
 	VERDICT,
 	DURATION,
@@ -1238,6 +1242,17 @@ static void mask_unrelated_struct_ops_progs(struct bpf_object *obj,
 	}
 }
 
+/* size of global arena data in pages (minimum 1) */
+static size_t arena_global_pages(struct bpf_map *map)
+{
+	size_t min_bytes;
+
+	if (!bpf_map__initial_value(map, &min_bytes))
+		return 1;
+
+	return div_round_up(min_bytes, sysconf(_SC_PAGESIZE)) ?: 1;
+}
+
 static void fixup_obj_maps(struct bpf_object *obj)
 {
 	struct bpf_map *map;
@@ -1248,6 +1263,37 @@ 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_RHASH:
+		case BPF_MAP_TYPE_SOCKHASH:
+		case BPF_MAP_TYPE_DEVMAP_HASH:
+		case BPF_MAP_TYPE_LPM_TRIE:
+		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;
+
+		/* arenas must be big enough to fit all globals */
+		case BPF_MAP_TYPE_ARENA:
+			bpf_map__set_max_entries(map, arena_global_pages(map));
+			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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: veristat: Minimize map size during verification
  2026-07-06 22:50 [PATCH bpf-next] selftests/bpf: veristat: Minimize map size during verification Emil Tsalapatis
@ 2026-07-06 23:03 ` sashiko-bot
  2026-07-07  0:21   ` Emil Tsalapatis
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-07-06 23:03 UTC (permalink / raw)
  To: Emil Tsalapatis; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Setting `max_entries` to 1 for `BPF_MAP_TYPE_RHASH` maps causes map creation to fail if `map_extra` > 1.
--

--- Patch [1]: [PATCH bpf-next] selftests/bpf: veristat: Minimize map size during verification ---
commit a11623d3f77bb05764867d10c3ff357f2a8a1403
Author: Emil Tsalapatis <emil@etsalapatis.com>

selftests/bpf: veristat: Minimize map size during verification

The veristat tool loads objects and maps into the kernel to verify them.
This patch expands veristat to resize maps whose max_entries are not
relevant to verification to as low as possible to avoid -ENOMEM errors
on smaller test machines.

> diff --git a/tools/testing/selftests/bpf/veristat.c b/tools/testing/selftests/bpf/veristat.c
> index a7db6f04f7e14..8d374e1406532 100644
> --- a/tools/testing/selftests/bpf/veristat.c
> +++ b/tools/testing/selftests/bpf/veristat.c
[ ... ]
> @@ -1248,6 +1263,37 @@ 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_RHASH:
> +		case BPF_MAP_TYPE_SOCKHASH:
> +		case BPF_MAP_TYPE_DEVMAP_HASH:
> +		case BPF_MAP_TYPE_LPM_TRIE:
> +		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;

[Severity: Medium]
Will setting max_entries to 1 cause map creation to fail for
BPF_MAP_TYPE_RHASH maps that have map_extra set to greater than 1?

The kernel's rhtab_map_alloc_check() enforces that map_extra must be less
than or equal to max_entries. If a BPF program legitimately uses an RHASH
map with a map_extra preallocation hint greater than 1, this unconditional
minimization will cause an -EINVAL load failure, making veristat erroneously
report a verification failure.

Does veristat also need to adjust map_extra when reducing max_entries for
RHASH maps?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260706225052.35543-1-emil@etsalapatis.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: veristat: Minimize map size during verification
  2026-07-06 23:03 ` sashiko-bot
@ 2026-07-07  0:21   ` Emil Tsalapatis
  0 siblings, 0 replies; 3+ messages in thread
From: Emil Tsalapatis @ 2026-07-07  0:21 UTC (permalink / raw)
  To: sashiko-reviews, Emil Tsalapatis; +Cc: bpf

On Mon Jul 6, 2026 at 7:03 PM EDT, sashiko-bot wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [Medium] Setting `max_entries` to 1 for `BPF_MAP_TYPE_RHASH` maps causes map creation to fail if `map_extra` > 1.
> --
>
> --- Patch [1]: [PATCH bpf-next] selftests/bpf: veristat: Minimize map size during verification ---
> commit a11623d3f77bb05764867d10c3ff357f2a8a1403
> Author: Emil Tsalapatis <emil@etsalapatis.com>
>
> selftests/bpf: veristat: Minimize map size during verification
>
> The veristat tool loads objects and maps into the kernel to verify them.
> This patch expands veristat to resize maps whose max_entries are not
> relevant to verification to as low as possible to avoid -ENOMEM errors
> on smaller test machines.
>
>> diff --git a/tools/testing/selftests/bpf/veristat.c b/tools/testing/selftests/bpf/veristat.c
>> index a7db6f04f7e14..8d374e1406532 100644
>> --- a/tools/testing/selftests/bpf/veristat.c
>> +++ b/tools/testing/selftests/bpf/veristat.c
> [ ... ]
>> @@ -1248,6 +1263,37 @@ 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_RHASH:
>> +		case BPF_MAP_TYPE_SOCKHASH:
>> +		case BPF_MAP_TYPE_DEVMAP_HASH:
>> +		case BPF_MAP_TYPE_LPM_TRIE:
>> +		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;
>
> [Severity: Medium]
> Will setting max_entries to 1 cause map creation to fail for
> BPF_MAP_TYPE_RHASH maps that have map_extra set to greater than 1?
>
> The kernel's rhtab_map_alloc_check() enforces that map_extra must be less
> than or equal to max_entries. If a BPF program legitimately uses an RHASH
> map with a map_extra preallocation hint greater than 1, this unconditional
> minimization will cause an -EINVAL load failure, making veristat erroneously
> report a verification failure.
>
> Does veristat also need to adjust map_extra when reducing max_entries for
> RHASH maps?

The concern is valid, the fix imo is not. Rhashtable is resizable in the
first place, so we shouldn't be trying to reduce its maximum entries.
Same holds for LPM_TRIE and for arena (my worry was we do a memcg
reservation upfront but that's not the case). We can just remove all
three from the list to fix this issue and simplify the patch.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-07-07  0:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-06 22:50 [PATCH bpf-next] selftests/bpf: veristat: Minimize map size during verification Emil Tsalapatis
2026-07-06 23:03 ` sashiko-bot
2026-07-07  0:21   ` Emil Tsalapatis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.