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