Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Resizable, Scalable, Concurrent Hash Table |
| 3 | * |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 4 | * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 5 | * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
| 6 | * |
| 7 | * Based on the following paper: |
| 8 | * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf |
| 9 | * |
| 10 | * Code partially derived from nft_hash |
| 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 |
| 30 | #define HASH_MIN_SIZE 4UL |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 31 | #define BUCKET_LOCKS_PER_CPU 128UL |
| 32 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 33 | /* Base bits plus 1 bit for nulls marker */ |
| 34 | #define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1) |
| 35 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 36 | /* The bucket lock is selected based on the hash and protects mutations |
| 37 | * on a group of hash buckets. |
| 38 | * |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 39 | * A maximum of tbl->size/2 bucket locks is allocated. This ensures that |
| 40 | * a single lock always covers both buckets which may both contains |
| 41 | * entries which link to the same bucket of the old table during resizing. |
| 42 | * This allows to simplify the locking as locking the bucket in both |
| 43 | * tables during resize always guarantee protection. |
| 44 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 45 | * IMPORTANT: When holding the bucket lock of both the old and new table |
| 46 | * during expansions and shrinking, the old bucket lock must always be |
| 47 | * acquired first. |
| 48 | */ |
| 49 | static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash) |
| 50 | { |
| 51 | return &tbl->locks[hash & tbl->locks_mask]; |
| 52 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 53 | |
Thomas Graf | c91eee5 | 2014-08-13 16:38:30 +0200 | [diff] [blame] | 54 | static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 55 | { |
| 56 | return (void *) he - ht->p.head_offset; |
| 57 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 58 | |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 59 | static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 60 | { |
Herbert Xu | ec9f71c | 2015-03-12 14:49:41 +1100 | [diff] [blame] | 61 | return (hash >> HASH_RESERVED_SPACE) & (tbl->size - 1); |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 64 | static u32 key_hashfn(struct rhashtable *ht, const struct bucket_table *tbl, |
Herbert Xu | cffaa9c | 2015-03-12 14:49:40 +1100 | [diff] [blame] | 65 | const void *key) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 66 | { |
Herbert Xu | cffaa9c | 2015-03-12 14:49:40 +1100 | [diff] [blame] | 67 | return rht_bucket_index(tbl, ht->p.hashfn(key, ht->p.key_len, |
Herbert Xu | ec9f71c | 2015-03-12 14:49:41 +1100 | [diff] [blame] | 68 | tbl->hash_rnd)); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 69 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 70 | |
Herbert Xu | 988dfbd | 2015-03-10 09:27:55 +1100 | [diff] [blame] | 71 | static u32 head_hashfn(struct rhashtable *ht, |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 72 | const struct bucket_table *tbl, |
| 73 | const struct rhash_head *he) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 74 | { |
Herbert Xu | ec9f71c | 2015-03-12 14:49:41 +1100 | [diff] [blame] | 75 | const char *ptr = rht_obj(ht, he); |
| 76 | |
| 77 | return likely(ht->p.key_len) ? |
| 78 | key_hashfn(ht, tbl, ptr + ht->p.key_offset) : |
| 79 | rht_bucket_index(tbl, ht->p.obj_hashfn(ptr, tbl->hash_rnd)); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 82 | #ifdef CONFIG_PROVE_LOCKING |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 83 | #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] | 84 | |
| 85 | int lockdep_rht_mutex_is_held(struct rhashtable *ht) |
| 86 | { |
| 87 | return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1; |
| 88 | } |
| 89 | EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); |
| 90 | |
| 91 | int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) |
| 92 | { |
| 93 | spinlock_t *lock = bucket_lock(tbl, hash); |
| 94 | |
| 95 | return (debug_locks) ? lockdep_is_held(lock) : 1; |
| 96 | } |
| 97 | EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); |
| 98 | #else |
| 99 | #define ASSERT_RHT_MUTEX(HT) |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 100 | #endif |
| 101 | |
| 102 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 103 | static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl) |
| 104 | { |
| 105 | unsigned int i, size; |
| 106 | #if defined(CONFIG_PROVE_LOCKING) |
| 107 | unsigned int nr_pcpus = 2; |
| 108 | #else |
| 109 | unsigned int nr_pcpus = num_possible_cpus(); |
| 110 | #endif |
| 111 | |
| 112 | nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL); |
| 113 | size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul); |
| 114 | |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 115 | /* Never allocate more than 0.5 locks per bucket */ |
| 116 | size = min_t(unsigned int, size, tbl->size >> 1); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 117 | |
| 118 | if (sizeof(spinlock_t) != 0) { |
| 119 | #ifdef CONFIG_NUMA |
| 120 | if (size * sizeof(spinlock_t) > PAGE_SIZE) |
| 121 | tbl->locks = vmalloc(size * sizeof(spinlock_t)); |
| 122 | else |
| 123 | #endif |
| 124 | tbl->locks = kmalloc_array(size, sizeof(spinlock_t), |
| 125 | GFP_KERNEL); |
| 126 | if (!tbl->locks) |
| 127 | return -ENOMEM; |
| 128 | for (i = 0; i < size; i++) |
| 129 | spin_lock_init(&tbl->locks[i]); |
| 130 | } |
| 131 | tbl->locks_mask = size - 1; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static void bucket_table_free(const struct bucket_table *tbl) |
| 137 | { |
| 138 | if (tbl) |
| 139 | kvfree(tbl->locks); |
| 140 | |
| 141 | kvfree(tbl); |
| 142 | } |
| 143 | |
| 144 | static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame^] | 145 | size_t nbuckets) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 146 | { |
Daniel Borkmann | eb6d1ab | 2015-02-20 00:53:38 +0100 | [diff] [blame] | 147 | struct bucket_table *tbl = NULL; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 148 | size_t size; |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 149 | int i; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 150 | |
| 151 | size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); |
Daniel Borkmann | eb6d1ab | 2015-02-20 00:53:38 +0100 | [diff] [blame] | 152 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) |
| 153 | tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 154 | if (tbl == NULL) |
| 155 | tbl = vzalloc(size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 156 | if (tbl == NULL) |
| 157 | return NULL; |
| 158 | |
| 159 | tbl->size = nbuckets; |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 160 | tbl->shift = ilog2(nbuckets); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 161 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 162 | if (alloc_bucket_locks(ht, tbl) < 0) { |
| 163 | bucket_table_free(tbl); |
| 164 | return NULL; |
| 165 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 166 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 167 | INIT_LIST_HEAD(&tbl->walkers); |
| 168 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame^] | 169 | get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); |
| 170 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 171 | for (i = 0; i < nbuckets; i++) |
| 172 | INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i); |
| 173 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 174 | return tbl; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * rht_grow_above_75 - returns true if nelems > 0.75 * table-size |
| 179 | * @ht: hash table |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 180 | * @tbl: current table |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 181 | */ |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 182 | static bool rht_grow_above_75(const struct rhashtable *ht, |
| 183 | const struct bucket_table *tbl) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 184 | { |
| 185 | /* Expand table when exceeding 75% load */ |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 186 | return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) && |
| 187 | (!ht->p.max_shift || tbl->shift < ht->p.max_shift); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 188 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 189 | |
| 190 | /** |
| 191 | * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size |
| 192 | * @ht: hash table |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 193 | * @tbl: current table |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 194 | */ |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 195 | static bool rht_shrink_below_30(const struct rhashtable *ht, |
| 196 | const struct bucket_table *tbl) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 197 | { |
| 198 | /* Shrink table beneath 30% load */ |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 199 | return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) && |
| 200 | tbl->shift > ht->p.min_shift; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 201 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 202 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 203 | static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash) |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 204 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 205 | struct bucket_table *new_tbl = rht_dereference(ht->future_tbl, ht); |
| 206 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
| 207 | struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash]; |
| 208 | int err = -ENOENT; |
| 209 | struct rhash_head *head, *next, *entry; |
| 210 | spinlock_t *new_bucket_lock; |
| 211 | unsigned new_hash; |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 212 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 213 | rht_for_each(entry, old_tbl, old_hash) { |
| 214 | err = 0; |
| 215 | next = rht_dereference_bucket(entry->next, old_tbl, old_hash); |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 216 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 217 | if (rht_is_a_nulls(next)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 218 | break; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 219 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 220 | pprev = &entry->next; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 223 | if (err) |
| 224 | goto out; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 225 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 226 | new_hash = head_hashfn(ht, new_tbl, entry); |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 227 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 228 | new_bucket_lock = bucket_lock(new_tbl, new_hash); |
| 229 | |
Herbert Xu | 8f2484b | 2015-03-14 13:57:21 +1100 | [diff] [blame] | 230 | spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 231 | head = rht_dereference_bucket(new_tbl->buckets[new_hash], |
| 232 | new_tbl, new_hash); |
| 233 | |
| 234 | if (rht_is_a_nulls(head)) |
| 235 | INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash); |
| 236 | else |
| 237 | RCU_INIT_POINTER(entry->next, head); |
| 238 | |
| 239 | rcu_assign_pointer(new_tbl->buckets[new_hash], entry); |
| 240 | spin_unlock(new_bucket_lock); |
| 241 | |
| 242 | rcu_assign_pointer(*pprev, next); |
| 243 | |
| 244 | out: |
| 245 | return err; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 248 | static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 249 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 250 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
| 251 | spinlock_t *old_bucket_lock; |
Thomas Graf | 7cd10db | 2015-02-05 02:03:35 +0100 | [diff] [blame] | 252 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 253 | old_bucket_lock = bucket_lock(old_tbl, old_hash); |
| 254 | |
| 255 | spin_lock_bh(old_bucket_lock); |
| 256 | while (!rhashtable_rehash_one(ht, old_hash)) |
| 257 | ; |
| 258 | spin_unlock_bh(old_bucket_lock); |
| 259 | } |
| 260 | |
| 261 | static void rhashtable_rehash(struct rhashtable *ht, |
| 262 | struct bucket_table *new_tbl) |
| 263 | { |
| 264 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 265 | struct rhashtable_walker *walker; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 266 | unsigned old_hash; |
| 267 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 268 | /* Make insertions go into the new, empty table right away. Deletions |
| 269 | * and lookups will be attempted in both tables until we synchronize. |
| 270 | * The synchronize_rcu() guarantees for the new table to be picked up |
| 271 | * so no new additions go into the old table while we relink. |
| 272 | */ |
| 273 | rcu_assign_pointer(ht->future_tbl, new_tbl); |
| 274 | |
Herbert Xu | 9497df8 | 2015-03-12 22:07:49 +1100 | [diff] [blame] | 275 | /* Ensure the new table is visible to readers. */ |
| 276 | smp_wmb(); |
| 277 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 278 | for (old_hash = 0; old_hash < old_tbl->size; old_hash++) |
| 279 | rhashtable_rehash_chain(ht, old_hash); |
| 280 | |
| 281 | /* Publish the new table pointer. */ |
| 282 | rcu_assign_pointer(ht->tbl, new_tbl); |
| 283 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 284 | list_for_each_entry(walker, &old_tbl->walkers, list) |
| 285 | walker->tbl = NULL; |
| 286 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 287 | /* Wait for readers. All new readers will see the new |
| 288 | * table, and thus no references to the old table will |
| 289 | * remain. |
| 290 | */ |
| 291 | synchronize_rcu(); |
| 292 | |
| 293 | bucket_table_free(old_tbl); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
| 297 | * rhashtable_expand - Expand hash table while allowing concurrent lookups |
| 298 | * @ht: the hash table to expand |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 299 | * |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 300 | * A secondary bucket array is allocated and the hash entries are migrated. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 301 | * |
| 302 | * This function may only be called in a context where it is safe to call |
| 303 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. |
| 304 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 305 | * The caller must ensure that no concurrent resizing occurs by holding |
| 306 | * ht->mutex. |
| 307 | * |
| 308 | * It is valid to have concurrent insertions and deletions protected by per |
| 309 | * bucket locks or concurrent RCU protected lookups and traversals. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 310 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 311 | int rhashtable_expand(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 312 | { |
| 313 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 314 | |
| 315 | ASSERT_RHT_MUTEX(ht); |
| 316 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame^] | 317 | new_tbl = bucket_table_alloc(ht, old_tbl->size * 2); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 318 | if (new_tbl == NULL) |
| 319 | return -ENOMEM; |
| 320 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 321 | rhashtable_rehash(ht, new_tbl); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | EXPORT_SYMBOL_GPL(rhashtable_expand); |
| 325 | |
| 326 | /** |
| 327 | * rhashtable_shrink - Shrink hash table while allowing concurrent lookups |
| 328 | * @ht: the hash table to shrink |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 329 | * |
| 330 | * This function may only be called in a context where it is safe to call |
| 331 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. |
| 332 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 333 | * The caller must ensure that no concurrent resizing occurs by holding |
| 334 | * ht->mutex. |
| 335 | * |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 336 | * The caller must ensure that no concurrent table mutations take place. |
| 337 | * 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] | 338 | * |
| 339 | * It is valid to have concurrent insertions and deletions protected by per |
| 340 | * bucket locks or concurrent RCU protected lookups and traversals. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 341 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 342 | int rhashtable_shrink(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 343 | { |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 344 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 345 | |
| 346 | ASSERT_RHT_MUTEX(ht); |
| 347 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame^] | 348 | new_tbl = bucket_table_alloc(ht, old_tbl->size / 2); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 349 | if (new_tbl == NULL) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 350 | return -ENOMEM; |
| 351 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 352 | rhashtable_rehash(ht, new_tbl); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(rhashtable_shrink); |
| 356 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 357 | static void rht_deferred_worker(struct work_struct *work) |
| 358 | { |
| 359 | struct rhashtable *ht; |
| 360 | struct bucket_table *tbl; |
| 361 | |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 362 | ht = container_of(work, struct rhashtable, run_work); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 363 | mutex_lock(&ht->mutex); |
Herbert Xu | 28134a5 | 2015-02-04 07:33:22 +1100 | [diff] [blame] | 364 | if (ht->being_destroyed) |
| 365 | goto unlock; |
| 366 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 367 | tbl = rht_dereference(ht->tbl, ht); |
| 368 | |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 369 | if (rht_grow_above_75(ht, tbl)) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 370 | rhashtable_expand(ht); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 371 | else if (rht_shrink_below_30(ht, tbl)) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 372 | rhashtable_shrink(ht); |
Herbert Xu | 28134a5 | 2015-02-04 07:33:22 +1100 | [diff] [blame] | 373 | unlock: |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 374 | mutex_unlock(&ht->mutex); |
| 375 | } |
| 376 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 377 | static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj, |
| 378 | bool (*compare)(void *, void *), void *arg) |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 379 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 380 | struct bucket_table *tbl, *old_tbl; |
Thomas Graf | 020219a | 2015-02-06 16:08:43 +0000 | [diff] [blame] | 381 | struct rhash_head *head; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 382 | bool no_resize_running; |
| 383 | unsigned hash; |
| 384 | bool success = true; |
| 385 | |
| 386 | rcu_read_lock(); |
| 387 | |
| 388 | old_tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | eca8493 | 2015-03-12 14:49:39 +1100 | [diff] [blame] | 389 | hash = head_hashfn(ht, old_tbl, obj); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 390 | |
| 391 | spin_lock_bh(bucket_lock(old_tbl, hash)); |
| 392 | |
| 393 | /* Because we have already taken the bucket lock in old_tbl, |
| 394 | * if we find that future_tbl is not yet visible then that |
| 395 | * guarantees all other insertions of the same entry will |
| 396 | * also grab the bucket lock in old_tbl because until the |
| 397 | * rehash completes ht->tbl won't be changed. |
| 398 | */ |
| 399 | tbl = rht_dereference_rcu(ht->future_tbl, ht); |
| 400 | if (tbl != old_tbl) { |
Herbert Xu | eca8493 | 2015-03-12 14:49:39 +1100 | [diff] [blame] | 401 | hash = head_hashfn(ht, tbl, obj); |
Herbert Xu | 8f2484b | 2015-03-14 13:57:21 +1100 | [diff] [blame] | 402 | spin_lock_nested(bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | if (compare && |
| 406 | rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset, |
| 407 | compare, arg)) { |
| 408 | success = false; |
| 409 | goto exit; |
| 410 | } |
| 411 | |
| 412 | no_resize_running = tbl == old_tbl; |
Thomas Graf | 020219a | 2015-02-06 16:08:43 +0000 | [diff] [blame] | 413 | |
Thomas Graf | 020219a | 2015-02-06 16:08:43 +0000 | [diff] [blame] | 414 | head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 415 | |
| 416 | if (rht_is_a_nulls(head)) |
| 417 | INIT_RHT_NULLS_HEAD(obj->next, ht, hash); |
| 418 | else |
| 419 | RCU_INIT_POINTER(obj->next, head); |
| 420 | |
| 421 | rcu_assign_pointer(tbl->buckets[hash], obj); |
| 422 | |
| 423 | atomic_inc(&ht->nelems); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 424 | if (no_resize_running && rht_grow_above_75(ht, tbl)) |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 425 | schedule_work(&ht->run_work); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 426 | |
| 427 | exit: |
| 428 | if (tbl != old_tbl) { |
Herbert Xu | eca8493 | 2015-03-12 14:49:39 +1100 | [diff] [blame] | 429 | hash = head_hashfn(ht, tbl, obj); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 430 | spin_unlock(bucket_lock(tbl, hash)); |
| 431 | } |
| 432 | |
Herbert Xu | eca8493 | 2015-03-12 14:49:39 +1100 | [diff] [blame] | 433 | hash = head_hashfn(ht, old_tbl, obj); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 434 | spin_unlock_bh(bucket_lock(old_tbl, hash)); |
| 435 | |
| 436 | rcu_read_unlock(); |
| 437 | |
| 438 | return success; |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 439 | } |
| 440 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 441 | /** |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 442 | * rhashtable_insert - insert object into hash table |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 443 | * @ht: hash table |
| 444 | * @obj: pointer to hash head inside object |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 445 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 446 | * Will take a per bucket spinlock to protect against mutual mutations |
| 447 | * on the same bucket. Multiple insertions may occur in parallel unless |
| 448 | * they map to the same bucket lock. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 449 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 450 | * It is safe to call this function from atomic context. |
| 451 | * |
| 452 | * Will trigger an automatic deferred table resizing if the size grows |
| 453 | * beyond the watermark indicated by grow_decision() which can be passed |
| 454 | * to rhashtable_init(). |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 455 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 456 | void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 457 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 458 | __rhashtable_insert(ht, obj, NULL, NULL); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 459 | } |
| 460 | EXPORT_SYMBOL_GPL(rhashtable_insert); |
| 461 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 462 | static bool __rhashtable_remove(struct rhashtable *ht, |
| 463 | struct bucket_table *tbl, |
| 464 | struct rhash_head *obj) |
| 465 | { |
| 466 | struct rhash_head __rcu **pprev; |
| 467 | struct rhash_head *he; |
| 468 | spinlock_t * lock; |
| 469 | unsigned hash; |
| 470 | bool ret = false; |
| 471 | |
Herbert Xu | eca8493 | 2015-03-12 14:49:39 +1100 | [diff] [blame] | 472 | hash = head_hashfn(ht, tbl, obj); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 473 | lock = bucket_lock(tbl, hash); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 474 | |
| 475 | spin_lock_bh(lock); |
| 476 | |
| 477 | pprev = &tbl->buckets[hash]; |
| 478 | rht_for_each(he, tbl, hash) { |
| 479 | if (he != obj) { |
| 480 | pprev = &he->next; |
| 481 | continue; |
| 482 | } |
| 483 | |
| 484 | rcu_assign_pointer(*pprev, obj->next); |
| 485 | ret = true; |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | spin_unlock_bh(lock); |
| 490 | |
| 491 | return ret; |
| 492 | } |
| 493 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 494 | /** |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 495 | * rhashtable_remove - remove object from hash table |
| 496 | * @ht: hash table |
| 497 | * @obj: pointer to hash head inside object |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 498 | * |
| 499 | * Since the hash chain is single linked, the removal operation needs to |
| 500 | * walk the bucket chain upon removal. The removal operation is thus |
| 501 | * considerable slow if the hash table is not correctly sized. |
| 502 | * |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 503 | * Will automatically shrink the table via rhashtable_expand() if the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 504 | * shrink_decision function specified at rhashtable_init() returns true. |
| 505 | * |
| 506 | * The caller must ensure that no concurrent table mutations occur. It is |
| 507 | * however valid to have concurrent lookups if they are RCU protected. |
| 508 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 509 | bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 510 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 511 | struct bucket_table *tbl, *old_tbl; |
| 512 | bool ret; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 513 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 514 | rcu_read_lock(); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 515 | |
Thomas Graf | 020219a | 2015-02-06 16:08:43 +0000 | [diff] [blame] | 516 | old_tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 517 | ret = __rhashtable_remove(ht, old_tbl, obj); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 518 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 519 | /* Because we have already taken (and released) the bucket |
| 520 | * lock in old_tbl, if we find that future_tbl is not yet |
| 521 | * visible then that guarantees the entry to still be in |
| 522 | * old_tbl if it exists. |
Thomas Graf | fe6a043 | 2015-01-21 11:54:01 +0000 | [diff] [blame] | 523 | */ |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 524 | tbl = rht_dereference_rcu(ht->future_tbl, ht); |
| 525 | if (!ret && old_tbl != tbl) |
| 526 | ret = __rhashtable_remove(ht, tbl, obj); |
Thomas Graf | fe6a043 | 2015-01-21 11:54:01 +0000 | [diff] [blame] | 527 | |
| 528 | if (ret) { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 529 | bool no_resize_running = tbl == old_tbl; |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 530 | |
Thomas Graf | fe6a043 | 2015-01-21 11:54:01 +0000 | [diff] [blame] | 531 | atomic_dec(&ht->nelems); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 532 | if (no_resize_running && rht_shrink_below_30(ht, tbl)) |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 533 | schedule_work(&ht->run_work); |
Thomas Graf | fe6a043 | 2015-01-21 11:54:01 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 536 | rcu_read_unlock(); |
| 537 | |
Thomas Graf | fe6a043 | 2015-01-21 11:54:01 +0000 | [diff] [blame] | 538 | return ret; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 539 | } |
| 540 | EXPORT_SYMBOL_GPL(rhashtable_remove); |
| 541 | |
Ying Xue | efb975a6 | 2015-01-07 13:41:52 +0800 | [diff] [blame] | 542 | struct rhashtable_compare_arg { |
| 543 | struct rhashtable *ht; |
| 544 | const void *key; |
| 545 | }; |
| 546 | |
| 547 | static bool rhashtable_compare(void *ptr, void *arg) |
| 548 | { |
| 549 | struct rhashtable_compare_arg *x = arg; |
| 550 | struct rhashtable *ht = x->ht; |
| 551 | |
| 552 | return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len); |
| 553 | } |
| 554 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 555 | /** |
| 556 | * rhashtable_lookup - lookup key in hash table |
| 557 | * @ht: hash table |
| 558 | * @key: pointer to key |
| 559 | * |
| 560 | * Computes the hash value for the key and traverses the bucket chain looking |
| 561 | * for a entry with an identical key. The first matching entry is returned. |
| 562 | * |
| 563 | * This lookup function may only be used for fixed key hash table (key_len |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 564 | * parameter set). It will BUG() if used inappropriately. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 565 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 566 | * Lookups may occur in parallel with hashtable mutations and resizing. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 567 | */ |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 568 | void *rhashtable_lookup(struct rhashtable *ht, const void *key) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 569 | { |
Ying Xue | efb975a6 | 2015-01-07 13:41:52 +0800 | [diff] [blame] | 570 | struct rhashtable_compare_arg arg = { |
| 571 | .ht = ht, |
| 572 | .key = key, |
| 573 | }; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 574 | |
| 575 | BUG_ON(!ht->p.key_len); |
| 576 | |
Ying Xue | efb975a6 | 2015-01-07 13:41:52 +0800 | [diff] [blame] | 577 | return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 578 | } |
| 579 | EXPORT_SYMBOL_GPL(rhashtable_lookup); |
| 580 | |
| 581 | /** |
| 582 | * rhashtable_lookup_compare - search hash table with compare function |
| 583 | * @ht: hash table |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 584 | * @key: the pointer to the key |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 585 | * @compare: compare function, must return true on match |
| 586 | * @arg: argument passed on to compare function |
| 587 | * |
| 588 | * Traverses the bucket chain behind the provided hash value and calls the |
| 589 | * specified compare function for each entry. |
| 590 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 591 | * Lookups may occur in parallel with hashtable mutations and resizing. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 592 | * |
| 593 | * Returns the first entry on which the compare function returned true. |
| 594 | */ |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 595 | void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 596 | bool (*compare)(void *, void *), void *arg) |
| 597 | { |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 598 | const struct bucket_table *tbl, *old_tbl; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 599 | struct rhash_head *he; |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 600 | u32 hash; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 601 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 602 | rcu_read_lock(); |
| 603 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 604 | tbl = rht_dereference_rcu(ht->tbl, ht); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 605 | restart: |
Herbert Xu | 3936194 | 2015-03-13 12:54:10 +1100 | [diff] [blame] | 606 | hash = key_hashfn(ht, tbl, key); |
Herbert Xu | 8d2b187 | 2015-03-12 14:49:38 +1100 | [diff] [blame] | 607 | rht_for_each_rcu(he, tbl, hash) { |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 608 | if (!compare(rht_obj(ht, he), arg)) |
| 609 | continue; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 610 | rcu_read_unlock(); |
Thomas Graf | a4b18cd | 2015-01-02 23:00:15 +0100 | [diff] [blame] | 611 | return rht_obj(ht, he); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Herbert Xu | 9497df8 | 2015-03-12 22:07:49 +1100 | [diff] [blame] | 614 | /* Ensure we see any new tables. */ |
| 615 | smp_rmb(); |
| 616 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 617 | old_tbl = tbl; |
| 618 | tbl = rht_dereference_rcu(ht->future_tbl, ht); |
| 619 | if (unlikely(tbl != old_tbl)) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 620 | goto restart; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 621 | rcu_read_unlock(); |
| 622 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 623 | return NULL; |
| 624 | } |
| 625 | EXPORT_SYMBOL_GPL(rhashtable_lookup_compare); |
| 626 | |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 627 | /** |
| 628 | * rhashtable_lookup_insert - lookup and insert object into hash table |
| 629 | * @ht: hash table |
| 630 | * @obj: pointer to hash head inside object |
| 631 | * |
| 632 | * Locks down the bucket chain in both the old and new table if a resize |
| 633 | * is in progress to ensure that writers can't remove from the old table |
| 634 | * and can't insert to the new table during the atomic operation of search |
| 635 | * and insertion. Searches for duplicates in both the old and new table if |
| 636 | * a resize is in progress. |
| 637 | * |
| 638 | * This lookup function may only be used for fixed key hash table (key_len |
| 639 | * parameter set). It will BUG() if used inappropriately. |
| 640 | * |
| 641 | * It is safe to call this function from atomic context. |
| 642 | * |
| 643 | * Will trigger an automatic deferred table resizing if the size grows |
| 644 | * beyond the watermark indicated by grow_decision() which can be passed |
| 645 | * to rhashtable_init(). |
| 646 | */ |
| 647 | bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj) |
| 648 | { |
Ying Xue | 7a868d1 | 2015-01-12 14:52:22 +0800 | [diff] [blame] | 649 | struct rhashtable_compare_arg arg = { |
| 650 | .ht = ht, |
| 651 | .key = rht_obj(ht, obj) + ht->p.key_offset, |
| 652 | }; |
| 653 | |
| 654 | BUG_ON(!ht->p.key_len); |
| 655 | |
| 656 | return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare, |
| 657 | &arg); |
| 658 | } |
| 659 | EXPORT_SYMBOL_GPL(rhashtable_lookup_insert); |
| 660 | |
| 661 | /** |
| 662 | * rhashtable_lookup_compare_insert - search and insert object to hash table |
| 663 | * with compare function |
| 664 | * @ht: hash table |
| 665 | * @obj: pointer to hash head inside object |
| 666 | * @compare: compare function, must return true on match |
| 667 | * @arg: argument passed on to compare function |
| 668 | * |
| 669 | * Locks down the bucket chain in both the old and new table if a resize |
| 670 | * is in progress to ensure that writers can't remove from the old table |
| 671 | * and can't insert to the new table during the atomic operation of search |
| 672 | * and insertion. Searches for duplicates in both the old and new table if |
| 673 | * a resize is in progress. |
| 674 | * |
| 675 | * Lookups may occur in parallel with hashtable mutations and resizing. |
| 676 | * |
| 677 | * Will trigger an automatic deferred table resizing if the size grows |
| 678 | * beyond the watermark indicated by grow_decision() which can be passed |
| 679 | * to rhashtable_init(). |
| 680 | */ |
| 681 | bool rhashtable_lookup_compare_insert(struct rhashtable *ht, |
| 682 | struct rhash_head *obj, |
| 683 | bool (*compare)(void *, void *), |
| 684 | void *arg) |
| 685 | { |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 686 | BUG_ON(!ht->p.key_len); |
| 687 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 688 | return __rhashtable_insert(ht, obj, compare, arg); |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 689 | } |
Ying Xue | 7a868d1 | 2015-01-12 14:52:22 +0800 | [diff] [blame] | 690 | EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert); |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 691 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 692 | /** |
| 693 | * rhashtable_walk_init - Initialise an iterator |
| 694 | * @ht: Table to walk over |
| 695 | * @iter: Hash table Iterator |
| 696 | * |
| 697 | * This function prepares a hash table walk. |
| 698 | * |
| 699 | * Note that if you restart a walk after rhashtable_walk_stop you |
| 700 | * may see the same object twice. Also, you may miss objects if |
| 701 | * there are removals in between rhashtable_walk_stop and the next |
| 702 | * call to rhashtable_walk_start. |
| 703 | * |
| 704 | * For a completely stable walk you should construct your own data |
| 705 | * structure outside the hash table. |
| 706 | * |
| 707 | * This function may sleep so you must not call it from interrupt |
| 708 | * context or with spin locks held. |
| 709 | * |
| 710 | * You must call rhashtable_walk_exit if this function returns |
| 711 | * successfully. |
| 712 | */ |
| 713 | int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter) |
| 714 | { |
| 715 | iter->ht = ht; |
| 716 | iter->p = NULL; |
| 717 | iter->slot = 0; |
| 718 | iter->skip = 0; |
| 719 | |
| 720 | iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL); |
| 721 | if (!iter->walker) |
| 722 | return -ENOMEM; |
| 723 | |
| 724 | mutex_lock(&ht->mutex); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 725 | iter->walker->tbl = rht_dereference(ht->tbl, ht); |
| 726 | list_add(&iter->walker->list, &iter->walker->tbl->walkers); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 727 | mutex_unlock(&ht->mutex); |
| 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | EXPORT_SYMBOL_GPL(rhashtable_walk_init); |
| 732 | |
| 733 | /** |
| 734 | * rhashtable_walk_exit - Free an iterator |
| 735 | * @iter: Hash table Iterator |
| 736 | * |
| 737 | * This function frees resources allocated by rhashtable_walk_init. |
| 738 | */ |
| 739 | void rhashtable_walk_exit(struct rhashtable_iter *iter) |
| 740 | { |
| 741 | mutex_lock(&iter->ht->mutex); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 742 | if (iter->walker->tbl) |
| 743 | list_del(&iter->walker->list); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 744 | mutex_unlock(&iter->ht->mutex); |
| 745 | kfree(iter->walker); |
| 746 | } |
| 747 | EXPORT_SYMBOL_GPL(rhashtable_walk_exit); |
| 748 | |
| 749 | /** |
| 750 | * rhashtable_walk_start - Start a hash table walk |
| 751 | * @iter: Hash table iterator |
| 752 | * |
| 753 | * Start a hash table walk. Note that we take the RCU lock in all |
| 754 | * cases including when we return an error. So you must always call |
| 755 | * rhashtable_walk_stop to clean up. |
| 756 | * |
| 757 | * Returns zero if successful. |
| 758 | * |
| 759 | * Returns -EAGAIN if resize event occured. Note that the iterator |
| 760 | * will rewind back to the beginning and you may use it immediately |
| 761 | * by calling rhashtable_walk_next. |
| 762 | */ |
| 763 | int rhashtable_walk_start(struct rhashtable_iter *iter) |
| 764 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 765 | struct rhashtable *ht = iter->ht; |
| 766 | |
| 767 | mutex_lock(&ht->mutex); |
| 768 | |
| 769 | if (iter->walker->tbl) |
| 770 | list_del(&iter->walker->list); |
| 771 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 772 | rcu_read_lock(); |
| 773 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 774 | mutex_unlock(&ht->mutex); |
| 775 | |
| 776 | if (!iter->walker->tbl) { |
| 777 | iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 778 | return -EAGAIN; |
| 779 | } |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | EXPORT_SYMBOL_GPL(rhashtable_walk_start); |
| 784 | |
| 785 | /** |
| 786 | * rhashtable_walk_next - Return the next object and advance the iterator |
| 787 | * @iter: Hash table iterator |
| 788 | * |
| 789 | * Note that you must call rhashtable_walk_stop when you are finished |
| 790 | * with the walk. |
| 791 | * |
| 792 | * Returns the next object or NULL when the end of the table is reached. |
| 793 | * |
| 794 | * Returns -EAGAIN if resize event occured. Note that the iterator |
| 795 | * will rewind back to the beginning and you may continue to use it. |
| 796 | */ |
| 797 | void *rhashtable_walk_next(struct rhashtable_iter *iter) |
| 798 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 799 | struct bucket_table *tbl = iter->walker->tbl; |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 800 | struct rhashtable *ht = iter->ht; |
| 801 | struct rhash_head *p = iter->p; |
| 802 | void *obj = NULL; |
| 803 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 804 | if (p) { |
| 805 | p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot); |
| 806 | goto next; |
| 807 | } |
| 808 | |
| 809 | for (; iter->slot < tbl->size; iter->slot++) { |
| 810 | int skip = iter->skip; |
| 811 | |
| 812 | rht_for_each_rcu(p, tbl, iter->slot) { |
| 813 | if (!skip) |
| 814 | break; |
| 815 | skip--; |
| 816 | } |
| 817 | |
| 818 | next: |
| 819 | if (!rht_is_a_nulls(p)) { |
| 820 | iter->skip++; |
| 821 | iter->p = p; |
| 822 | obj = rht_obj(ht, p); |
| 823 | goto out; |
| 824 | } |
| 825 | |
| 826 | iter->skip = 0; |
| 827 | } |
| 828 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 829 | iter->walker->tbl = rht_dereference_rcu(ht->future_tbl, ht); |
| 830 | if (iter->walker->tbl != tbl) { |
| 831 | iter->slot = 0; |
| 832 | iter->skip = 0; |
| 833 | return ERR_PTR(-EAGAIN); |
| 834 | } |
| 835 | |
| 836 | iter->walker->tbl = NULL; |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 837 | iter->p = NULL; |
| 838 | |
| 839 | out: |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 840 | |
| 841 | return obj; |
| 842 | } |
| 843 | EXPORT_SYMBOL_GPL(rhashtable_walk_next); |
| 844 | |
| 845 | /** |
| 846 | * rhashtable_walk_stop - Finish a hash table walk |
| 847 | * @iter: Hash table iterator |
| 848 | * |
| 849 | * Finish a hash table walk. |
| 850 | */ |
| 851 | void rhashtable_walk_stop(struct rhashtable_iter *iter) |
| 852 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 853 | struct rhashtable *ht; |
| 854 | struct bucket_table *tbl = iter->walker->tbl; |
| 855 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 856 | rcu_read_unlock(); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 857 | |
| 858 | if (!tbl) |
| 859 | return; |
| 860 | |
| 861 | ht = iter->ht; |
| 862 | |
| 863 | mutex_lock(&ht->mutex); |
| 864 | if (rht_dereference(ht->tbl, ht) == tbl || |
| 865 | rht_dereference(ht->future_tbl, ht) == tbl) |
| 866 | list_add(&iter->walker->list, &tbl->walkers); |
| 867 | else |
| 868 | iter->walker->tbl = NULL; |
| 869 | mutex_unlock(&ht->mutex); |
| 870 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 871 | iter->p = NULL; |
| 872 | } |
| 873 | EXPORT_SYMBOL_GPL(rhashtable_walk_stop); |
| 874 | |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 875 | static size_t rounded_hashtable_size(struct rhashtable_params *params) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 876 | { |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 877 | return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), |
| 878 | 1UL << params->min_shift); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /** |
| 882 | * rhashtable_init - initialize a new hash table |
| 883 | * @ht: hash table to be initialized |
| 884 | * @params: configuration parameters |
| 885 | * |
| 886 | * Initializes a new hash table based on the provided configuration |
| 887 | * parameters. A table can be configured either with a variable or |
| 888 | * fixed length key: |
| 889 | * |
| 890 | * Configuration Example 1: Fixed length keys |
| 891 | * struct test_obj { |
| 892 | * int key; |
| 893 | * void * my_member; |
| 894 | * struct rhash_head node; |
| 895 | * }; |
| 896 | * |
| 897 | * struct rhashtable_params params = { |
| 898 | * .head_offset = offsetof(struct test_obj, node), |
| 899 | * .key_offset = offsetof(struct test_obj, key), |
| 900 | * .key_len = sizeof(int), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 901 | * .hashfn = jhash, |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 902 | * .nulls_base = (1U << RHT_BASE_SHIFT), |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 903 | * }; |
| 904 | * |
| 905 | * Configuration Example 2: Variable length keys |
| 906 | * struct test_obj { |
| 907 | * [...] |
| 908 | * struct rhash_head node; |
| 909 | * }; |
| 910 | * |
| 911 | * u32 my_hash_fn(const void *data, u32 seed) |
| 912 | * { |
| 913 | * struct test_obj *obj = data; |
| 914 | * |
| 915 | * return [... hash ...]; |
| 916 | * } |
| 917 | * |
| 918 | * struct rhashtable_params params = { |
| 919 | * .head_offset = offsetof(struct test_obj, node), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 920 | * .hashfn = jhash, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 921 | * .obj_hashfn = my_hash_fn, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 922 | * }; |
| 923 | */ |
| 924 | int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params) |
| 925 | { |
| 926 | struct bucket_table *tbl; |
| 927 | size_t size; |
| 928 | |
| 929 | size = HASH_DEFAULT_SIZE; |
| 930 | |
| 931 | if ((params->key_len && !params->hashfn) || |
| 932 | (!params->key_len && !params->obj_hashfn)) |
| 933 | return -EINVAL; |
| 934 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 935 | if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) |
| 936 | return -EINVAL; |
| 937 | |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 938 | params->min_shift = max_t(size_t, params->min_shift, |
| 939 | ilog2(HASH_MIN_SIZE)); |
| 940 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 941 | if (params->nelem_hint) |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 942 | size = rounded_hashtable_size(params); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 943 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 944 | memset(ht, 0, sizeof(*ht)); |
| 945 | mutex_init(&ht->mutex); |
| 946 | memcpy(&ht->p, params, sizeof(*params)); |
| 947 | |
| 948 | if (params->locks_mul) |
| 949 | ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); |
| 950 | else |
| 951 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; |
| 952 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame^] | 953 | tbl = bucket_table_alloc(ht, size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 954 | if (tbl == NULL) |
| 955 | return -ENOMEM; |
| 956 | |
Ying Xue | 545a148 | 2015-01-07 13:41:57 +0800 | [diff] [blame] | 957 | atomic_set(&ht->nelems, 0); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 958 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 959 | RCU_INIT_POINTER(ht->tbl, tbl); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 960 | RCU_INIT_POINTER(ht->future_tbl, tbl); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 961 | |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 962 | INIT_WORK(&ht->run_work, rht_deferred_worker); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 963 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | EXPORT_SYMBOL_GPL(rhashtable_init); |
| 967 | |
| 968 | /** |
| 969 | * rhashtable_destroy - destroy hash table |
| 970 | * @ht: the hash table to destroy |
| 971 | * |
Pablo Neira Ayuso | ae82ddc | 2014-09-02 00:26:05 +0200 | [diff] [blame] | 972 | * Frees the bucket array. This function is not rcu safe, therefore the caller |
| 973 | * has to make sure that no resizing may happen by unpublishing the hashtable |
| 974 | * and waiting for the quiescent cycle before releasing the bucket array. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 975 | */ |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 976 | void rhashtable_destroy(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 977 | { |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 978 | ht->being_destroyed = true; |
| 979 | |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 980 | cancel_work_sync(&ht->run_work); |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 981 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 982 | mutex_lock(&ht->mutex); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 983 | bucket_table_free(rht_dereference(ht->tbl, ht)); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 984 | mutex_unlock(&ht->mutex); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 985 | } |
| 986 | EXPORT_SYMBOL_GPL(rhashtable_destroy); |