Skip to content

Commit 2d2ab65

Browse files
herbertxdavem330
authored andcommitted
rhashtable: Do not lower max_elems when max_size is zero
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]>
1 parent e221c1f commit 2d2ab65

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lib/rhashtable.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -958,13 +958,14 @@ int rhashtable_init(struct rhashtable *ht,
958958
if (params->min_size)
959959
ht->p.min_size = roundup_pow_of_two(params->min_size);
960960

961-
if (params->max_size)
962-
ht->p.max_size = rounddown_pow_of_two(params->max_size);
963-
964961
/* Cap total entries at 2^31 to avoid nelems overflow. */
965962
ht->max_elems = 1u << 31;
966-
if (ht->p.max_size < ht->max_elems / 2)
967-
ht->max_elems = ht->p.max_size * 2;
963+
964+
if (params->max_size) {
965+
ht->p.max_size = rounddown_pow_of_two(params->max_size);
966+
if (ht->p.max_size < ht->max_elems / 2)
967+
ht->max_elems = ht->p.max_size * 2;
968+
}
968969

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

0 commit comments

Comments
 (0)