aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2015-05-14 23:30:47 -0400
committerDavid S. Miller <davem@davemloft.net>2015-05-16 18:08:26 -0400
commit07ee0722bf941960fb3888f9c9b5839473372fd1 (patch)
treed80ad157b570da14e08e25bb32c5d58af5d969c3
parentdb9683fb412d4af33f66b9fe3d8dace1c6d113c9 (diff)
rhashtable: Add cap on number of elements in hash table
We currently have no limit on the number of elements in a hash table. This is a problem because some users (tipc) set a ceiling on the maximum table size and when that is reached the hash table may degenerate. Others may encounter OOM when growing and if we allow insertions when that happens the hash table perofrmance may also suffer. This patch adds a new paramater insecure_max_entries which becomes the cap on the table. If unset it defaults to max_size * 2. If it is also zero it means that there is no cap on the number of elements in the table. However, the table will grow whenever the utilisation hits 100% and if that growth fails, you will get ENOMEM on insertion. As allowing oversubscription is potentially dangerous, the name contains the word insecure. Note that the cap is not a hard limit. This is done for performance reasons as enforcing a hard limit will result in use of atomic ops that are heavier than the ones we currently use. The reasoning is that we're only guarding against a gross over- subscription of the table, rather than a small breach of the limit. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/linux/rhashtable.h19
-rw-r--r--lib/rhashtable.c11
2 files changed, 30 insertions, 0 deletions
diff --git a/include/linux/rhashtable.h b/include/linux/rhashtable.h
index dbcbcc59aa92..843ceca9a21e 100644
--- a/include/linux/rhashtable.h
+++ b/include/linux/rhashtable.h
@@ -17,6 +17,7 @@
17#ifndef _LINUX_RHASHTABLE_H 17#ifndef _LINUX_RHASHTABLE_H
18#define _LINUX_RHASHTABLE_H 18#define _LINUX_RHASHTABLE_H
19 19
20#include <linux/atomic.h>
20#include <linux/compiler.h> 21#include <linux/compiler.h>
21#include <linux/errno.h> 22#include <linux/errno.h>
22#include <linux/jhash.h> 23#include <linux/jhash.h>
@@ -100,6 +101,7 @@ struct rhashtable;
100 * @key_len: Length of key 101 * @key_len: Length of key
101 * @key_offset: Offset of key in struct to be hashed 102 * @key_offset: Offset of key in struct to be hashed
102 * @head_offset: Offset of rhash_head in struct to be hashed 103 * @head_offset: Offset of rhash_head in struct to be hashed
104 * @insecure_max_entries: Maximum number of entries (may be exceeded)
103 * @max_size: Maximum size while expanding 105 * @max_size: Maximum size while expanding
104 * @min_size: Minimum size while shrinking 106 * @min_size: Minimum size while shrinking
105 * @nulls_base: Base value to generate nulls marker 107 * @nulls_base: Base value to generate nulls marker
@@ -115,6 +117,7 @@ struct rhashtable_params {
115 size_t key_len; 117 size_t key_len;
116 size_t key_offset; 118 size_t key_offset;
117 size_t head_offset; 119 size_t head_offset;
120 unsigned int insecure_max_entries;
118 unsigned int max_size; 121 unsigned int max_size;
119 unsigned int min_size; 122 unsigned int min_size;
120 u32 nulls_base; 123 u32 nulls_base;
@@ -286,6 +289,18 @@ static inline bool rht_grow_above_100(const struct rhashtable *ht,
286 (!ht->p.max_size || tbl->size < ht->p.max_size); 289 (!ht->p.max_size || tbl->size < ht->p.max_size);
287} 290}
288 291
292/**
293 * rht_grow_above_max - returns true if table is above maximum
294 * @ht: hash table
295 * @tbl: current table
296 */
297static inline bool rht_grow_above_max(const struct rhashtable *ht,
298 const struct bucket_table *tbl)
299{
300 return ht->p.insecure_max_entries &&
301 atomic_read(&ht->nelems) >= ht->p.insecure_max_entries;
302}
303
289/* The bucket lock is selected based on the hash and protects mutations 304/* The bucket lock is selected based on the hash and protects mutations
290 * on a group of hash buckets. 305 * on a group of hash buckets.
291 * 306 *
@@ -589,6 +604,10 @@ restart:
589 goto out; 604 goto out;
590 } 605 }
591 606
607 err = -E2BIG;
608 if (unlikely(rht_grow_above_max(ht, tbl)))
609 goto out;
610
592 if (unlikely(rht_grow_above_100(ht, tbl))) { 611 if (unlikely(rht_grow_above_100(ht, tbl))) {
593slow_path: 612slow_path:
594 spin_unlock_bh(lock); 613 spin_unlock_bh(lock);
diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index b28df4019ade..4396434e4715 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -14,6 +14,7 @@
14 * published by the Free Software Foundation. 14 * published by the Free Software Foundation.
15 */ 15 */
16 16
17#include <linux/atomic.h>
17#include <linux/kernel.h> 18#include <linux/kernel.h>
18#include <linux/init.h> 19#include <linux/init.h>
19#include <linux/log2.h> 20#include <linux/log2.h>
@@ -446,6 +447,10 @@ int rhashtable_insert_slow(struct rhashtable *ht, const void *key,
446 if (key && rhashtable_lookup_fast(ht, key, ht->p)) 447 if (key && rhashtable_lookup_fast(ht, key, ht->p))
447 goto exit; 448 goto exit;
448 449
450 err = -E2BIG;
451 if (unlikely(rht_grow_above_max(ht, tbl)))
452 goto exit;
453
449 err = -EAGAIN; 454 err = -EAGAIN;
450 if (rhashtable_check_elasticity(ht, tbl, hash) || 455 if (rhashtable_check_elasticity(ht, tbl, hash) ||
451 rht_grow_above_100(ht, tbl)) 456 rht_grow_above_100(ht, tbl))
@@ -738,6 +743,12 @@ int rhashtable_init(struct rhashtable *ht,
738 if (params->max_size) 743 if (params->max_size)
739 ht->p.max_size = rounddown_pow_of_two(params->max_size); 744 ht->p.max_size = rounddown_pow_of_two(params->max_size);
740 745
746 if (params->insecure_max_entries)
747 ht->p.insecure_max_entries =
748 rounddown_pow_of_two(params->insecure_max_entries);
749 else
750 ht->p.insecure_max_entries = ht->p.max_size * 2;
751
741 ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE); 752 ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
742 753
743 /* The maximum (not average) chain length grows with the 754 /* The maximum (not average) chain length grows with the