Skip to content

Commit

Permalink
rhashtable: Round up/down min/max_size to ensure we respect limit
Browse files Browse the repository at this point in the history
Round up min_size respectively round down max_size to the next power
of two to make sure we always respect the limit specified by the
user. This is required because we compare the table size against the
limit before we expand or shrink.

Also fixes a minor bug where we modified min_size in the params
provided instead of the copy stored in struct rhashtable.

Signed-off-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Thomas Graf authored and David S. Miller committed Mar 20, 2015
1 parent 91a0f93 commit a998f71
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rhashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,15 +933,21 @@ int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
return -EINVAL;

params->min_size = max(params->min_size, HASH_MIN_SIZE);

if (params->nelem_hint)
size = rounded_hashtable_size(params);

memset(ht, 0, sizeof(*ht));
mutex_init(&ht->mutex);
memcpy(&ht->p, params, sizeof(*params));

if (params->min_size)
ht->p.min_size = roundup_pow_of_two(params->min_size);

if (params->max_size)
ht->p.max_size = rounddown_pow_of_two(params->max_size);

ht->p.min_size = max(params->min_size, HASH_MIN_SIZE);

if (params->locks_mul)
ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
else
Expand Down

0 comments on commit a998f71

Please sign in to comment.