BPF List
 help / color / mirror / Atom feed
* [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding
@ 2026-11-30 14:03 Peng Hao
  2026-12-01  3:17 ` Alexei Starovoitov
  0 siblings, 1 reply; 2+ messages in thread
From: Peng Hao @ 2026-11-30 14:03 UTC (permalink / raw)
  To: ast, andrii; +Cc: bpf

rhtab_hashfn_long() folds the two halves of the key together and only
then XORs in the seed:

	return (u32)(k ^ (k >> 32)) ^ seed;

Two keys that fold to the same value therefore collide under every seed,
so rhashtable's defence against long chains, a rehash with a new seed,
can't separate them. Keys with equal halves all hash to the seed itself;
bpf_get_current_pid_tgid() of a single-threaded task is such a key.

Inserting 20000 keys of the form (i << 32 | i) into a
BPF_MAP_TYPE_RHASH map with max_entries 40000 on arm64:

  random   n=20000 insert 0.285s lookup 0.154s (7.7 us/lookup) insert failures 0
  collide  n=20000 insert 3.896s lookup 2.460s (123.0 us/lookup) insert failures 18447 [Device or resource busy x18447]

Almost all inserts fail with -EBUSY once the chain is too long and the
rehash doesn't help, and lookups of the keys that did go in degrade to a
linear scan of one bucket.

Use jhash_2words(), as htab_map_hash() does for 8-byte keys, so that the
seed affects every bit of the result.

Fixes: 9dcbb5045fe5 ("bpf: Optimize word-sized keys for resizable hashtable")
Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
 kernel/bpf/hashtab.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index a2d0bfd4c3ec..6cdd8e1dc2bd 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -3029,7 +3029,7 @@ static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed
 {
 	u64 k = *(const unsigned long *)data;
 
-	return (u32)(k ^ (k >> 32)) ^ seed;
+	return jhash_2words((u32)k, (u32)(k >> 32), seed);
 }
 
 static const struct rhashtable_params rhtab_params_long = {
-- 
2.43.7


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

end of thread, other threads:[~2026-12-01  3:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 14:03 [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding Peng Hao
2026-12-01  3:17 ` Alexei Starovoitov

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