BPF List
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Peng Hao" <flyingpenghao@gmail.com>, <andrii@kernel.org>
Cc: <bpf@vger.kernel.org>
Subject: Re: [PATCH] bpf: Bound the resizable hashtab batch buffers by max_entries
Date: Thu, 03 Dec 2026 03:43:07 +0000	[thread overview]
Message-ID: <DNASCSMUQWRU.2WUWIJPC7XBLX@gmail.com> (raw)
In-Reply-To: <20261202053720.92474-1-flyingpeng@tencent.com>

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

      parent reply	other threads:[~2026-12-03  3:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DNASCSMUQWRU.2WUWIJPC7XBLX@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=flyingpenghao@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox