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