Commit c0203475 authored by Daniel Borkmann's avatar Daniel Borkmann

bpf: use per htab salt for bucket hash

All BPF hash and LRU maps currently have a known and global seed
we feed into jhash() which is 0. This is suboptimal, thus fix it
by generating a random seed upon hashtab setup time which we can
later on feed into jhash() on lookup, update and deletions.

Fixes: 0f8e4bd8 ("bpf: add hashtable type of eBPF maps")
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Reviewed-by: default avatarEduardo Valentin <eduval@amazon.com>
parent 9b2e0388
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <linux/jhash.h> #include <linux/jhash.h>
#include <linux/filter.h> #include <linux/filter.h>
#include <linux/rculist_nulls.h> #include <linux/rculist_nulls.h>
#include <linux/random.h>
#include <uapi/linux/btf.h> #include <uapi/linux/btf.h>
#include "percpu_freelist.h" #include "percpu_freelist.h"
#include "bpf_lru_list.h" #include "bpf_lru_list.h"
...@@ -41,6 +42,7 @@ struct bpf_htab { ...@@ -41,6 +42,7 @@ struct bpf_htab {
atomic_t count; /* number of elements in this hashtable */ atomic_t count; /* number of elements in this hashtable */
u32 n_buckets; /* number of hash buckets */ u32 n_buckets; /* number of hash buckets */
u32 elem_size; /* size of each element in bytes */ u32 elem_size; /* size of each element in bytes */
u32 hashrnd;
}; };
/* each htab element is struct htab_elem + key + value */ /* each htab element is struct htab_elem + key + value */
...@@ -371,6 +373,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) ...@@ -371,6 +373,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
if (!htab->buckets) if (!htab->buckets)
goto free_htab; goto free_htab;
htab->hashrnd = get_random_int();
for (i = 0; i < htab->n_buckets; i++) { for (i = 0; i < htab->n_buckets; i++) {
INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i); INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
raw_spin_lock_init(&htab->buckets[i].lock); raw_spin_lock_init(&htab->buckets[i].lock);
...@@ -402,9 +405,9 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) ...@@ -402,9 +405,9 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
return ERR_PTR(err); return ERR_PTR(err);
} }
static inline u32 htab_map_hash(const void *key, u32 key_len) static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
{ {
return jhash(key, key_len, 0); return jhash(key, key_len, hashrnd);
} }
static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash) static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
...@@ -470,7 +473,7 @@ static void *__htab_map_lookup_elem(struct bpf_map *map, void *key) ...@@ -470,7 +473,7 @@ static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
key_size = map->key_size; key_size = map->key_size;
hash = htab_map_hash(key, key_size); hash = htab_map_hash(key, key_size, htab->hashrnd);
head = select_bucket(htab, hash); head = select_bucket(htab, hash);
...@@ -597,7 +600,7 @@ static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key) ...@@ -597,7 +600,7 @@ static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
if (!key) if (!key)
goto find_first_elem; goto find_first_elem;
hash = htab_map_hash(key, key_size); hash = htab_map_hash(key, key_size, htab->hashrnd);
head = select_bucket(htab, hash); head = select_bucket(htab, hash);
...@@ -824,7 +827,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value, ...@@ -824,7 +827,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
key_size = map->key_size; key_size = map->key_size;
hash = htab_map_hash(key, key_size); hash = htab_map_hash(key, key_size, htab->hashrnd);
b = __select_bucket(htab, hash); b = __select_bucket(htab, hash);
head = &b->head; head = &b->head;
...@@ -880,7 +883,7 @@ static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value, ...@@ -880,7 +883,7 @@ static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
key_size = map->key_size; key_size = map->key_size;
hash = htab_map_hash(key, key_size); hash = htab_map_hash(key, key_size, htab->hashrnd);
b = __select_bucket(htab, hash); b = __select_bucket(htab, hash);
head = &b->head; head = &b->head;
...@@ -945,7 +948,7 @@ static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key, ...@@ -945,7 +948,7 @@ static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
key_size = map->key_size; key_size = map->key_size;
hash = htab_map_hash(key, key_size); hash = htab_map_hash(key, key_size, htab->hashrnd);
b = __select_bucket(htab, hash); b = __select_bucket(htab, hash);
head = &b->head; head = &b->head;
...@@ -998,7 +1001,7 @@ static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key, ...@@ -998,7 +1001,7 @@ static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
key_size = map->key_size; key_size = map->key_size;
hash = htab_map_hash(key, key_size); hash = htab_map_hash(key, key_size, htab->hashrnd);
b = __select_bucket(htab, hash); b = __select_bucket(htab, hash);
head = &b->head; head = &b->head;
...@@ -1071,7 +1074,7 @@ static int htab_map_delete_elem(struct bpf_map *map, void *key) ...@@ -1071,7 +1074,7 @@ static int htab_map_delete_elem(struct bpf_map *map, void *key)
key_size = map->key_size; key_size = map->key_size;
hash = htab_map_hash(key, key_size); hash = htab_map_hash(key, key_size, htab->hashrnd);
b = __select_bucket(htab, hash); b = __select_bucket(htab, hash);
head = &b->head; head = &b->head;
...@@ -1103,7 +1106,7 @@ static int htab_lru_map_delete_elem(struct bpf_map *map, void *key) ...@@ -1103,7 +1106,7 @@ static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
key_size = map->key_size; key_size = map->key_size;
hash = htab_map_hash(key, key_size); hash = htab_map_hash(key, key_size, htab->hashrnd);
b = __select_bucket(htab, hash); b = __select_bucket(htab, hash);
head = &b->head; head = &b->head;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment