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