Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Resizable, Scalable, Concurrent Hash Table |
| 3 | * |
Herbert Xu | dc0ee26 | 2015-03-20 21:57:06 +1100 | [diff] [blame] | 4 | * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au> |
Thomas Graf | b5e2c15 | 2015-03-24 20:42:19 +0000 | [diff] [blame] | 5 | * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 6 | * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
| 7 | * |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 8 | * Code partially derived from nft_hash |
Herbert Xu | dc0ee26 | 2015-03-20 21:57:06 +1100 | [diff] [blame] | 9 | * Rewritten with rehash code from br_multicast plus single list |
| 10 | * pointer as suggested by Josh Triplett |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _LINUX_RHASHTABLE_H |
| 18 | #define _LINUX_RHASHTABLE_H |
| 19 | |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 20 | #include <linux/atomic.h> |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 21 | #include <linux/compiler.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> |
Ying Xue | 86b35b6 | 2015-01-04 15:25:09 +0800 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 27 | #include <linux/rcupdate.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 28 | |
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 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 51 | struct rhash_head { |
Thomas Graf | 5300fdc | 2014-08-13 16:38:29 +0200 | [diff] [blame] | 52 | struct rhash_head __rcu *next; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 55 | /** |
| 56 | * struct bucket_table - Table of hash buckets |
| 57 | * @size: Number of hash buckets |
Herbert Xu | 63d512d | 2015-03-14 13:57:24 +1100 | [diff] [blame] | 58 | * @rehash: Current bucket being rehashed |
Herbert Xu | 988dfbd | 2015-03-10 09:27:55 +1100 | [diff] [blame] | 59 | * @hash_rnd: Random seed to fold into hash |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 60 | * @locks_mask: Mask to apply before accessing locks[] |
| 61 | * @locks: Array of spinlocks protecting individual buckets |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 62 | * @walkers: List of active walkers |
Herbert Xu | 9d901bc | 2015-03-14 13:57:23 +1100 | [diff] [blame] | 63 | * @rcu: RCU structure for freeing the table |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 64 | * @future_tbl: Table under construction during rehashing |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 65 | * @buckets: size * hash buckets |
| 66 | */ |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 67 | struct bucket_table { |
Herbert Xu | 63d512d | 2015-03-14 13:57:24 +1100 | [diff] [blame] | 68 | unsigned int size; |
| 69 | unsigned int rehash; |
Herbert Xu | 988dfbd | 2015-03-10 09:27:55 +1100 | [diff] [blame] | 70 | u32 hash_rnd; |
Eric Dumazet | b9ebafb | 2015-02-20 06:48:57 -0800 | [diff] [blame] | 71 | unsigned int locks_mask; |
| 72 | spinlock_t *locks; |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 73 | struct list_head walkers; |
Herbert Xu | 9d901bc | 2015-03-14 13:57:23 +1100 | [diff] [blame] | 74 | struct rcu_head rcu; |
Eric Dumazet | b9ebafb | 2015-02-20 06:48:57 -0800 | [diff] [blame] | 75 | |
Herbert Xu | c4db884 | 2015-03-14 13:57:25 +1100 | [diff] [blame] | 76 | struct bucket_table __rcu *future_tbl; |
| 77 | |
Eric Dumazet | b9ebafb | 2015-02-20 06:48:57 -0800 | [diff] [blame] | 78 | struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 81 | /** |
| 82 | * struct rhashtable_compare_arg - Key for the function rhashtable_compare |
| 83 | * @ht: Hash table |
| 84 | * @key: Key to compare against |
| 85 | */ |
| 86 | struct rhashtable_compare_arg { |
| 87 | struct rhashtable *ht; |
| 88 | const void *key; |
| 89 | }; |
| 90 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 91 | typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed); |
Patrick McHardy | 49f7b33 | 2015-03-25 13:07:45 +0000 | [diff] [blame] | 92 | typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 93 | typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg, |
| 94 | const void *obj); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 95 | |
| 96 | struct rhashtable; |
| 97 | |
| 98 | /** |
| 99 | * struct rhashtable_params - Hash table construction parameters |
| 100 | * @nelem_hint: Hint on number of elements, should be 75% of desired size |
| 101 | * @key_len: Length of key |
| 102 | * @key_offset: Offset of key in struct to be hashed |
| 103 | * @head_offset: Offset of rhash_head in struct to be hashed |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 104 | * @insecure_max_entries: Maximum number of entries (may be exceeded) |
Herbert Xu | c2e213c | 2015-03-18 20:01:16 +1100 | [diff] [blame] | 105 | * @max_size: Maximum size while expanding |
| 106 | * @min_size: Minimum size while shrinking |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 107 | * @nulls_base: Base value to generate nulls marker |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 108 | * @insecure_elasticity: Set to true to disable chain length checks |
Thomas Graf | b5e2c15 | 2015-03-24 20:42:19 +0000 | [diff] [blame] | 109 | * @automatic_shrinking: Enable automatic shrinking of tables |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 110 | * @locks_mul: Number of bucket locks to allocate per cpu (default: 128) |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 111 | * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 112 | * @obj_hashfn: Function to hash object |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 113 | * @obj_cmpfn: Function to compare key with object |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 114 | */ |
| 115 | struct rhashtable_params { |
| 116 | size_t nelem_hint; |
| 117 | size_t key_len; |
| 118 | size_t key_offset; |
| 119 | size_t head_offset; |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 120 | unsigned int insecure_max_entries; |
Herbert Xu | c2e213c | 2015-03-18 20:01:16 +1100 | [diff] [blame] | 121 | unsigned int max_size; |
| 122 | unsigned int min_size; |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 123 | u32 nulls_base; |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 124 | bool insecure_elasticity; |
Thomas Graf | b5e2c15 | 2015-03-24 20:42:19 +0000 | [diff] [blame] | 125 | bool automatic_shrinking; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 126 | size_t locks_mul; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 127 | rht_hashfn_t hashfn; |
| 128 | rht_obj_hashfn_t obj_hashfn; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 129 | rht_obj_cmpfn_t obj_cmpfn; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | /** |
| 133 | * struct rhashtable - Hash table handle |
| 134 | * @tbl: Bucket table |
| 135 | * @nelems: Number of elements in table |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 136 | * @key_len: Key length for hashfn |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 137 | * @elasticity: Maximum chain length before rehash |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 138 | * @p: Configuration parameters |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 139 | * @run_work: Deferred worker to expand/shrink asynchronously |
| 140 | * @mutex: Mutex to protect current/future table swapping |
Herbert Xu | ba7c95e | 2015-03-24 09:53:17 +1100 | [diff] [blame] | 141 | * @lock: Spin lock to protect walker list |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 142 | */ |
| 143 | struct rhashtable { |
| 144 | struct bucket_table __rcu *tbl; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 145 | atomic_t nelems; |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 146 | unsigned int key_len; |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 147 | unsigned int elasticity; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 148 | struct rhashtable_params p; |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 149 | struct work_struct run_work; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 150 | struct mutex mutex; |
Herbert Xu | ba7c95e | 2015-03-24 09:53:17 +1100 | [diff] [blame] | 151 | spinlock_t lock; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 152 | }; |
| 153 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 154 | /** |
| 155 | * struct rhashtable_walker - Hash table walker |
| 156 | * @list: List entry on list of walkers |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 157 | * @tbl: The table that we were walking over |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 158 | */ |
| 159 | struct rhashtable_walker { |
| 160 | struct list_head list; |
Herbert Xu | eddee5ba | 2015-03-14 13:57:20 +1100 | [diff] [blame] | 161 | struct bucket_table *tbl; |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | /** |
| 165 | * struct rhashtable_iter - Hash table iterator, fits into netlink cb |
| 166 | * @ht: Table to iterate through |
| 167 | * @p: Current pointer |
| 168 | * @walker: Associated rhashtable walker |
| 169 | * @slot: Current slot |
| 170 | * @skip: Number of entries to skip in slot |
| 171 | */ |
| 172 | struct rhashtable_iter { |
| 173 | struct rhashtable *ht; |
| 174 | struct rhash_head *p; |
| 175 | struct rhashtable_walker *walker; |
| 176 | unsigned int slot; |
| 177 | unsigned int skip; |
| 178 | }; |
| 179 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 180 | static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash) |
| 181 | { |
| 182 | return NULLS_MARKER(ht->p.nulls_base + hash); |
| 183 | } |
| 184 | |
| 185 | #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \ |
| 186 | ((ptr) = (typeof(ptr)) rht_marker(ht, hash)) |
| 187 | |
| 188 | static inline bool rht_is_a_nulls(const struct rhash_head *ptr) |
| 189 | { |
| 190 | return ((unsigned long) ptr & 1); |
| 191 | } |
| 192 | |
| 193 | static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr) |
| 194 | { |
| 195 | return ((unsigned long) ptr) >> 1; |
| 196 | } |
| 197 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 198 | static inline void *rht_obj(const struct rhashtable *ht, |
| 199 | const struct rhash_head *he) |
| 200 | { |
| 201 | return (char *)he - ht->p.head_offset; |
| 202 | } |
| 203 | |
| 204 | static inline unsigned int rht_bucket_index(const struct bucket_table *tbl, |
| 205 | unsigned int hash) |
| 206 | { |
| 207 | return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1); |
| 208 | } |
| 209 | |
| 210 | static inline unsigned int rht_key_hashfn( |
| 211 | struct rhashtable *ht, const struct bucket_table *tbl, |
| 212 | const void *key, const struct rhashtable_params params) |
| 213 | { |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 214 | unsigned int hash; |
Herbert Xu | de91b25 | 2015-03-24 00:50:20 +1100 | [diff] [blame] | 215 | |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 216 | /* params must be equal to ht->p if it isn't constant. */ |
| 217 | if (!__builtin_constant_p(params.key_len)) |
| 218 | hash = ht->p.hashfn(key, ht->key_len, tbl->hash_rnd); |
| 219 | else if (params.key_len) { |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 220 | unsigned int key_len = params.key_len; |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 221 | |
| 222 | if (params.hashfn) |
| 223 | hash = params.hashfn(key, key_len, tbl->hash_rnd); |
| 224 | else if (key_len & (sizeof(u32) - 1)) |
| 225 | hash = jhash(key, key_len, tbl->hash_rnd); |
| 226 | else |
| 227 | hash = jhash2(key, key_len / sizeof(u32), |
| 228 | tbl->hash_rnd); |
| 229 | } else { |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 230 | unsigned int key_len = ht->p.key_len; |
Herbert Xu | 31ccde2 | 2015-03-24 00:50:21 +1100 | [diff] [blame] | 231 | |
| 232 | if (params.hashfn) |
| 233 | hash = params.hashfn(key, key_len, tbl->hash_rnd); |
| 234 | else |
| 235 | hash = jhash(key, key_len, tbl->hash_rnd); |
| 236 | } |
| 237 | |
| 238 | return rht_bucket_index(tbl, hash); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static inline unsigned int rht_head_hashfn( |
| 242 | struct rhashtable *ht, const struct bucket_table *tbl, |
| 243 | const struct rhash_head *he, const struct rhashtable_params params) |
| 244 | { |
| 245 | const char *ptr = rht_obj(ht, he); |
| 246 | |
| 247 | return likely(params.obj_hashfn) ? |
Patrick McHardy | 49f7b33 | 2015-03-25 13:07:45 +0000 | [diff] [blame] | 248 | rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?: |
| 249 | ht->p.key_len, |
| 250 | tbl->hash_rnd)) : |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 251 | rht_key_hashfn(ht, tbl, ptr + params.key_offset, params); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * rht_grow_above_75 - returns true if nelems > 0.75 * table-size |
| 256 | * @ht: hash table |
| 257 | * @tbl: current table |
| 258 | */ |
| 259 | static inline bool rht_grow_above_75(const struct rhashtable *ht, |
| 260 | const struct bucket_table *tbl) |
| 261 | { |
| 262 | /* Expand table when exceeding 75% load */ |
| 263 | return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) && |
| 264 | (!ht->p.max_size || tbl->size < ht->p.max_size); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size |
| 269 | * @ht: hash table |
| 270 | * @tbl: current table |
| 271 | */ |
| 272 | static inline bool rht_shrink_below_30(const struct rhashtable *ht, |
| 273 | const struct bucket_table *tbl) |
| 274 | { |
| 275 | /* Shrink table beneath 30% load */ |
| 276 | return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) && |
| 277 | tbl->size > ht->p.min_size; |
| 278 | } |
| 279 | |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 280 | /** |
| 281 | * rht_grow_above_100 - returns true if nelems > table-size |
| 282 | * @ht: hash table |
| 283 | * @tbl: current table |
| 284 | */ |
| 285 | static inline bool rht_grow_above_100(const struct rhashtable *ht, |
| 286 | const struct bucket_table *tbl) |
| 287 | { |
Johannes Berg | 1d8dc3d | 2015-04-23 16:38:43 +0200 | [diff] [blame] | 288 | return atomic_read(&ht->nelems) > tbl->size && |
| 289 | (!ht->p.max_size || tbl->size < ht->p.max_size); |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 290 | } |
| 291 | |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 292 | /** |
| 293 | * rht_grow_above_max - returns true if table is above maximum |
| 294 | * @ht: hash table |
| 295 | * @tbl: current table |
| 296 | */ |
| 297 | static inline bool rht_grow_above_max(const struct rhashtable *ht, |
| 298 | const struct bucket_table *tbl) |
| 299 | { |
| 300 | return ht->p.insecure_max_entries && |
| 301 | atomic_read(&ht->nelems) >= ht->p.insecure_max_entries; |
| 302 | } |
| 303 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 304 | /* The bucket lock is selected based on the hash and protects mutations |
| 305 | * on a group of hash buckets. |
| 306 | * |
| 307 | * A maximum of tbl->size/2 bucket locks is allocated. This ensures that |
| 308 | * a single lock always covers both buckets which may both contains |
| 309 | * entries which link to the same bucket of the old table during resizing. |
| 310 | * This allows to simplify the locking as locking the bucket in both |
| 311 | * tables during resize always guarantee protection. |
| 312 | * |
| 313 | * IMPORTANT: When holding the bucket lock of both the old and new table |
| 314 | * during expansions and shrinking, the old bucket lock must always be |
| 315 | * acquired first. |
| 316 | */ |
| 317 | static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl, |
| 318 | unsigned int hash) |
| 319 | { |
| 320 | return &tbl->locks[hash & tbl->locks_mask]; |
| 321 | } |
| 322 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 323 | #ifdef CONFIG_PROVE_LOCKING |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 324 | int lockdep_rht_mutex_is_held(struct rhashtable *ht); |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 325 | 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] | 326 | #else |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 327 | static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 328 | { |
| 329 | return 1; |
| 330 | } |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 331 | |
| 332 | static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, |
| 333 | u32 hash) |
| 334 | { |
| 335 | return 1; |
| 336 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 337 | #endif /* CONFIG_PROVE_LOCKING */ |
| 338 | |
Herbert Xu | 488fb86e | 2015-03-20 21:56:59 +1100 | [diff] [blame] | 339 | int rhashtable_init(struct rhashtable *ht, |
| 340 | const struct rhashtable_params *params); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 341 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 342 | int rhashtable_insert_slow(struct rhashtable *ht, const void *key, |
| 343 | struct rhash_head *obj, |
| 344 | struct bucket_table *old_tbl); |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 345 | int rhashtable_insert_rehash(struct rhashtable *ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 346 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame] | 347 | int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter); |
| 348 | void rhashtable_walk_exit(struct rhashtable_iter *iter); |
| 349 | int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU); |
| 350 | void *rhashtable_walk_next(struct rhashtable_iter *iter); |
| 351 | void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); |
| 352 | |
Thomas Graf | 6b6f302 | 2015-03-24 14:18:20 +0100 | [diff] [blame] | 353 | void rhashtable_free_and_destroy(struct rhashtable *ht, |
| 354 | void (*free_fn)(void *ptr, void *arg), |
| 355 | void *arg); |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 356 | void rhashtable_destroy(struct rhashtable *ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 357 | |
| 358 | #define rht_dereference(p, ht) \ |
| 359 | rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht)) |
| 360 | |
| 361 | #define rht_dereference_rcu(p, ht) \ |
| 362 | rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht)) |
| 363 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 364 | #define rht_dereference_bucket(p, tbl, hash) \ |
| 365 | rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 366 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 367 | #define rht_dereference_bucket_rcu(p, tbl, hash) \ |
| 368 | rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash)) |
| 369 | |
| 370 | #define rht_entry(tpos, pos, member) \ |
| 371 | ({ tpos = container_of(pos, typeof(*tpos), member); 1; }) |
| 372 | |
| 373 | /** |
| 374 | * rht_for_each_continue - continue iterating over hash chain |
| 375 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 376 | * @head: the previous &struct rhash_head to continue from |
| 377 | * @tbl: the &struct bucket_table |
| 378 | * @hash: the hash value / bucket index |
| 379 | */ |
| 380 | #define rht_for_each_continue(pos, head, tbl, hash) \ |
| 381 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 382 | !rht_is_a_nulls(pos); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 383 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 384 | |
| 385 | /** |
| 386 | * rht_for_each - iterate over hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 387 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 388 | * @tbl: the &struct bucket_table |
| 389 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 390 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 391 | #define rht_for_each(pos, tbl, hash) \ |
| 392 | rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash) |
| 393 | |
| 394 | /** |
| 395 | * rht_for_each_entry_continue - continue iterating over hash chain |
| 396 | * @tpos: the type * to use as a loop cursor. |
| 397 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 398 | * @head: the previous &struct rhash_head to continue from |
| 399 | * @tbl: the &struct bucket_table |
| 400 | * @hash: the hash value / bucket index |
| 401 | * @member: name of the &struct rhash_head within the hashable struct. |
| 402 | */ |
| 403 | #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \ |
| 404 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 405 | (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 406 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 407 | |
| 408 | /** |
| 409 | * rht_for_each_entry - iterate over hash chain of given type |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 410 | * @tpos: the type * to use as a loop cursor. |
| 411 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 412 | * @tbl: the &struct bucket_table |
| 413 | * @hash: the hash value / bucket index |
| 414 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 415 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 416 | #define rht_for_each_entry(tpos, pos, tbl, hash, member) \ |
| 417 | rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \ |
| 418 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 419 | |
| 420 | /** |
| 421 | * 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] | 422 | * @tpos: the type * to use as a loop cursor. |
| 423 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 424 | * @next: the &struct rhash_head to use as next in loop cursor. |
| 425 | * @tbl: the &struct bucket_table |
| 426 | * @hash: the hash value / bucket index |
| 427 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 428 | * |
| 429 | * This hash chain list-traversal primitive allows for the looped code to |
| 430 | * remove the loop cursor from the list. |
| 431 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 432 | #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \ |
| 433 | for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 434 | next = !rht_is_a_nulls(pos) ? \ |
| 435 | rht_dereference_bucket(pos->next, tbl, hash) : NULL; \ |
| 436 | (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ |
Patrick McHardy | 607954b | 2015-01-21 11:12:13 +0000 | [diff] [blame] | 437 | pos = next, \ |
| 438 | next = !rht_is_a_nulls(pos) ? \ |
| 439 | rht_dereference_bucket(pos->next, tbl, hash) : NULL) |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 440 | |
| 441 | /** |
| 442 | * rht_for_each_rcu_continue - continue iterating over rcu hash chain |
| 443 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 444 | * @head: the previous &struct rhash_head to continue from |
| 445 | * @tbl: the &struct bucket_table |
| 446 | * @hash: the hash value / bucket index |
| 447 | * |
| 448 | * This hash chain list-traversal primitive may safely run concurrently with |
| 449 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 450 | * traversal is guarded by rcu_read_lock(). |
| 451 | */ |
| 452 | #define rht_for_each_rcu_continue(pos, head, tbl, hash) \ |
| 453 | for (({barrier(); }), \ |
| 454 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 455 | !rht_is_a_nulls(pos); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 456 | pos = rcu_dereference_raw(pos->next)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 457 | |
| 458 | /** |
| 459 | * rht_for_each_rcu - iterate over rcu hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 460 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 461 | * @tbl: the &struct bucket_table |
| 462 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 463 | * |
| 464 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 465 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 466 | * traversal is guarded by rcu_read_lock(). |
| 467 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 468 | #define rht_for_each_rcu(pos, tbl, hash) \ |
| 469 | rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash) |
| 470 | |
| 471 | /** |
| 472 | * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain |
| 473 | * @tpos: the type * to use as a loop cursor. |
| 474 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 475 | * @head: the previous &struct rhash_head to continue from |
| 476 | * @tbl: the &struct bucket_table |
| 477 | * @hash: the hash value / bucket index |
| 478 | * @member: name of the &struct rhash_head within the hashable struct. |
| 479 | * |
| 480 | * This hash chain list-traversal primitive may safely run concurrently with |
| 481 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 482 | * traversal is guarded by rcu_read_lock(). |
| 483 | */ |
| 484 | #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \ |
| 485 | for (({barrier(); }), \ |
| 486 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 487 | (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 488 | pos = rht_dereference_bucket_rcu(pos->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 489 | |
| 490 | /** |
| 491 | * 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] | 492 | * @tpos: the type * to use as a loop cursor. |
| 493 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 494 | * @tbl: the &struct bucket_table |
| 495 | * @hash: the hash value / bucket index |
| 496 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 497 | * |
| 498 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 499 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 500 | * traversal is guarded by rcu_read_lock(). |
| 501 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 502 | #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \ |
| 503 | rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\ |
| 504 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 505 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 506 | static inline int rhashtable_compare(struct rhashtable_compare_arg *arg, |
| 507 | const void *obj) |
| 508 | { |
| 509 | struct rhashtable *ht = arg->ht; |
| 510 | const char *ptr = obj; |
| 511 | |
| 512 | return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * rhashtable_lookup_fast - search hash table, inlined version |
| 517 | * @ht: hash table |
| 518 | * @key: the pointer to the key |
| 519 | * @params: hash table parameters |
| 520 | * |
| 521 | * Computes the hash value for the key and traverses the bucket chain looking |
| 522 | * for a entry with an identical key. The first matching entry is returned. |
| 523 | * |
| 524 | * Returns the first entry on which the compare function returned true. |
| 525 | */ |
| 526 | static inline void *rhashtable_lookup_fast( |
| 527 | struct rhashtable *ht, const void *key, |
| 528 | const struct rhashtable_params params) |
| 529 | { |
| 530 | struct rhashtable_compare_arg arg = { |
| 531 | .ht = ht, |
| 532 | .key = key, |
| 533 | }; |
| 534 | const struct bucket_table *tbl; |
| 535 | struct rhash_head *he; |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 536 | unsigned int hash; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 537 | |
| 538 | rcu_read_lock(); |
| 539 | |
| 540 | tbl = rht_dereference_rcu(ht->tbl, ht); |
| 541 | restart: |
| 542 | hash = rht_key_hashfn(ht, tbl, key, params); |
| 543 | rht_for_each_rcu(he, tbl, hash) { |
| 544 | if (params.obj_cmpfn ? |
| 545 | params.obj_cmpfn(&arg, rht_obj(ht, he)) : |
| 546 | rhashtable_compare(&arg, rht_obj(ht, he))) |
| 547 | continue; |
| 548 | rcu_read_unlock(); |
| 549 | return rht_obj(ht, he); |
| 550 | } |
| 551 | |
| 552 | /* Ensure we see any new tables. */ |
| 553 | smp_rmb(); |
| 554 | |
| 555 | tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 556 | if (unlikely(tbl)) |
| 557 | goto restart; |
| 558 | rcu_read_unlock(); |
| 559 | |
| 560 | return NULL; |
| 561 | } |
| 562 | |
Thomas Graf | ac833bd | 2015-03-24 14:18:18 +0100 | [diff] [blame] | 563 | /* Internal function, please use rhashtable_insert_fast() instead */ |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 564 | static inline int __rhashtable_insert_fast( |
| 565 | struct rhashtable *ht, const void *key, struct rhash_head *obj, |
| 566 | const struct rhashtable_params params) |
| 567 | { |
| 568 | struct rhashtable_compare_arg arg = { |
| 569 | .ht = ht, |
| 570 | .key = key, |
| 571 | }; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 572 | struct bucket_table *tbl, *new_tbl; |
| 573 | struct rhash_head *head; |
| 574 | spinlock_t *lock; |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 575 | unsigned int elasticity; |
| 576 | unsigned int hash; |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 577 | int err; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 578 | |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 579 | restart: |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 580 | rcu_read_lock(); |
| 581 | |
| 582 | tbl = rht_dereference_rcu(ht->tbl, ht); |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 583 | |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 584 | /* All insertions must grab the oldest table containing |
| 585 | * the hashed bucket that is yet to be rehashed. |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 586 | */ |
Herbert Xu | b824478 | 2015-03-24 00:50:26 +1100 | [diff] [blame] | 587 | for (;;) { |
| 588 | hash = rht_head_hashfn(ht, tbl, obj, params); |
| 589 | lock = rht_bucket_lock(tbl, hash); |
| 590 | spin_lock_bh(lock); |
| 591 | |
| 592 | if (tbl->rehash <= hash) |
| 593 | break; |
| 594 | |
| 595 | spin_unlock_bh(lock); |
| 596 | tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 597 | } |
| 598 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 599 | new_tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
| 600 | if (unlikely(new_tbl)) { |
| 601 | err = rhashtable_insert_slow(ht, key, obj, new_tbl); |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 602 | if (err == -EAGAIN) |
| 603 | goto slow_path; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 604 | goto out; |
| 605 | } |
| 606 | |
Herbert Xu | 07ee072 | 2015-05-15 11:30:47 +0800 | [diff] [blame] | 607 | err = -E2BIG; |
| 608 | if (unlikely(rht_grow_above_max(ht, tbl))) |
| 609 | goto out; |
| 610 | |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 611 | if (unlikely(rht_grow_above_100(ht, tbl))) { |
| 612 | slow_path: |
| 613 | spin_unlock_bh(lock); |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 614 | err = rhashtable_insert_rehash(ht); |
Thomas Graf | 58be8a5 | 2015-03-24 14:18:16 +0100 | [diff] [blame] | 615 | rcu_read_unlock(); |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 616 | if (err) |
| 617 | return err; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 618 | |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 619 | goto restart; |
| 620 | } |
| 621 | |
| 622 | err = -EEXIST; |
| 623 | elasticity = ht->elasticity; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 624 | rht_for_each(head, tbl, hash) { |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 625 | if (key && |
| 626 | unlikely(!(params.obj_cmpfn ? |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 627 | params.obj_cmpfn(&arg, rht_obj(ht, head)) : |
| 628 | rhashtable_compare(&arg, rht_obj(ht, head))))) |
| 629 | goto out; |
Herbert Xu | ccd57b1 | 2015-03-24 00:50:28 +1100 | [diff] [blame] | 630 | if (!--elasticity) |
| 631 | goto slow_path; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 632 | } |
| 633 | |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 634 | err = 0; |
| 635 | |
| 636 | head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); |
| 637 | |
| 638 | RCU_INIT_POINTER(obj->next, head); |
| 639 | |
| 640 | rcu_assign_pointer(tbl->buckets[hash], obj); |
| 641 | |
| 642 | atomic_inc(&ht->nelems); |
| 643 | if (rht_grow_above_75(ht, tbl)) |
| 644 | schedule_work(&ht->run_work); |
| 645 | |
| 646 | out: |
| 647 | spin_unlock_bh(lock); |
| 648 | rcu_read_unlock(); |
| 649 | |
| 650 | return err; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * rhashtable_insert_fast - insert object into hash table |
| 655 | * @ht: hash table |
| 656 | * @obj: pointer to hash head inside object |
| 657 | * @params: hash table parameters |
| 658 | * |
| 659 | * Will take a per bucket spinlock to protect against mutual mutations |
| 660 | * on the same bucket. Multiple insertions may occur in parallel unless |
| 661 | * they map to the same bucket lock. |
| 662 | * |
| 663 | * It is safe to call this function from atomic context. |
| 664 | * |
| 665 | * Will trigger an automatic deferred table resizing if the size grows |
| 666 | * beyond the watermark indicated by grow_decision() which can be passed |
| 667 | * to rhashtable_init(). |
| 668 | */ |
| 669 | static inline int rhashtable_insert_fast( |
| 670 | struct rhashtable *ht, struct rhash_head *obj, |
| 671 | const struct rhashtable_params params) |
| 672 | { |
| 673 | return __rhashtable_insert_fast(ht, NULL, obj, params); |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * rhashtable_lookup_insert_fast - lookup and insert object into hash table |
| 678 | * @ht: hash table |
| 679 | * @obj: pointer to hash head inside object |
| 680 | * @params: hash table parameters |
| 681 | * |
| 682 | * Locks down the bucket chain in both the old and new table if a resize |
| 683 | * is in progress to ensure that writers can't remove from the old table |
| 684 | * and can't insert to the new table during the atomic operation of search |
| 685 | * and insertion. Searches for duplicates in both the old and new table if |
| 686 | * a resize is in progress. |
| 687 | * |
| 688 | * This lookup function may only be used for fixed key hash table (key_len |
| 689 | * parameter set). It will BUG() if used inappropriately. |
| 690 | * |
| 691 | * It is safe to call this function from atomic context. |
| 692 | * |
| 693 | * Will trigger an automatic deferred table resizing if the size grows |
| 694 | * beyond the watermark indicated by grow_decision() which can be passed |
| 695 | * to rhashtable_init(). |
| 696 | */ |
| 697 | static inline int rhashtable_lookup_insert_fast( |
| 698 | struct rhashtable *ht, struct rhash_head *obj, |
| 699 | const struct rhashtable_params params) |
| 700 | { |
| 701 | const char *key = rht_obj(ht, obj); |
| 702 | |
| 703 | BUG_ON(ht->p.obj_hashfn); |
| 704 | |
| 705 | return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, |
| 706 | params); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * rhashtable_lookup_insert_key - search and insert object to hash table |
| 711 | * with explicit key |
| 712 | * @ht: hash table |
| 713 | * @key: key |
| 714 | * @obj: pointer to hash head inside object |
| 715 | * @params: hash table parameters |
| 716 | * |
| 717 | * Locks down the bucket chain in both the old and new table if a resize |
| 718 | * is in progress to ensure that writers can't remove from the old table |
| 719 | * and can't insert to the new table during the atomic operation of search |
| 720 | * and insertion. Searches for duplicates in both the old and new table if |
| 721 | * a resize is in progress. |
| 722 | * |
| 723 | * Lookups may occur in parallel with hashtable mutations and resizing. |
| 724 | * |
| 725 | * Will trigger an automatic deferred table resizing if the size grows |
| 726 | * beyond the watermark indicated by grow_decision() which can be passed |
| 727 | * to rhashtable_init(). |
| 728 | * |
| 729 | * Returns zero on success. |
| 730 | */ |
| 731 | static inline int rhashtable_lookup_insert_key( |
| 732 | struct rhashtable *ht, const void *key, struct rhash_head *obj, |
| 733 | const struct rhashtable_params params) |
| 734 | { |
| 735 | BUG_ON(!ht->p.obj_hashfn || !key); |
| 736 | |
| 737 | return __rhashtable_insert_fast(ht, key, obj, params); |
| 738 | } |
| 739 | |
Thomas Graf | ac833bd | 2015-03-24 14:18:18 +0100 | [diff] [blame] | 740 | /* Internal function, please use rhashtable_remove_fast() instead */ |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 741 | static inline int __rhashtable_remove_fast( |
| 742 | struct rhashtable *ht, struct bucket_table *tbl, |
| 743 | struct rhash_head *obj, const struct rhashtable_params params) |
| 744 | { |
| 745 | struct rhash_head __rcu **pprev; |
| 746 | struct rhash_head *he; |
| 747 | spinlock_t * lock; |
Thomas Graf | 299e5c3 | 2015-03-24 14:18:17 +0100 | [diff] [blame] | 748 | unsigned int hash; |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 749 | int err = -ENOENT; |
| 750 | |
| 751 | hash = rht_head_hashfn(ht, tbl, obj, params); |
| 752 | lock = rht_bucket_lock(tbl, hash); |
| 753 | |
| 754 | spin_lock_bh(lock); |
| 755 | |
| 756 | pprev = &tbl->buckets[hash]; |
| 757 | rht_for_each(he, tbl, hash) { |
| 758 | if (he != obj) { |
| 759 | pprev = &he->next; |
| 760 | continue; |
| 761 | } |
| 762 | |
| 763 | rcu_assign_pointer(*pprev, obj->next); |
| 764 | err = 0; |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | spin_unlock_bh(lock); |
| 769 | |
| 770 | return err; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * rhashtable_remove_fast - remove object from hash table |
| 775 | * @ht: hash table |
| 776 | * @obj: pointer to hash head inside object |
| 777 | * @params: hash table parameters |
| 778 | * |
| 779 | * Since the hash chain is single linked, the removal operation needs to |
| 780 | * walk the bucket chain upon removal. The removal operation is thus |
| 781 | * considerable slow if the hash table is not correctly sized. |
| 782 | * |
| 783 | * Will automatically shrink the table via rhashtable_expand() if the |
| 784 | * shrink_decision function specified at rhashtable_init() returns true. |
| 785 | * |
| 786 | * Returns zero on success, -ENOENT if the entry could not be found. |
| 787 | */ |
| 788 | static inline int rhashtable_remove_fast( |
| 789 | struct rhashtable *ht, struct rhash_head *obj, |
| 790 | const struct rhashtable_params params) |
| 791 | { |
| 792 | struct bucket_table *tbl; |
| 793 | int err; |
| 794 | |
| 795 | rcu_read_lock(); |
| 796 | |
| 797 | tbl = rht_dereference_rcu(ht->tbl, ht); |
| 798 | |
| 799 | /* Because we have already taken (and released) the bucket |
| 800 | * lock in old_tbl, if we find that future_tbl is not yet |
| 801 | * visible then that guarantees the entry to still be in |
| 802 | * the old tbl if it exists. |
| 803 | */ |
| 804 | while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) && |
| 805 | (tbl = rht_dereference_rcu(tbl->future_tbl, ht))) |
| 806 | ; |
| 807 | |
| 808 | if (err) |
| 809 | goto out; |
| 810 | |
| 811 | atomic_dec(&ht->nelems); |
Thomas Graf | b5e2c15 | 2015-03-24 20:42:19 +0000 | [diff] [blame] | 812 | if (unlikely(ht->p.automatic_shrinking && |
| 813 | rht_shrink_below_30(ht, tbl))) |
Herbert Xu | 02fd97c | 2015-03-20 21:57:00 +1100 | [diff] [blame] | 814 | schedule_work(&ht->run_work); |
| 815 | |
| 816 | out: |
| 817 | rcu_read_unlock(); |
| 818 | |
| 819 | return err; |
| 820 | } |
| 821 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 822 | #endif /* _LINUX_RHASHTABLE_H */ |