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 | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 139 | static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash) |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 140 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 141 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 142 | struct bucket_table *new_tbl = |
| 143 | rht_dereference(old_tbl->future_tbl, ht) ?: old_tbl; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 144 | struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash]; |
| 145 | int err = -ENOENT; |
| 146 | struct rhash_head *head, *next, *entry; |
| 147 | spinlock_t *new_bucket_lock; |
| 148 | unsigned new_hash; |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 149 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 150 | rht_for_each(entry, old_tbl, old_hash) { |
| 151 | err = 0; |
| 152 | next = rht_dereference_bucket(entry->next, old_tbl, 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 | if (rht_is_a_nulls(next)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 155 | break; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 156 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 157 | pprev = &entry->next; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 160 | if (err) |
| 161 | goto out; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 162 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 163 | new_hash = head_hashfn(ht, new_tbl, entry); |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 164 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 165 | new_bucket_lock = rht_bucket_lock(new_tbl, new_hash); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 166 | |
Herbert Xu | 8f2484b | 2015-03-14 13:57:21 +1100 | [diff] [blame] | 167 | spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 168 | head = rht_dereference_bucket(new_tbl->buckets[new_hash], |
| 169 | new_tbl, new_hash); |
| 170 | |
| 171 | if (rht_is_a_nulls(head)) |
| 172 | INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash); |
| 173 | else |
| 174 | RCU_INIT_POINTER(entry->next, head); |
| 175 | |
| 176 | rcu_assign_pointer(new_tbl->buckets[new_hash], entry); |
| 177 | spin_unlock(new_bucket_lock); |
| 178 | |
| 179 | rcu_assign_pointer(*pprev, next); |
| 180 | |
| 181 | out: |
| 182 | return err; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 185 | static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 186 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 187 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
| 188 | spinlock_t *old_bucket_lock; |
Thomas Graf | 7cd10db | 2015-02-05 02:03:35 +0100 | [diff] [blame] | 189 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 190 | old_bucket_lock = rht_bucket_lock(old_tbl, old_hash); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 191 | |
| 192 | spin_lock_bh(old_bucket_lock); |
| 193 | while (!rhashtable_rehash_one(ht, old_hash)) |
| 194 | ; |
Herbert Xu | 63d512d | 2015-03-14 13:57:24 +1100 | [diff] [blame] | 195 | old_tbl->rehash++; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 196 | spin_unlock_bh(old_bucket_lock); |
| 197 | } |
| 198 | |
| 199 | static void rhashtable_rehash(struct rhashtable *ht, |
| 200 | struct bucket_table *new_tbl) |
| 201 | { |
| 202 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 203 | struct rhashtable_walker *walker; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 204 | unsigned old_hash; |
| 205 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 206 | /* Make insertions go into the new, empty table right away. Deletions |
| 207 | * and lookups will be attempted in both tables until we synchronize. |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 208 | */ |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 209 | rcu_assign_pointer(old_tbl->future_tbl, new_tbl); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 210 | |
Herbert Xu | 9497df8 | 2015-03-12 22:07:49 +1100 | [diff] [blame] | 211 | /* Ensure the new table is visible to readers. */ |
| 212 | smp_wmb(); |
| 213 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 214 | for (old_hash = 0; old_hash < old_tbl->size; old_hash++) |
| 215 | rhashtable_rehash_chain(ht, old_hash); |
| 216 | |
| 217 | /* Publish the new table pointer. */ |
| 218 | rcu_assign_pointer(ht->tbl, new_tbl); |
| 219 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 220 | list_for_each_entry(walker, &old_tbl->walkers, list) |
| 221 | walker->tbl = NULL; |
| 222 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 223 | /* Wait for readers. All new readers will see the new |
| 224 | * table, and thus no references to the old table will |
| 225 | * remain. |
| 226 | */ |
Herbert Xu | 9d901bc | 2015-03-14 13:57:23 +1100 | [diff] [blame] | 227 | call_rcu(&old_tbl->rcu, bucket_table_free_rcu); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
| 231 | * rhashtable_expand - Expand hash table while allowing concurrent lookups |
| 232 | * @ht: the hash table to expand |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 233 | * |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 234 | * A secondary bucket array is allocated and the hash entries are migrated. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 235 | * |
| 236 | * This function may only be called in a context where it is safe to call |
| 237 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. |
| 238 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 239 | * The caller must ensure that no concurrent resizing occurs by holding |
| 240 | * ht->mutex. |
| 241 | * |
| 242 | * It is valid to have concurrent insertions and deletions protected by per |
| 243 | * bucket locks or concurrent RCU protected lookups and traversals. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 244 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 245 | int rhashtable_expand(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 246 | { |
| 247 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 248 | |
| 249 | ASSERT_RHT_MUTEX(ht); |
| 250 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame] | 251 | new_tbl = bucket_table_alloc(ht, old_tbl->size * 2); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 252 | if (new_tbl == NULL) |
| 253 | return -ENOMEM; |
| 254 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 255 | rhashtable_rehash(ht, new_tbl); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | EXPORT_SYMBOL_GPL(rhashtable_expand); |
| 259 | |
| 260 | /** |
| 261 | * rhashtable_shrink - Shrink hash table while allowing concurrent lookups |
| 262 | * @ht: the hash table to shrink |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 263 | * |
Herbert Xu | 18093d1 | 2015-03-24 00:50:25 +1100 | [diff] [blame^] | 264 | * This function shrinks the hash table to fit, i.e., the smallest |
| 265 | * size would not cause it to expand right away automatically. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 266 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 267 | * The caller must ensure that no concurrent resizing occurs by holding |
| 268 | * ht->mutex. |
| 269 | * |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 270 | * The caller must ensure that no concurrent table mutations take place. |
| 271 | * 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] | 272 | * |
| 273 | * It is valid to have concurrent insertions and deletions protected by per |
| 274 | * bucket locks or concurrent RCU protected lookups and traversals. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 275 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 276 | int rhashtable_shrink(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 277 | { |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 278 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); |
Herbert Xu | 18093d1 | 2015-03-24 00:50:25 +1100 | [diff] [blame^] | 279 | unsigned size = roundup_pow_of_two(atomic_read(&ht->nelems) * 3 / 2); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 280 | |
| 281 | ASSERT_RHT_MUTEX(ht); |
| 282 | |
Herbert Xu | 18093d1 | 2015-03-24 00:50:25 +1100 | [diff] [blame^] | 283 | if (size < ht->p.min_size) |
| 284 | size = ht->p.min_size; |
| 285 | |
| 286 | if (old_tbl->size <= size) |
| 287 | return 0; |
| 288 | |
| 289 | new_tbl = bucket_table_alloc(ht, size); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 290 | if (new_tbl == NULL) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 291 | return -ENOMEM; |
| 292 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 293 | rhashtable_rehash(ht, new_tbl); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | EXPORT_SYMBOL_GPL(rhashtable_shrink); |
| 297 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 298 | static void rht_deferred_worker(struct work_struct *work) |
| 299 | { |
| 300 | struct rhashtable *ht; |
| 301 | struct bucket_table *tbl; |
| 302 | |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 303 | ht = container_of(work, struct rhashtable, run_work); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 304 | mutex_lock(&ht->mutex); |
Herbert Xu | 28134a5 | 2015-02-04 07:33:22 +1100 | [diff] [blame] | 305 | if (ht->being_destroyed) |
| 306 | goto unlock; |
| 307 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 308 | tbl = rht_dereference(ht->tbl, ht); |
| 309 | |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 310 | if (rht_grow_above_75(ht, tbl)) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 311 | rhashtable_expand(ht); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 312 | else if (rht_shrink_below_30(ht, tbl)) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 313 | rhashtable_shrink(ht); |
Herbert Xu | 28134a5 | 2015-02-04 07:33:22 +1100 | [diff] [blame] | 314 | unlock: |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 315 | mutex_unlock(&ht->mutex); |
| 316 | } |
| 317 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 318 | int rhashtable_insert_slow(struct rhashtable *ht, const void *key, |
| 319 | struct rhash_head *obj, |
| 320 | struct bucket_table *tbl) |
| 321 | { |
| 322 | struct rhash_head *head; |
| 323 | unsigned hash; |
| 324 | int err = -EEXIST; |
| 325 | |
| 326 | hash = head_hashfn(ht, tbl, obj); |
| 327 | spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING); |
| 328 | |
| 329 | if (key && rhashtable_lookup_fast(ht, key, ht->p)) |
| 330 | goto exit; |
| 331 | |
| 332 | err = 0; |
| 333 | |
| 334 | head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); |
| 335 | |
| 336 | RCU_INIT_POINTER(obj->next, head); |
| 337 | |
| 338 | rcu_assign_pointer(tbl->buckets[hash], obj); |
| 339 | |
| 340 | atomic_inc(&ht->nelems); |
| 341 | |
| 342 | exit: |
| 343 | spin_unlock(rht_bucket_lock(tbl, hash)); |
| 344 | |
| 345 | return err; |
| 346 | } |
| 347 | EXPORT_SYMBOL_GPL(rhashtable_insert_slow); |
| 348 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 349 | /** |
| 350 | * rhashtable_walk_init - Initialise an iterator |
| 351 | * @ht: Table to walk over |
| 352 | * @iter: Hash table Iterator |
| 353 | * |
| 354 | * This function prepares a hash table walk. |
| 355 | * |
| 356 | * Note that if you restart a walk after rhashtable_walk_stop you |
| 357 | * may see the same object twice. Also, you may miss objects if |
| 358 | * there are removals in between rhashtable_walk_stop and the next |
| 359 | * call to rhashtable_walk_start. |
| 360 | * |
| 361 | * For a completely stable walk you should construct your own data |
| 362 | * structure outside the hash table. |
| 363 | * |
| 364 | * This function may sleep so you must not call it from interrupt |
| 365 | * context or with spin locks held. |
| 366 | * |
| 367 | * You must call rhashtable_walk_exit if this function returns |
| 368 | * successfully. |
| 369 | */ |
| 370 | int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter) |
| 371 | { |
| 372 | iter->ht = ht; |
| 373 | iter->p = NULL; |
| 374 | iter->slot = 0; |
| 375 | iter->skip = 0; |
| 376 | |
| 377 | iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL); |
| 378 | if (!iter->walker) |
| 379 | return -ENOMEM; |
| 380 | |
| 381 | mutex_lock(&ht->mutex); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 382 | iter->walker->tbl = rht_dereference(ht->tbl, ht); |
| 383 | list_add(&iter->walker->list, &iter->walker->tbl->walkers); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 384 | mutex_unlock(&ht->mutex); |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | EXPORT_SYMBOL_GPL(rhashtable_walk_init); |
| 389 | |
| 390 | /** |
| 391 | * rhashtable_walk_exit - Free an iterator |
| 392 | * @iter: Hash table Iterator |
| 393 | * |
| 394 | * This function frees resources allocated by rhashtable_walk_init. |
| 395 | */ |
| 396 | void rhashtable_walk_exit(struct rhashtable_iter *iter) |
| 397 | { |
| 398 | mutex_lock(&iter->ht->mutex); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 399 | if (iter->walker->tbl) |
| 400 | list_del(&iter->walker->list); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 401 | mutex_unlock(&iter->ht->mutex); |
| 402 | kfree(iter->walker); |
| 403 | } |
| 404 | EXPORT_SYMBOL_GPL(rhashtable_walk_exit); |
| 405 | |
| 406 | /** |
| 407 | * rhashtable_walk_start - Start a hash table walk |
| 408 | * @iter: Hash table iterator |
| 409 | * |
| 410 | * Start a hash table walk. Note that we take the RCU lock in all |
| 411 | * cases including when we return an error. So you must always call |
| 412 | * rhashtable_walk_stop to clean up. |
| 413 | * |
| 414 | * Returns zero if successful. |
| 415 | * |
| 416 | * Returns -EAGAIN if resize event occured. Note that the iterator |
| 417 | * will rewind back to the beginning and you may use it immediately |
| 418 | * by calling rhashtable_walk_next. |
| 419 | */ |
| 420 | int rhashtable_walk_start(struct rhashtable_iter *iter) |
Thomas Graf | db4374f | 2015-03-16 10:42:27 +0100 | [diff] [blame] | 421 | __acquires(RCU) |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 422 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 423 | struct rhashtable *ht = iter->ht; |
| 424 | |
| 425 | mutex_lock(&ht->mutex); |
| 426 | |
| 427 | if (iter->walker->tbl) |
| 428 | list_del(&iter->walker->list); |
| 429 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 430 | rcu_read_lock(); |
| 431 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 432 | mutex_unlock(&ht->mutex); |
| 433 | |
| 434 | if (!iter->walker->tbl) { |
| 435 | iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 436 | return -EAGAIN; |
| 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | EXPORT_SYMBOL_GPL(rhashtable_walk_start); |
| 442 | |
| 443 | /** |
| 444 | * rhashtable_walk_next - Return the next object and advance the iterator |
| 445 | * @iter: Hash table iterator |
| 446 | * |
| 447 | * Note that you must call rhashtable_walk_stop when you are finished |
| 448 | * with the walk. |
| 449 | * |
| 450 | * Returns the next object or NULL when the end of the table is reached. |
| 451 | * |
| 452 | * Returns -EAGAIN if resize event occured. Note that the iterator |
| 453 | * will rewind back to the beginning and you may continue to use it. |
| 454 | */ |
| 455 | void *rhashtable_walk_next(struct rhashtable_iter *iter) |
| 456 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 457 | struct bucket_table *tbl = iter->walker->tbl; |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 458 | struct rhashtable *ht = iter->ht; |
| 459 | struct rhash_head *p = iter->p; |
| 460 | void *obj = NULL; |
| 461 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 462 | if (p) { |
| 463 | p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot); |
| 464 | goto next; |
| 465 | } |
| 466 | |
| 467 | for (; iter->slot < tbl->size; iter->slot++) { |
| 468 | int skip = iter->skip; |
| 469 | |
| 470 | rht_for_each_rcu(p, tbl, iter->slot) { |
| 471 | if (!skip) |
| 472 | break; |
| 473 | skip--; |
| 474 | } |
| 475 | |
| 476 | next: |
| 477 | if (!rht_is_a_nulls(p)) { |
| 478 | iter->skip++; |
| 479 | iter->p = p; |
| 480 | obj = rht_obj(ht, p); |
| 481 | goto out; |
| 482 | } |
| 483 | |
| 484 | iter->skip = 0; |
| 485 | } |
| 486 | |
Herbert Xu | d88252f | 2015-03-24 00:50:19 +1100 | [diff] [blame] | 487 | /* Ensure we see any new tables. */ |
| 488 | smp_rmb(); |
| 489 | |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 490 | iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 491 | if (iter->walker->tbl) { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 492 | iter->slot = 0; |
| 493 | iter->skip = 0; |
| 494 | return ERR_PTR(-EAGAIN); |
| 495 | } |
| 496 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 497 | iter->p = NULL; |
| 498 | |
| 499 | out: |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 500 | |
| 501 | return obj; |
| 502 | } |
| 503 | EXPORT_SYMBOL_GPL(rhashtable_walk_next); |
| 504 | |
| 505 | /** |
| 506 | * rhashtable_walk_stop - Finish a hash table walk |
| 507 | * @iter: Hash table iterator |
| 508 | * |
| 509 | * Finish a hash table walk. |
| 510 | */ |
| 511 | void rhashtable_walk_stop(struct rhashtable_iter *iter) |
Thomas Graf | db4374f | 2015-03-16 10:42:27 +0100 | [diff] [blame] | 512 | __releases(RCU) |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 513 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 514 | struct rhashtable *ht; |
| 515 | struct bucket_table *tbl = iter->walker->tbl; |
| 516 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 517 | if (!tbl) |
Herbert Xu | 963ecbd | 2015-03-15 21:12:04 +1100 | [diff] [blame] | 518 | goto out; |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 519 | |
| 520 | ht = iter->ht; |
| 521 | |
| 522 | mutex_lock(&ht->mutex); |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 523 | if (tbl->rehash < tbl->size) |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 524 | list_add(&iter->walker->list, &tbl->walkers); |
| 525 | else |
| 526 | iter->walker->tbl = NULL; |
| 527 | mutex_unlock(&ht->mutex); |
| 528 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 529 | iter->p = NULL; |
Herbert Xu | 963ecbd | 2015-03-15 21:12:04 +1100 | [diff] [blame] | 530 | |
| 531 | out: |
| 532 | rcu_read_unlock(); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 533 | } |
| 534 | EXPORT_SYMBOL_GPL(rhashtable_walk_stop); |
| 535 | |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 536 | static size_t rounded_hashtable_size(const struct rhashtable_params *params) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 537 | { |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 538 | return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), |
Herbert Xu | e2e21c1 | 2015-03-18 20:01:21 +1100 | [diff] [blame] | 539 | (unsigned long)params->min_size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 542 | static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) |
| 543 | { |
| 544 | return jhash2(key, length, seed); |
| 545 | } |
| 546 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 547 | /** |
| 548 | * rhashtable_init - initialize a new hash table |
| 549 | * @ht: hash table to be initialized |
| 550 | * @params: configuration parameters |
| 551 | * |
| 552 | * Initializes a new hash table based on the provided configuration |
| 553 | * parameters. A table can be configured either with a variable or |
| 554 | * fixed length key: |
| 555 | * |
| 556 | * Configuration Example 1: Fixed length keys |
| 557 | * struct test_obj { |
| 558 | * int key; |
| 559 | * void * my_member; |
| 560 | * struct rhash_head node; |
| 561 | * }; |
| 562 | * |
| 563 | * struct rhashtable_params params = { |
| 564 | * .head_offset = offsetof(struct test_obj, node), |
| 565 | * .key_offset = offsetof(struct test_obj, key), |
| 566 | * .key_len = sizeof(int), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 567 | * .hashfn = jhash, |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 568 | * .nulls_base = (1U << RHT_BASE_SHIFT), |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 569 | * }; |
| 570 | * |
| 571 | * Configuration Example 2: Variable length keys |
| 572 | * struct test_obj { |
| 573 | * [...] |
| 574 | * struct rhash_head node; |
| 575 | * }; |
| 576 | * |
| 577 | * u32 my_hash_fn(const void *data, u32 seed) |
| 578 | * { |
| 579 | * struct test_obj *obj = data; |
| 580 | * |
| 581 | * return [... hash ...]; |
| 582 | * } |
| 583 | * |
| 584 | * struct rhashtable_params params = { |
| 585 | * .head_offset = offsetof(struct test_obj, node), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 586 | * .hashfn = jhash, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 587 | * .obj_hashfn = my_hash_fn, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 588 | * }; |
| 589 | */ |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 590 | int rhashtable_init(struct rhashtable *ht, |
| 591 | const struct rhashtable_params *params) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 592 | { |
| 593 | struct bucket_table *tbl; |
| 594 | size_t size; |
| 595 | |
| 596 | size = HASH_DEFAULT_SIZE; |
| 597 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 598 | if ((!params->key_len && !params->obj_hashfn) || |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 599 | (params->obj_hashfn && !params->obj_cmpfn)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 600 | return -EINVAL; |
| 601 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 602 | if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) |
| 603 | return -EINVAL; |
| 604 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 605 | if (params->nelem_hint) |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 606 | size = rounded_hashtable_size(params); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 607 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 608 | memset(ht, 0, sizeof(*ht)); |
| 609 | mutex_init(&ht->mutex); |
| 610 | memcpy(&ht->p, params, sizeof(*params)); |
| 611 | |
Thomas Graf | a998f71 | 2015-03-19 22:31:13 +0000 | [diff] [blame] | 612 | if (params->min_size) |
| 613 | ht->p.min_size = roundup_pow_of_two(params->min_size); |
| 614 | |
| 615 | if (params->max_size) |
| 616 | ht->p.max_size = rounddown_pow_of_two(params->max_size); |
| 617 | |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 618 | ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE); |
Thomas Graf | a998f71 | 2015-03-19 22:31:13 +0000 | [diff] [blame] | 619 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 620 | if (params->locks_mul) |
| 621 | ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); |
| 622 | else |
| 623 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; |
| 624 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 625 | ht->key_len = ht->p.key_len; |
| 626 | if (!params->hashfn) { |
| 627 | ht->p.hashfn = jhash; |
| 628 | |
| 629 | if (!(ht->key_len & (sizeof(u32) - 1))) { |
| 630 | ht->key_len /= sizeof(u32); |
| 631 | ht->p.hashfn = rhashtable_jhash2; |
| 632 | } |
| 633 | } |
| 634 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame] | 635 | tbl = bucket_table_alloc(ht, size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 636 | if (tbl == NULL) |
| 637 | return -ENOMEM; |
| 638 | |
Ying Xue | 545a148 | 2015-01-07 13:41:57 +0800 | [diff] [blame] | 639 | atomic_set(&ht->nelems, 0); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 640 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 641 | RCU_INIT_POINTER(ht->tbl, tbl); |
| 642 | |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 643 | INIT_WORK(&ht->run_work, rht_deferred_worker); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 644 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 645 | return 0; |
| 646 | } |
| 647 | EXPORT_SYMBOL_GPL(rhashtable_init); |
| 648 | |
| 649 | /** |
| 650 | * rhashtable_destroy - destroy hash table |
| 651 | * @ht: the hash table to destroy |
| 652 | * |
Pablo Neira Ayuso | ae82ddc | 2014-09-02 00:26:05 +0200 | [diff] [blame] | 653 | * Frees the bucket array. This function is not rcu safe, therefore the caller |
| 654 | * has to make sure that no resizing may happen by unpublishing the hashtable |
| 655 | * and waiting for the quiescent cycle before releasing the bucket array. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 656 | */ |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 657 | void rhashtable_destroy(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 658 | { |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 659 | ht->being_destroyed = true; |
| 660 | |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 661 | cancel_work_sync(&ht->run_work); |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 662 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 663 | mutex_lock(&ht->mutex); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 664 | bucket_table_free(rht_dereference(ht->tbl, ht)); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 665 | mutex_unlock(&ht->mutex); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 666 | } |
| 667 | EXPORT_SYMBOL_GPL(rhashtable_destroy); |