From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: Hao Peng <flyingpenghao@gmail.com>,
Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: ast@kernel.org, andrii@kernel.org, bpf@vger.kernel.org,
yatsenko@meta.com
Subject: Re: [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding
Date: Wed, 2 Dec 2026 06:29:07 +0000 [thread overview]
Message-ID: <cd92450a-b149-425e-b4f6-397c1d0479e4@gmail.com> (raw)
In-Reply-To: <CAPm50aKE55mmQ6wmaJVcJxY-e+TK1N9p9CEwFu==aadr-NnKsw@mail.gmail.com>
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
>
prev parent reply other threads:[~2026-12-02 6:29 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 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=cd92450a-b149-425e-b4f6-397c1d0479e4@gmail.com \
--to=mykyta.yatsenko5@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=flyingpenghao@gmail.com \
--cc=yatsenko@meta.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