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