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