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