* [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; 8+ 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] 8+ 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; 8+ 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] 8+ 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; 8+ 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] 8+ 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
2026-12-03 4:03 ` Alexei Starovoitov
0 siblings, 1 reply; 8+ 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] 8+ messages in thread
* Re: [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding
2026-12-02 6:29 ` Mykyta Yatsenko
@ 2026-12-03 4:03 ` Alexei Starovoitov
2026-12-04 1:11 ` Hao Peng
0 siblings, 1 reply; 8+ messages in thread
From: Alexei Starovoitov @ 2026-12-03 4:03 UTC (permalink / raw)
To: Mykyta Yatsenko, Hao Peng; +Cc: andrii, bpf, yatsenko
On Tue, Dec 01, 2026 at 10:29 PM Mykyta Yatsenko <mykyta.yatsenko5@gmail.com> wrote:
> My results: jhash_2words() makes lookups with 8-byte keys slower:
> -35% at 1K and -74% to -77% at 1M.
That looks like the default keys of the bench, key[0] = i + 1.
On random keys Hao sees -10% to -22%. It's still a lot for this path.
> 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;
> }
Makes sense to me.
It fixes pointer keys too. Today 64 byte aligned pointers from one 4G
range use one bucket out of 64. They hit the same -EBUSY.
> Of course it does not protect from malicious uses.
That's ok.
Hao,
pls respin with this function and add its seq and rand numbers.
pw-bot: cr
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding
2026-12-03 4:03 ` Alexei Starovoitov
@ 2026-12-04 1:11 ` Hao Peng
2026-12-04 1:35 ` Alexei Starovoitov
0 siblings, 1 reply; 8+ messages in thread
From: Hao Peng @ 2026-12-04 1:11 UTC (permalink / raw)
To: Alexei Starovoitov; +Cc: Mykyta Yatsenko, andrii, bpf, yatsenko
On Thu, Dec 3, 2026 at 12:03 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Tue, Dec 01, 2026 at 10:29 PM Mykyta Yatsenko <mykyta.yatsenko5@gmail.com> wrote:
> > My results: jhash_2words() makes lookups with 8-byte keys slower:
> > -35% at 1K and -74% to -77% at 1M.
>
> That looks like the default keys of the bench, key[0] = i + 1.
> On random keys Hao sees -10% to -22%. It's still a lot for this path.
>
> > 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;
> > }
>
> Makes sense to me.
> It fixes pointer keys too. Today 64 byte aligned pointers from one 4G
> range use one bucket out of 64. They hit the same -EBUSY.
>
> > Of course it does not protect from malicious uses.
>
> That's ok.
>
> Hao,
> pls respin with this function and add its seq and rand numbers.
>
┌───────── ┬──── ┬───── ─┬────────┐
│ keys max nr │ fold. │ gr (new) │ gr vs fold │
├──────── ┼─────┼───────┼────────┤
│ seq 1K 750 │ 103.7 │ 85.5 │ −17.5% │
├──────── ┼─────┼──────┼─────── ─┤
│ seq 1K 1K │ 95.0 │ 81.2 │ −14.5% │
├──────── ┼─────┼──────┼──────── ┤
│ seq 1M 750K│ 82.4 │ 20.1 │ −75.7% │
├──────── ┼─────┼──────┼─────────┤
│ seq 1M 1M │ 79.1 │ 18.2 │ −77.0% │
├─────────┼────┼──────┼─────────┤
│ rand 1K 750 │ 53.6 │ 47.8 │ −10.8% │
├────────┼─────┼──────┼─────────┤
│ rand 1K 1K │ 61.6 │ 53.5 │ −13.2% │
├─────────┼────┼──────┼─────────┤
│ rand 1M 750K │ 20.2 │ 18.7 │ −7.8% │
├─────────┼────┼──────┼─────────┤
│ rand 1M 1M │ 19.4 │ 18.3 │ −5.3% │
└─────────┴────┴──────┴─────────
> pw-bot: cr
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding
2026-12-04 1:11 ` Hao Peng
@ 2026-12-04 1:35 ` Alexei Starovoitov
2026-12-04 5:55 ` Mykyta Yatsenko
0 siblings, 1 reply; 8+ messages in thread
From: Alexei Starovoitov @ 2026-12-04 1:35 UTC (permalink / raw)
To: Hao Peng; +Cc: Mykyta Yatsenko, andrii, bpf, yatsenko
On Thu, Dec 03, 2026 at 05:11 PM Hao Peng <flyingpenghao@gmail.com> wrote:
> │ keys max nr │ fold. │ gr (new) │ gr vs fold │
> │ seq 1M 750K│ 82.4 │ 20.1 │ −75.7% │
> ├──────── ┼─────┼──────┼─────────┤
> │ seq 1M 1M │ 79.1 │ 18.2 │ −77.0% │
Same as jhash. gr scatters sequential keys over buckets[] too.
> │ rand 1K 750 │ 53.6 │ 47.8 │ −10.8% │
> ├────────┼─────┼──────┼─────────┤
> │ rand 1K 1K │ 61.6 │ 53.5 │ −13.2% │
> ├─────────┼────┼──────┼─────────┤
> │ rand 1M 750K │ 20.2 │ 18.7 │ −7.8% │
> ├─────────┼────┼──────┼─────────┤
> │ rand 1M 1M │ 19.4 │ 18.3 │ −5.3% │
That's about half of the jhash cost.
Pls rerun the table with the diff below.
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index b849110..d882de5 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -4,6 +4,7 @@
*/
#include <linux/bpf.h>
#include <linux/btf.h>
+#include <linux/hash.h>
#include <linux/jhash.h>
#include <linux/filter.h>
#include <linux/rculist_nulls.h>
@@ -3025,11 +3026,18 @@ static __always_inline int rhtab_key_cmp_long(struct rhashtable_compare_arg *arg
return key1 != *(const unsigned long *)key2->data;
}
+/*
+ * rht_bucket_index() takes the low bits of the hash, so fold the top bits of
+ * the product into them. The low 3 bits of the key keep 8 sequential keys in
+ * one cache line of buckets.
+ */
static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed)
{
u64 k = *(const unsigned long *)data;
+ u64 x = (k >> 3) ^ seed;
- return (u32)(k ^ (k >> 32)) ^ seed;
+ x = (x ^ (x >> 33)) * GOLDEN_RATIO_64;
+ return (x >> 32) ^ (x >> 48) ^ (k & 7);
}
static const struct rhashtable_params rhtab_params_long = {
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding
2026-12-04 1:35 ` Alexei Starovoitov
@ 2026-12-04 5:55 ` Mykyta Yatsenko
0 siblings, 0 replies; 8+ messages in thread
From: Mykyta Yatsenko @ 2026-12-04 5:55 UTC (permalink / raw)
To: Alexei Starovoitov, Hao Peng; +Cc: andrii, bpf, yatsenko
On 12/4/26 1:35 AM, Alexei Starovoitov wrote:
> On Thu, Dec 03, 2026 at 05:11 PM Hao Peng <flyingpenghao@gmail.com> wrote:
>> │ keys max nr │ fold. │ gr (new) │ gr vs fold │
>
>> │ seq 1M 750K│ 82.4 │ 20.1 │ −75.7% │
>> ├──────── ┼─────┼──────┼─────────┤
>> │ seq 1M 1M │ 79.1 │ 18.2 │ −77.0% │
>
> Same as jhash. gr scatters sequential keys over buckets[] too.
>
>> │ rand 1K 750 │ 53.6 │ 47.8 │ −10.8% │
>> ├────────┼─────┼──────┼─────────┤
>> │ rand 1K 1K │ 61.6 │ 53.5 │ −13.2% │
>> ├─────────┼────┼──────┼─────────┤
>> │ rand 1M 750K │ 20.2 │ 18.7 │ −7.8% │
>> ├─────────┼────┼──────┼─────────┤
>> │ rand 1M 1M │ 19.4 │ 18.3 │ −5.3% │
>
> That's about half of the jhash cost.
>
> Pls rerun the table with the diff below.
>
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index b849110..d882de5 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
> @@ -4,6 +4,7 @@
> */
> #include <linux/bpf.h>
> #include <linux/btf.h>
> +#include <linux/hash.h>
> #include <linux/jhash.h>
> #include <linux/filter.h>
> #include <linux/rculist_nulls.h>
> @@ -3025,11 +3026,18 @@ static __always_inline int rhtab_key_cmp_long(struct rhashtable_compare_arg *arg
> return key1 != *(const unsigned long *)key2->data;
> }
>
> +/*
> + * rht_bucket_index() takes the low bits of the hash, so fold the top bits of
> + * the product into them. The low 3 bits of the key keep 8 sequential keys in
> + * one cache line of buckets.
> + */
> static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed)
> {
> u64 k = *(const unsigned long *)data;
> + u64 x = (k >> 3) ^ seed;
>
> - return (u32)(k ^ (k >> 32)) ^ seed;
> + x = (x ^ (x >> 33)) * GOLDEN_RATIO_64;
> + return (x >> 32) ^ (x >> 48) ^ (k & 7);
> }
>
> static const struct rhashtable_params rhtab_params_long = {
I tried this variant:
static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed)
{
u64 k = *(const unsigned long *)data;
u64 x = ((k >> 3) ^ seed) * GOLDEN_RATIO_64;
return (u32)swab64(x) ^ (k & 7);
}
which performed a bit better on my setup:
keys max/nr xor jhash GR Alexei GR swap
seq 1K/750 81.86 52.90 (-35.4%) 63.49 (-22.4%) 72.34 (-11.6%)
seq 1K/1K 81.32 51.61 (-36.5%) 65.86 (-19.0%) 72.40 (-11.0%)
seq 1M/750K 76.91 19.07 (-75.2%) 48.72 (-36.7%) 48.70 (-36.7%)
seq 1M/1M 76.12 16.52 (-78.3%) 44.74 (-41.2%) 47.19 (-38.0%)
rand 1K/750 67.45 51.09 (-24.3%) 59.08 (-12.4%) 61.22 (-9.2%)
rand 1K/1K 65.89 49.44 (-25.0%) 58.20 (-11.7%) 58.83 (-10.7%)
rand 1M/750K 20.56 18.57 (-9.7%) 20.12 (-2.1%) 19.97 (-2.9%)
rand 1M/1M 17.55 15.88 (-9.5%) 16.92 (-3.6%) 17.18 (-2.1%)
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-04 5:55 UTC | newest]
Thread overview: 8+ 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
2026-12-03 4:03 ` Alexei Starovoitov
2026-12-04 1:11 ` Hao Peng
2026-12-04 1:35 ` Alexei Starovoitov
2026-12-04 5:55 ` Mykyta Yatsenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox