BPF List
 help / color / mirror / Atom feed
* [PATCH] bpf: Bound the resizable hashtab batch buffers by max_entries
@ 2026-12-02  5:37 Peng Hao
  2026-12-02  5:43 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Peng Hao @ 2026-12-02  5:37 UTC (permalink / raw)
  To: ast, andrii; +Cc: bpf

__rhtab_map_lookup_and_delete_batch() allocates the key, value and
element pointer buffers for attr->batch.count entries before it looks at
the map. count comes straight from userspace and is not limited by the
size of the map, so a lookup batch on a one-element map with a 4K value
and count = 65536 has the kernel allocate about 257 MB, and
count = 1048576 asks for about 4 GB and fails with -ENOMEM instead of
returning the element.

A batch never returns more than max_entries elements, so clamp the
buffer size to that. The regular hash map sizes its buffers by bucket
size instead, so it doesn't have this problem.

Fixes: 16b4d3e2fb24 ("bpf: Implement resizable hashmap basic functions")
Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
 kernel/bpf/hashtab.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index 6cdd8e1dc2bd..278718d1948b 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -3503,6 +3503,9 @@ static int __rhtab_map_lookup_and_delete_batch(struct bpf_map *map,
 	if (put_user(0, &uattr->batch.count))
 		return -EFAULT;
 
+	/* A batch can't return more elements than the map can hold. */
+	max_count = min(max_count, map->max_entries);
+
 	key_size = map->key_size;
 	value_size = map->value_size;
 
-- 
2.43.7


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

end of thread, other threads:[~2026-12-02  5:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  5:37 [PATCH] bpf: Bound the resizable hashtab batch buffers by max_entries Peng Hao
2026-12-02  5:43 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox