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