Skip to content

Commit

Permalink
rhashtable: Do not lower max_elems when max_size is zero
Browse files Browse the repository at this point in the history
The commit 6d684e5 ("rhashtable: Cap total number of entries
to 2^31") breaks rhashtable users that do not set max_size.  This
is because when max_size is zero max_elems is also incorrectly set
to zero instead of 2^31.

This patch fixes it by only lowering max_elems when max_size is not
zero.

Fixes: 6d684e5 ("rhashtable: Cap total number of entries to 2^31")
Reported-by: Florian Fainelli <[email protected]>
Reported-by: kernel test robot <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
herbertx authored and davem330 committed Apr 28, 2017
1 parent e221c1f commit 2d2ab65
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/rhashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -958,13 +958,14 @@ int rhashtable_init(struct rhashtable *ht,
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);

/* Cap total entries at 2^31 to avoid nelems overflow. */
ht->max_elems = 1u << 31;
if (ht->p.max_size < ht->max_elems / 2)
ht->max_elems = ht->p.max_size * 2;

if (params->max_size) {
ht->p.max_size = rounddown_pow_of_two(params->max_size);
if (ht->p.max_size < ht->max_elems / 2)
ht->max_elems = ht->p.max_size * 2;
}

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

Expand Down

0 comments on commit 2d2ab65

Please sign in to comment.