Skip to contentSkip to conversation

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

PATCH In review Peng Hao 3 participants 9 messages Nov 30 – Dec 5 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

author
#2 lore raw
Quoted 39 lines · Alexei Starovoitov
Quoted 37 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
@@ -3029,7 +3029,7 @@ static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed
 {
      u64 k = *(const unsigned long *)data;
 
Quoted 5 lines

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

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

Quoted 1 line

pw-bot: cr

#3 lore raw
Quoted 50 lines · Hao Peng
Quoted 39 lines · Alexei Starovoitov
Quoted 37 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
@@ -3029,7 +3029,7 @@ static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed
 {
      u64 k = *(const unsigned long *)data;
 
Quoted 5 lines

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

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

Quoted 11 lines
    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.

Quoted 20 lines

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

Quoted 1 line

pw-bot: cr

#4 lore raw
Quoted 2 lines · Mykyta Yatsenko

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.

Quoted 9 lines

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.

Quoted 1 line

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

author
#5 lore raw
Quoted 22 lines · Alexei Starovoitov
Quoted 2 lines · Mykyta Yatsenko

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.

Quoted 9 lines

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.

Quoted 7 lines
Quoted 1 line

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%         │
  └─────────┴────┴──────┴─────────
Quoted 1 line

pw-bot: cr

#6 lore raw
Quoted 5 lines · Hao Peng

│ 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.

Quoted 7 lines
  │ 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.

kernel/bpf/hashtab.c +9 -1
index b849110..d882de5 100644
@@ -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 = {
#7 lore raw
Quoted 43 lines · Alexei Starovoitov
Quoted 5 lines · Hao Peng

│ 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.

Quoted 7 lines
  │ 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.

kernel/bpf/hashtab.c +6 -0
index b849110..d882de5 100644
@@ -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)

kernel/bpf/hashtab.c static __always_inline int rhtab_key_cmp_long(struct rhashtable_compare_arg *arg · line 3043

Quoted 10 lines
kernel/bpf/hashtab.c +3 -1
 {
 	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%)
#8 lore raw
Quoted 8 lines · Mykyta Yatsenko

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);
}

yep. Even better.
All bits of k >> 3 reach the top byte of the product. swab64() moves that byte to the low bits of the hash, and rht_bucket_index() takes those.

Quoted 3 lines
keys  max/nr   xor     jhash            GR Alexei        GR swap
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%)

I think it's almost there. not quite xor, but clearly better than jhash.

Could you do one more experiment and keep 6 bits of the key instead of 3 ?

	u64 x = ((k >> 6) ^ seed) * GOLDEN_RATIO_64;

	return (u32)swab64(x) ^ (k & 63);

Then 64 sequential keys use 8 cache lines in a row and hopefully hw prefetcher will do its job.