* [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; 4+ 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] 4+ messages in thread
* Re: [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding
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
2026-12-02 4:09 ` Hao Peng
0 siblings, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2026-12-01 3:17 UTC (permalink / raw)
To: Peng Hao, ast, andrii; +Cc: bpf
On Mon Nov 30, 2026 at 2:03 PM UTC, Peng Hao wrote:
> 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);
Looks like real issue, but please benchmark it first and cc Mykyta.
pw-bot: cr
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding
2026-12-01 3:17 ` Alexei Starovoitov
@ 2026-12-02 4:09 ` Hao Peng
2026-12-02 6:29 ` Mykyta Yatsenko
0 siblings, 1 reply; 4+ messages in thread
From: Hao Peng @ 2026-12-02 4:09 UTC (permalink / raw)
To: Alexei Starovoitov; +Cc: ast, andrii, bpf, yatsenko
On Tue, Dec 1, 2026 at 11:17 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Mon Nov 30, 2026 at 2:03 PM UTC, Peng Hao wrote:
> > 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);
>
> Looks like real issue, but please benchmark it first and cc Mykyta.
>
Benchmarked on a Kunpeng 920 (arm64) guest, non-debug kernels that only
differ in rhtab_hashfn_long(), bpf-rhashmap-lookup --key_size 8, single
producer, 3 alternating boots x 2 runs per kernel, M lookups/sec:
keys | max | nr | fold (current) | jhash | delta
------+-----+------+------------------+------------------+-------
seq | 1K | 750 | 104.12 +- 3.48 | 44.70 +- 0.55 | -57.1%
seq | 1K | 1K | 94.17 +- 2.10 | 48.18 +- 1.67 | -48.8%
seq | 1M | 750K | 81.15 +- 3.34 | 18.42 +- 0.33 | -77.3%
seq | 1M | 1M | 78.18 +- 7.35 | 18.69 +- 0.70 | -76.1%
rand | 1K | 750 | 54.00 +- 1.24 | 42.36 +- 1.13 | -21.6%
rand | 1K | 1K | 60.79 +- 0.66 | 47.17 +- 0.90 | -22.4%
rand | 1M | 750K | 20.12 +- 0.19 | 17.62 +- 0.15 | -12.4%
rand | 1M | 1M | 19.39 +- 0.12 | 17.41 +- 0.10 | -10.2%
seq = the benchmark's default keys (low word i + 1, high word constant)
rand = random 8-byte keys (getrandom())
The "seq" rows mostly measure the key pattern rather than the hash: XOR
folding maps the sequential keys to consecutive buckets, so a 1M lookup
sweep walks the bucket array in order. With random keys the current fold
drops to the same ~19 M/s at 1M that jhash gets with sequential keys.
With random keys jhash_2words() costs about 5ns per lookup: 16.4 vs 21.2
ns at 1K entries, 51.6 vs 57.4 ns at 1M.
On the other side, inserting 20000 keys of the form (i << 32 | i), e.g.
bpf_get_current_pid_tgid() of single-threaded tasks, into a map with
max_entries 40000:
current fold: 19845 of 20000 inserts fail with -EBUSY, 0.83 us/lookup
jhash: 0 failures, 0.33 us/lookup
> pw-bot: cr
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding
2026-12-02 4:09 ` Hao Peng
@ 2026-12-02 6:29 ` Mykyta Yatsenko
0 siblings, 0 replies; 4+ messages in thread
From: Mykyta Yatsenko @ 2026-12-02 6:29 UTC (permalink / raw)
To: Hao Peng, Alexei Starovoitov; +Cc: ast, andrii, bpf, yatsenko
On 12/2/26 4:09 AM, Hao Peng wrote:
> On Tue, Dec 1, 2026 at 11:17 AM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
>>
>> On Mon Nov 30, 2026 at 2:03 PM UTC, Peng Hao wrote:
>>> 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);
>>
>> Looks like real issue, but please benchmark it first and cc Mykyta.
>>
> Benchmarked on a Kunpeng 920 (arm64) guest, non-debug kernels that only
> differ in rhtab_hashfn_long(), bpf-rhashmap-lookup --key_size 8, single
> producer, 3 alternating boots x 2 runs per kernel, M lookups/sec:
>
> keys | max | nr | fold (current) | jhash | delta
> ------+-----+------+------------------+------------------+-------
> seq | 1K | 750 | 104.12 +- 3.48 | 44.70 +- 0.55 | -57.1%
> seq | 1K | 1K | 94.17 +- 2.10 | 48.18 +- 1.67 | -48.8%
> seq | 1M | 750K | 81.15 +- 3.34 | 18.42 +- 0.33 | -77.3%
> seq | 1M | 1M | 78.18 +- 7.35 | 18.69 +- 0.70 | -76.1%
> rand | 1K | 750 | 54.00 +- 1.24 | 42.36 +- 1.13 | -21.6%
> rand | 1K | 1K | 60.79 +- 0.66 | 47.17 +- 0.90 | -22.4%
> rand | 1M | 750K | 20.12 +- 0.19 | 17.62 +- 0.15 | -12.4%
> rand | 1M | 1M | 19.39 +- 0.12 | 17.41 +- 0.10 | -10.2%
>
My results: jhash_2words() makes lookups with 8-byte keys slower:
-35% at 1K and -74% to -77% at 1M.
We can use another simple hash function to protect from equal halves
hashing to same values, for example:
static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed)
{
u64 x = *(const unsigned long *)data ^ seed;
return (x ^ (x >> 33)) * GOLDEN_RATIO_64 >> 32;
}
Of course it does not protect from malicious uses.
> seq = the benchmark's default keys (low word i + 1, high word constant)
> rand = random 8-byte keys (getrandom())
>
> The "seq" rows mostly measure the key pattern rather than the hash: XOR
> folding maps the sequential keys to consecutive buckets, so a 1M lookup
> sweep walks the bucket array in order. With random keys the current fold
> drops to the same ~19 M/s at 1M that jhash gets with sequential keys.
> With random keys jhash_2words() costs about 5ns per lookup: 16.4 vs 21.2
> ns at 1K entries, 51.6 vs 57.4 ns at 1M.
>
> On the other side, inserting 20000 keys of the form (i << 32 | i), e.g.
> bpf_get_current_pid_tgid() of single-threaded tasks, into a map with
> max_entries 40000:
>
> current fold: 19845 of 20000 inserts fail with -EBUSY, 0.83 us/lookup
> jhash: 0 failures, 0.33 us/lookup
>
>
>> pw-bot: cr
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 6:29 UTC | newest]
Thread overview: 4+ 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
2026-12-02 4:09 ` Hao Peng
2026-12-02 6:29 ` Mykyta Yatsenko
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.