Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Resizable, Scalable, Concurrent Hash Table |
| 3 | * |
| 4 | * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch> |
| 5 | * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
| 6 | * |
| 7 | * Based on the following paper by Josh Triplett, Paul E. McKenney |
| 8 | * and Jonathan Walpole: |
| 9 | * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf |
| 10 | * |
| 11 | * Code partially derived from nft_hash |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 as |
| 15 | * published by the Free Software Foundation. |
| 16 | */ |
| 17 | |
| 18 | #ifndef _LINUX_RHASHTABLE_H |
| 19 | #define _LINUX_RHASHTABLE_H |
| 20 | |
| 21 | #include <linux/rculist.h> |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 22 | #include <linux/workqueue.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 23 | |
| 24 | struct rhash_head { |
Thomas Graf | 5300fdc | 2014-08-13 16:38:29 +0200 | [diff] [blame] | 25 | struct rhash_head __rcu *next; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | #define INIT_HASH_HEAD(ptr) ((ptr)->next = NULL) |
| 29 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 30 | /** |
| 31 | * struct bucket_table - Table of hash buckets |
| 32 | * @size: Number of hash buckets |
| 33 | * @locks_mask: Mask to apply before accessing locks[] |
| 34 | * @locks: Array of spinlocks protecting individual buckets |
| 35 | * @buckets: size * hash buckets |
| 36 | */ |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 37 | struct bucket_table { |
| 38 | size_t size; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 39 | unsigned int locks_mask; |
| 40 | spinlock_t *locks; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 41 | struct rhash_head __rcu *buckets[]; |
| 42 | }; |
| 43 | |
| 44 | typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed); |
| 45 | typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed); |
| 46 | |
| 47 | struct rhashtable; |
| 48 | |
| 49 | /** |
| 50 | * struct rhashtable_params - Hash table construction parameters |
| 51 | * @nelem_hint: Hint on number of elements, should be 75% of desired size |
| 52 | * @key_len: Length of key |
| 53 | * @key_offset: Offset of key in struct to be hashed |
| 54 | * @head_offset: Offset of rhash_head in struct to be hashed |
| 55 | * @hash_rnd: Seed to use while hashing |
| 56 | * @max_shift: Maximum number of shifts while expanding |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 57 | * @min_shift: Minimum number of shifts while shrinking |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 58 | * @locks_mul: Number of bucket locks to allocate per cpu (default: 128) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 59 | * @hashfn: Function to hash key |
| 60 | * @obj_hashfn: Function to hash object |
| 61 | * @grow_decision: If defined, may return true if table should expand |
| 62 | * @shrink_decision: If defined, may return true if table should shrink |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 63 | */ |
| 64 | struct rhashtable_params { |
| 65 | size_t nelem_hint; |
| 66 | size_t key_len; |
| 67 | size_t key_offset; |
| 68 | size_t head_offset; |
| 69 | u32 hash_rnd; |
| 70 | size_t max_shift; |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 71 | size_t min_shift; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 72 | size_t locks_mul; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 73 | rht_hashfn_t hashfn; |
| 74 | rht_obj_hashfn_t obj_hashfn; |
| 75 | bool (*grow_decision)(const struct rhashtable *ht, |
| 76 | size_t new_size); |
| 77 | bool (*shrink_decision)(const struct rhashtable *ht, |
| 78 | size_t new_size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * struct rhashtable - Hash table handle |
| 83 | * @tbl: Bucket table |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 84 | * @future_tbl: Table under construction during expansion/shrinking |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 85 | * @nelems: Number of elements in table |
| 86 | * @shift: Current size (1 << shift) |
| 87 | * @p: Configuration parameters |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 88 | * @run_work: Deferred worker to expand/shrink asynchronously |
| 89 | * @mutex: Mutex to protect current/future table swapping |
| 90 | * @being_destroyed: True if table is set up for destruction |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 91 | */ |
| 92 | struct rhashtable { |
| 93 | struct bucket_table __rcu *tbl; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 94 | struct bucket_table __rcu *future_tbl; |
| 95 | atomic_t nelems; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 96 | size_t shift; |
| 97 | struct rhashtable_params p; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 98 | struct delayed_work run_work; |
| 99 | struct mutex mutex; |
| 100 | bool being_destroyed; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | #ifdef CONFIG_PROVE_LOCKING |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 104 | int lockdep_rht_mutex_is_held(struct rhashtable *ht); |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 105 | 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] | 106 | #else |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 107 | static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 108 | { |
| 109 | return 1; |
| 110 | } |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 111 | |
| 112 | static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, |
| 113 | u32 hash) |
| 114 | { |
| 115 | return 1; |
| 116 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 117 | #endif /* CONFIG_PROVE_LOCKING */ |
| 118 | |
| 119 | int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params); |
| 120 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 121 | void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node); |
| 122 | bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 123 | |
| 124 | bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size); |
| 125 | bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size); |
| 126 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 127 | int rhashtable_expand(struct rhashtable *ht); |
| 128 | int rhashtable_shrink(struct rhashtable *ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 129 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 130 | void *rhashtable_lookup(struct rhashtable *ht, const void *key); |
| 131 | void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 132 | bool (*compare)(void *, void *), void *arg); |
| 133 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame^] | 134 | void rhashtable_destroy(struct rhashtable *ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 135 | |
| 136 | #define rht_dereference(p, ht) \ |
| 137 | rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht)) |
| 138 | |
| 139 | #define rht_dereference_rcu(p, ht) \ |
| 140 | rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht)) |
| 141 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 142 | #define rht_dereference_bucket(p, tbl, hash) \ |
| 143 | rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 144 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 145 | #define rht_dereference_bucket_rcu(p, tbl, hash) \ |
| 146 | rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash)) |
| 147 | |
| 148 | #define rht_entry(tpos, pos, member) \ |
| 149 | ({ tpos = container_of(pos, typeof(*tpos), member); 1; }) |
| 150 | |
| 151 | /** |
| 152 | * rht_for_each_continue - continue iterating over hash chain |
| 153 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 154 | * @head: the previous &struct rhash_head to continue from |
| 155 | * @tbl: the &struct bucket_table |
| 156 | * @hash: the hash value / bucket index |
| 157 | */ |
| 158 | #define rht_for_each_continue(pos, head, tbl, hash) \ |
| 159 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
| 160 | pos; \ |
| 161 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * rht_for_each - iterate over hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 165 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 166 | * @tbl: the &struct bucket_table |
| 167 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 168 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 169 | #define rht_for_each(pos, tbl, hash) \ |
| 170 | rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash) |
| 171 | |
| 172 | /** |
| 173 | * rht_for_each_entry_continue - continue iterating over hash chain |
| 174 | * @tpos: the type * to use as a loop cursor. |
| 175 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 176 | * @head: the previous &struct rhash_head to continue from |
| 177 | * @tbl: the &struct bucket_table |
| 178 | * @hash: the hash value / bucket index |
| 179 | * @member: name of the &struct rhash_head within the hashable struct. |
| 180 | */ |
| 181 | #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \ |
| 182 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
| 183 | pos && rht_entry(tpos, pos, member); \ |
| 184 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * rht_for_each_entry - iterate over hash chain of given type |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 188 | * @tpos: the type * to use as a loop cursor. |
| 189 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 190 | * @tbl: the &struct bucket_table |
| 191 | * @hash: the hash value / bucket index |
| 192 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 193 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 194 | #define rht_for_each_entry(tpos, pos, tbl, hash, member) \ |
| 195 | rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \ |
| 196 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * 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] | 200 | * @tpos: the type * to use as a loop cursor. |
| 201 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 202 | * @next: the &struct rhash_head to use as next in loop cursor. |
| 203 | * @tbl: the &struct bucket_table |
| 204 | * @hash: the hash value / bucket index |
| 205 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 206 | * |
| 207 | * This hash chain list-traversal primitive allows for the looped code to |
| 208 | * remove the loop cursor from the list. |
| 209 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 210 | #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \ |
| 211 | for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \ |
| 212 | next = pos ? rht_dereference_bucket(pos->next, tbl, hash) \ |
| 213 | : NULL; \ |
| 214 | pos && rht_entry(tpos, pos, member); \ |
| 215 | pos = next) |
| 216 | |
| 217 | /** |
| 218 | * rht_for_each_rcu_continue - continue iterating over rcu hash chain |
| 219 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 220 | * @head: the previous &struct rhash_head to continue from |
| 221 | * @tbl: the &struct bucket_table |
| 222 | * @hash: the hash value / bucket index |
| 223 | * |
| 224 | * This hash chain list-traversal primitive may safely run concurrently with |
| 225 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 226 | * traversal is guarded by rcu_read_lock(). |
| 227 | */ |
| 228 | #define rht_for_each_rcu_continue(pos, head, tbl, hash) \ |
| 229 | for (({barrier(); }), \ |
| 230 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
| 231 | pos; \ |
| 232 | pos = rcu_dereference_raw(pos->next)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * rht_for_each_rcu - iterate over rcu hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 236 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 237 | * @tbl: the &struct bucket_table |
| 238 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 239 | * |
| 240 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 241 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 242 | * traversal is guarded by rcu_read_lock(). |
| 243 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 244 | #define rht_for_each_rcu(pos, tbl, hash) \ |
| 245 | rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash) |
| 246 | |
| 247 | /** |
| 248 | * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain |
| 249 | * @tpos: the type * to use as a loop cursor. |
| 250 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 251 | * @head: the previous &struct rhash_head to continue from |
| 252 | * @tbl: the &struct bucket_table |
| 253 | * @hash: the hash value / bucket index |
| 254 | * @member: name of the &struct rhash_head within the hashable struct. |
| 255 | * |
| 256 | * This hash chain list-traversal primitive may safely run concurrently with |
| 257 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 258 | * traversal is guarded by rcu_read_lock(). |
| 259 | */ |
| 260 | #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \ |
| 261 | for (({barrier(); }), \ |
| 262 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
| 263 | pos && rht_entry(tpos, pos, member); \ |
| 264 | pos = rht_dereference_bucket_rcu(pos->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * 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] | 268 | * @tpos: the type * to use as a loop cursor. |
| 269 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 270 | * @tbl: the &struct bucket_table |
| 271 | * @hash: the hash value / bucket index |
| 272 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 273 | * |
| 274 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 275 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 276 | * traversal is guarded by rcu_read_lock(). |
| 277 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 278 | #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \ |
| 279 | rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\ |
| 280 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 281 | |
| 282 | #endif /* _LINUX_RHASHTABLE_H */ |