Commit a7c4e5bf authored by Davidlohr Bueso's avatar Davidlohr Bueso Committed by Kleber Sacilotto de Souza

lib/rhashtable: consider param->min_size when setting initial table size

BugLink: https://bugs.launchpad.net/bugs/1791080

[ Upstream commit 107d01f5 ]

rhashtable_init() currently does not take into account the user-passed
min_size parameter unless param->nelem_hint is set as well. As such,
the default size (number of buckets) will always be HASH_DEFAULT_SIZE
even if the smallest allowed size is larger than that. Remediate this
by unconditionally calling into rounded_hashtable_size() and handling
things accordingly.
Signed-off-by: default avatarDavidlohr Bueso <dbueso@suse.de>
Acked-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarStefan Bader <stefan.bader@canonical.com>
Signed-off-by: default avatarKleber Sacilotto de Souza <kleber.souza@canonical.com>
parent 1d8c2aea
...@@ -670,8 +670,16 @@ EXPORT_SYMBOL_GPL(rhashtable_walk_stop); ...@@ -670,8 +670,16 @@ EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
static size_t rounded_hashtable_size(const struct rhashtable_params *params) static size_t rounded_hashtable_size(const struct rhashtable_params *params)
{ {
return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), size_t retsize;
(unsigned long)params->min_size);
if (params->nelem_hint)
retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
(unsigned long)params->min_size);
else
retsize = max(HASH_DEFAULT_SIZE,
(unsigned long)params->min_size);
return retsize;
} }
static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
...@@ -728,8 +736,6 @@ int rhashtable_init(struct rhashtable *ht, ...@@ -728,8 +736,6 @@ int rhashtable_init(struct rhashtable *ht,
struct bucket_table *tbl; struct bucket_table *tbl;
size_t size; size_t size;
size = HASH_DEFAULT_SIZE;
if ((!params->key_len && !params->obj_hashfn) || if ((!params->key_len && !params->obj_hashfn) ||
(params->obj_hashfn && !params->obj_cmpfn)) (params->obj_hashfn && !params->obj_cmpfn))
return -EINVAL; return -EINVAL;
...@@ -756,8 +762,7 @@ int rhashtable_init(struct rhashtable *ht, ...@@ -756,8 +762,7 @@ int rhashtable_init(struct rhashtable *ht,
ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE); ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
if (params->nelem_hint) size = rounded_hashtable_size(&ht->p);
size = rounded_hashtable_size(&ht->p);
/* The maximum (not average) chain length grows with the /* The maximum (not average) chain length grows with the
* size of the hash table, at a rate of (log N)/(log log N). * size of the hash table, at a rate of (log N)/(log log N).
......
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