Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Resizable, Scalable, Concurrent Hash Table |
| 3 | * |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 4 | * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au> |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 5 | * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 6 | * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
| 7 | * |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 8 | * Code partially derived from nft_hash |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 9 | * Rewritten with rehash code from br_multicast plus single list |
| 10 | * pointer as suggested by Josh Triplett |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | */ |
| 16 | |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 17 | #include <linux/atomic.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/log2.h> |
Eric Dumazet | 5beb5c9 | 2015-02-26 07:20:34 -0800 | [diff] [blame] | 21 | #include <linux/sched.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/vmalloc.h> |
| 24 | #include <linux/mm.h> |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 25 | #include <linux/jhash.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 26 | #include <linux/random.h> |
| 27 | #include <linux/rhashtable.h> |
Stephen Rothwell | 61d7b09 | 2015-02-09 14:04:03 +1100 | [diff] [blame] | 28 | #include <linux/err.h> |
Hauke Mehrtens | 6d79541 | 2015-06-06 22:07:23 +0200 | [diff] [blame] | 29 | #include <linux/export.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 30 | |
| 31 | #define HASH_DEFAULT_SIZE 64UL |
Herbert Xu | c2e213c | 2015-03-18 20:01:16 +1100 | [diff] [blame] | 32 | #define HASH_MIN_SIZE 4U |
Florian Westphal | 4cf0b35 | 2016-08-12 12:03:52 +0200 | [diff] [blame] | 33 | #define BUCKET_LOCKS_PER_CPU 32UL |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 34 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 35 | union nested_table { |
| 36 | union nested_table __rcu *table; |
| 37 | struct rhash_head __rcu *bucket; |
| 38 | }; |
| 39 | |
Herbert Xu | 988dfbd | 2015-03-10 09:27:55 +1100 | [diff] [blame] | 40 | static u32 head_hashfn(struct rhashtable *ht, |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 41 | const struct bucket_table *tbl, |
| 42 | const struct rhash_head *he) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 43 | { |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 44 | return rht_head_hashfn(ht, tbl, he, ht->p); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 47 | #ifdef CONFIG_PROVE_LOCKING |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 48 | #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] | 49 | |
| 50 | int lockdep_rht_mutex_is_held(struct rhashtable *ht) |
| 51 | { |
| 52 | return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1; |
| 53 | } |
| 54 | EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); |
| 55 | |
| 56 | int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) |
| 57 | { |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 58 | spinlock_t *lock = rht_bucket_lock(tbl, hash); |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 59 | |
| 60 | return (debug_locks) ? lockdep_is_held(lock) : 1; |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); |
| 63 | #else |
| 64 | #define ASSERT_RHT_MUTEX(HT) |
Thomas Graf | a03eaec | 2015-02-05 02:03:34 +0100 | [diff] [blame] | 65 | #endif |
| 66 | |
| 67 | |
Herbert Xu | b9ecfda | 2015-03-24 00:50:27 +1100 | [diff] [blame] | 68 | static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl, |
| 69 | gfp_t gfp) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 70 | { |
| 71 | unsigned int i, size; |
| 72 | #if defined(CONFIG_PROVE_LOCKING) |
| 73 | unsigned int nr_pcpus = 2; |
| 74 | #else |
| 75 | unsigned int nr_pcpus = num_possible_cpus(); |
| 76 | #endif |
| 77 | |
Florian Westphal | 4cf0b35 | 2016-08-12 12:03:52 +0200 | [diff] [blame] | 78 | nr_pcpus = min_t(unsigned int, nr_pcpus, 64UL); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 79 | size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul); |
| 80 | |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 81 | /* Never allocate more than 0.5 locks per bucket */ |
| 82 | size = min_t(unsigned int, size, tbl->size >> 1); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 83 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 84 | if (tbl->nest) |
| 85 | size = min(size, 1U << tbl->nest); |
| 86 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 87 | if (sizeof(spinlock_t) != 0) { |
Eric Dumazet | 9dbeea7 | 2016-08-26 08:51:39 -0700 | [diff] [blame] | 88 | tbl->locks = NULL; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 89 | #ifdef CONFIG_NUMA |
Herbert Xu | b9ecfda | 2015-03-24 00:50:27 +1100 | [diff] [blame] | 90 | if (size * sizeof(spinlock_t) > PAGE_SIZE && |
| 91 | gfp == GFP_KERNEL) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 92 | tbl->locks = vmalloc(size * sizeof(spinlock_t)); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 93 | #endif |
Florian Westphal | 4cf0b35 | 2016-08-12 12:03:52 +0200 | [diff] [blame] | 94 | if (gfp != GFP_KERNEL) |
| 95 | gfp |= __GFP_NOWARN | __GFP_NORETRY; |
| 96 | |
Eric Dumazet | 9dbeea7 | 2016-08-26 08:51:39 -0700 | [diff] [blame] | 97 | if (!tbl->locks) |
| 98 | tbl->locks = kmalloc_array(size, sizeof(spinlock_t), |
| 99 | gfp); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 100 | if (!tbl->locks) |
| 101 | return -ENOMEM; |
| 102 | for (i = 0; i < size; i++) |
| 103 | spin_lock_init(&tbl->locks[i]); |
| 104 | } |
| 105 | tbl->locks_mask = size - 1; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 110 | static void nested_table_free(union nested_table *ntbl, unsigned int size) |
| 111 | { |
| 112 | const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); |
| 113 | const unsigned int len = 1 << shift; |
| 114 | unsigned int i; |
| 115 | |
| 116 | ntbl = rcu_dereference_raw(ntbl->table); |
| 117 | if (!ntbl) |
| 118 | return; |
| 119 | |
| 120 | if (size > len) { |
| 121 | size >>= shift; |
| 122 | for (i = 0; i < len; i++) |
| 123 | nested_table_free(ntbl + i, size); |
| 124 | } |
| 125 | |
| 126 | kfree(ntbl); |
| 127 | } |
| 128 | |
| 129 | static void nested_bucket_table_free(const struct bucket_table *tbl) |
| 130 | { |
| 131 | unsigned int size = tbl->size >> tbl->nest; |
| 132 | unsigned int len = 1 << tbl->nest; |
| 133 | union nested_table *ntbl; |
| 134 | unsigned int i; |
| 135 | |
| 136 | ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]); |
| 137 | |
| 138 | for (i = 0; i < len; i++) |
| 139 | nested_table_free(ntbl + i, size); |
| 140 | |
| 141 | kfree(ntbl); |
| 142 | } |
| 143 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 144 | static void bucket_table_free(const struct bucket_table *tbl) |
| 145 | { |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 146 | if (tbl->nest) |
| 147 | nested_bucket_table_free(tbl); |
| 148 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 149 | if (tbl) |
| 150 | kvfree(tbl->locks); |
| 151 | |
| 152 | kvfree(tbl); |
| 153 | } |
| 154 | |
Herbert Xu | 9d901bc | 2015-03-14 13:57:23 +1100 | [diff] [blame] | 155 | static void bucket_table_free_rcu(struct rcu_head *head) |
| 156 | { |
| 157 | bucket_table_free(container_of(head, struct bucket_table, rcu)); |
| 158 | } |
| 159 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 160 | static union nested_table *nested_table_alloc(struct rhashtable *ht, |
| 161 | union nested_table __rcu **prev, |
| 162 | unsigned int shifted, |
| 163 | unsigned int nhash) |
| 164 | { |
| 165 | union nested_table *ntbl; |
| 166 | int i; |
| 167 | |
| 168 | ntbl = rcu_dereference(*prev); |
| 169 | if (ntbl) |
| 170 | return ntbl; |
| 171 | |
| 172 | ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC); |
| 173 | |
| 174 | if (ntbl && shifted) { |
| 175 | for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0].bucket); i++) |
| 176 | INIT_RHT_NULLS_HEAD(ntbl[i].bucket, ht, |
| 177 | (i << shifted) | nhash); |
| 178 | } |
| 179 | |
| 180 | rcu_assign_pointer(*prev, ntbl); |
| 181 | |
| 182 | return ntbl; |
| 183 | } |
| 184 | |
| 185 | static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht, |
| 186 | size_t nbuckets, |
| 187 | gfp_t gfp) |
| 188 | { |
| 189 | const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); |
| 190 | struct bucket_table *tbl; |
| 191 | size_t size; |
| 192 | |
| 193 | if (nbuckets < (1 << (shift + 1))) |
| 194 | return NULL; |
| 195 | |
| 196 | size = sizeof(*tbl) + sizeof(tbl->buckets[0]); |
| 197 | |
| 198 | tbl = kzalloc(size, gfp); |
| 199 | if (!tbl) |
| 200 | return NULL; |
| 201 | |
| 202 | if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets, |
| 203 | 0, 0)) { |
| 204 | kfree(tbl); |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | tbl->nest = (ilog2(nbuckets) - 1) % shift + 1; |
| 209 | |
| 210 | return tbl; |
| 211 | } |
| 212 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 213 | static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, |
Herbert Xu | b9ecfda | 2015-03-24 00:50:27 +1100 | [diff] [blame] | 214 | size_t nbuckets, |
| 215 | gfp_t gfp) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 216 | { |
Daniel Borkmann | eb6d1ab | 2015-02-20 00:53:38 +0100 | [diff] [blame] | 217 | struct bucket_table *tbl = NULL; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 218 | size_t size; |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 219 | int i; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 220 | |
| 221 | size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); |
Herbert Xu | b9ecfda | 2015-03-24 00:50:27 +1100 | [diff] [blame] | 222 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER) || |
| 223 | gfp != GFP_KERNEL) |
| 224 | tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY); |
| 225 | if (tbl == NULL && gfp == GFP_KERNEL) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 226 | tbl = vzalloc(size); |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 227 | |
| 228 | size = nbuckets; |
| 229 | |
| 230 | if (tbl == NULL && gfp != GFP_KERNEL) { |
| 231 | tbl = nested_bucket_table_alloc(ht, nbuckets, gfp); |
| 232 | nbuckets = 0; |
| 233 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 234 | if (tbl == NULL) |
| 235 | return NULL; |
| 236 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 237 | tbl->size = size; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 238 | |
Herbert Xu | b9ecfda | 2015-03-24 00:50:27 +1100 | [diff] [blame] | 239 | if (alloc_bucket_locks(ht, tbl, gfp) < 0) { |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 240 | bucket_table_free(tbl); |
| 241 | return NULL; |
| 242 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 243 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 244 | INIT_LIST_HEAD(&tbl->walkers); |
| 245 | |
Herbert Xu | 5269b53 | 2015-03-14 13:57:22 +1100 | [diff] [blame] | 246 | get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); |
| 247 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 248 | for (i = 0; i < nbuckets; i++) |
| 249 | INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i); |
| 250 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 251 | return tbl; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 254 | static struct bucket_table *rhashtable_last_table(struct rhashtable *ht, |
| 255 | struct bucket_table *tbl) |
| 256 | { |
| 257 | struct bucket_table *new_tbl; |
| 258 | |
| 259 | do { |
| 260 | new_tbl = tbl; |
| 261 | tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 262 | } while (tbl); |
| 263 | |
| 264 | return new_tbl; |
| 265 | } |
| 266 | |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 267 | static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash) |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 268 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 269 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 270 | struct bucket_table *new_tbl = rhashtable_last_table(ht, |
| 271 | rht_dereference_rcu(old_tbl->future_tbl, ht)); |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 272 | struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash); |
| 273 | int err = -EAGAIN; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 274 | struct rhash_head *head, *next, *entry; |
| 275 | spinlock_t *new_bucket_lock; |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 276 | unsigned int new_hash; |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 277 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 278 | if (new_tbl->nest) |
| 279 | goto out; |
| 280 | |
| 281 | err = -ENOENT; |
| 282 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 283 | rht_for_each(entry, old_tbl, old_hash) { |
| 284 | err = 0; |
| 285 | next = rht_dereference_bucket(entry->next, old_tbl, old_hash); |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 286 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 287 | if (rht_is_a_nulls(next)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 288 | break; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 289 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 290 | pprev = &entry->next; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 293 | if (err) |
| 294 | goto out; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 295 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 296 | new_hash = head_hashfn(ht, new_tbl, entry); |
Thomas Graf | a5ec68e | 2015-02-05 02:03:32 +0100 | [diff] [blame] | 297 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 298 | new_bucket_lock = rht_bucket_lock(new_tbl, new_hash); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 299 | |
Herbert Xu | 8f2484b | 2015-03-14 13:57:21 +1100 | [diff] [blame] | 300 | spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 301 | head = rht_dereference_bucket(new_tbl->buckets[new_hash], |
| 302 | new_tbl, new_hash); |
| 303 | |
Dmitriy Vyukov | 7def0f9 | 2015-09-22 10:51:52 +0200 | [diff] [blame] | 304 | RCU_INIT_POINTER(entry->next, head); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 305 | |
| 306 | rcu_assign_pointer(new_tbl->buckets[new_hash], entry); |
| 307 | spin_unlock(new_bucket_lock); |
| 308 | |
| 309 | rcu_assign_pointer(*pprev, next); |
| 310 | |
| 311 | out: |
| 312 | return err; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 315 | static int rhashtable_rehash_chain(struct rhashtable *ht, |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 316 | unsigned int old_hash) |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 317 | { |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 318 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
| 319 | spinlock_t *old_bucket_lock; |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 320 | int err; |
Thomas Graf | 7cd10db | 2015-02-05 02:03:35 +0100 | [diff] [blame] | 321 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 322 | old_bucket_lock = rht_bucket_lock(old_tbl, old_hash); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 323 | |
| 324 | spin_lock_bh(old_bucket_lock); |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 325 | while (!(err = rhashtable_rehash_one(ht, old_hash))) |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 326 | ; |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 327 | |
| 328 | if (err == -ENOENT) { |
| 329 | old_tbl->rehash++; |
| 330 | err = 0; |
| 331 | } |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 332 | spin_unlock_bh(old_bucket_lock); |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 333 | |
| 334 | return err; |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 335 | } |
| 336 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 337 | static int rhashtable_rehash_attach(struct rhashtable *ht, |
| 338 | struct bucket_table *old_tbl, |
| 339 | struct bucket_table *new_tbl) |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 340 | { |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 341 | /* Protect future_tbl using the first bucket lock. */ |
| 342 | spin_lock_bh(old_tbl->locks); |
| 343 | |
| 344 | /* Did somebody beat us to it? */ |
| 345 | if (rcu_access_pointer(old_tbl->future_tbl)) { |
| 346 | spin_unlock_bh(old_tbl->locks); |
| 347 | return -EEXIST; |
| 348 | } |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 349 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 350 | /* Make insertions go into the new, empty table right away. Deletions |
| 351 | * and lookups will be attempted in both tables until we synchronize. |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 352 | */ |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 353 | rcu_assign_pointer(old_tbl->future_tbl, new_tbl); |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 354 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 355 | spin_unlock_bh(old_tbl->locks); |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int rhashtable_rehash_table(struct rhashtable *ht) |
| 361 | { |
| 362 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
| 363 | struct bucket_table *new_tbl; |
| 364 | struct rhashtable_walker *walker; |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 365 | unsigned int old_hash; |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 366 | int err; |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 367 | |
| 368 | new_tbl = rht_dereference(old_tbl->future_tbl, ht); |
| 369 | if (!new_tbl) |
| 370 | return 0; |
| 371 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 372 | for (old_hash = 0; old_hash < old_tbl->size; old_hash++) { |
| 373 | err = rhashtable_rehash_chain(ht, old_hash); |
| 374 | if (err) |
| 375 | return err; |
| 376 | } |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 377 | |
| 378 | /* Publish the new table pointer. */ |
| 379 | rcu_assign_pointer(ht->tbl, new_tbl); |
| 380 | |
Herbert Xu | ba7c95e | 2015-03-24 09:53:17 +1100 | [diff] [blame] | 381 | spin_lock(&ht->lock); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 382 | list_for_each_entry(walker, &old_tbl->walkers, list) |
| 383 | walker->tbl = NULL; |
Herbert Xu | ba7c95e | 2015-03-24 09:53:17 +1100 | [diff] [blame] | 384 | spin_unlock(&ht->lock); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 385 | |
Herbert Xu | aa34a6cb0 | 2015-03-11 09:43:48 +1100 | [diff] [blame] | 386 | /* Wait for readers. All new readers will see the new |
| 387 | * table, and thus no references to the old table will |
| 388 | * remain. |
| 389 | */ |
Herbert Xu | 9d901bc | 2015-03-14 13:57:23 +1100 | [diff] [blame] | 390 | call_rcu(&old_tbl->rcu, bucket_table_free_rcu); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 391 | |
| 392 | return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 395 | static int rhashtable_rehash_alloc(struct rhashtable *ht, |
| 396 | struct bucket_table *old_tbl, |
| 397 | unsigned int size) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 398 | { |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 399 | struct bucket_table *new_tbl; |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 400 | int err; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 401 | |
| 402 | ASSERT_RHT_MUTEX(ht); |
| 403 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 404 | new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 405 | if (new_tbl == NULL) |
| 406 | return -ENOMEM; |
| 407 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 408 | err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); |
| 409 | if (err) |
| 410 | bucket_table_free(new_tbl); |
| 411 | |
| 412 | return err; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 413 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 414 | |
| 415 | /** |
| 416 | * rhashtable_shrink - Shrink hash table while allowing concurrent lookups |
| 417 | * @ht: the hash table to shrink |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 418 | * |
Herbert Xu | 18093d1 | 2015-03-24 00:50:25 +1100 | [diff] [blame] | 419 | * This function shrinks the hash table to fit, i.e., the smallest |
| 420 | * size would not cause it to expand right away automatically. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 421 | * |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 422 | * The caller must ensure that no concurrent resizing occurs by holding |
| 423 | * ht->mutex. |
| 424 | * |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 425 | * The caller must ensure that no concurrent table mutations take place. |
| 426 | * 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] | 427 | * |
| 428 | * It is valid to have concurrent insertions and deletions protected by per |
| 429 | * bucket locks or concurrent RCU protected lookups and traversals. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 430 | */ |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 431 | static int rhashtable_shrink(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 432 | { |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 433 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
Vegard Nossum | 1231195 | 2016-08-12 20:10:44 +0200 | [diff] [blame] | 434 | unsigned int nelems = atomic_read(&ht->nelems); |
| 435 | unsigned int size = 0; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 436 | |
Vegard Nossum | 1231195 | 2016-08-12 20:10:44 +0200 | [diff] [blame] | 437 | if (nelems) |
| 438 | size = roundup_pow_of_two(nelems * 3 / 2); |
Herbert Xu | 18093d1 | 2015-03-24 00:50:25 +1100 | [diff] [blame] | 439 | if (size < ht->p.min_size) |
| 440 | size = ht->p.min_size; |
| 441 | |
| 442 | if (old_tbl->size <= size) |
| 443 | return 0; |
| 444 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 445 | if (rht_dereference(old_tbl->future_tbl, ht)) |
| 446 | return -EEXIST; |
| 447 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 448 | return rhashtable_rehash_alloc(ht, old_tbl, size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 449 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 450 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 451 | static void rht_deferred_worker(struct work_struct *work) |
| 452 | { |
| 453 | struct rhashtable *ht; |
| 454 | struct bucket_table *tbl; |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 455 | int err = 0; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 456 | |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 457 | ht = container_of(work, struct rhashtable, run_work); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 458 | mutex_lock(&ht->mutex); |
Herbert Xu | 28134a5 | 2015-02-04 07:33:22 +1100 | [diff] [blame] | 459 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 460 | tbl = rht_dereference(ht->tbl, ht); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 461 | tbl = rhashtable_last_table(ht, tbl); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 462 | |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 463 | if (rht_grow_above_75(ht, tbl)) |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 464 | err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2); |
Thomas Graf | b5e2c15 | 2015-03-24 20:42:19 +0000 | [diff] [blame] | 465 | else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl)) |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 466 | err = rhashtable_shrink(ht); |
| 467 | else if (tbl->nest) |
| 468 | err = rhashtable_rehash_alloc(ht, tbl, tbl->size); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 469 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 470 | if (!err) |
| 471 | err = rhashtable_rehash_table(ht); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 472 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 473 | mutex_unlock(&ht->mutex); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 474 | |
| 475 | if (err) |
| 476 | schedule_work(&ht->run_work); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 479 | static int rhashtable_insert_rehash(struct rhashtable *ht, |
| 480 | struct bucket_table *tbl) |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 481 | { |
| 482 | struct bucket_table *old_tbl; |
| 483 | struct bucket_table *new_tbl; |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 484 | unsigned int size; |
| 485 | int err; |
| 486 | |
| 487 | old_tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 488 | |
| 489 | size = tbl->size; |
| 490 | |
Herbert Xu | 3cf9222 | 2015-12-03 20:41:29 +0800 | [diff] [blame] | 491 | err = -EBUSY; |
| 492 | |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 493 | if (rht_grow_above_75(ht, tbl)) |
| 494 | size *= 2; |
Thomas Graf | a87b9eb | 2015-04-22 09:41:46 +0200 | [diff] [blame] | 495 | /* Do not schedule more than one rehash */ |
| 496 | else if (old_tbl != tbl) |
Herbert Xu | 3cf9222 | 2015-12-03 20:41:29 +0800 | [diff] [blame] | 497 | goto fail; |
| 498 | |
| 499 | err = -ENOMEM; |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 500 | |
| 501 | new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC); |
Herbert Xu | 3cf9222 | 2015-12-03 20:41:29 +0800 | [diff] [blame] | 502 | if (new_tbl == NULL) |
| 503 | goto fail; |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 504 | |
| 505 | err = rhashtable_rehash_attach(ht, tbl, new_tbl); |
| 506 | if (err) { |
| 507 | bucket_table_free(new_tbl); |
| 508 | if (err == -EEXIST) |
| 509 | err = 0; |
| 510 | } else |
| 511 | schedule_work(&ht->run_work); |
| 512 | |
| 513 | return err; |
Herbert Xu | 3cf9222 | 2015-12-03 20:41:29 +0800 | [diff] [blame] | 514 | |
| 515 | fail: |
| 516 | /* Do not fail the insert if someone else did a rehash. */ |
| 517 | if (likely(rcu_dereference_raw(tbl->future_tbl))) |
| 518 | return 0; |
| 519 | |
| 520 | /* Schedule async rehash to retry allocation in process context. */ |
| 521 | if (err == -ENOMEM) |
| 522 | schedule_work(&ht->run_work); |
| 523 | |
| 524 | return err; |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 525 | } |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 526 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 527 | static void *rhashtable_lookup_one(struct rhashtable *ht, |
| 528 | struct bucket_table *tbl, unsigned int hash, |
| 529 | const void *key, struct rhash_head *obj) |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 530 | { |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 531 | struct rhashtable_compare_arg arg = { |
| 532 | .ht = ht, |
| 533 | .key = key, |
| 534 | }; |
| 535 | struct rhash_head __rcu **pprev; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 536 | struct rhash_head *head; |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 537 | int elasticity; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 538 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 539 | elasticity = ht->elasticity; |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 540 | pprev = rht_bucket_var(tbl, hash); |
| 541 | rht_for_each_continue(head, *pprev, tbl, hash) { |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 542 | struct rhlist_head *list; |
| 543 | struct rhlist_head *plist; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 544 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 545 | elasticity--; |
| 546 | if (!key || |
| 547 | (ht->p.obj_cmpfn ? |
| 548 | ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) : |
| 549 | rhashtable_compare(&arg, rht_obj(ht, head)))) |
| 550 | continue; |
| 551 | |
| 552 | if (!ht->rhlist) |
| 553 | return rht_obj(ht, head); |
| 554 | |
| 555 | list = container_of(obj, struct rhlist_head, rhead); |
| 556 | plist = container_of(head, struct rhlist_head, rhead); |
| 557 | |
| 558 | RCU_INIT_POINTER(list->next, plist); |
| 559 | head = rht_dereference_bucket(head->next, tbl, hash); |
| 560 | RCU_INIT_POINTER(list->rhead.next, head); |
| 561 | rcu_assign_pointer(*pprev, obj); |
| 562 | |
| 563 | return NULL; |
Pablo Neira Ayuso | 5ca8cc5 | 2016-08-24 12:31:31 +0200 | [diff] [blame] | 564 | } |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 565 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 566 | if (elasticity <= 0) |
| 567 | return ERR_PTR(-EAGAIN); |
| 568 | |
| 569 | return ERR_PTR(-ENOENT); |
| 570 | } |
| 571 | |
| 572 | static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht, |
| 573 | struct bucket_table *tbl, |
| 574 | unsigned int hash, |
| 575 | struct rhash_head *obj, |
| 576 | void *data) |
| 577 | { |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 578 | struct rhash_head __rcu **pprev; |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 579 | struct bucket_table *new_tbl; |
| 580 | struct rhash_head *head; |
| 581 | |
| 582 | if (!IS_ERR_OR_NULL(data)) |
| 583 | return ERR_PTR(-EEXIST); |
| 584 | |
| 585 | if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT) |
| 586 | return ERR_CAST(data); |
| 587 | |
| 588 | new_tbl = rcu_dereference(tbl->future_tbl); |
| 589 | if (new_tbl) |
| 590 | return new_tbl; |
| 591 | |
| 592 | if (PTR_ERR(data) != -ENOENT) |
| 593 | return ERR_CAST(data); |
| 594 | |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 595 | if (unlikely(rht_grow_above_max(ht, tbl))) |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 596 | return ERR_PTR(-E2BIG); |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 597 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 598 | if (unlikely(rht_grow_above_100(ht, tbl))) |
| 599 | return ERR_PTR(-EAGAIN); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 600 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 601 | pprev = rht_bucket_insert(ht, tbl, hash); |
| 602 | if (!pprev) |
| 603 | return ERR_PTR(-ENOMEM); |
| 604 | |
| 605 | head = rht_dereference_bucket(*pprev, tbl, hash); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 606 | |
| 607 | RCU_INIT_POINTER(obj->next, head); |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 608 | if (ht->rhlist) { |
| 609 | struct rhlist_head *list; |
| 610 | |
| 611 | list = container_of(obj, struct rhlist_head, rhead); |
| 612 | RCU_INIT_POINTER(list->next, NULL); |
| 613 | } |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 614 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 615 | rcu_assign_pointer(*pprev, obj); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 616 | |
| 617 | atomic_inc(&ht->nelems); |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 618 | if (rht_grow_above_75(ht, tbl)) |
| 619 | schedule_work(&ht->run_work); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 620 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 621 | return NULL; |
| 622 | } |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 623 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 624 | static void *rhashtable_try_insert(struct rhashtable *ht, const void *key, |
| 625 | struct rhash_head *obj) |
| 626 | { |
| 627 | struct bucket_table *new_tbl; |
| 628 | struct bucket_table *tbl; |
| 629 | unsigned int hash; |
| 630 | spinlock_t *lock; |
| 631 | void *data; |
| 632 | |
| 633 | tbl = rcu_dereference(ht->tbl); |
| 634 | |
| 635 | /* All insertions must grab the oldest table containing |
| 636 | * the hashed bucket that is yet to be rehashed. |
| 637 | */ |
| 638 | for (;;) { |
| 639 | hash = rht_head_hashfn(ht, tbl, obj, ht->p); |
| 640 | lock = rht_bucket_lock(tbl, hash); |
| 641 | spin_lock_bh(lock); |
| 642 | |
| 643 | if (tbl->rehash <= hash) |
| 644 | break; |
| 645 | |
| 646 | spin_unlock_bh(lock); |
| 647 | tbl = rcu_dereference(tbl->future_tbl); |
| 648 | } |
| 649 | |
| 650 | data = rhashtable_lookup_one(ht, tbl, hash, key, obj); |
| 651 | new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data); |
| 652 | if (PTR_ERR(new_tbl) != -EEXIST) |
| 653 | data = ERR_CAST(new_tbl); |
| 654 | |
| 655 | while (!IS_ERR_OR_NULL(new_tbl)) { |
| 656 | tbl = new_tbl; |
| 657 | hash = rht_head_hashfn(ht, tbl, obj, ht->p); |
| 658 | spin_lock_nested(rht_bucket_lock(tbl, hash), |
| 659 | SINGLE_DEPTH_NESTING); |
| 660 | |
| 661 | data = rhashtable_lookup_one(ht, tbl, hash, key, obj); |
| 662 | new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data); |
| 663 | if (PTR_ERR(new_tbl) != -EEXIST) |
| 664 | data = ERR_CAST(new_tbl); |
| 665 | |
| 666 | spin_unlock(rht_bucket_lock(tbl, hash)); |
| 667 | } |
| 668 | |
| 669 | spin_unlock_bh(lock); |
| 670 | |
| 671 | if (PTR_ERR(data) == -EAGAIN) |
| 672 | data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?: |
| 673 | -EAGAIN); |
| 674 | |
| 675 | return data; |
| 676 | } |
| 677 | |
| 678 | void *rhashtable_insert_slow(struct rhashtable *ht, const void *key, |
| 679 | struct rhash_head *obj) |
| 680 | { |
| 681 | void *data; |
| 682 | |
| 683 | do { |
| 684 | rcu_read_lock(); |
| 685 | data = rhashtable_try_insert(ht, key, obj); |
| 686 | rcu_read_unlock(); |
| 687 | } while (PTR_ERR(data) == -EAGAIN); |
| 688 | |
| 689 | return data; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 690 | } |
| 691 | EXPORT_SYMBOL_GPL(rhashtable_insert_slow); |
| 692 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 693 | /** |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 694 | * rhashtable_walk_enter - Initialise an iterator |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 695 | * @ht: Table to walk over |
| 696 | * @iter: Hash table Iterator |
| 697 | * |
| 698 | * This function prepares a hash table walk. |
| 699 | * |
| 700 | * Note that if you restart a walk after rhashtable_walk_stop you |
| 701 | * may see the same object twice. Also, you may miss objects if |
| 702 | * there are removals in between rhashtable_walk_stop and the next |
| 703 | * call to rhashtable_walk_start. |
| 704 | * |
| 705 | * For a completely stable walk you should construct your own data |
| 706 | * structure outside the hash table. |
| 707 | * |
| 708 | * This function may sleep so you must not call it from interrupt |
| 709 | * context or with spin locks held. |
| 710 | * |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 711 | * You must call rhashtable_walk_exit after this function returns. |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 712 | */ |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 713 | void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter) |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 714 | { |
| 715 | iter->ht = ht; |
| 716 | iter->p = NULL; |
| 717 | iter->slot = 0; |
| 718 | iter->skip = 0; |
| 719 | |
Herbert Xu | c6ff526 | 2015-12-16 16:45:54 +0800 | [diff] [blame] | 720 | spin_lock(&ht->lock); |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 721 | iter->walker.tbl = |
Herbert Xu | 179ccc0 | 2015-12-19 10:45:28 +0800 | [diff] [blame] | 722 | rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock)); |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 723 | list_add(&iter->walker.list, &iter->walker.tbl->walkers); |
Herbert Xu | c6ff526 | 2015-12-16 16:45:54 +0800 | [diff] [blame] | 724 | spin_unlock(&ht->lock); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 725 | } |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 726 | EXPORT_SYMBOL_GPL(rhashtable_walk_enter); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 727 | |
| 728 | /** |
| 729 | * rhashtable_walk_exit - Free an iterator |
| 730 | * @iter: Hash table Iterator |
| 731 | * |
| 732 | * This function frees resources allocated by rhashtable_walk_init. |
| 733 | */ |
| 734 | void rhashtable_walk_exit(struct rhashtable_iter *iter) |
| 735 | { |
Herbert Xu | c6ff526 | 2015-12-16 16:45:54 +0800 | [diff] [blame] | 736 | spin_lock(&iter->ht->lock); |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 737 | if (iter->walker.tbl) |
| 738 | list_del(&iter->walker.list); |
Herbert Xu | c6ff526 | 2015-12-16 16:45:54 +0800 | [diff] [blame] | 739 | spin_unlock(&iter->ht->lock); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 740 | } |
| 741 | EXPORT_SYMBOL_GPL(rhashtable_walk_exit); |
| 742 | |
| 743 | /** |
| 744 | * rhashtable_walk_start - Start a hash table walk |
| 745 | * @iter: Hash table iterator |
| 746 | * |
| 747 | * Start a hash table walk. Note that we take the RCU lock in all |
| 748 | * cases including when we return an error. So you must always call |
| 749 | * rhashtable_walk_stop to clean up. |
| 750 | * |
| 751 | * Returns zero if successful. |
| 752 | * |
| 753 | * Returns -EAGAIN if resize event occured. Note that the iterator |
| 754 | * will rewind back to the beginning and you may use it immediately |
| 755 | * by calling rhashtable_walk_next. |
| 756 | */ |
| 757 | int rhashtable_walk_start(struct rhashtable_iter *iter) |
Thomas Graf | db4374f | 2015-03-16 10:42:27 +0100 | [diff] [blame] | 758 | __acquires(RCU) |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 759 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 760 | struct rhashtable *ht = iter->ht; |
| 761 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 762 | rcu_read_lock(); |
| 763 | |
Herbert Xu | c6ff526 | 2015-12-16 16:45:54 +0800 | [diff] [blame] | 764 | spin_lock(&ht->lock); |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 765 | if (iter->walker.tbl) |
| 766 | list_del(&iter->walker.list); |
Herbert Xu | c6ff526 | 2015-12-16 16:45:54 +0800 | [diff] [blame] | 767 | spin_unlock(&ht->lock); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 768 | |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 769 | if (!iter->walker.tbl) { |
| 770 | iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 771 | return -EAGAIN; |
| 772 | } |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | EXPORT_SYMBOL_GPL(rhashtable_walk_start); |
| 777 | |
| 778 | /** |
| 779 | * rhashtable_walk_next - Return the next object and advance the iterator |
| 780 | * @iter: Hash table iterator |
| 781 | * |
| 782 | * Note that you must call rhashtable_walk_stop when you are finished |
| 783 | * with the walk. |
| 784 | * |
| 785 | * Returns the next object or NULL when the end of the table is reached. |
| 786 | * |
| 787 | * Returns -EAGAIN if resize event occured. Note that the iterator |
| 788 | * will rewind back to the beginning and you may continue to use it. |
| 789 | */ |
| 790 | void *rhashtable_walk_next(struct rhashtable_iter *iter) |
| 791 | { |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 792 | struct bucket_table *tbl = iter->walker.tbl; |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 793 | struct rhlist_head *list = iter->list; |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 794 | struct rhashtable *ht = iter->ht; |
| 795 | struct rhash_head *p = iter->p; |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 796 | bool rhlist = ht->rhlist; |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 797 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 798 | if (p) { |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 799 | if (!rhlist || !(list = rcu_dereference(list->next))) { |
| 800 | p = rcu_dereference(p->next); |
| 801 | list = container_of(p, struct rhlist_head, rhead); |
| 802 | } |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 803 | goto next; |
| 804 | } |
| 805 | |
| 806 | for (; iter->slot < tbl->size; iter->slot++) { |
| 807 | int skip = iter->skip; |
| 808 | |
| 809 | rht_for_each_rcu(p, tbl, iter->slot) { |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 810 | if (rhlist) { |
| 811 | list = container_of(p, struct rhlist_head, |
| 812 | rhead); |
| 813 | do { |
| 814 | if (!skip) |
| 815 | goto next; |
| 816 | skip--; |
| 817 | list = rcu_dereference(list->next); |
| 818 | } while (list); |
| 819 | |
| 820 | continue; |
| 821 | } |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 822 | if (!skip) |
| 823 | break; |
| 824 | skip--; |
| 825 | } |
| 826 | |
| 827 | next: |
| 828 | if (!rht_is_a_nulls(p)) { |
| 829 | iter->skip++; |
| 830 | iter->p = p; |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 831 | iter->list = list; |
| 832 | return rht_obj(ht, rhlist ? &list->rhead : p); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | iter->skip = 0; |
| 836 | } |
| 837 | |
Phil Sutter | 142b942 | 2015-07-06 15:51:20 +0200 | [diff] [blame] | 838 | iter->p = NULL; |
| 839 | |
Herbert Xu | d88252f | 2015-03-24 00:50:19 +1100 | [diff] [blame] | 840 | /* Ensure we see any new tables. */ |
| 841 | smp_rmb(); |
| 842 | |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 843 | iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 844 | if (iter->walker.tbl) { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 845 | iter->slot = 0; |
| 846 | iter->skip = 0; |
| 847 | return ERR_PTR(-EAGAIN); |
| 848 | } |
| 849 | |
Thomas Graf | c936a79 | 2015-05-05 02:22:53 +0200 | [diff] [blame] | 850 | return NULL; |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 851 | } |
| 852 | EXPORT_SYMBOL_GPL(rhashtable_walk_next); |
| 853 | |
| 854 | /** |
| 855 | * rhashtable_walk_stop - Finish a hash table walk |
| 856 | * @iter: Hash table iterator |
| 857 | * |
| 858 | * Finish a hash table walk. |
| 859 | */ |
| 860 | void rhashtable_walk_stop(struct rhashtable_iter *iter) |
Thomas Graf | db4374f | 2015-03-16 10:42:27 +0100 | [diff] [blame] | 861 | __releases(RCU) |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 862 | { |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 863 | struct rhashtable *ht; |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 864 | struct bucket_table *tbl = iter->walker.tbl; |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 865 | |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 866 | if (!tbl) |
Herbert Xu | 963ecbd | 2015-03-15 21:12:04 +1100 | [diff] [blame] | 867 | goto out; |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 868 | |
| 869 | ht = iter->ht; |
| 870 | |
Herbert Xu | ba7c95e | 2015-03-24 09:53:17 +1100 | [diff] [blame] | 871 | spin_lock(&ht->lock); |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 872 | if (tbl->rehash < tbl->size) |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 873 | list_add(&iter->walker.list, &tbl->walkers); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 874 | else |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 875 | iter->walker.tbl = NULL; |
Herbert Xu | ba7c95e | 2015-03-24 09:53:17 +1100 | [diff] [blame] | 876 | spin_unlock(&ht->lock); |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 877 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 878 | iter->p = NULL; |
Herbert Xu | 963ecbd | 2015-03-15 21:12:04 +1100 | [diff] [blame] | 879 | |
| 880 | out: |
| 881 | rcu_read_unlock(); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 882 | } |
| 883 | EXPORT_SYMBOL_GPL(rhashtable_walk_stop); |
| 884 | |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 885 | static size_t rounded_hashtable_size(const struct rhashtable_params *params) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 886 | { |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 887 | return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), |
Herbert Xu | e2e21c1 | 2015-03-18 20:01:21 +1100 | [diff] [blame] | 888 | (unsigned long)params->min_size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 889 | } |
| 890 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 891 | static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) |
| 892 | { |
| 893 | return jhash2(key, length, seed); |
| 894 | } |
| 895 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 896 | /** |
| 897 | * rhashtable_init - initialize a new hash table |
| 898 | * @ht: hash table to be initialized |
| 899 | * @params: configuration parameters |
| 900 | * |
| 901 | * Initializes a new hash table based on the provided configuration |
| 902 | * parameters. A table can be configured either with a variable or |
| 903 | * fixed length key: |
| 904 | * |
| 905 | * Configuration Example 1: Fixed length keys |
| 906 | * struct test_obj { |
| 907 | * int key; |
| 908 | * void * my_member; |
| 909 | * struct rhash_head node; |
| 910 | * }; |
| 911 | * |
| 912 | * struct rhashtable_params params = { |
| 913 | * .head_offset = offsetof(struct test_obj, node), |
| 914 | * .key_offset = offsetof(struct test_obj, key), |
| 915 | * .key_len = sizeof(int), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 916 | * .hashfn = jhash, |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 917 | * .nulls_base = (1U << RHT_BASE_SHIFT), |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 918 | * }; |
| 919 | * |
| 920 | * Configuration Example 2: Variable length keys |
| 921 | * struct test_obj { |
| 922 | * [...] |
| 923 | * struct rhash_head node; |
| 924 | * }; |
| 925 | * |
Patrick McHardy | 49f7b33 | 2015-03-25 13:07:45 +0000 | [diff] [blame] | 926 | * u32 my_hash_fn(const void *data, u32 len, u32 seed) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 927 | * { |
| 928 | * struct test_obj *obj = data; |
| 929 | * |
| 930 | * return [... hash ...]; |
| 931 | * } |
| 932 | * |
| 933 | * struct rhashtable_params params = { |
| 934 | * .head_offset = offsetof(struct test_obj, node), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 935 | * .hashfn = jhash, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 936 | * .obj_hashfn = my_hash_fn, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 937 | * }; |
| 938 | */ |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 939 | int rhashtable_init(struct rhashtable *ht, |
| 940 | const struct rhashtable_params *params) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 941 | { |
| 942 | struct bucket_table *tbl; |
| 943 | size_t size; |
| 944 | |
| 945 | size = HASH_DEFAULT_SIZE; |
| 946 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 947 | if ((!params->key_len && !params->obj_hashfn) || |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 948 | (params->obj_hashfn && !params->obj_cmpfn)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 949 | return -EINVAL; |
| 950 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 951 | if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) |
| 952 | return -EINVAL; |
| 953 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 954 | memset(ht, 0, sizeof(*ht)); |
| 955 | mutex_init(&ht->mutex); |
Herbert Xu | ba7c95e | 2015-03-24 09:53:17 +1100 | [diff] [blame] | 956 | spin_lock_init(&ht->lock); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 957 | memcpy(&ht->p, params, sizeof(*params)); |
| 958 | |
Thomas Graf | a998f71 | 2015-03-19 22:31:13 +0000 | [diff] [blame] | 959 | if (params->min_size) |
| 960 | ht->p.min_size = roundup_pow_of_two(params->min_size); |
| 961 | |
| 962 | if (params->max_size) |
| 963 | ht->p.max_size = rounddown_pow_of_two(params->max_size); |
| 964 | |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 965 | if (params->insecure_max_entries) |
| 966 | ht->p.insecure_max_entries = |
| 967 | rounddown_pow_of_two(params->insecure_max_entries); |
| 968 | else |
| 969 | ht->p.insecure_max_entries = ht->p.max_size * 2; |
| 970 | |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 971 | ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE); |
Thomas Graf | a998f71 | 2015-03-19 22:31:13 +0000 | [diff] [blame] | 972 | |
Herbert Xu | 3a32460 | 2015-12-16 18:13:14 +0800 | [diff] [blame] | 973 | if (params->nelem_hint) |
| 974 | size = rounded_hashtable_size(&ht->p); |
| 975 | |
Herbert Xu | 27ed44a | 2015-03-24 13:37:30 +1100 | [diff] [blame] | 976 | /* The maximum (not average) chain length grows with the |
| 977 | * size of the hash table, at a rate of (log N)/(log log N). |
| 978 | * The value of 16 is selected so that even if the hash |
| 979 | * table grew to 2^32 you would not expect the maximum |
| 980 | * chain length to exceed it unless we are under attack |
| 981 | * (or extremely unlucky). |
| 982 | * |
| 983 | * As this limit is only to detect attacks, we don't need |
| 984 | * to set it to a lower value as you'd need the chain |
| 985 | * length to vastly exceed 16 to have any real effect |
| 986 | * on the system. |
| 987 | */ |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 988 | if (!params->insecure_elasticity) |
| 989 | ht->elasticity = 16; |
| 990 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 991 | if (params->locks_mul) |
| 992 | ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); |
| 993 | else |
| 994 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; |
| 995 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 996 | ht->key_len = ht->p.key_len; |
| 997 | if (!params->hashfn) { |
| 998 | ht->p.hashfn = jhash; |
| 999 | |
| 1000 | if (!(ht->key_len & (sizeof(u32) - 1))) { |
| 1001 | ht->key_len /= sizeof(u32); |
| 1002 | ht->p.hashfn = rhashtable_jhash2; |
| 1003 | } |
| 1004 | } |
| 1005 | |
Herbert Xu | b9ecfda | 2015-03-24 00:50:27 +1100 | [diff] [blame] | 1006 | tbl = bucket_table_alloc(ht, size, GFP_KERNEL); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1007 | if (tbl == NULL) |
| 1008 | return -ENOMEM; |
| 1009 | |
Ying Xue | 545a148 | 2015-01-07 13:41:57 +0800 | [diff] [blame] | 1010 | atomic_set(&ht->nelems, 0); |
Daniel Borkmann | a5b6846 | 2015-03-12 15:28:40 +0100 | [diff] [blame] | 1011 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1012 | RCU_INIT_POINTER(ht->tbl, tbl); |
| 1013 | |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 1014 | INIT_WORK(&ht->run_work, rht_deferred_worker); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 1015 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1016 | return 0; |
| 1017 | } |
| 1018 | EXPORT_SYMBOL_GPL(rhashtable_init); |
| 1019 | |
| 1020 | /** |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 1021 | * rhltable_init - initialize a new hash list table |
| 1022 | * @hlt: hash list table to be initialized |
| 1023 | * @params: configuration parameters |
| 1024 | * |
| 1025 | * Initializes a new hash list table. |
| 1026 | * |
| 1027 | * See documentation for rhashtable_init. |
| 1028 | */ |
| 1029 | int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params) |
| 1030 | { |
| 1031 | int err; |
| 1032 | |
| 1033 | /* No rhlist NULLs marking for now. */ |
| 1034 | if (params->nulls_base) |
| 1035 | return -EINVAL; |
| 1036 | |
| 1037 | err = rhashtable_init(&hlt->ht, params); |
| 1038 | hlt->ht.rhlist = true; |
| 1039 | return err; |
| 1040 | } |
| 1041 | EXPORT_SYMBOL_GPL(rhltable_init); |
| 1042 | |
| 1043 | static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj, |
| 1044 | void (*free_fn)(void *ptr, void *arg), |
| 1045 | void *arg) |
| 1046 | { |
| 1047 | struct rhlist_head *list; |
| 1048 | |
| 1049 | if (!ht->rhlist) { |
| 1050 | free_fn(rht_obj(ht, obj), arg); |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | list = container_of(obj, struct rhlist_head, rhead); |
| 1055 | do { |
| 1056 | obj = &list->rhead; |
| 1057 | list = rht_dereference(list->next, ht); |
| 1058 | free_fn(rht_obj(ht, obj), arg); |
| 1059 | } while (list); |
| 1060 | } |
| 1061 | |
| 1062 | /** |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 1063 | * rhashtable_free_and_destroy - free elements and destroy hash table |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1064 | * @ht: the hash table to destroy |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 1065 | * @free_fn: callback to release resources of element |
| 1066 | * @arg: pointer passed to free_fn |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1067 | * |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 1068 | * Stops an eventual async resize. If defined, invokes free_fn for each |
| 1069 | * element to releasal resources. Please note that RCU protected |
| 1070 | * readers may still be accessing the elements. Releasing of resources |
| 1071 | * must occur in a compatible manner. Then frees the bucket array. |
| 1072 | * |
| 1073 | * This function will eventually sleep to wait for an async resize |
| 1074 | * to complete. The caller is responsible that no further write operations |
| 1075 | * occurs in parallel. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1076 | */ |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 1077 | void rhashtable_free_and_destroy(struct rhashtable *ht, |
| 1078 | void (*free_fn)(void *ptr, void *arg), |
| 1079 | void *arg) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1080 | { |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 1081 | struct bucket_table *tbl; |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 1082 | unsigned int i; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 1083 | |
Daniel Borkmann | 4c4b52d | 2015-02-25 16:31:54 +0100 | [diff] [blame] | 1084 | cancel_work_sync(&ht->run_work); |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 1085 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 1086 | mutex_lock(&ht->mutex); |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 1087 | tbl = rht_dereference(ht->tbl, ht); |
| 1088 | if (free_fn) { |
| 1089 | for (i = 0; i < tbl->size; i++) { |
| 1090 | struct rhash_head *pos, *next; |
| 1091 | |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 1092 | for (pos = rht_dereference(*rht_bucket(tbl, i), ht), |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 1093 | next = !rht_is_a_nulls(pos) ? |
| 1094 | rht_dereference(pos->next, ht) : NULL; |
| 1095 | !rht_is_a_nulls(pos); |
| 1096 | pos = next, |
| 1097 | next = !rht_is_a_nulls(pos) ? |
| 1098 | rht_dereference(pos->next, ht) : NULL) |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 1099 | rhashtable_free_one(ht, pos, free_fn, arg); |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | bucket_table_free(tbl); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 1104 | mutex_unlock(&ht->mutex); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1105 | } |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 1106 | EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy); |
| 1107 | |
| 1108 | void rhashtable_destroy(struct rhashtable *ht) |
| 1109 | { |
| 1110 | return rhashtable_free_and_destroy(ht, NULL, NULL); |
| 1111 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1112 | EXPORT_SYMBOL_GPL(rhashtable_destroy); |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 1113 | |
| 1114 | struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, |
| 1115 | unsigned int hash) |
| 1116 | { |
| 1117 | const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); |
| 1118 | static struct rhash_head __rcu *rhnull = |
| 1119 | (struct rhash_head __rcu *)NULLS_MARKER(0); |
| 1120 | unsigned int index = hash & ((1 << tbl->nest) - 1); |
| 1121 | unsigned int size = tbl->size >> tbl->nest; |
| 1122 | unsigned int subhash = hash; |
| 1123 | union nested_table *ntbl; |
| 1124 | |
| 1125 | ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]); |
| 1126 | ntbl = rht_dereference_bucket(ntbl[index].table, tbl, hash); |
| 1127 | subhash >>= tbl->nest; |
| 1128 | |
| 1129 | while (ntbl && size > (1 << shift)) { |
| 1130 | index = subhash & ((1 << shift) - 1); |
| 1131 | ntbl = rht_dereference_bucket(ntbl[index].table, tbl, hash); |
| 1132 | size >>= shift; |
| 1133 | subhash >>= shift; |
| 1134 | } |
| 1135 | |
| 1136 | if (!ntbl) |
| 1137 | return &rhnull; |
| 1138 | |
| 1139 | return &ntbl[subhash].bucket; |
| 1140 | |
| 1141 | } |
| 1142 | EXPORT_SYMBOL_GPL(rht_bucket_nested); |
| 1143 | |
| 1144 | struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht, |
| 1145 | struct bucket_table *tbl, |
| 1146 | unsigned int hash) |
| 1147 | { |
| 1148 | const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); |
| 1149 | unsigned int index = hash & ((1 << tbl->nest) - 1); |
| 1150 | unsigned int size = tbl->size >> tbl->nest; |
| 1151 | union nested_table *ntbl; |
| 1152 | unsigned int shifted; |
| 1153 | unsigned int nhash; |
| 1154 | |
| 1155 | ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]); |
| 1156 | hash >>= tbl->nest; |
| 1157 | nhash = index; |
| 1158 | shifted = tbl->nest; |
| 1159 | ntbl = nested_table_alloc(ht, &ntbl[index].table, |
| 1160 | size <= (1 << shift) ? shifted : 0, nhash); |
| 1161 | |
| 1162 | while (ntbl && size > (1 << shift)) { |
| 1163 | index = hash & ((1 << shift) - 1); |
| 1164 | size >>= shift; |
| 1165 | hash >>= shift; |
| 1166 | nhash |= index << shifted; |
| 1167 | shifted += shift; |
| 1168 | ntbl = nested_table_alloc(ht, &ntbl[index].table, |
| 1169 | size <= (1 << shift) ? shifted : 0, |
| 1170 | nhash); |
| 1171 | } |
| 1172 | |
| 1173 | if (!ntbl) |
| 1174 | return NULL; |
| 1175 | |
| 1176 | return &ntbl[hash].bucket; |
| 1177 | |
| 1178 | } |
| 1179 | EXPORT_SYMBOL_GPL(rht_bucket_nested_insert); |