NeilBrown | 0eb71a9 | 2018-06-18 12:52:50 +1000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Resizable, Scalable, Concurrent Hash Table |
| 4 | * |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 5 | * Copyright (c) 2015-2016 Herbert Xu <herbert@gondor.apana.org.au> |
Thomas Graf | b5e2c15 | 2015-03-24 20:42:19 +0000 | [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 | dc0ee26 | 2015-03-20 21:57:06 +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 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 as |
| 15 | * published by the Free Software Foundation. |
| 16 | */ |
| 17 | |
| 18 | #ifndef _LINUX_RHASHTABLE_H |
| 19 | #define _LINUX_RHASHTABLE_H |
| 20 | |
Herbert Xu | 3cf9222 | 2015-12-03 20:41:29 +0800 | [diff] [blame] | 21 | #include <linux/err.h> |
Herbert Xu | 6626af6 | 2015-03-20 18:18:45 -0400 | [diff] [blame] | 22 | #include <linux/errno.h> |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 23 | #include <linux/jhash.h> |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 24 | #include <linux/list_nulls.h> |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 25 | #include <linux/workqueue.h> |
Ingo Molnar | b2d0910 | 2017-02-04 01:27:20 +0100 | [diff] [blame] | 26 | #include <linux/rculist.h> |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 27 | #include <linux/bit_spinlock.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 28 | |
NeilBrown | 0eb71a9 | 2018-06-18 12:52:50 +1000 | [diff] [blame] | 29 | #include <linux/rhashtable-types.h> |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 30 | /* |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 31 | * Objects in an rhashtable have an embedded struct rhash_head |
| 32 | * which is linked into as hash chain from the hash table - or one |
| 33 | * of two or more hash tables when the rhashtable is being resized. |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 34 | * The end of the chain is marked with a special nulls marks which has |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 35 | * the least significant bit set but otherwise stores the address of |
| 36 | * the hash bucket. This allows us to be be sure we've found the end |
| 37 | * of the right list. |
| 38 | * The value stored in the hash bucket has BIT(2) used as a lock bit. |
| 39 | * This bit must be atomically set before any changes are made to |
| 40 | * the chain. To avoid dereferencing this pointer without clearing |
| 41 | * the bit first, we use an opaque 'struct rhash_lock_head *' for the |
| 42 | * pointer stored in the bucket. This struct needs to be defined so |
NeilBrown | e4edbe3 | 2019-04-12 11:52:07 +1000 | [diff] [blame^] | 43 | * that rcu_dereference() works on it, but it has no content so a |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 44 | * cast is needed for it to be useful. This ensures it isn't |
| 45 | * used by mistake with clearing the lock bit first. |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 46 | */ |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 47 | struct rhash_lock_head {}; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 48 | |
Florian Westphal | 5f8ddea | 2017-04-16 02:55:09 +0200 | [diff] [blame] | 49 | /* Maximum chain length before rehash |
| 50 | * |
| 51 | * The maximum (not average) chain length grows with the size of the hash |
| 52 | * table, at a rate of (log N)/(log log N). |
| 53 | * |
| 54 | * The value of 16 is selected so that even if the hash table grew to |
| 55 | * 2^32 you would not expect the maximum chain length to exceed it |
| 56 | * unless we are under attack (or extremely unlucky). |
| 57 | * |
| 58 | * As this limit is only to detect attacks, we don't need to set it to a |
| 59 | * lower value as you'd need the chain length to vastly exceed 16 to have |
| 60 | * any real effect on the system. |
| 61 | */ |
| 62 | #define RHT_ELASTICITY 16u |
| 63 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 64 | /** |
| 65 | * struct bucket_table - Table of hash buckets |
| 66 | * @size: Number of hash buckets |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 67 | * @nest: Number of bits of first-level nested table. |
Herbert Xu | 63d512d | 2015-03-14 13:57:24 +1100 | [diff] [blame] | 68 | * @rehash: Current bucket being rehashed |
Herbert Xu | 988dfbd | 2015-03-10 09:27:55 +1100 | [diff] [blame] | 69 | * @hash_rnd: Random seed to fold into hash |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 70 | * @walkers: List of active walkers |
Herbert Xu | 9d901bc | 2015-03-14 13:57:23 +1100 | [diff] [blame] | 71 | * @rcu: RCU structure for freeing the table |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 72 | * @future_tbl: Table under construction during rehashing |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 73 | * @ntbl: Nested table used when out of memory. |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 74 | * @buckets: size * hash buckets |
| 75 | */ |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 76 | struct bucket_table { |
Herbert Xu | 63d512d | 2015-03-14 13:57:24 +1100 | [diff] [blame] | 77 | unsigned int size; |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 78 | unsigned int nest; |
Herbert Xu | 988dfbd | 2015-03-10 09:27:55 +1100 | [diff] [blame] | 79 | u32 hash_rnd; |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 80 | struct list_head walkers; |
Herbert Xu | 9d901bc | 2015-03-14 13:57:23 +1100 | [diff] [blame] | 81 | struct rcu_head rcu; |
Eric Dumazet | b9ebafb | 2015-02-20 06:48:57 -0800 | [diff] [blame] | 82 | |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 83 | struct bucket_table __rcu *future_tbl; |
| 84 | |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 85 | struct lockdep_map dep_map; |
| 86 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 87 | struct rhash_lock_head __rcu *buckets[] ____cacheline_aligned_in_smp; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 88 | }; |
| 89 | |
NeilBrown | 82208d0d | 2018-11-30 10:26:50 +1100 | [diff] [blame] | 90 | /* |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 91 | * We lock a bucket by setting BIT(1) in the pointer - this is always |
| 92 | * zero in real pointers and in the nulls marker. |
| 93 | * bit_spin_locks do not handle contention well, but the whole point |
| 94 | * of the hashtable design is to achieve minimum per-bucket contention. |
| 95 | * A nested hash table might not have a bucket pointer. In that case |
| 96 | * we cannot get a lock. For remove and replace the bucket cannot be |
| 97 | * interesting and doesn't need locking. |
| 98 | * For insert we allocate the bucket if this is the last bucket_table, |
| 99 | * and then take the lock. |
| 100 | * Sometimes we unlock a bucket by writing a new pointer there. In that |
| 101 | * case we don't need to unlock, but we do need to reset state such as |
| 102 | * local_bh. For that we have rht_assign_unlock(). As rcu_assign_pointer() |
| 103 | * provides the same release semantics that bit_spin_unlock() provides, |
| 104 | * this is safe. |
| 105 | */ |
| 106 | |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 107 | static inline void rht_lock(struct bucket_table *tbl, |
| 108 | struct rhash_lock_head **bkt) |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 109 | { |
| 110 | local_bh_disable(); |
| 111 | bit_spin_lock(1, (unsigned long *)bkt); |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 112 | lock_map_acquire(&tbl->dep_map); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 113 | } |
| 114 | |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 115 | static inline void rht_lock_nested(struct bucket_table *tbl, |
| 116 | struct rhash_lock_head **bucket, |
| 117 | unsigned int subclass) |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 118 | { |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 119 | local_bh_disable(); |
| 120 | bit_spin_lock(1, (unsigned long *)bucket); |
| 121 | lock_acquire_exclusive(&tbl->dep_map, subclass, 0, NULL, _THIS_IP_); |
| 122 | } |
| 123 | |
| 124 | static inline void rht_unlock(struct bucket_table *tbl, |
| 125 | struct rhash_lock_head **bkt) |
| 126 | { |
| 127 | lock_map_release(&tbl->dep_map); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 128 | bit_spin_unlock(1, (unsigned long *)bkt); |
| 129 | local_bh_enable(); |
| 130 | } |
| 131 | |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 132 | static inline void rht_assign_unlock(struct bucket_table *tbl, |
NeilBrown | e4edbe3 | 2019-04-12 11:52:07 +1000 | [diff] [blame^] | 133 | struct rhash_lock_head __rcu **bkt, |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 134 | struct rhash_head *obj) |
| 135 | { |
NeilBrown | e4edbe3 | 2019-04-12 11:52:07 +1000 | [diff] [blame^] | 136 | struct rhash_head __rcu **p = (struct rhash_head __rcu **)bkt; |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 137 | |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 138 | lock_map_release(&tbl->dep_map); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 139 | rcu_assign_pointer(*p, obj); |
| 140 | preempt_enable(); |
| 141 | __release(bitlock); |
| 142 | local_bh_enable(); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * If 'p' is a bucket head and might be locked: |
| 147 | * rht_ptr() returns the address without the lock bit. |
| 148 | * rht_ptr_locked() returns the address WITH the lock bit. |
| 149 | */ |
| 150 | static inline struct rhash_head __rcu *rht_ptr(const struct rhash_lock_head *p) |
| 151 | { |
| 152 | return (void *)(((unsigned long)p) & ~BIT(1)); |
| 153 | } |
| 154 | |
| 155 | static inline struct rhash_lock_head __rcu *rht_ptr_locked(const |
| 156 | struct rhash_head *p) |
| 157 | { |
| 158 | return (void *)(((unsigned long)p) | BIT(1)); |
| 159 | } |
| 160 | |
| 161 | /* |
NeilBrown | 82208d0d | 2018-11-30 10:26:50 +1100 | [diff] [blame] | 162 | * NULLS_MARKER() expects a hash value with the low |
| 163 | * bits mostly likely to be significant, and it discards |
| 164 | * the msb. |
| 165 | * We git it an address, in which the bottom 2 bits are |
| 166 | * always 0, and the msb might be significant. |
| 167 | * So we shift the address down one bit to align with |
| 168 | * expectations and avoid losing a significant bit. |
| 169 | */ |
| 170 | #define RHT_NULLS_MARKER(ptr) \ |
| 171 | ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1)) |
NeilBrown | 9b4f64a | 2018-06-18 12:52:50 +1000 | [diff] [blame] | 172 | #define INIT_RHT_NULLS_HEAD(ptr) \ |
NeilBrown | 82208d0d | 2018-11-30 10:26:50 +1100 | [diff] [blame] | 173 | ((ptr) = RHT_NULLS_MARKER(&(ptr))) |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 174 | |
| 175 | static inline bool rht_is_a_nulls(const struct rhash_head *ptr) |
| 176 | { |
| 177 | return ((unsigned long) ptr & 1); |
| 178 | } |
| 179 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 180 | static inline void *rht_obj(const struct rhashtable *ht, |
| 181 | const struct rhash_head *he) |
| 182 | { |
| 183 | return (char *)he - ht->p.head_offset; |
| 184 | } |
| 185 | |
| 186 | static inline unsigned int rht_bucket_index(const struct bucket_table *tbl, |
| 187 | unsigned int hash) |
| 188 | { |
NeilBrown | 9f9a707 | 2018-06-18 12:52:50 +1000 | [diff] [blame] | 189 | return hash & (tbl->size - 1); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 190 | } |
| 191 | |
Tom Herbert | 2b86093 | 2017-12-04 10:31:43 -0800 | [diff] [blame] | 192 | static inline unsigned int rht_key_get_hash(struct rhashtable *ht, |
| 193 | const void *key, const struct rhashtable_params params, |
| 194 | unsigned int hash_rnd) |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 195 | { |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 196 | unsigned int hash; |
Herbert Xu | de91b25 | 2015-03-24 00:50:20 +1100 | [diff] [blame] | 197 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 198 | /* params must be equal to ht->p if it isn't constant. */ |
| 199 | if (!__builtin_constant_p(params.key_len)) |
Tom Herbert | 2b86093 | 2017-12-04 10:31:43 -0800 | [diff] [blame] | 200 | hash = ht->p.hashfn(key, ht->key_len, hash_rnd); |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 201 | else if (params.key_len) { |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 202 | unsigned int key_len = params.key_len; |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 203 | |
| 204 | if (params.hashfn) |
Tom Herbert | 2b86093 | 2017-12-04 10:31:43 -0800 | [diff] [blame] | 205 | hash = params.hashfn(key, key_len, hash_rnd); |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 206 | else if (key_len & (sizeof(u32) - 1)) |
Tom Herbert | 2b86093 | 2017-12-04 10:31:43 -0800 | [diff] [blame] | 207 | hash = jhash(key, key_len, hash_rnd); |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 208 | else |
Tom Herbert | 2b86093 | 2017-12-04 10:31:43 -0800 | [diff] [blame] | 209 | hash = jhash2(key, key_len / sizeof(u32), hash_rnd); |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 210 | } else { |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 211 | unsigned int key_len = ht->p.key_len; |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 212 | |
| 213 | if (params.hashfn) |
Tom Herbert | 2b86093 | 2017-12-04 10:31:43 -0800 | [diff] [blame] | 214 | hash = params.hashfn(key, key_len, hash_rnd); |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 215 | else |
Tom Herbert | 2b86093 | 2017-12-04 10:31:43 -0800 | [diff] [blame] | 216 | hash = jhash(key, key_len, hash_rnd); |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 217 | } |
| 218 | |
Tom Herbert | 2b86093 | 2017-12-04 10:31:43 -0800 | [diff] [blame] | 219 | return hash; |
| 220 | } |
| 221 | |
| 222 | static inline unsigned int rht_key_hashfn( |
| 223 | struct rhashtable *ht, const struct bucket_table *tbl, |
| 224 | const void *key, const struct rhashtable_params params) |
| 225 | { |
| 226 | unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd); |
| 227 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 228 | return rht_bucket_index(tbl, hash); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static inline unsigned int rht_head_hashfn( |
| 232 | struct rhashtable *ht, const struct bucket_table *tbl, |
| 233 | const struct rhash_head *he, const struct rhashtable_params params) |
| 234 | { |
| 235 | const char *ptr = rht_obj(ht, he); |
| 236 | |
| 237 | return likely(params.obj_hashfn) ? |
Patrick McHardy | 49f7b33 | 2015-03-25 13:07:45 +0000 | [diff] [blame] | 238 | rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?: |
| 239 | ht->p.key_len, |
| 240 | tbl->hash_rnd)) : |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 241 | rht_key_hashfn(ht, tbl, ptr + params.key_offset, params); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * rht_grow_above_75 - returns true if nelems > 0.75 * table-size |
| 246 | * @ht: hash table |
| 247 | * @tbl: current table |
| 248 | */ |
| 249 | static inline bool rht_grow_above_75(const struct rhashtable *ht, |
| 250 | const struct bucket_table *tbl) |
| 251 | { |
| 252 | /* Expand table when exceeding 75% load */ |
| 253 | return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) && |
| 254 | (!ht->p.max_size || tbl->size < ht->p.max_size); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size |
| 259 | * @ht: hash table |
| 260 | * @tbl: current table |
| 261 | */ |
| 262 | static inline bool rht_shrink_below_30(const struct rhashtable *ht, |
| 263 | const struct bucket_table *tbl) |
| 264 | { |
| 265 | /* Shrink table beneath 30% load */ |
| 266 | return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) && |
| 267 | tbl->size > ht->p.min_size; |
| 268 | } |
| 269 | |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 270 | /** |
| 271 | * rht_grow_above_100 - returns true if nelems > table-size |
| 272 | * @ht: hash table |
| 273 | * @tbl: current table |
| 274 | */ |
| 275 | static inline bool rht_grow_above_100(const struct rhashtable *ht, |
| 276 | const struct bucket_table *tbl) |
| 277 | { |
Johannes Berg | 1d8dc3d | 2015-04-23 16:38:43 +0200 | [diff] [blame] | 278 | return atomic_read(&ht->nelems) > tbl->size && |
| 279 | (!ht->p.max_size || tbl->size < ht->p.max_size); |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 280 | } |
| 281 | |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 282 | /** |
| 283 | * rht_grow_above_max - returns true if table is above maximum |
| 284 | * @ht: hash table |
| 285 | * @tbl: current table |
| 286 | */ |
| 287 | static inline bool rht_grow_above_max(const struct rhashtable *ht, |
| 288 | const struct bucket_table *tbl) |
| 289 | { |
Herbert Xu | 6d684e5 | 2017-04-27 13:44:51 +0800 | [diff] [blame] | 290 | return atomic_read(&ht->nelems) >= ht->max_elems; |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 291 | } |
| 292 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 293 | #ifdef CONFIG_PROVE_LOCKING |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 294 | int lockdep_rht_mutex_is_held(struct rhashtable *ht); |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 295 | int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 296 | #else |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 297 | static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 298 | { |
| 299 | return 1; |
| 300 | } |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 301 | |
| 302 | static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, |
| 303 | u32 hash) |
| 304 | { |
| 305 | return 1; |
| 306 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 307 | #endif /* CONFIG_PROVE_LOCKING */ |
| 308 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 309 | void *rhashtable_insert_slow(struct rhashtable *ht, const void *key, |
| 310 | struct rhash_head *obj); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 311 | |
Herbert Xu | 246779d | 2016-08-18 16:50:56 +0800 | [diff] [blame] | 312 | void rhashtable_walk_enter(struct rhashtable *ht, |
| 313 | struct rhashtable_iter *iter); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 314 | void rhashtable_walk_exit(struct rhashtable_iter *iter); |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 315 | int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU); |
| 316 | |
| 317 | static inline void rhashtable_walk_start(struct rhashtable_iter *iter) |
| 318 | { |
| 319 | (void)rhashtable_walk_start_check(iter); |
| 320 | } |
| 321 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 322 | void *rhashtable_walk_next(struct rhashtable_iter *iter); |
Tom Herbert | 2db54b4 | 2017-12-04 10:31:42 -0800 | [diff] [blame] | 323 | void *rhashtable_walk_peek(struct rhashtable_iter *iter); |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 324 | void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); |
| 325 | |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 326 | void rhashtable_free_and_destroy(struct rhashtable *ht, |
| 327 | void (*free_fn)(void *ptr, void *arg), |
| 328 | void *arg); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 329 | void rhashtable_destroy(struct rhashtable *ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 330 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 331 | struct rhash_lock_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, |
| 332 | unsigned int hash); |
| 333 | struct rhash_lock_head __rcu **__rht_bucket_nested(const struct bucket_table *tbl, |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 334 | unsigned int hash); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 335 | struct rhash_lock_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht, |
| 336 | struct bucket_table *tbl, |
| 337 | unsigned int hash); |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 338 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 339 | #define rht_dereference(p, ht) \ |
| 340 | rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht)) |
| 341 | |
| 342 | #define rht_dereference_rcu(p, ht) \ |
| 343 | rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht)) |
| 344 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 345 | #define rht_dereference_bucket(p, tbl, hash) \ |
| 346 | rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 347 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 348 | #define rht_dereference_bucket_rcu(p, tbl, hash) \ |
| 349 | rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash)) |
| 350 | |
| 351 | #define rht_entry(tpos, pos, member) \ |
| 352 | ({ tpos = container_of(pos, typeof(*tpos), member); 1; }) |
| 353 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 354 | static inline struct rhash_lock_head __rcu *const *rht_bucket( |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 355 | const struct bucket_table *tbl, unsigned int hash) |
| 356 | { |
| 357 | return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) : |
| 358 | &tbl->buckets[hash]; |
| 359 | } |
| 360 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 361 | static inline struct rhash_lock_head __rcu **rht_bucket_var( |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 362 | struct bucket_table *tbl, unsigned int hash) |
| 363 | { |
NeilBrown | ff302db | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 364 | return unlikely(tbl->nest) ? __rht_bucket_nested(tbl, hash) : |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 365 | &tbl->buckets[hash]; |
| 366 | } |
| 367 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 368 | static inline struct rhash_lock_head __rcu **rht_bucket_insert( |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 369 | struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash) |
| 370 | { |
| 371 | return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) : |
| 372 | &tbl->buckets[hash]; |
| 373 | } |
| 374 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 375 | /** |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 376 | * rht_for_each_from - iterate over hash chain from given head |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 377 | * @pos: the &struct rhash_head to use as a loop cursor. |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 378 | * @head: the &struct rhash_head to start from |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 379 | * @tbl: the &struct bucket_table |
| 380 | * @hash: the hash value / bucket index |
| 381 | */ |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 382 | #define rht_for_each_from(pos, head, tbl, hash) \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 383 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 384 | !rht_is_a_nulls(pos); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 385 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 386 | |
| 387 | /** |
| 388 | * rht_for_each - iterate over hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 389 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 390 | * @tbl: the &struct bucket_table |
| 391 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 392 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 393 | #define rht_for_each(pos, tbl, hash) \ |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 394 | rht_for_each_from(pos, rht_ptr(*rht_bucket(tbl, hash)), tbl, hash) |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 395 | |
| 396 | /** |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 397 | * rht_for_each_entry_from - iterate over hash chain from given head |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 398 | * @tpos: the type * to use as a loop cursor. |
| 399 | * @pos: the &struct rhash_head to use as a loop cursor. |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 400 | * @head: the &struct rhash_head to start from |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 401 | * @tbl: the &struct bucket_table |
| 402 | * @hash: the hash value / bucket index |
| 403 | * @member: name of the &struct rhash_head within the hashable struct. |
| 404 | */ |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 405 | #define rht_for_each_entry_from(tpos, pos, head, tbl, hash, member) \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 406 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 407 | (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 408 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 409 | |
| 410 | /** |
| 411 | * rht_for_each_entry - iterate over hash chain of given type |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 412 | * @tpos: the type * to use as a loop cursor. |
| 413 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 414 | * @tbl: the &struct bucket_table |
| 415 | * @hash: the hash value / bucket index |
| 416 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 417 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 418 | #define rht_for_each_entry(tpos, pos, tbl, hash, member) \ |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 419 | rht_for_each_entry_from(tpos, pos, rht_ptr(*rht_bucket(tbl, hash)), \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 420 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 421 | |
| 422 | /** |
| 423 | * rht_for_each_entry_safe - safely iterate over hash chain of given type |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 424 | * @tpos: the type * to use as a loop cursor. |
| 425 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 426 | * @next: the &struct rhash_head to use as next in loop cursor. |
| 427 | * @tbl: the &struct bucket_table |
| 428 | * @hash: the hash value / bucket index |
| 429 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 430 | * |
| 431 | * This hash chain list-traversal primitive allows for the looped code to |
| 432 | * remove the loop cursor from the list. |
| 433 | */ |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 434 | #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \ |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 435 | for (pos = rht_dereference_bucket(rht_ptr(*rht_bucket(tbl, hash)), \ |
| 436 | tbl, hash), \ |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 437 | next = !rht_is_a_nulls(pos) ? \ |
| 438 | rht_dereference_bucket(pos->next, tbl, hash) : NULL; \ |
| 439 | (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ |
| 440 | pos = next, \ |
| 441 | next = !rht_is_a_nulls(pos) ? \ |
Patrick McHardy | 607954b | 2015-01-21 11:12:13 +0000 | [diff] [blame] | 442 | rht_dereference_bucket(pos->next, tbl, hash) : NULL) |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 443 | |
| 444 | /** |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 445 | * rht_for_each_rcu_from - iterate over rcu hash chain from given head |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 446 | * @pos: the &struct rhash_head to use as a loop cursor. |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 447 | * @head: the &struct rhash_head to start from |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 448 | * @tbl: the &struct bucket_table |
| 449 | * @hash: the hash value / bucket index |
| 450 | * |
| 451 | * This hash chain list-traversal primitive may safely run concurrently with |
| 452 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 453 | * traversal is guarded by rcu_read_lock(). |
| 454 | */ |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 455 | #define rht_for_each_rcu_from(pos, head, tbl, hash) \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 456 | for (({barrier(); }), \ |
| 457 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 458 | !rht_is_a_nulls(pos); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 459 | pos = rcu_dereference_raw(pos->next)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 460 | |
| 461 | /** |
| 462 | * rht_for_each_rcu - iterate over rcu hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 463 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 464 | * @tbl: the &struct bucket_table |
| 465 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 466 | * |
| 467 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 468 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 469 | * traversal is guarded by rcu_read_lock(). |
| 470 | */ |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 471 | #define rht_for_each_rcu(pos, tbl, hash) \ |
| 472 | for (({barrier(); }), \ |
| 473 | pos = rht_ptr(rht_dereference_bucket_rcu( \ |
| 474 | *rht_bucket(tbl, hash), tbl, hash)); \ |
| 475 | !rht_is_a_nulls(pos); \ |
| 476 | pos = rcu_dereference_raw(pos->next)) |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 477 | |
| 478 | /** |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 479 | * rht_for_each_entry_rcu_from - iterated over rcu hash chain from given head |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 480 | * @tpos: the type * to use as a loop cursor. |
| 481 | * @pos: the &struct rhash_head to use as a loop cursor. |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 482 | * @head: the &struct rhash_head to start from |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 483 | * @tbl: the &struct bucket_table |
| 484 | * @hash: the hash value / bucket index |
| 485 | * @member: name of the &struct rhash_head within the hashable struct. |
| 486 | * |
| 487 | * This hash chain list-traversal primitive may safely run concurrently with |
| 488 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 489 | * traversal is guarded by rcu_read_lock(). |
| 490 | */ |
NeilBrown | f7ad68b | 2019-03-21 14:42:40 +1100 | [diff] [blame] | 491 | #define rht_for_each_entry_rcu_from(tpos, pos, head, tbl, hash, member) \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 492 | for (({barrier(); }), \ |
| 493 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 494 | (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 495 | pos = rht_dereference_bucket_rcu(pos->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 496 | |
| 497 | /** |
| 498 | * rht_for_each_entry_rcu - iterate over rcu hash chain of given type |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 499 | * @tpos: the type * to use as a loop cursor. |
| 500 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 501 | * @tbl: the &struct bucket_table |
| 502 | * @hash: the hash value / bucket index |
| 503 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 504 | * |
| 505 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 506 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 507 | * traversal is guarded by rcu_read_lock(). |
| 508 | */ |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 509 | #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \ |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 510 | rht_for_each_entry_rcu_from(tpos, pos, \ |
| 511 | rht_ptr(*rht_bucket(tbl, hash)), \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 512 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 513 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 514 | /** |
| 515 | * rhl_for_each_rcu - iterate over rcu hash table list |
| 516 | * @pos: the &struct rlist_head to use as a loop cursor. |
| 517 | * @list: the head of the list |
| 518 | * |
| 519 | * This hash chain list-traversal primitive should be used on the |
| 520 | * list returned by rhltable_lookup. |
| 521 | */ |
| 522 | #define rhl_for_each_rcu(pos, list) \ |
| 523 | for (pos = list; pos; pos = rcu_dereference_raw(pos->next)) |
| 524 | |
| 525 | /** |
| 526 | * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type |
| 527 | * @tpos: the type * to use as a loop cursor. |
| 528 | * @pos: the &struct rlist_head to use as a loop cursor. |
| 529 | * @list: the head of the list |
| 530 | * @member: name of the &struct rlist_head within the hashable struct. |
| 531 | * |
| 532 | * This hash chain list-traversal primitive should be used on the |
| 533 | * list returned by rhltable_lookup. |
| 534 | */ |
| 535 | #define rhl_for_each_entry_rcu(tpos, pos, list, member) \ |
| 536 | for (pos = list; pos && rht_entry(tpos, pos, member); \ |
| 537 | pos = rcu_dereference_raw(pos->next)) |
| 538 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 539 | static inline int rhashtable_compare(struct rhashtable_compare_arg *arg, |
| 540 | const void *obj) |
| 541 | { |
| 542 | struct rhashtable *ht = arg->ht; |
| 543 | const char *ptr = obj; |
| 544 | |
| 545 | return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len); |
| 546 | } |
| 547 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 548 | /* Internal function, do not use. */ |
| 549 | static inline struct rhash_head *__rhashtable_lookup( |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 550 | struct rhashtable *ht, const void *key, |
| 551 | const struct rhashtable_params params) |
| 552 | { |
| 553 | struct rhashtable_compare_arg arg = { |
| 554 | .ht = ht, |
| 555 | .key = key, |
| 556 | }; |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 557 | struct rhash_lock_head __rcu * const *bkt; |
Herbert Xu | da20420 | 2017-02-11 19:26:47 +0800 | [diff] [blame] | 558 | struct bucket_table *tbl; |
NeilBrown | e4edbe3 | 2019-04-12 11:52:07 +1000 | [diff] [blame^] | 559 | struct rhash_head __rcu *head; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 560 | struct rhash_head *he; |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 561 | unsigned int hash; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 562 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 563 | tbl = rht_dereference_rcu(ht->tbl, ht); |
| 564 | restart: |
| 565 | hash = rht_key_hashfn(ht, tbl, key, params); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 566 | bkt = rht_bucket(tbl, hash); |
NeilBrown | 82208d0d | 2018-11-30 10:26:50 +1100 | [diff] [blame] | 567 | do { |
NeilBrown | e4edbe3 | 2019-04-12 11:52:07 +1000 | [diff] [blame^] | 568 | head = rht_ptr(rht_dereference_bucket_rcu(*bkt, tbl, hash)); |
| 569 | rht_for_each_rcu_from(he, head, tbl, hash) { |
NeilBrown | 82208d0d | 2018-11-30 10:26:50 +1100 | [diff] [blame] | 570 | if (params.obj_cmpfn ? |
| 571 | params.obj_cmpfn(&arg, rht_obj(ht, he)) : |
| 572 | rhashtable_compare(&arg, rht_obj(ht, he))) |
| 573 | continue; |
| 574 | return he; |
| 575 | } |
| 576 | /* An object might have been moved to a different hash chain, |
| 577 | * while we walk along it - better check and retry. |
| 578 | */ |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 579 | } while (he != RHT_NULLS_MARKER(bkt)); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 580 | |
| 581 | /* Ensure we see any new tables. */ |
| 582 | smp_rmb(); |
| 583 | |
| 584 | tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 585 | if (unlikely(tbl)) |
| 586 | goto restart; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 587 | |
| 588 | return NULL; |
| 589 | } |
| 590 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 591 | /** |
| 592 | * rhashtable_lookup - search hash table |
| 593 | * @ht: hash table |
| 594 | * @key: the pointer to the key |
| 595 | * @params: hash table parameters |
| 596 | * |
| 597 | * Computes the hash value for the key and traverses the bucket chain looking |
| 598 | * for a entry with an identical key. The first matching entry is returned. |
| 599 | * |
| 600 | * This must only be called under the RCU read lock. |
| 601 | * |
| 602 | * Returns the first entry on which the compare function returned true. |
| 603 | */ |
| 604 | static inline void *rhashtable_lookup( |
| 605 | struct rhashtable *ht, const void *key, |
| 606 | const struct rhashtable_params params) |
| 607 | { |
| 608 | struct rhash_head *he = __rhashtable_lookup(ht, key, params); |
| 609 | |
| 610 | return he ? rht_obj(ht, he) : NULL; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * rhashtable_lookup_fast - search hash table, without RCU read lock |
| 615 | * @ht: hash table |
| 616 | * @key: the pointer to the key |
| 617 | * @params: hash table parameters |
| 618 | * |
| 619 | * Computes the hash value for the key and traverses the bucket chain looking |
| 620 | * for a entry with an identical key. The first matching entry is returned. |
| 621 | * |
| 622 | * Only use this function when you have other mechanisms guaranteeing |
| 623 | * that the object won't go away after the RCU read lock is released. |
| 624 | * |
| 625 | * Returns the first entry on which the compare function returned true. |
| 626 | */ |
| 627 | static inline void *rhashtable_lookup_fast( |
| 628 | struct rhashtable *ht, const void *key, |
| 629 | const struct rhashtable_params params) |
| 630 | { |
| 631 | void *obj; |
| 632 | |
| 633 | rcu_read_lock(); |
| 634 | obj = rhashtable_lookup(ht, key, params); |
| 635 | rcu_read_unlock(); |
| 636 | |
| 637 | return obj; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * rhltable_lookup - search hash list table |
| 642 | * @hlt: hash table |
| 643 | * @key: the pointer to the key |
| 644 | * @params: hash table parameters |
| 645 | * |
| 646 | * Computes the hash value for the key and traverses the bucket chain looking |
| 647 | * for a entry with an identical key. All matching entries are returned |
| 648 | * in a list. |
| 649 | * |
| 650 | * This must only be called under the RCU read lock. |
| 651 | * |
| 652 | * Returns the list of entries that match the given key. |
| 653 | */ |
| 654 | static inline struct rhlist_head *rhltable_lookup( |
| 655 | struct rhltable *hlt, const void *key, |
| 656 | const struct rhashtable_params params) |
| 657 | { |
| 658 | struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params); |
| 659 | |
| 660 | return he ? container_of(he, struct rhlist_head, rhead) : NULL; |
| 661 | } |
| 662 | |
Pablo Neira Ayuso | 5ca8cc5 | 2016-08-24 12:31:31 +0200 | [diff] [blame] | 663 | /* Internal function, please use rhashtable_insert_fast() instead. This |
| 664 | * function returns the existing element already in hashes in there is a clash, |
| 665 | * otherwise it returns an error via ERR_PTR(). |
| 666 | */ |
| 667 | static inline void *__rhashtable_insert_fast( |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 668 | struct rhashtable *ht, const void *key, struct rhash_head *obj, |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 669 | const struct rhashtable_params params, bool rhlist) |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 670 | { |
| 671 | struct rhashtable_compare_arg arg = { |
| 672 | .ht = ht, |
| 673 | .key = key, |
| 674 | }; |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 675 | struct rhash_lock_head __rcu **bkt; |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 676 | struct rhash_head __rcu **pprev; |
| 677 | struct bucket_table *tbl; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 678 | struct rhash_head *head; |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 679 | unsigned int hash; |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 680 | int elasticity; |
| 681 | void *data; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 682 | |
| 683 | rcu_read_lock(); |
| 684 | |
| 685 | tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 686 | hash = rht_head_hashfn(ht, tbl, obj, params); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 687 | elasticity = RHT_ELASTICITY; |
| 688 | bkt = rht_bucket_insert(ht, tbl, hash); |
| 689 | data = ERR_PTR(-ENOMEM); |
| 690 | if (!bkt) |
| 691 | goto out; |
| 692 | pprev = NULL; |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 693 | rht_lock(tbl, bkt); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 694 | |
NeilBrown | c069001 | 2018-06-18 12:52:50 +1000 | [diff] [blame] | 695 | if (unlikely(rcu_access_pointer(tbl->future_tbl))) { |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 696 | slow_path: |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 697 | rht_unlock(tbl, bkt); |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 698 | rcu_read_unlock(); |
| 699 | return rhashtable_insert_slow(ht, key, obj); |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 700 | } |
| 701 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 702 | rht_for_each_from(head, rht_ptr(*bkt), tbl, hash) { |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 703 | struct rhlist_head *plist; |
| 704 | struct rhlist_head *list; |
Herbert Xu | 3cf9222 | 2015-12-03 20:41:29 +0800 | [diff] [blame] | 705 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 706 | elasticity--; |
| 707 | if (!key || |
| 708 | (params.obj_cmpfn ? |
| 709 | params.obj_cmpfn(&arg, rht_obj(ht, head)) : |
Paul Blakey | d3dcf8e | 2018-03-04 17:29:48 +0200 | [diff] [blame] | 710 | rhashtable_compare(&arg, rht_obj(ht, head)))) { |
| 711 | pprev = &head->next; |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 712 | continue; |
Paul Blakey | d3dcf8e | 2018-03-04 17:29:48 +0200 | [diff] [blame] | 713 | } |
Pablo Neira Ayuso | 5ca8cc5 | 2016-08-24 12:31:31 +0200 | [diff] [blame] | 714 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 715 | data = rht_obj(ht, head); |
| 716 | |
| 717 | if (!rhlist) |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 718 | goto out_unlock; |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 719 | |
| 720 | |
| 721 | list = container_of(obj, struct rhlist_head, rhead); |
| 722 | plist = container_of(head, struct rhlist_head, rhead); |
| 723 | |
| 724 | RCU_INIT_POINTER(list->next, plist); |
| 725 | head = rht_dereference_bucket(head->next, tbl, hash); |
| 726 | RCU_INIT_POINTER(list->rhead.next, head); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 727 | if (pprev) { |
| 728 | rcu_assign_pointer(*pprev, obj); |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 729 | rht_unlock(tbl, bkt); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 730 | } else |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 731 | rht_assign_unlock(tbl, bkt, obj); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 732 | data = NULL; |
| 733 | goto out; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 734 | } |
| 735 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 736 | if (elasticity <= 0) |
| 737 | goto slow_path; |
| 738 | |
| 739 | data = ERR_PTR(-E2BIG); |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 740 | if (unlikely(rht_grow_above_max(ht, tbl))) |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 741 | goto out_unlock; |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 742 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 743 | if (unlikely(rht_grow_above_100(ht, tbl))) |
| 744 | goto slow_path; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 745 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 746 | /* Inserting at head of list makes unlocking free. */ |
| 747 | head = rht_ptr(rht_dereference_bucket(*bkt, tbl, hash)); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 748 | |
| 749 | RCU_INIT_POINTER(obj->next, head); |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 750 | if (rhlist) { |
| 751 | struct rhlist_head *list; |
| 752 | |
| 753 | list = container_of(obj, struct rhlist_head, rhead); |
| 754 | RCU_INIT_POINTER(list->next, NULL); |
| 755 | } |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 756 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 757 | atomic_inc(&ht->nelems); |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 758 | rht_assign_unlock(tbl, bkt, obj); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 759 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 760 | if (rht_grow_above_75(ht, tbl)) |
| 761 | schedule_work(&ht->run_work); |
| 762 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 763 | data = NULL; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 764 | out: |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 765 | rcu_read_unlock(); |
| 766 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 767 | return data; |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 768 | |
| 769 | out_unlock: |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 770 | rht_unlock(tbl, bkt); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 771 | goto out; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /** |
| 775 | * rhashtable_insert_fast - insert object into hash table |
| 776 | * @ht: hash table |
| 777 | * @obj: pointer to hash head inside object |
| 778 | * @params: hash table parameters |
| 779 | * |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 780 | * Will take the per bucket bitlock to protect against mutual mutations |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 781 | * on the same bucket. Multiple insertions may occur in parallel unless |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 782 | * they map to the same bucket. |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 783 | * |
| 784 | * It is safe to call this function from atomic context. |
| 785 | * |
NeilBrown | 0c6f69a | 2018-04-24 08:29:13 +1000 | [diff] [blame] | 786 | * Will trigger an automatic deferred table resizing if residency in the |
| 787 | * table grows beyond 70%. |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 788 | */ |
| 789 | static inline int rhashtable_insert_fast( |
| 790 | struct rhashtable *ht, struct rhash_head *obj, |
| 791 | const struct rhashtable_params params) |
| 792 | { |
Pablo Neira Ayuso | 5ca8cc5 | 2016-08-24 12:31:31 +0200 | [diff] [blame] | 793 | void *ret; |
| 794 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 795 | ret = __rhashtable_insert_fast(ht, NULL, obj, params, false); |
Pablo Neira Ayuso | 5ca8cc5 | 2016-08-24 12:31:31 +0200 | [diff] [blame] | 796 | if (IS_ERR(ret)) |
| 797 | return PTR_ERR(ret); |
| 798 | |
| 799 | return ret == NULL ? 0 : -EEXIST; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /** |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 803 | * rhltable_insert_key - insert object into hash list table |
| 804 | * @hlt: hash list table |
| 805 | * @key: the pointer to the key |
| 806 | * @list: pointer to hash list head inside object |
| 807 | * @params: hash table parameters |
| 808 | * |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 809 | * Will take the per bucket bitlock to protect against mutual mutations |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 810 | * on the same bucket. Multiple insertions may occur in parallel unless |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 811 | * they map to the same bucket. |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 812 | * |
| 813 | * It is safe to call this function from atomic context. |
| 814 | * |
NeilBrown | 0c6f69a | 2018-04-24 08:29:13 +1000 | [diff] [blame] | 815 | * Will trigger an automatic deferred table resizing if residency in the |
| 816 | * table grows beyond 70%. |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 817 | */ |
| 818 | static inline int rhltable_insert_key( |
| 819 | struct rhltable *hlt, const void *key, struct rhlist_head *list, |
| 820 | const struct rhashtable_params params) |
| 821 | { |
| 822 | return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead, |
| 823 | params, true)); |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * rhltable_insert - insert object into hash list table |
| 828 | * @hlt: hash list table |
| 829 | * @list: pointer to hash list head inside object |
| 830 | * @params: hash table parameters |
| 831 | * |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 832 | * Will take the per bucket bitlock to protect against mutual mutations |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 833 | * on the same bucket. Multiple insertions may occur in parallel unless |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 834 | * they map to the same bucket. |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 835 | * |
| 836 | * It is safe to call this function from atomic context. |
| 837 | * |
NeilBrown | 0c6f69a | 2018-04-24 08:29:13 +1000 | [diff] [blame] | 838 | * Will trigger an automatic deferred table resizing if residency in the |
| 839 | * table grows beyond 70%. |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 840 | */ |
| 841 | static inline int rhltable_insert( |
| 842 | struct rhltable *hlt, struct rhlist_head *list, |
| 843 | const struct rhashtable_params params) |
| 844 | { |
| 845 | const char *key = rht_obj(&hlt->ht, &list->rhead); |
| 846 | |
| 847 | key += params.key_offset; |
| 848 | |
| 849 | return rhltable_insert_key(hlt, key, list, params); |
| 850 | } |
| 851 | |
| 852 | /** |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 853 | * rhashtable_lookup_insert_fast - lookup and insert object into hash table |
| 854 | * @ht: hash table |
| 855 | * @obj: pointer to hash head inside object |
| 856 | * @params: hash table parameters |
| 857 | * |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 858 | * This lookup function may only be used for fixed key hash table (key_len |
| 859 | * parameter set). It will BUG() if used inappropriately. |
| 860 | * |
| 861 | * It is safe to call this function from atomic context. |
| 862 | * |
NeilBrown | 0c6f69a | 2018-04-24 08:29:13 +1000 | [diff] [blame] | 863 | * Will trigger an automatic deferred table resizing if residency in the |
| 864 | * table grows beyond 70%. |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 865 | */ |
| 866 | static inline int rhashtable_lookup_insert_fast( |
| 867 | struct rhashtable *ht, struct rhash_head *obj, |
| 868 | const struct rhashtable_params params) |
| 869 | { |
| 870 | const char *key = rht_obj(ht, obj); |
Pablo Neira Ayuso | 5ca8cc5 | 2016-08-24 12:31:31 +0200 | [diff] [blame] | 871 | void *ret; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 872 | |
| 873 | BUG_ON(ht->p.obj_hashfn); |
| 874 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 875 | ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params, |
| 876 | false); |
Pablo Neira Ayuso | 5ca8cc5 | 2016-08-24 12:31:31 +0200 | [diff] [blame] | 877 | if (IS_ERR(ret)) |
| 878 | return PTR_ERR(ret); |
| 879 | |
| 880 | return ret == NULL ? 0 : -EEXIST; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /** |
Andreas Gruenbacher | f9fe1c1 | 2017-03-18 00:36:15 +0100 | [diff] [blame] | 884 | * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table |
| 885 | * @ht: hash table |
| 886 | * @obj: pointer to hash head inside object |
| 887 | * @params: hash table parameters |
| 888 | * |
| 889 | * Just like rhashtable_lookup_insert_fast(), but this function returns the |
| 890 | * object if it exists, NULL if it did not and the insertion was successful, |
| 891 | * and an ERR_PTR otherwise. |
| 892 | */ |
| 893 | static inline void *rhashtable_lookup_get_insert_fast( |
| 894 | struct rhashtable *ht, struct rhash_head *obj, |
| 895 | const struct rhashtable_params params) |
| 896 | { |
| 897 | const char *key = rht_obj(ht, obj); |
| 898 | |
| 899 | BUG_ON(ht->p.obj_hashfn); |
| 900 | |
| 901 | return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params, |
| 902 | false); |
| 903 | } |
| 904 | |
| 905 | /** |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 906 | * rhashtable_lookup_insert_key - search and insert object to hash table |
| 907 | * with explicit key |
| 908 | * @ht: hash table |
| 909 | * @key: key |
| 910 | * @obj: pointer to hash head inside object |
| 911 | * @params: hash table parameters |
| 912 | * |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 913 | * Lookups may occur in parallel with hashtable mutations and resizing. |
| 914 | * |
NeilBrown | 0c6f69a | 2018-04-24 08:29:13 +1000 | [diff] [blame] | 915 | * Will trigger an automatic deferred table resizing if residency in the |
| 916 | * table grows beyond 70%. |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 917 | * |
| 918 | * Returns zero on success. |
| 919 | */ |
| 920 | static inline int rhashtable_lookup_insert_key( |
| 921 | struct rhashtable *ht, const void *key, struct rhash_head *obj, |
| 922 | const struct rhashtable_params params) |
| 923 | { |
Pablo Neira Ayuso | 5ca8cc5 | 2016-08-24 12:31:31 +0200 | [diff] [blame] | 924 | void *ret; |
| 925 | |
| 926 | BUG_ON(!ht->p.obj_hashfn || !key); |
| 927 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 928 | ret = __rhashtable_insert_fast(ht, key, obj, params, false); |
Pablo Neira Ayuso | 5ca8cc5 | 2016-08-24 12:31:31 +0200 | [diff] [blame] | 929 | if (IS_ERR(ret)) |
| 930 | return PTR_ERR(ret); |
| 931 | |
| 932 | return ret == NULL ? 0 : -EEXIST; |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * rhashtable_lookup_get_insert_key - lookup and insert object into hash table |
| 937 | * @ht: hash table |
| 938 | * @obj: pointer to hash head inside object |
| 939 | * @params: hash table parameters |
| 940 | * @data: pointer to element data already in hashes |
| 941 | * |
| 942 | * Just like rhashtable_lookup_insert_key(), but this function returns the |
| 943 | * object if it exists, NULL if it does not and the insertion was successful, |
| 944 | * and an ERR_PTR otherwise. |
| 945 | */ |
| 946 | static inline void *rhashtable_lookup_get_insert_key( |
| 947 | struct rhashtable *ht, const void *key, struct rhash_head *obj, |
| 948 | const struct rhashtable_params params) |
| 949 | { |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 950 | BUG_ON(!ht->p.obj_hashfn || !key); |
| 951 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 952 | return __rhashtable_insert_fast(ht, key, obj, params, false); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 953 | } |
| 954 | |
Thomas Graf | ac833bd | 2015-03-24 14:18:18 +0100 | [diff] [blame] | 955 | /* Internal function, please use rhashtable_remove_fast() instead */ |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 956 | static inline int __rhashtable_remove_fast_one( |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 957 | struct rhashtable *ht, struct bucket_table *tbl, |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 958 | struct rhash_head *obj, const struct rhashtable_params params, |
| 959 | bool rhlist) |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 960 | { |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 961 | struct rhash_lock_head __rcu **bkt; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 962 | struct rhash_head __rcu **pprev; |
| 963 | struct rhash_head *he; |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 964 | unsigned int hash; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 965 | int err = -ENOENT; |
| 966 | |
| 967 | hash = rht_head_hashfn(ht, tbl, obj, params); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 968 | bkt = rht_bucket_var(tbl, hash); |
| 969 | if (!bkt) |
| 970 | return -ENOENT; |
| 971 | pprev = NULL; |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 972 | rht_lock(tbl, bkt); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 973 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 974 | rht_for_each_from(he, rht_ptr(*bkt), tbl, hash) { |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 975 | struct rhlist_head *list; |
| 976 | |
| 977 | list = container_of(he, struct rhlist_head, rhead); |
| 978 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 979 | if (he != obj) { |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 980 | struct rhlist_head __rcu **lpprev; |
| 981 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 982 | pprev = &he->next; |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 983 | |
| 984 | if (!rhlist) |
| 985 | continue; |
| 986 | |
| 987 | do { |
| 988 | lpprev = &list->next; |
| 989 | list = rht_dereference_bucket(list->next, |
| 990 | tbl, hash); |
| 991 | } while (list && obj != &list->rhead); |
| 992 | |
| 993 | if (!list) |
| 994 | continue; |
| 995 | |
| 996 | list = rht_dereference_bucket(list->next, tbl, hash); |
| 997 | RCU_INIT_POINTER(*lpprev, list); |
| 998 | err = 0; |
| 999 | break; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 1000 | } |
| 1001 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 1002 | obj = rht_dereference_bucket(obj->next, tbl, hash); |
| 1003 | err = 1; |
| 1004 | |
| 1005 | if (rhlist) { |
| 1006 | list = rht_dereference_bucket(list->next, tbl, hash); |
| 1007 | if (list) { |
| 1008 | RCU_INIT_POINTER(list->rhead.next, obj); |
| 1009 | obj = &list->rhead; |
| 1010 | err = 0; |
| 1011 | } |
| 1012 | } |
| 1013 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1014 | if (pprev) { |
| 1015 | rcu_assign_pointer(*pprev, obj); |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1016 | rht_unlock(tbl, bkt); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1017 | } else { |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1018 | rht_assign_unlock(tbl, bkt, obj); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1019 | } |
| 1020 | goto unlocked; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 1021 | } |
| 1022 | |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1023 | rht_unlock(tbl, bkt); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1024 | unlocked: |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 1025 | if (err > 0) { |
| 1026 | atomic_dec(&ht->nelems); |
| 1027 | if (unlikely(ht->p.automatic_shrinking && |
| 1028 | rht_shrink_below_30(ht, tbl))) |
| 1029 | schedule_work(&ht->run_work); |
| 1030 | err = 0; |
| 1031 | } |
| 1032 | |
| 1033 | return err; |
| 1034 | } |
| 1035 | |
| 1036 | /* Internal function, please use rhashtable_remove_fast() instead */ |
| 1037 | static inline int __rhashtable_remove_fast( |
| 1038 | struct rhashtable *ht, struct rhash_head *obj, |
| 1039 | const struct rhashtable_params params, bool rhlist) |
| 1040 | { |
| 1041 | struct bucket_table *tbl; |
| 1042 | int err; |
| 1043 | |
| 1044 | rcu_read_lock(); |
| 1045 | |
| 1046 | tbl = rht_dereference_rcu(ht->tbl, ht); |
| 1047 | |
| 1048 | /* Because we have already taken (and released) the bucket |
| 1049 | * lock in old_tbl, if we find that future_tbl is not yet |
| 1050 | * visible then that guarantees the entry to still be in |
| 1051 | * the old tbl if it exists. |
| 1052 | */ |
| 1053 | while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params, |
| 1054 | rhlist)) && |
| 1055 | (tbl = rht_dereference_rcu(tbl->future_tbl, ht))) |
| 1056 | ; |
| 1057 | |
| 1058 | rcu_read_unlock(); |
| 1059 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 1060 | return err; |
| 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * rhashtable_remove_fast - remove object from hash table |
| 1065 | * @ht: hash table |
| 1066 | * @obj: pointer to hash head inside object |
| 1067 | * @params: hash table parameters |
| 1068 | * |
| 1069 | * Since the hash chain is single linked, the removal operation needs to |
| 1070 | * walk the bucket chain upon removal. The removal operation is thus |
| 1071 | * considerable slow if the hash table is not correctly sized. |
| 1072 | * |
NeilBrown | 0c6f69a | 2018-04-24 08:29:13 +1000 | [diff] [blame] | 1073 | * Will automatically shrink the table if permitted when residency drops |
| 1074 | * below 30%. |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 1075 | * |
| 1076 | * Returns zero on success, -ENOENT if the entry could not be found. |
| 1077 | */ |
| 1078 | static inline int rhashtable_remove_fast( |
| 1079 | struct rhashtable *ht, struct rhash_head *obj, |
| 1080 | const struct rhashtable_params params) |
| 1081 | { |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 1082 | return __rhashtable_remove_fast(ht, obj, params, false); |
| 1083 | } |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 1084 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 1085 | /** |
| 1086 | * rhltable_remove - remove object from hash list table |
| 1087 | * @hlt: hash list table |
| 1088 | * @list: pointer to hash list head inside object |
| 1089 | * @params: hash table parameters |
| 1090 | * |
| 1091 | * Since the hash chain is single linked, the removal operation needs to |
| 1092 | * walk the bucket chain upon removal. The removal operation is thus |
| 1093 | * considerable slow if the hash table is not correctly sized. |
| 1094 | * |
NeilBrown | 0c6f69a | 2018-04-24 08:29:13 +1000 | [diff] [blame] | 1095 | * Will automatically shrink the table if permitted when residency drops |
| 1096 | * below 30% |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 1097 | * |
| 1098 | * Returns zero on success, -ENOENT if the entry could not be found. |
| 1099 | */ |
| 1100 | static inline int rhltable_remove( |
| 1101 | struct rhltable *hlt, struct rhlist_head *list, |
| 1102 | const struct rhashtable_params params) |
| 1103 | { |
| 1104 | return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 1105 | } |
| 1106 | |
Tom Herbert | 3502cad | 2015-12-15 15:41:36 -0800 | [diff] [blame] | 1107 | /* Internal function, please use rhashtable_replace_fast() instead */ |
| 1108 | static inline int __rhashtable_replace_fast( |
| 1109 | struct rhashtable *ht, struct bucket_table *tbl, |
| 1110 | struct rhash_head *obj_old, struct rhash_head *obj_new, |
| 1111 | const struct rhashtable_params params) |
| 1112 | { |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1113 | struct rhash_lock_head __rcu **bkt; |
Tom Herbert | 3502cad | 2015-12-15 15:41:36 -0800 | [diff] [blame] | 1114 | struct rhash_head __rcu **pprev; |
| 1115 | struct rhash_head *he; |
Tom Herbert | 3502cad | 2015-12-15 15:41:36 -0800 | [diff] [blame] | 1116 | unsigned int hash; |
| 1117 | int err = -ENOENT; |
| 1118 | |
| 1119 | /* Minimally, the old and new objects must have same hash |
| 1120 | * (which should mean identifiers are the same). |
| 1121 | */ |
| 1122 | hash = rht_head_hashfn(ht, tbl, obj_old, params); |
| 1123 | if (hash != rht_head_hashfn(ht, tbl, obj_new, params)) |
| 1124 | return -EINVAL; |
| 1125 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1126 | bkt = rht_bucket_var(tbl, hash); |
| 1127 | if (!bkt) |
| 1128 | return -ENOENT; |
Tom Herbert | 3502cad | 2015-12-15 15:41:36 -0800 | [diff] [blame] | 1129 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1130 | pprev = NULL; |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1131 | rht_lock(tbl, bkt); |
Tom Herbert | 3502cad | 2015-12-15 15:41:36 -0800 | [diff] [blame] | 1132 | |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1133 | rht_for_each_from(he, rht_ptr(*bkt), tbl, hash) { |
Tom Herbert | 3502cad | 2015-12-15 15:41:36 -0800 | [diff] [blame] | 1134 | if (he != obj_old) { |
| 1135 | pprev = &he->next; |
| 1136 | continue; |
| 1137 | } |
| 1138 | |
| 1139 | rcu_assign_pointer(obj_new->next, obj_old->next); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1140 | if (pprev) { |
| 1141 | rcu_assign_pointer(*pprev, obj_new); |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1142 | rht_unlock(tbl, bkt); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1143 | } else { |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1144 | rht_assign_unlock(tbl, bkt, obj_new); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1145 | } |
Tom Herbert | 3502cad | 2015-12-15 15:41:36 -0800 | [diff] [blame] | 1146 | err = 0; |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1147 | goto unlocked; |
Tom Herbert | 3502cad | 2015-12-15 15:41:36 -0800 | [diff] [blame] | 1148 | } |
Tom Herbert | 3502cad | 2015-12-15 15:41:36 -0800 | [diff] [blame] | 1149 | |
NeilBrown | 149212f | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1150 | rht_unlock(tbl, bkt); |
NeilBrown | 8f0db01 | 2019-04-02 10:07:45 +1100 | [diff] [blame] | 1151 | |
| 1152 | unlocked: |
Tom Herbert | 3502cad | 2015-12-15 15:41:36 -0800 | [diff] [blame] | 1153 | return err; |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * rhashtable_replace_fast - replace an object in hash table |
| 1158 | * @ht: hash table |
| 1159 | * @obj_old: pointer to hash head inside object being replaced |
| 1160 | * @obj_new: pointer to hash head inside object which is new |
| 1161 | * @params: hash table parameters |
| 1162 | * |
| 1163 | * Replacing an object doesn't affect the number of elements in the hash table |
| 1164 | * or bucket, so we don't need to worry about shrinking or expanding the |
| 1165 | * table here. |
| 1166 | * |
| 1167 | * Returns zero on success, -ENOENT if the entry could not be found, |
| 1168 | * -EINVAL if hash is not the same for the old and new objects. |
| 1169 | */ |
| 1170 | static inline int rhashtable_replace_fast( |
| 1171 | struct rhashtable *ht, struct rhash_head *obj_old, |
| 1172 | struct rhash_head *obj_new, |
| 1173 | const struct rhashtable_params params) |
| 1174 | { |
| 1175 | struct bucket_table *tbl; |
| 1176 | int err; |
| 1177 | |
| 1178 | rcu_read_lock(); |
| 1179 | |
| 1180 | tbl = rht_dereference_rcu(ht->tbl, ht); |
| 1181 | |
| 1182 | /* Because we have already taken (and released) the bucket |
| 1183 | * lock in old_tbl, if we find that future_tbl is not yet |
| 1184 | * visible then that guarantees the entry to still be in |
| 1185 | * the old tbl if it exists. |
| 1186 | */ |
| 1187 | while ((err = __rhashtable_replace_fast(ht, tbl, obj_old, |
| 1188 | obj_new, params)) && |
| 1189 | (tbl = rht_dereference_rcu(tbl->future_tbl, ht))) |
| 1190 | ; |
| 1191 | |
| 1192 | rcu_read_unlock(); |
| 1193 | |
| 1194 | return err; |
| 1195 | } |
| 1196 | |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 1197 | /** |
| 1198 | * rhltable_walk_enter - Initialise an iterator |
| 1199 | * @hlt: Table to walk over |
| 1200 | * @iter: Hash table Iterator |
| 1201 | * |
| 1202 | * This function prepares a hash table walk. |
| 1203 | * |
| 1204 | * Note that if you restart a walk after rhashtable_walk_stop you |
| 1205 | * may see the same object twice. Also, you may miss objects if |
| 1206 | * there are removals in between rhashtable_walk_stop and the next |
| 1207 | * call to rhashtable_walk_start. |
| 1208 | * |
| 1209 | * For a completely stable walk you should construct your own data |
| 1210 | * structure outside the hash table. |
| 1211 | * |
NeilBrown | 82266e9 | 2018-04-24 08:29:13 +1000 | [diff] [blame] | 1212 | * This function may be called from any process context, including |
| 1213 | * non-preemptable context, but cannot be called from softirq or |
| 1214 | * hardirq context. |
Herbert Xu | ca26893 | 2016-09-19 19:00:09 +0800 | [diff] [blame] | 1215 | * |
| 1216 | * You must call rhashtable_walk_exit after this function returns. |
| 1217 | */ |
| 1218 | static inline void rhltable_walk_enter(struct rhltable *hlt, |
| 1219 | struct rhashtable_iter *iter) |
| 1220 | { |
| 1221 | return rhashtable_walk_enter(&hlt->ht, iter); |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * rhltable_free_and_destroy - free elements and destroy hash list table |
| 1226 | * @hlt: the hash list table to destroy |
| 1227 | * @free_fn: callback to release resources of element |
| 1228 | * @arg: pointer passed to free_fn |
| 1229 | * |
| 1230 | * See documentation for rhashtable_free_and_destroy. |
| 1231 | */ |
| 1232 | static inline void rhltable_free_and_destroy(struct rhltable *hlt, |
| 1233 | void (*free_fn)(void *ptr, |
| 1234 | void *arg), |
| 1235 | void *arg) |
| 1236 | { |
| 1237 | return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg); |
| 1238 | } |
| 1239 | |
| 1240 | static inline void rhltable_destroy(struct rhltable *hlt) |
| 1241 | { |
| 1242 | return rhltable_free_and_destroy(hlt, NULL, NULL); |
| 1243 | } |
| 1244 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1245 | #endif /* _LINUX_RHASHTABLE_H */ |