Skip to contentSkip to conversation

bpf: Mix the seed into 8-byte resizable hashtab keys before folding

PATCH In review Peng Hao 2 participants 2 messages Nov 30 – Dec 1 BPF

lore
Share & tools
lore

Peng Hao

author
#0 lore raw

PATCH 1/1 bpf: Mix the seed into 8-byte resizable hashtab keys before folding

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")
---
 kernel/bpf/hashtab.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/hashtab.c +1 -1
index a2d0bfd4c3ec..6cdd8e1dc2bd 100644
@@ -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 = {
Signature · 1 line
2.43.7
#1 lore raw
Quoted 34 lines · Peng Hao

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")
---
 kernel/bpf/hashtab.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/hashtab.c +0 -0
index a2d0bfd4c3ec..6cdd8e1dc2bd 100644

kernel/bpf/hashtab.c static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed · line 3032

Quoted 5 lines
kernel/bpf/hashtab.c +1 -1
@@ -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