summaryrefslogtreecommitdiff
path: root/include/linux/rhashtable.h
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2017-04-27 08:44:51 +0300
committerDavid S. Miller <davem@davemloft.net>2017-04-27 18:48:24 +0300
commit6d684e54690caef45cf14051ddeb7c71beeb681b (patch)
tree1544faf80a19bf2fa47d9f2a1e5f2f98dbb17545 /include/linux/rhashtable.h
parent4b726e81dab2cefefd2c1cb00e3074ac0eec4b15 (diff)
downloadlinux-6d684e54690caef45cf14051ddeb7c71beeb681b.tar.xz
rhashtable: Cap total number of entries to 2^31
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>
Diffstat (limited to 'include/linux/rhashtable.h')
-rw-r--r--include/linux/rhashtable.h5
1 files changed, 3 insertions, 2 deletions
diff --git a/include/linux/rhashtable.h b/include/linux/rhashtable.h
index ae93b65d13d7..45f89369c4c8 100644
--- a/include/linux/rhashtable.h
+++ b/include/linux/rhashtable.h
@@ -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