Skip to contentSkip to conversation

bpf: Bound the resizable hashtab batch buffers by max_entries

PATCH In review Peng Hao 3 participants 3 messages Dec 2 – Dec 3 BPF

lore
Share & tools
lore

Peng Hao

author
#0 lore raw

PATCH 1/1 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")
---
 kernel/bpf/hashtab.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/hashtab.c +3 -0
index 6cdd8e1dc2bd..278718d1948b 100644
@@ -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;
 
Signature · 1 line
2.43.7
#1 lore raw
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() --

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 <[email protected]>

Quoted 10 lines

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.

Quoted 6 lines

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")

[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")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Peng Hao
Quoted 3 lines · Peng Hao

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.

Quoted 1 line
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.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	/* 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:

kernel/bpf/hashtab.c +2 -2
index ce46faf..0ff2673 100644
@@ -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