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 | * |
| 264 | * This function may only be called in a context where it is safe to call |
| 265 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. |
| 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); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 279 | |
| 280 | ASSERT_RHT_MUTEX(ht); |
| 281 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame] | 282 | new_tbl = bucket_table_alloc(ht, old_tbl->size / 2); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 283 | if (new_tbl == NULL) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 284 | return -ENOMEM; |
| 285 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 286 | rhashtable_rehash(ht, new_tbl); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | EXPORT_SYMBOL_GPL(rhashtable_shrink); |
| 290 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 291 | static void rht_deferred_worker(struct work_struct *work) |
| 292 | { |
| 293 | struct rhashtable *ht; |
| 294 | struct bucket_table *tbl; |
| 295 | |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 296 | ht = container_of(work, struct rhashtable, run_work); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 297 | mutex_lock(&ht->mutex); |
Herbert Xu | 28134a5 | 2015-02-04 07:33:22 +1100 | [diff] [blame] | 298 | if (ht->being_destroyed) |
| 299 | goto unlock; |
| 300 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 301 | tbl = rht_dereference(ht->tbl, ht); |
| 302 | |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 303 | if (rht_grow_above_75(ht, tbl)) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 304 | rhashtable_expand(ht); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 305 | else if (rht_shrink_below_30(ht, tbl)) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 306 | rhashtable_shrink(ht); |
Herbert Xu | 28134a5 | 2015-02-04 07:33:22 +1100 | [diff] [blame] | 307 | unlock: |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 308 | mutex_unlock(&ht->mutex); |
| 309 | } |
| 310 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame^] | 311 | int rhashtable_insert_slow(struct rhashtable *ht, const void *key, |
| 312 | struct rhash_head *obj, |
| 313 | struct bucket_table *tbl) |
| 314 | { |
| 315 | struct rhash_head *head; |
| 316 | unsigned hash; |
| 317 | int err = -EEXIST; |
| 318 | |
| 319 | hash = head_hashfn(ht, tbl, obj); |
| 320 | spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING); |
| 321 | |
| 322 | if (key && rhashtable_lookup_fast(ht, key, ht->p)) |
| 323 | goto exit; |
| 324 | |
| 325 | err = 0; |
| 326 | |
| 327 | head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); |
| 328 | |
| 329 | RCU_INIT_POINTER(obj->next, head); |
| 330 | |
| 331 | rcu_assign_pointer(tbl->buckets[hash], obj); |
| 332 | |
| 333 | atomic_inc(&ht->nelems); |
| 334 | |
| 335 | exit: |
| 336 | spin_unlock(rht_bucket_lock(tbl, hash)); |
| 337 | |
| 338 | return err; |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(rhashtable_insert_slow); |
| 341 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 342 | static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj, |
| 343 | bool (*compare)(void *, void *), void *arg) |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 344 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 345 | struct bucket_table *tbl, *old_tbl; |
Thomas Graf | 020219a | 2015-02-06 16:08:43 +0000 | [diff] [blame] | 346 | struct rhash_head *head; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 347 | bool no_resize_running; |
| 348 | unsigned hash; |
Thomas Graf | 617011e | 2015-03-16 10:42:26 +0100 | [diff] [blame] | 349 | spinlock_t *old_lock; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 350 | bool success = true; |
| 351 | |
| 352 | rcu_read_lock(); |
| 353 | |
| 354 | old_tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | eca8493 | 2015-03-12 14:49:39 +1100 | [diff] [blame] | 355 | hash = head_hashfn(ht, old_tbl, obj); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame^] | 356 | old_lock = rht_bucket_lock(old_tbl, hash); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 357 | |
Thomas Graf | 617011e | 2015-03-16 10:42:26 +0100 | [diff] [blame] | 358 | spin_lock_bh(old_lock); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 359 | |
| 360 | /* Because we have already taken the bucket lock in old_tbl, |
| 361 | * if we find that future_tbl is not yet visible then that |
| 362 | * guarantees all other insertions of the same entry will |
| 363 | * also grab the bucket lock in old_tbl because until the |
| 364 | * rehash completes ht->tbl won't be changed. |
| 365 | */ |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 366 | tbl = rht_dereference_rcu(old_tbl->future_tbl, ht) ?: old_tbl; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 367 | if (tbl != old_tbl) { |
Herbert Xu | eca8493 | 2015-03-12 14:49:39 +1100 | [diff] [blame] | 368 | hash = head_hashfn(ht, tbl, obj); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame^] | 369 | spin_lock_nested(rht_bucket_lock(tbl, hash), |
| 370 | SINGLE_DEPTH_NESTING); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (compare && |
| 374 | rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset, |
| 375 | compare, arg)) { |
| 376 | success = false; |
| 377 | goto exit; |
| 378 | } |
| 379 | |
| 380 | no_resize_running = tbl == old_tbl; |
Thomas Graf | 020219a | 2015-02-06 16:08:43 +0000 | [diff] [blame] | 381 | |
Thomas Graf | 020219a | 2015-02-06 16:08:43 +0000 | [diff] [blame] | 382 | head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 383 | |
| 384 | if (rht_is_a_nulls(head)) |
| 385 | INIT_RHT_NULLS_HEAD(obj->next, ht, hash); |
| 386 | else |
| 387 | RCU_INIT_POINTER(obj->next, head); |
| 388 | |
| 389 | rcu_assign_pointer(tbl->buckets[hash], obj); |
| 390 | |
| 391 | atomic_inc(&ht->nelems); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 392 | if (no_resize_running && rht_grow_above_75(ht, tbl)) |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 393 | schedule_work(&ht->run_work); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 394 | |
| 395 | exit: |
Thomas Graf | 617011e | 2015-03-16 10:42:26 +0100 | [diff] [blame] | 396 | if (tbl != old_tbl) |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame^] | 397 | spin_unlock(rht_bucket_lock(tbl, hash)); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 398 | |
Thomas Graf | 617011e | 2015-03-16 10:42:26 +0100 | [diff] [blame] | 399 | spin_unlock_bh(old_lock); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 400 | |
| 401 | rcu_read_unlock(); |
| 402 | |
| 403 | return success; |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 404 | } |
| 405 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 406 | /** |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 407 | * rhashtable_insert - insert object into hash table |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 408 | * @ht: hash table |
| 409 | * @obj: pointer to hash head inside object |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 410 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 411 | * Will take a per bucket spinlock to protect against mutual mutations |
| 412 | * on the same bucket. Multiple insertions may occur in parallel unless |
| 413 | * they map to the same bucket lock. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 414 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 415 | * It is safe to call this function from atomic context. |
| 416 | * |
| 417 | * Will trigger an automatic deferred table resizing if the size grows |
| 418 | * beyond the watermark indicated by grow_decision() which can be passed |
| 419 | * to rhashtable_init(). |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 420 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 421 | void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 422 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 423 | __rhashtable_insert(ht, obj, NULL, NULL); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 424 | } |
| 425 | EXPORT_SYMBOL_GPL(rhashtable_insert); |
| 426 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 427 | static bool __rhashtable_remove(struct rhashtable *ht, |
| 428 | struct bucket_table *tbl, |
| 429 | struct rhash_head *obj) |
| 430 | { |
| 431 | struct rhash_head __rcu **pprev; |
| 432 | struct rhash_head *he; |
| 433 | spinlock_t * lock; |
| 434 | unsigned hash; |
| 435 | bool ret = false; |
| 436 | |
Herbert Xu | eca8493 | 2015-03-12 14:49:39 +1100 | [diff] [blame] | 437 | hash = head_hashfn(ht, tbl, obj); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame^] | 438 | lock = rht_bucket_lock(tbl, hash); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 439 | |
| 440 | spin_lock_bh(lock); |
| 441 | |
| 442 | pprev = &tbl->buckets[hash]; |
| 443 | rht_for_each(he, tbl, hash) { |
| 444 | if (he != obj) { |
| 445 | pprev = &he->next; |
| 446 | continue; |
| 447 | } |
| 448 | |
| 449 | rcu_assign_pointer(*pprev, obj->next); |
| 450 | ret = true; |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | spin_unlock_bh(lock); |
| 455 | |
| 456 | return ret; |
| 457 | } |
| 458 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 459 | /** |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 460 | * rhashtable_remove - remove object from hash table |
| 461 | * @ht: hash table |
| 462 | * @obj: pointer to hash head inside object |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 463 | * |
| 464 | * Since the hash chain is single linked, the removal operation needs to |
| 465 | * walk the bucket chain upon removal. The removal operation is thus |
| 466 | * considerable slow if the hash table is not correctly sized. |
| 467 | * |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 468 | * Will automatically shrink the table via rhashtable_expand() if the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 469 | * shrink_decision function specified at rhashtable_init() returns true. |
| 470 | * |
| 471 | * The caller must ensure that no concurrent table mutations occur. It is |
| 472 | * however valid to have concurrent lookups if they are RCU protected. |
| 473 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 474 | bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 475 | { |
Herbert Xu | 565e864 | 2015-03-15 21:12:05 +1100 | [diff] [blame] | 476 | struct bucket_table *tbl; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 477 | bool ret; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 478 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 479 | rcu_read_lock(); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 480 | |
Herbert Xu | 565e864 | 2015-03-15 21:12:05 +1100 | [diff] [blame] | 481 | tbl = rht_dereference_rcu(ht->tbl, ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 482 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 483 | /* Because we have already taken (and released) the bucket |
| 484 | * lock in old_tbl, if we find that future_tbl is not yet |
| 485 | * visible then that guarantees the entry to still be in |
Herbert Xu | 565e864 | 2015-03-15 21:12:05 +1100 | [diff] [blame] | 486 | * the old tbl if it exists. |
Thomas Graf | fe6a043 | 2015-01-21 11:54:01 +0000 | [diff] [blame] | 487 | */ |
Herbert Xu | 565e864 | 2015-03-15 21:12:05 +1100 | [diff] [blame] | 488 | while (!(ret = __rhashtable_remove(ht, tbl, obj)) && |
| 489 | (tbl = rht_dereference_rcu(tbl->future_tbl, ht))) |
| 490 | ; |
Thomas Graf | fe6a043 | 2015-01-21 11:54:01 +0000 | [diff] [blame] | 491 | |
| 492 | if (ret) { |
| 493 | atomic_dec(&ht->nelems); |
Herbert Xu | 565e864 | 2015-03-15 21:12:05 +1100 | [diff] [blame] | 494 | if (rht_shrink_below_30(ht, tbl)) |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 495 | schedule_work(&ht->run_work); |
Thomas Graf | fe6a043 | 2015-01-21 11:54:01 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 498 | rcu_read_unlock(); |
| 499 | |
Thomas Graf | fe6a043 | 2015-01-21 11:54:01 +0000 | [diff] [blame] | 500 | return ret; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 501 | } |
| 502 | EXPORT_SYMBOL_GPL(rhashtable_remove); |
| 503 | |
| 504 | /** |
| 505 | * rhashtable_lookup - lookup key in hash table |
| 506 | * @ht: hash table |
| 507 | * @key: pointer to key |
| 508 | * |
| 509 | * Computes the hash value for the key and traverses the bucket chain looking |
| 510 | * for a entry with an identical key. The first matching entry is returned. |
| 511 | * |
| 512 | * 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] | 513 | * parameter set). It will BUG() if used inappropriately. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 514 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 515 | * Lookups may occur in parallel with hashtable mutations and resizing. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 516 | */ |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 517 | void *rhashtable_lookup(struct rhashtable *ht, const void *key) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 518 | { |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame^] | 519 | return rhashtable_lookup_fast(ht, key, ht->p); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 520 | } |
| 521 | EXPORT_SYMBOL_GPL(rhashtable_lookup); |
| 522 | |
| 523 | /** |
| 524 | * rhashtable_lookup_compare - search hash table with compare function |
| 525 | * @ht: hash table |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 526 | * @key: the pointer to the key |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 527 | * @compare: compare function, must return true on match |
| 528 | * @arg: argument passed on to compare function |
| 529 | * |
| 530 | * Traverses the bucket chain behind the provided hash value and calls the |
| 531 | * specified compare function for each entry. |
| 532 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 533 | * Lookups may occur in parallel with hashtable mutations and resizing. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 534 | * |
| 535 | * Returns the first entry on which the compare function returned true. |
| 536 | */ |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 537 | void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key, |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame^] | 538 | bool (*compare)(void *, void *), |
| 539 | void *arg) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 540 | { |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 541 | const struct bucket_table *tbl; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 542 | struct rhash_head *he; |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 543 | u32 hash; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 544 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 545 | rcu_read_lock(); |
| 546 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 547 | tbl = rht_dereference_rcu(ht->tbl, ht); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 548 | restart: |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame^] | 549 | hash = rht_key_hashfn(ht, tbl, key, ht->p); |
Herbert Xu | 8d2b187 | 2015-03-12 14:49:38 +1100 | [diff] [blame] | 550 | rht_for_each_rcu(he, tbl, hash) { |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 551 | if (!compare(rht_obj(ht, he), arg)) |
| 552 | continue; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 553 | rcu_read_unlock(); |
Thomas Graf | a4b18cd | 2015-01-02 23:00:15 +0100 | [diff] [blame] | 554 | return rht_obj(ht, he); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Herbert Xu | 9497df8 | 2015-03-12 22:07:49 +1100 | [diff] [blame] | 557 | /* Ensure we see any new tables. */ |
| 558 | smp_rmb(); |
| 559 | |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 560 | tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 561 | if (unlikely(tbl)) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 562 | goto restart; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 563 | rcu_read_unlock(); |
| 564 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 565 | return NULL; |
| 566 | } |
| 567 | EXPORT_SYMBOL_GPL(rhashtable_lookup_compare); |
| 568 | |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 569 | /** |
| 570 | * rhashtable_lookup_insert - lookup and insert object into hash table |
| 571 | * @ht: hash table |
| 572 | * @obj: pointer to hash head inside object |
| 573 | * |
| 574 | * Locks down the bucket chain in both the old and new table if a resize |
| 575 | * is in progress to ensure that writers can't remove from the old table |
| 576 | * and can't insert to the new table during the atomic operation of search |
| 577 | * and insertion. Searches for duplicates in both the old and new table if |
| 578 | * a resize is in progress. |
| 579 | * |
| 580 | * This lookup function may only be used for fixed key hash table (key_len |
| 581 | * parameter set). It will BUG() if used inappropriately. |
| 582 | * |
| 583 | * It is safe to call this function from atomic context. |
| 584 | * |
| 585 | * Will trigger an automatic deferred table resizing if the size grows |
| 586 | * beyond the watermark indicated by grow_decision() which can be passed |
| 587 | * to rhashtable_init(). |
| 588 | */ |
| 589 | bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj) |
| 590 | { |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame^] | 591 | return rhashtable_lookup_insert_fast(ht, obj, ht->p); |
Ying Xue | 7a868d1 | 2015-01-12 14:52:22 +0800 | [diff] [blame] | 592 | } |
| 593 | EXPORT_SYMBOL_GPL(rhashtable_lookup_insert); |
| 594 | |
| 595 | /** |
| 596 | * rhashtable_lookup_compare_insert - search and insert object to hash table |
| 597 | * with compare function |
| 598 | * @ht: hash table |
| 599 | * @obj: pointer to hash head inside object |
| 600 | * @compare: compare function, must return true on match |
| 601 | * @arg: argument passed on to compare function |
| 602 | * |
| 603 | * Locks down the bucket chain in both the old and new table if a resize |
| 604 | * is in progress to ensure that writers can't remove from the old table |
| 605 | * and can't insert to the new table during the atomic operation of search |
| 606 | * and insertion. Searches for duplicates in both the old and new table if |
| 607 | * a resize is in progress. |
| 608 | * |
| 609 | * Lookups may occur in parallel with hashtable mutations and resizing. |
| 610 | * |
| 611 | * Will trigger an automatic deferred table resizing if the size grows |
| 612 | * beyond the watermark indicated by grow_decision() which can be passed |
| 613 | * to rhashtable_init(). |
| 614 | */ |
| 615 | bool rhashtable_lookup_compare_insert(struct rhashtable *ht, |
| 616 | struct rhash_head *obj, |
| 617 | bool (*compare)(void *, void *), |
| 618 | void *arg) |
| 619 | { |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 620 | BUG_ON(!ht->p.key_len); |
| 621 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 622 | return __rhashtable_insert(ht, obj, compare, arg); |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 623 | } |
Ying Xue | 7a868d1 | 2015-01-12 14:52:22 +0800 | [diff] [blame] | 624 | EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert); |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 625 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 626 | /** |
| 627 | * rhashtable_walk_init - Initialise an iterator |
| 628 | * @ht: Table to walk over |
| 629 | * @iter: Hash table Iterator |
| 630 | * |
| 631 | * This function prepares a hash table walk. |
| 632 | * |
| 633 | * Note that if you restart a walk after rhashtable_walk_stop you |
| 634 | * may see the same object twice. Also, you may miss objects if |
| 635 | * there are removals in between rhashtable_walk_stop and the next |
| 636 | * call to rhashtable_walk_start. |
| 637 | * |
| 638 | * For a completely stable walk you should construct your own data |
| 639 | * structure outside the hash table. |
| 640 | * |
| 641 | * This function may sleep so you must not call it from interrupt |
| 642 | * context or with spin locks held. |
| 643 | * |
| 644 | * You must call rhashtable_walk_exit if this function returns |
| 645 | * successfully. |
| 646 | */ |
| 647 | int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter) |
| 648 | { |
| 649 | iter->ht = ht; |
| 650 | iter->p = NULL; |
| 651 | iter->slot = 0; |
| 652 | iter->skip = 0; |
| 653 | |
| 654 | iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL); |
| 655 | if (!iter->walker) |
| 656 | return -ENOMEM; |
| 657 | |
| 658 | mutex_lock(&ht->mutex); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 659 | iter->walker->tbl = rht_dereference(ht->tbl, ht); |
| 660 | list_add(&iter->walker->list, &iter->walker->tbl->walkers); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 661 | mutex_unlock(&ht->mutex); |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | EXPORT_SYMBOL_GPL(rhashtable_walk_init); |
| 666 | |
| 667 | /** |
| 668 | * rhashtable_walk_exit - Free an iterator |
| 669 | * @iter: Hash table Iterator |
| 670 | * |
| 671 | * This function frees resources allocated by rhashtable_walk_init. |
| 672 | */ |
| 673 | void rhashtable_walk_exit(struct rhashtable_iter *iter) |
| 674 | { |
| 675 | mutex_lock(&iter->ht->mutex); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 676 | if (iter->walker->tbl) |
| 677 | list_del(&iter->walker->list); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 678 | mutex_unlock(&iter->ht->mutex); |
| 679 | kfree(iter->walker); |
| 680 | } |
| 681 | EXPORT_SYMBOL_GPL(rhashtable_walk_exit); |
| 682 | |
| 683 | /** |
| 684 | * rhashtable_walk_start - Start a hash table walk |
| 685 | * @iter: Hash table iterator |
| 686 | * |
| 687 | * Start a hash table walk. Note that we take the RCU lock in all |
| 688 | * cases including when we return an error. So you must always call |
| 689 | * rhashtable_walk_stop to clean up. |
| 690 | * |
| 691 | * Returns zero if successful. |
| 692 | * |
| 693 | * Returns -EAGAIN if resize event occured. Note that the iterator |
| 694 | * will rewind back to the beginning and you may use it immediately |
| 695 | * by calling rhashtable_walk_next. |
| 696 | */ |
| 697 | int rhashtable_walk_start(struct rhashtable_iter *iter) |
Thomas Graf | db4374f | 2015-03-16 10:42:27 +0100 | [diff] [blame] | 698 | __acquires(RCU) |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 699 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 700 | struct rhashtable *ht = iter->ht; |
| 701 | |
| 702 | mutex_lock(&ht->mutex); |
| 703 | |
| 704 | if (iter->walker->tbl) |
| 705 | list_del(&iter->walker->list); |
| 706 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 707 | rcu_read_lock(); |
| 708 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 709 | mutex_unlock(&ht->mutex); |
| 710 | |
| 711 | if (!iter->walker->tbl) { |
| 712 | iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 713 | return -EAGAIN; |
| 714 | } |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | EXPORT_SYMBOL_GPL(rhashtable_walk_start); |
| 719 | |
| 720 | /** |
| 721 | * rhashtable_walk_next - Return the next object and advance the iterator |
| 722 | * @iter: Hash table iterator |
| 723 | * |
| 724 | * Note that you must call rhashtable_walk_stop when you are finished |
| 725 | * with the walk. |
| 726 | * |
| 727 | * Returns the next object or NULL when the end of the table is reached. |
| 728 | * |
| 729 | * Returns -EAGAIN if resize event occured. Note that the iterator |
| 730 | * will rewind back to the beginning and you may continue to use it. |
| 731 | */ |
| 732 | void *rhashtable_walk_next(struct rhashtable_iter *iter) |
| 733 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 734 | struct bucket_table *tbl = iter->walker->tbl; |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 735 | struct rhashtable *ht = iter->ht; |
| 736 | struct rhash_head *p = iter->p; |
| 737 | void *obj = NULL; |
| 738 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 739 | if (p) { |
| 740 | p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot); |
| 741 | goto next; |
| 742 | } |
| 743 | |
| 744 | for (; iter->slot < tbl->size; iter->slot++) { |
| 745 | int skip = iter->skip; |
| 746 | |
| 747 | rht_for_each_rcu(p, tbl, iter->slot) { |
| 748 | if (!skip) |
| 749 | break; |
| 750 | skip--; |
| 751 | } |
| 752 | |
| 753 | next: |
| 754 | if (!rht_is_a_nulls(p)) { |
| 755 | iter->skip++; |
| 756 | iter->p = p; |
| 757 | obj = rht_obj(ht, p); |
| 758 | goto out; |
| 759 | } |
| 760 | |
| 761 | iter->skip = 0; |
| 762 | } |
| 763 | |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 764 | iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 765 | if (iter->walker->tbl) { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 766 | iter->slot = 0; |
| 767 | iter->skip = 0; |
| 768 | return ERR_PTR(-EAGAIN); |
| 769 | } |
| 770 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 771 | iter->p = NULL; |
| 772 | |
| 773 | out: |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 774 | |
| 775 | return obj; |
| 776 | } |
| 777 | EXPORT_SYMBOL_GPL(rhashtable_walk_next); |
| 778 | |
| 779 | /** |
| 780 | * rhashtable_walk_stop - Finish a hash table walk |
| 781 | * @iter: Hash table iterator |
| 782 | * |
| 783 | * Finish a hash table walk. |
| 784 | */ |
| 785 | void rhashtable_walk_stop(struct rhashtable_iter *iter) |
Thomas Graf | db4374f | 2015-03-16 10:42:27 +0100 | [diff] [blame] | 786 | __releases(RCU) |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 787 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 788 | struct rhashtable *ht; |
| 789 | struct bucket_table *tbl = iter->walker->tbl; |
| 790 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 791 | if (!tbl) |
Herbert Xu | 963ecbd | 2015-03-15 21:12:04 +1100 | [diff] [blame] | 792 | goto out; |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 793 | |
| 794 | ht = iter->ht; |
| 795 | |
| 796 | mutex_lock(&ht->mutex); |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 797 | if (tbl->rehash < tbl->size) |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 798 | list_add(&iter->walker->list, &tbl->walkers); |
| 799 | else |
| 800 | iter->walker->tbl = NULL; |
| 801 | mutex_unlock(&ht->mutex); |
| 802 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 803 | iter->p = NULL; |
Herbert Xu | 963ecbd | 2015-03-15 21:12:04 +1100 | [diff] [blame] | 804 | |
| 805 | out: |
| 806 | rcu_read_unlock(); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 807 | } |
| 808 | EXPORT_SYMBOL_GPL(rhashtable_walk_stop); |
| 809 | |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 810 | static size_t rounded_hashtable_size(const struct rhashtable_params *params) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 811 | { |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 812 | return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), |
Herbert Xu | e2e21c1 | 2015-03-18 20:01:21 +1100 | [diff] [blame] | 813 | (unsigned long)params->min_size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /** |
| 817 | * rhashtable_init - initialize a new hash table |
| 818 | * @ht: hash table to be initialized |
| 819 | * @params: configuration parameters |
| 820 | * |
| 821 | * Initializes a new hash table based on the provided configuration |
| 822 | * parameters. A table can be configured either with a variable or |
| 823 | * fixed length key: |
| 824 | * |
| 825 | * Configuration Example 1: Fixed length keys |
| 826 | * struct test_obj { |
| 827 | * int key; |
| 828 | * void * my_member; |
| 829 | * struct rhash_head node; |
| 830 | * }; |
| 831 | * |
| 832 | * struct rhashtable_params params = { |
| 833 | * .head_offset = offsetof(struct test_obj, node), |
| 834 | * .key_offset = offsetof(struct test_obj, key), |
| 835 | * .key_len = sizeof(int), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 836 | * .hashfn = jhash, |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 837 | * .nulls_base = (1U << RHT_BASE_SHIFT), |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 838 | * }; |
| 839 | * |
| 840 | * Configuration Example 2: Variable length keys |
| 841 | * struct test_obj { |
| 842 | * [...] |
| 843 | * struct rhash_head node; |
| 844 | * }; |
| 845 | * |
| 846 | * u32 my_hash_fn(const void *data, u32 seed) |
| 847 | * { |
| 848 | * struct test_obj *obj = data; |
| 849 | * |
| 850 | * return [... hash ...]; |
| 851 | * } |
| 852 | * |
| 853 | * struct rhashtable_params params = { |
| 854 | * .head_offset = offsetof(struct test_obj, node), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 855 | * .hashfn = jhash, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 856 | * .obj_hashfn = my_hash_fn, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 857 | * }; |
| 858 | */ |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 859 | int rhashtable_init(struct rhashtable *ht, |
| 860 | const struct rhashtable_params *params) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 861 | { |
| 862 | struct bucket_table *tbl; |
| 863 | size_t size; |
| 864 | |
| 865 | size = HASH_DEFAULT_SIZE; |
| 866 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame^] | 867 | if ((!(params->key_len && params->hashfn) && !params->obj_hashfn) || |
| 868 | (params->obj_hashfn && !params->obj_cmpfn)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 869 | return -EINVAL; |
| 870 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 871 | if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) |
| 872 | return -EINVAL; |
| 873 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 874 | if (params->nelem_hint) |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 875 | size = rounded_hashtable_size(params); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 876 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 877 | memset(ht, 0, sizeof(*ht)); |
| 878 | mutex_init(&ht->mutex); |
| 879 | memcpy(&ht->p, params, sizeof(*params)); |
| 880 | |
Thomas Graf | a998f71 | 2015-03-19 22:31:13 +0000 | [diff] [blame] | 881 | if (params->min_size) |
| 882 | ht->p.min_size = roundup_pow_of_two(params->min_size); |
| 883 | |
| 884 | if (params->max_size) |
| 885 | ht->p.max_size = rounddown_pow_of_two(params->max_size); |
| 886 | |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 887 | ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE); |
Thomas Graf | a998f71 | 2015-03-19 22:31:13 +0000 | [diff] [blame] | 888 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 889 | if (params->locks_mul) |
| 890 | ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); |
| 891 | else |
| 892 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; |
| 893 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame] | 894 | tbl = bucket_table_alloc(ht, size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 895 | if (tbl == NULL) |
| 896 | return -ENOMEM; |
| 897 | |
Ying Xue | 545a148 | 2015-01-07 13:41:57 +0800 | [diff] [blame] | 898 | atomic_set(&ht->nelems, 0); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 899 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 900 | RCU_INIT_POINTER(ht->tbl, tbl); |
| 901 | |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 902 | INIT_WORK(&ht->run_work, rht_deferred_worker); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 903 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 904 | return 0; |
| 905 | } |
| 906 | EXPORT_SYMBOL_GPL(rhashtable_init); |
| 907 | |
| 908 | /** |
| 909 | * rhashtable_destroy - destroy hash table |
| 910 | * @ht: the hash table to destroy |
| 911 | * |
Pablo Neira Ayuso | ae82ddc | 2014-09-02 00:26:05 +0200 | [diff] [blame] | 912 | * Frees the bucket array. This function is not rcu safe, therefore the caller |
| 913 | * has to make sure that no resizing may happen by unpublishing the hashtable |
| 914 | * and waiting for the quiescent cycle before releasing the bucket array. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 915 | */ |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 916 | void rhashtable_destroy(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 917 | { |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 918 | ht->being_destroyed = true; |
| 919 | |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 920 | cancel_work_sync(&ht->run_work); |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 921 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 922 | mutex_lock(&ht->mutex); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 923 | bucket_table_free(rht_dereference(ht->tbl, ht)); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 924 | mutex_unlock(&ht->mutex); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 925 | } |
| 926 | EXPORT_SYMBOL_GPL(rhashtable_destroy); |