Skip to content

Commit 107d01f

Browse files
Davidlohr Buesodavem330
authored andcommitted
lib/rhashtable: consider param->min_size when setting initial table size
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: Davidlohr Bueso <[email protected]> Acked-by: Herbert Xu <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 08239d4 commit 107d01f

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/rhashtable.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,16 @@ EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
964964

965965
static size_t rounded_hashtable_size(const struct rhashtable_params *params)
966966
{
967-
return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
968-
(unsigned long)params->min_size);
967+
size_t retsize;
968+
969+
if (params->nelem_hint)
970+
retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
971+
(unsigned long)params->min_size);
972+
else
973+
retsize = max(HASH_DEFAULT_SIZE,
974+
(unsigned long)params->min_size);
975+
976+
return retsize;
969977
}
970978

971979
static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
@@ -1022,8 +1030,6 @@ int rhashtable_init(struct rhashtable *ht,
10221030
struct bucket_table *tbl;
10231031
size_t size;
10241032

1025-
size = HASH_DEFAULT_SIZE;
1026-
10271033
if ((!params->key_len && !params->obj_hashfn) ||
10281034
(params->obj_hashfn && !params->obj_cmpfn))
10291035
return -EINVAL;
@@ -1050,8 +1056,7 @@ int rhashtable_init(struct rhashtable *ht,
10501056

10511057
ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
10521058

1053-
if (params->nelem_hint)
1054-
size = rounded_hashtable_size(&ht->p);
1059+
size = rounded_hashtable_size(&ht->p);
10551060

10561061
if (params->locks_mul)
10571062
ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);

0 commit comments

Comments
 (0)