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