Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Resizable, Scalable, Concurrent Hash Table |
| 3 | * |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 4 | * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au> |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 5 | * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 6 | * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
| 7 | * |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 8 | * Code partially derived from nft_hash |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 9 | * Rewritten with rehash code from br_multicast plus single list |
| 10 | * pointer as suggested by Josh Triplett |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/log2.h> |
Eric Dumazet | 5beb5c9 | 2015-02-26 07:20:34 -0800 | [diff] [blame] | 20 | #include <linux/sched.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include <linux/vmalloc.h> |
| 23 | #include <linux/mm.h> |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 24 | #include <linux/jhash.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 25 | #include <linux/random.h> |
| 26 | #include <linux/rhashtable.h> |
Stephen Rothwell | 61d7b09 | 2015-02-09 14:04:03 +1100 | [diff] [blame] | 27 | #include <linux/err.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 28 | |
| 29 | #define HASH_DEFAULT_SIZE 64UL |
Herbert Xu | c2e213c | 2015-03-18 20:01:16 +1100 | [diff] [blame] | 30 | #define HASH_MIN_SIZE 4U |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 31 | #define BUCKET_LOCKS_PER_CPU 128UL |
| 32 | |
Herbert Xu | 988dfbd | 2015-03-10 09:27:55 +1100 | [diff] [blame] | 33 | static u32 head_hashfn(struct rhashtable *ht, |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 34 | const struct bucket_table *tbl, |
| 35 | const struct rhash_head *he) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 36 | { |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 37 | return rht_head_hashfn(ht, tbl, he, ht->p); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 40 | #ifdef CONFIG_PROVE_LOCKING |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 41 | #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT)) |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 42 | |
| 43 | int lockdep_rht_mutex_is_held(struct rhashtable *ht) |
| 44 | { |
| 45 | return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1; |
| 46 | } |
| 47 | EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); |
| 48 | |
| 49 | int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) |
| 50 | { |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 51 | spinlock_t *lock = rht_bucket_lock(tbl, hash); |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 52 | |
| 53 | return (debug_locks) ? lockdep_is_held(lock) : 1; |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); |
| 56 | #else |
| 57 | #define ASSERT_RHT_MUTEX(HT) |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 58 | #endif |
| 59 | |
| 60 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 61 | static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl) |
| 62 | { |
| 63 | unsigned int i, size; |
| 64 | #if defined(CONFIG_PROVE_LOCKING) |
| 65 | unsigned int nr_pcpus = 2; |
| 66 | #else |
| 67 | unsigned int nr_pcpus = num_possible_cpus(); |
| 68 | #endif |
| 69 | |
| 70 | nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL); |
| 71 | size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul); |
| 72 | |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 73 | /* Never allocate more than 0.5 locks per bucket */ |
| 74 | size = min_t(unsigned int, size, tbl->size >> 1); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 75 | |
| 76 | if (sizeof(spinlock_t) != 0) { |
| 77 | #ifdef CONFIG_NUMA |
| 78 | if (size * sizeof(spinlock_t) > PAGE_SIZE) |
| 79 | tbl->locks = vmalloc(size * sizeof(spinlock_t)); |
| 80 | else |
| 81 | #endif |
| 82 | tbl->locks = kmalloc_array(size, sizeof(spinlock_t), |
| 83 | GFP_KERNEL); |
| 84 | if (!tbl->locks) |
| 85 | return -ENOMEM; |
| 86 | for (i = 0; i < size; i++) |
| 87 | spin_lock_init(&tbl->locks[i]); |
| 88 | } |
| 89 | tbl->locks_mask = size - 1; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static void bucket_table_free(const struct bucket_table *tbl) |
| 95 | { |
| 96 | if (tbl) |
| 97 | kvfree(tbl->locks); |
| 98 | |
| 99 | kvfree(tbl); |
| 100 | } |
| 101 | |
Herbert Xu | 9d901bc | 2015-03-14 13:57:23 +1100 | [diff] [blame] | 102 | static void bucket_table_free_rcu(struct rcu_head *head) |
| 103 | { |
| 104 | bucket_table_free(container_of(head, struct bucket_table, rcu)); |
| 105 | } |
| 106 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 107 | static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame] | 108 | size_t nbuckets) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 109 | { |
Daniel Borkmann | eb6d1ab | 2015-02-20 00:53:38 +0100 | [diff] [blame] | 110 | struct bucket_table *tbl = NULL; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 111 | size_t size; |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 112 | int i; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 113 | |
| 114 | size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); |
Daniel Borkmann | eb6d1ab | 2015-02-20 00:53:38 +0100 | [diff] [blame] | 115 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) |
| 116 | tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 117 | if (tbl == NULL) |
| 118 | tbl = vzalloc(size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 119 | if (tbl == NULL) |
| 120 | return NULL; |
| 121 | |
| 122 | tbl->size = nbuckets; |
| 123 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 124 | if (alloc_bucket_locks(ht, tbl) < 0) { |
| 125 | bucket_table_free(tbl); |
| 126 | return NULL; |
| 127 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 128 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 129 | INIT_LIST_HEAD(&tbl->walkers); |
| 130 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame] | 131 | get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); |
| 132 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 133 | for (i = 0; i < nbuckets; i++) |
| 134 | INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i); |
| 135 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 136 | return tbl; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 139 | static struct bucket_table *rhashtable_last_table(struct rhashtable *ht, |
| 140 | struct bucket_table *tbl) |
| 141 | { |
| 142 | struct bucket_table *new_tbl; |
| 143 | |
| 144 | do { |
| 145 | new_tbl = tbl; |
| 146 | tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 147 | } while (tbl); |
| 148 | |
| 149 | return new_tbl; |
| 150 | } |
| 151 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 152 | static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash) |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 153 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 154 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 155 | struct bucket_table *new_tbl = rhashtable_last_table(ht, |
| 156 | rht_dereference_rcu(old_tbl->future_tbl, ht)); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 157 | struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash]; |
| 158 | int err = -ENOENT; |
| 159 | struct rhash_head *head, *next, *entry; |
| 160 | spinlock_t *new_bucket_lock; |
| 161 | unsigned new_hash; |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 162 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 163 | rht_for_each(entry, old_tbl, old_hash) { |
| 164 | err = 0; |
| 165 | next = rht_dereference_bucket(entry->next, old_tbl, old_hash); |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 166 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 167 | if (rht_is_a_nulls(next)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 168 | break; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 169 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 170 | pprev = &entry->next; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 173 | if (err) |
| 174 | goto out; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 175 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 176 | new_hash = head_hashfn(ht, new_tbl, entry); |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 177 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 178 | new_bucket_lock = rht_bucket_lock(new_tbl, new_hash); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 179 | |
Herbert Xu | 8f2484b | 2015-03-14 13:57:21 +1100 | [diff] [blame] | 180 | spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 181 | head = rht_dereference_bucket(new_tbl->buckets[new_hash], |
| 182 | new_tbl, new_hash); |
| 183 | |
| 184 | if (rht_is_a_nulls(head)) |
| 185 | INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash); |
| 186 | else |
| 187 | RCU_INIT_POINTER(entry->next, head); |
| 188 | |
| 189 | rcu_assign_pointer(new_tbl->buckets[new_hash], entry); |
| 190 | spin_unlock(new_bucket_lock); |
| 191 | |
| 192 | rcu_assign_pointer(*pprev, next); |
| 193 | |
| 194 | out: |
| 195 | return err; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 198 | static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 199 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 200 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
| 201 | spinlock_t *old_bucket_lock; |
Thomas Graf | 7cd10db | 2015-02-05 02:03:35 +0100 | [diff] [blame] | 202 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 203 | old_bucket_lock = rht_bucket_lock(old_tbl, old_hash); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 204 | |
| 205 | spin_lock_bh(old_bucket_lock); |
| 206 | while (!rhashtable_rehash_one(ht, old_hash)) |
| 207 | ; |
Herbert Xu | 63d512d | 2015-03-14 13:57:24 +1100 | [diff] [blame] | 208 | old_tbl->rehash++; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 209 | spin_unlock_bh(old_bucket_lock); |
| 210 | } |
| 211 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 212 | static int rhashtable_rehash_attach(struct rhashtable *ht, |
| 213 | struct bucket_table *old_tbl, |
| 214 | struct bucket_table *new_tbl) |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 215 | { |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 216 | /* Protect future_tbl using the first bucket lock. */ |
| 217 | spin_lock_bh(old_tbl->locks); |
| 218 | |
| 219 | /* Did somebody beat us to it? */ |
| 220 | if (rcu_access_pointer(old_tbl->future_tbl)) { |
| 221 | spin_unlock_bh(old_tbl->locks); |
| 222 | return -EEXIST; |
| 223 | } |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 224 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 225 | /* Make insertions go into the new, empty table right away. Deletions |
| 226 | * and lookups will be attempted in both tables until we synchronize. |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 227 | */ |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 228 | rcu_assign_pointer(old_tbl->future_tbl, new_tbl); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 229 | |
Herbert Xu | 9497df8 | 2015-03-12 22:07:49 +1100 | [diff] [blame] | 230 | /* Ensure the new table is visible to readers. */ |
| 231 | smp_wmb(); |
| 232 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 233 | spin_unlock_bh(old_tbl->locks); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int rhashtable_rehash_table(struct rhashtable *ht) |
| 239 | { |
| 240 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
| 241 | struct bucket_table *new_tbl; |
| 242 | struct rhashtable_walker *walker; |
| 243 | unsigned old_hash; |
| 244 | |
| 245 | new_tbl = rht_dereference(old_tbl->future_tbl, ht); |
| 246 | if (!new_tbl) |
| 247 | return 0; |
| 248 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 249 | for (old_hash = 0; old_hash < old_tbl->size; old_hash++) |
| 250 | rhashtable_rehash_chain(ht, old_hash); |
| 251 | |
| 252 | /* Publish the new table pointer. */ |
| 253 | rcu_assign_pointer(ht->tbl, new_tbl); |
| 254 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 255 | list_for_each_entry(walker, &old_tbl->walkers, list) |
| 256 | walker->tbl = NULL; |
| 257 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 258 | /* Wait for readers. All new readers will see the new |
| 259 | * table, and thus no references to the old table will |
| 260 | * remain. |
| 261 | */ |
Herbert Xu | 9d901bc | 2015-03-14 13:57:23 +1100 | [diff] [blame] | 262 | call_rcu(&old_tbl->rcu, bucket_table_free_rcu); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 263 | |
| 264 | return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
| 268 | * rhashtable_expand - Expand hash table while allowing concurrent lookups |
| 269 | * @ht: the hash table to expand |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 270 | * |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 271 | * A secondary bucket array is allocated and the hash entries are migrated. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 272 | * |
| 273 | * This function may only be called in a context where it is safe to call |
| 274 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. |
| 275 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 276 | * The caller must ensure that no concurrent resizing occurs by holding |
| 277 | * ht->mutex. |
| 278 | * |
| 279 | * It is valid to have concurrent insertions and deletions protected by per |
| 280 | * bucket locks or concurrent RCU protected lookups and traversals. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 281 | */ |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 282 | static int rhashtable_expand(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 283 | { |
| 284 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 285 | int err; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 286 | |
| 287 | ASSERT_RHT_MUTEX(ht); |
| 288 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 289 | old_tbl = rhashtable_last_table(ht, old_tbl); |
| 290 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame] | 291 | new_tbl = bucket_table_alloc(ht, old_tbl->size * 2); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 292 | if (new_tbl == NULL) |
| 293 | return -ENOMEM; |
| 294 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 295 | err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); |
| 296 | if (err) |
| 297 | bucket_table_free(new_tbl); |
| 298 | |
| 299 | return err; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 300 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * rhashtable_shrink - Shrink hash table while allowing concurrent lookups |
| 304 | * @ht: the hash table to shrink |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 305 | * |
Herbert Xu | 18093d1 | 2015-03-24 00:50:25 +1100 | [diff] [blame] | 306 | * This function shrinks the hash table to fit, i.e., the smallest |
| 307 | * size would not cause it to expand right away automatically. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 308 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 309 | * The caller must ensure that no concurrent resizing occurs by holding |
| 310 | * ht->mutex. |
| 311 | * |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 312 | * The caller must ensure that no concurrent table mutations take place. |
| 313 | * It is however valid to have concurrent lookups if they are RCU protected. |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 314 | * |
| 315 | * It is valid to have concurrent insertions and deletions protected by per |
| 316 | * bucket locks or concurrent RCU protected lookups and traversals. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 317 | */ |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 318 | static int rhashtable_shrink(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 319 | { |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 320 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); |
Herbert Xu | 18093d1 | 2015-03-24 00:50:25 +1100 | [diff] [blame] | 321 | unsigned size = roundup_pow_of_two(atomic_read(&ht->nelems) * 3 / 2); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 322 | int err; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 323 | |
| 324 | ASSERT_RHT_MUTEX(ht); |
| 325 | |
Herbert Xu | 18093d1 | 2015-03-24 00:50:25 +1100 | [diff] [blame] | 326 | if (size < ht->p.min_size) |
| 327 | size = ht->p.min_size; |
| 328 | |
| 329 | if (old_tbl->size <= size) |
| 330 | return 0; |
| 331 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 332 | if (rht_dereference(old_tbl->future_tbl, ht)) |
| 333 | return -EEXIST; |
| 334 | |
Herbert Xu | 18093d1 | 2015-03-24 00:50:25 +1100 | [diff] [blame] | 335 | new_tbl = bucket_table_alloc(ht, size); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 336 | if (new_tbl == NULL) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 337 | return -ENOMEM; |
| 338 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 339 | err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); |
| 340 | if (err) |
| 341 | bucket_table_free(new_tbl); |
| 342 | |
| 343 | return err; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 344 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 345 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 346 | static void rht_deferred_worker(struct work_struct *work) |
| 347 | { |
| 348 | struct rhashtable *ht; |
| 349 | struct bucket_table *tbl; |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 350 | int err = 0; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 351 | |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 352 | ht = container_of(work, struct rhashtable, run_work); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 353 | mutex_lock(&ht->mutex); |
Herbert Xu | 28134a5 | 2015-02-04 07:33:22 +1100 | [diff] [blame] | 354 | if (ht->being_destroyed) |
| 355 | goto unlock; |
| 356 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 357 | tbl = rht_dereference(ht->tbl, ht); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 358 | tbl = rhashtable_last_table(ht, tbl); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 359 | |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 360 | if (rht_grow_above_75(ht, tbl)) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 361 | rhashtable_expand(ht); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 362 | else if (rht_shrink_below_30(ht, tbl)) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 363 | rhashtable_shrink(ht); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 364 | |
| 365 | err = rhashtable_rehash_table(ht); |
| 366 | |
Herbert Xu | 28134a5 | 2015-02-04 07:33:22 +1100 | [diff] [blame] | 367 | unlock: |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 368 | mutex_unlock(&ht->mutex); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 369 | |
| 370 | if (err) |
| 371 | schedule_work(&ht->run_work); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 372 | } |
| 373 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 374 | int rhashtable_insert_slow(struct rhashtable *ht, const void *key, |
| 375 | struct rhash_head *obj, |
| 376 | struct bucket_table *tbl) |
| 377 | { |
| 378 | struct rhash_head *head; |
| 379 | unsigned hash; |
| 380 | int err = -EEXIST; |
| 381 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame^] | 382 | tbl = rhashtable_last_table(ht, tbl); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 383 | hash = head_hashfn(ht, tbl, obj); |
| 384 | spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING); |
| 385 | |
| 386 | if (key && rhashtable_lookup_fast(ht, key, ht->p)) |
| 387 | goto exit; |
| 388 | |
| 389 | err = 0; |
| 390 | |
| 391 | head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); |
| 392 | |
| 393 | RCU_INIT_POINTER(obj->next, head); |
| 394 | |
| 395 | rcu_assign_pointer(tbl->buckets[hash], obj); |
| 396 | |
| 397 | atomic_inc(&ht->nelems); |
| 398 | |
| 399 | exit: |
| 400 | spin_unlock(rht_bucket_lock(tbl, hash)); |
| 401 | |
| 402 | return err; |
| 403 | } |
| 404 | EXPORT_SYMBOL_GPL(rhashtable_insert_slow); |
| 405 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 406 | /** |
| 407 | * rhashtable_walk_init - Initialise an iterator |
| 408 | * @ht: Table to walk over |
| 409 | * @iter: Hash table Iterator |
| 410 | * |
| 411 | * This function prepares a hash table walk. |
| 412 | * |
| 413 | * Note that if you restart a walk after rhashtable_walk_stop you |
| 414 | * may see the same object twice. Also, you may miss objects if |
| 415 | * there are removals in between rhashtable_walk_stop and the next |
| 416 | * call to rhashtable_walk_start. |
| 417 | * |
| 418 | * For a completely stable walk you should construct your own data |
| 419 | * structure outside the hash table. |
| 420 | * |
| 421 | * This function may sleep so you must not call it from interrupt |
| 422 | * context or with spin locks held. |
| 423 | * |
| 424 | * You must call rhashtable_walk_exit if this function returns |
| 425 | * successfully. |
| 426 | */ |
| 427 | int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter) |
| 428 | { |
| 429 | iter->ht = ht; |
| 430 | iter->p = NULL; |
| 431 | iter->slot = 0; |
| 432 | iter->skip = 0; |
| 433 | |
| 434 | iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL); |
| 435 | if (!iter->walker) |
| 436 | return -ENOMEM; |
| 437 | |
| 438 | mutex_lock(&ht->mutex); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 439 | iter->walker->tbl = rht_dereference(ht->tbl, ht); |
| 440 | list_add(&iter->walker->list, &iter->walker->tbl->walkers); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 441 | mutex_unlock(&ht->mutex); |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(rhashtable_walk_init); |
| 446 | |
| 447 | /** |
| 448 | * rhashtable_walk_exit - Free an iterator |
| 449 | * @iter: Hash table Iterator |
| 450 | * |
| 451 | * This function frees resources allocated by rhashtable_walk_init. |
| 452 | */ |
| 453 | void rhashtable_walk_exit(struct rhashtable_iter *iter) |
| 454 | { |
| 455 | mutex_lock(&iter->ht->mutex); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 456 | if (iter->walker->tbl) |
| 457 | list_del(&iter->walker->list); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 458 | mutex_unlock(&iter->ht->mutex); |
| 459 | kfree(iter->walker); |
| 460 | } |
| 461 | EXPORT_SYMBOL_GPL(rhashtable_walk_exit); |
| 462 | |
| 463 | /** |
| 464 | * rhashtable_walk_start - Start a hash table walk |
| 465 | * @iter: Hash table iterator |
| 466 | * |
| 467 | * Start a hash table walk. Note that we take the RCU lock in all |
| 468 | * cases including when we return an error. So you must always call |
| 469 | * rhashtable_walk_stop to clean up. |
| 470 | * |
| 471 | * Returns zero if successful. |
| 472 | * |
| 473 | * Returns -EAGAIN if resize event occured. Note that the iterator |
| 474 | * will rewind back to the beginning and you may use it immediately |
| 475 | * by calling rhashtable_walk_next. |
| 476 | */ |
| 477 | int rhashtable_walk_start(struct rhashtable_iter *iter) |
Thomas Graf | db4374f | 2015-03-16 10:42:27 +0100 | [diff] [blame] | 478 | __acquires(RCU) |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 479 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 480 | struct rhashtable *ht = iter->ht; |
| 481 | |
| 482 | mutex_lock(&ht->mutex); |
| 483 | |
| 484 | if (iter->walker->tbl) |
| 485 | list_del(&iter->walker->list); |
| 486 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 487 | rcu_read_lock(); |
| 488 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 489 | mutex_unlock(&ht->mutex); |
| 490 | |
| 491 | if (!iter->walker->tbl) { |
| 492 | iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 493 | return -EAGAIN; |
| 494 | } |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | EXPORT_SYMBOL_GPL(rhashtable_walk_start); |
| 499 | |
| 500 | /** |
| 501 | * rhashtable_walk_next - Return the next object and advance the iterator |
| 502 | * @iter: Hash table iterator |
| 503 | * |
| 504 | * Note that you must call rhashtable_walk_stop when you are finished |
| 505 | * with the walk. |
| 506 | * |
| 507 | * Returns the next object or NULL when the end of the table is reached. |
| 508 | * |
| 509 | * Returns -EAGAIN if resize event occured. Note that the iterator |
| 510 | * will rewind back to the beginning and you may continue to use it. |
| 511 | */ |
| 512 | void *rhashtable_walk_next(struct rhashtable_iter *iter) |
| 513 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 514 | struct bucket_table *tbl = iter->walker->tbl; |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 515 | struct rhashtable *ht = iter->ht; |
| 516 | struct rhash_head *p = iter->p; |
| 517 | void *obj = NULL; |
| 518 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 519 | if (p) { |
| 520 | p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot); |
| 521 | goto next; |
| 522 | } |
| 523 | |
| 524 | for (; iter->slot < tbl->size; iter->slot++) { |
| 525 | int skip = iter->skip; |
| 526 | |
| 527 | rht_for_each_rcu(p, tbl, iter->slot) { |
| 528 | if (!skip) |
| 529 | break; |
| 530 | skip--; |
| 531 | } |
| 532 | |
| 533 | next: |
| 534 | if (!rht_is_a_nulls(p)) { |
| 535 | iter->skip++; |
| 536 | iter->p = p; |
| 537 | obj = rht_obj(ht, p); |
| 538 | goto out; |
| 539 | } |
| 540 | |
| 541 | iter->skip = 0; |
| 542 | } |
| 543 | |
Herbert Xu | d88252f | 2015-03-24 00:50:19 +1100 | [diff] [blame] | 544 | /* Ensure we see any new tables. */ |
| 545 | smp_rmb(); |
| 546 | |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 547 | iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 548 | if (iter->walker->tbl) { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 549 | iter->slot = 0; |
| 550 | iter->skip = 0; |
| 551 | return ERR_PTR(-EAGAIN); |
| 552 | } |
| 553 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 554 | iter->p = NULL; |
| 555 | |
| 556 | out: |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 557 | |
| 558 | return obj; |
| 559 | } |
| 560 | EXPORT_SYMBOL_GPL(rhashtable_walk_next); |
| 561 | |
| 562 | /** |
| 563 | * rhashtable_walk_stop - Finish a hash table walk |
| 564 | * @iter: Hash table iterator |
| 565 | * |
| 566 | * Finish a hash table walk. |
| 567 | */ |
| 568 | void rhashtable_walk_stop(struct rhashtable_iter *iter) |
Thomas Graf | db4374f | 2015-03-16 10:42:27 +0100 | [diff] [blame] | 569 | __releases(RCU) |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 570 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 571 | struct rhashtable *ht; |
| 572 | struct bucket_table *tbl = iter->walker->tbl; |
| 573 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 574 | if (!tbl) |
Herbert Xu | 963ecbd | 2015-03-15 21:12:04 +1100 | [diff] [blame] | 575 | goto out; |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 576 | |
| 577 | ht = iter->ht; |
| 578 | |
| 579 | mutex_lock(&ht->mutex); |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 580 | if (tbl->rehash < tbl->size) |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 581 | list_add(&iter->walker->list, &tbl->walkers); |
| 582 | else |
| 583 | iter->walker->tbl = NULL; |
| 584 | mutex_unlock(&ht->mutex); |
| 585 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 586 | iter->p = NULL; |
Herbert Xu | 963ecbd | 2015-03-15 21:12:04 +1100 | [diff] [blame] | 587 | |
| 588 | out: |
| 589 | rcu_read_unlock(); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 590 | } |
| 591 | EXPORT_SYMBOL_GPL(rhashtable_walk_stop); |
| 592 | |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 593 | static size_t rounded_hashtable_size(const struct rhashtable_params *params) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 594 | { |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 595 | return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), |
Herbert Xu | e2e21c1 | 2015-03-18 20:01:21 +1100 | [diff] [blame] | 596 | (unsigned long)params->min_size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 597 | } |
| 598 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 599 | static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) |
| 600 | { |
| 601 | return jhash2(key, length, seed); |
| 602 | } |
| 603 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 604 | /** |
| 605 | * rhashtable_init - initialize a new hash table |
| 606 | * @ht: hash table to be initialized |
| 607 | * @params: configuration parameters |
| 608 | * |
| 609 | * Initializes a new hash table based on the provided configuration |
| 610 | * parameters. A table can be configured either with a variable or |
| 611 | * fixed length key: |
| 612 | * |
| 613 | * Configuration Example 1: Fixed length keys |
| 614 | * struct test_obj { |
| 615 | * int key; |
| 616 | * void * my_member; |
| 617 | * struct rhash_head node; |
| 618 | * }; |
| 619 | * |
| 620 | * struct rhashtable_params params = { |
| 621 | * .head_offset = offsetof(struct test_obj, node), |
| 622 | * .key_offset = offsetof(struct test_obj, key), |
| 623 | * .key_len = sizeof(int), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 624 | * .hashfn = jhash, |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 625 | * .nulls_base = (1U << RHT_BASE_SHIFT), |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 626 | * }; |
| 627 | * |
| 628 | * Configuration Example 2: Variable length keys |
| 629 | * struct test_obj { |
| 630 | * [...] |
| 631 | * struct rhash_head node; |
| 632 | * }; |
| 633 | * |
| 634 | * u32 my_hash_fn(const void *data, u32 seed) |
| 635 | * { |
| 636 | * struct test_obj *obj = data; |
| 637 | * |
| 638 | * return [... hash ...]; |
| 639 | * } |
| 640 | * |
| 641 | * struct rhashtable_params params = { |
| 642 | * .head_offset = offsetof(struct test_obj, node), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 643 | * .hashfn = jhash, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 644 | * .obj_hashfn = my_hash_fn, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 645 | * }; |
| 646 | */ |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 647 | int rhashtable_init(struct rhashtable *ht, |
| 648 | const struct rhashtable_params *params) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 649 | { |
| 650 | struct bucket_table *tbl; |
| 651 | size_t size; |
| 652 | |
| 653 | size = HASH_DEFAULT_SIZE; |
| 654 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 655 | if ((!params->key_len && !params->obj_hashfn) || |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 656 | (params->obj_hashfn && !params->obj_cmpfn)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 657 | return -EINVAL; |
| 658 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 659 | if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) |
| 660 | return -EINVAL; |
| 661 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 662 | if (params->nelem_hint) |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 663 | size = rounded_hashtable_size(params); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 664 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 665 | memset(ht, 0, sizeof(*ht)); |
| 666 | mutex_init(&ht->mutex); |
| 667 | memcpy(&ht->p, params, sizeof(*params)); |
| 668 | |
Thomas Graf | a998f71 | 2015-03-19 22:31:13 +0000 | [diff] [blame] | 669 | if (params->min_size) |
| 670 | ht->p.min_size = roundup_pow_of_two(params->min_size); |
| 671 | |
| 672 | if (params->max_size) |
| 673 | ht->p.max_size = rounddown_pow_of_two(params->max_size); |
| 674 | |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 675 | ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE); |
Thomas Graf | a998f71 | 2015-03-19 22:31:13 +0000 | [diff] [blame] | 676 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 677 | if (params->locks_mul) |
| 678 | ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); |
| 679 | else |
| 680 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; |
| 681 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 682 | ht->key_len = ht->p.key_len; |
| 683 | if (!params->hashfn) { |
| 684 | ht->p.hashfn = jhash; |
| 685 | |
| 686 | if (!(ht->key_len & (sizeof(u32) - 1))) { |
| 687 | ht->key_len /= sizeof(u32); |
| 688 | ht->p.hashfn = rhashtable_jhash2; |
| 689 | } |
| 690 | } |
| 691 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame] | 692 | tbl = bucket_table_alloc(ht, size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 693 | if (tbl == NULL) |
| 694 | return -ENOMEM; |
| 695 | |
Ying Xue | 545a148 | 2015-01-07 13:41:57 +0800 | [diff] [blame] | 696 | atomic_set(&ht->nelems, 0); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 697 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 698 | RCU_INIT_POINTER(ht->tbl, tbl); |
| 699 | |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 700 | INIT_WORK(&ht->run_work, rht_deferred_worker); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 701 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | EXPORT_SYMBOL_GPL(rhashtable_init); |
| 705 | |
| 706 | /** |
| 707 | * rhashtable_destroy - destroy hash table |
| 708 | * @ht: the hash table to destroy |
| 709 | * |
Pablo Neira Ayuso | ae82ddc | 2014-09-02 00:26:05 +0200 | [diff] [blame] | 710 | * Frees the bucket array. This function is not rcu safe, therefore the caller |
| 711 | * has to make sure that no resizing may happen by unpublishing the hashtable |
| 712 | * and waiting for the quiescent cycle before releasing the bucket array. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 713 | */ |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 714 | void rhashtable_destroy(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 715 | { |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 716 | ht->being_destroyed = true; |
| 717 | |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 718 | cancel_work_sync(&ht->run_work); |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 719 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 720 | mutex_lock(&ht->mutex); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 721 | bucket_table_free(rht_dereference(ht->tbl, ht)); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 722 | mutex_unlock(&ht->mutex); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 723 | } |
| 724 | EXPORT_SYMBOL_GPL(rhashtable_destroy); |