]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
rhashtable: Cap total number of entries to 2^31
authorHerbert Xu <herbert@gondor.apana.org.au>
Thu, 27 Apr 2017 05:44:51 +0000 (13:44 +0800)
committerDavid S. Miller <davem@davemloft.net>
Thu, 27 Apr 2017 15:48:24 +0000 (11:48 -0400)
When max_size is not set or if it set to a sufficiently large
value, the nelems counter can overflow.  This would cause havoc
with the automatic shrinking as it would then attempt to fit a
huge number of entries into a tiny hash table.

This patch fixes this by adding max_elems to struct rhashtable
to cap the number of elements.  This is set to 2^31 as nelems is
not a precise count.  This is sufficiently smaller than UINT_MAX
that it should be safe.

When max_size is set max_elems will be lowered to at most twice
max_size as is the status quo.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/rhashtable.h
lib/rhashtable.c

index ae93b65d13d70bdc78e56f54006a9cd43822cd42..45f89369c4c89637796fcc298e36fc45c67fc572 100644 (file)
@@ -155,6 +155,7 @@ struct rhashtable_params {
  * @nelems: Number of elements in table
  * @key_len: Key length for hashfn
  * @p: Configuration parameters
+ * @max_elems: Maximum number of elements in table
  * @rhlist: True if this is an rhltable
  * @run_work: Deferred worker to expand/shrink asynchronously
  * @mutex: Mutex to protect current/future table swapping
@@ -165,6 +166,7 @@ struct rhashtable {
        atomic_t                        nelems;
        unsigned int                    key_len;
        struct rhashtable_params        p;
+       unsigned int                    max_elems;
        bool                            rhlist;
        struct work_struct              run_work;
        struct mutex                    mutex;
@@ -327,8 +329,7 @@ static inline bool rht_grow_above_100(const struct rhashtable *ht,
 static inline bool rht_grow_above_max(const struct rhashtable *ht,
                                      const struct bucket_table *tbl)
 {
-       return ht->p.max_size &&
-              (atomic_read(&ht->nelems) / 2u) >= ht->p.max_size;
+       return atomic_read(&ht->nelems) >= ht->max_elems;
 }
 
 /* The bucket lock is selected based on the hash and protects mutations
index f3b82e0d417b360e24d92b943f47547327b1e5ef..751630bbe40977f87cbbd6887fe663748131cc6a 100644 (file)
@@ -961,6 +961,11 @@ int rhashtable_init(struct rhashtable *ht,
        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;
+
        ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
 
        if (params->nelem_hint)