* [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
2026-12-03 3:43 ` Alexei Starovoitov
0 siblings, 2 replies; 3+ 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] 3+ messages in thread
* Re: [PATCH] bpf: Bound the resizable hashtab batch buffers by max_entries
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
2026-12-03 3:43 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-02 5:43 UTC (permalink / raw)
To: Peng Hao; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] bpf: incorrect Fixes tag for
__rhtab_map_lookup_and_delete_batch()
--
commit 9770249174f01abb3a3d1236fe3c14f234ab367c
Author: Peng Hao <flyingpenghao@gmail.com>
> bpf: Bound the resizable hashtab batch buffers by max_entries
>
> __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>
[Severity: Low]
__rhtab_map_lookup_and_delete_batch() was introduced in 818e00848227
("bpf: Implement iteration ops for resizable hashtab") rather than
16b4d3e2fb24 ("bpf: Implement resizable hashmap basic functions"). Should
the Fixes tag be updated to:
Fixes: 818e00848227 ("bpf: Implement iteration ops for resizable hashtab")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202053720.92474-1-flyingpeng@tencent.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] bpf: Bound the resizable hashtab batch buffers by max_entries
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
@ 2026-12-03 3:43 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-12-03 3:43 UTC (permalink / raw)
To: Peng Hao, andrii; +Cc: bpf
On Tue, Dec 01, 2026 at 09:37 PM Peng Hao <flyingpenghao@gmail.com> wrote:
> 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.
max_entries is not a useful bound for rhash.
rhtab_map_alloc() accepts up to 1 << 31 and allocates nothing based on it.
A large max_entries with few elements is what this map is for.
One element in the map, 4K value, this patch applied:
max_entries count max_count result
----------- ------- --------- ------------------
1 65536 1 fixed
1048576 65536 65536 257 MB, as before
1048576 1048576 1048576 -ENOMEM, as before
It's not "never" either. rht_grow_above_max() runs under the bucket lock.
Two CPUs insert into two buckets and both see nelems == 0.
Then a map with max_entries == 1 has 2 elements.
> Fixes: 16b4d3e2fb24 ("bpf: Implement resizable hashmap basic functions")
Wrong tag. There are no batch ops in that commit. It's
commit 818e00848227 ("bpf: Implement iteration ops for resizable hashtab")
Pls cc Mykyta on respin.
> + /* A batch can't return more elements than the map can hold. */
> + max_count = min(max_count, map->max_entries);
Use ht.nelems, like rhtab_map_mem_usage() does.
Then the buffers follow what the map holds.
A short batch is not new. The loop stops early on need_resched() today.
More than 2 GB of real values in one batch is still -ENOMEM.
On top of the patch. I built hashtab.c, not rhash.c, and ran nothing:
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index ce46faf..0ff2673 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -3503,8 +3503,8 @@ 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);
+ /* If the map grows after this point the next batch returns the rest. */
+ max_count = clamp_t(u32, atomic_read(&rhtab->ht.nelems), 1, max_count);
key_size = map->key_size;
value_size = map->value_size;
pw-bot: cr
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 3:43 UTC | newest]
Thread overview: 3+ 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
2026-12-03 3:43 ` Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox