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