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> |
| 22 | |
| 23 | struct rhash_head { |
Thomas Graf | 5300fdc | 2014-08-13 16:38:29 +0200 | [diff] [blame] | 24 | struct rhash_head __rcu *next; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | #define INIT_HASH_HEAD(ptr) ((ptr)->next = NULL) |
| 28 | |
| 29 | struct bucket_table { |
| 30 | size_t size; |
| 31 | struct rhash_head __rcu *buckets[]; |
| 32 | }; |
| 33 | |
| 34 | typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed); |
| 35 | typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed); |
| 36 | |
| 37 | struct rhashtable; |
| 38 | |
| 39 | /** |
| 40 | * struct rhashtable_params - Hash table construction parameters |
| 41 | * @nelem_hint: Hint on number of elements, should be 75% of desired size |
| 42 | * @key_len: Length of key |
| 43 | * @key_offset: Offset of key in struct to be hashed |
| 44 | * @head_offset: Offset of rhash_head in struct to be hashed |
| 45 | * @hash_rnd: Seed to use while hashing |
| 46 | * @max_shift: Maximum number of shifts while expanding |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 47 | * @min_shift: Minimum number of shifts while shrinking |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 48 | * @hashfn: Function to hash key |
| 49 | * @obj_hashfn: Function to hash object |
| 50 | * @grow_decision: If defined, may return true if table should expand |
| 51 | * @shrink_decision: If defined, may return true if table should shrink |
| 52 | * @mutex_is_held: Must return true if protecting mutex is held |
| 53 | */ |
| 54 | struct rhashtable_params { |
| 55 | size_t nelem_hint; |
| 56 | size_t key_len; |
| 57 | size_t key_offset; |
| 58 | size_t head_offset; |
| 59 | u32 hash_rnd; |
| 60 | size_t max_shift; |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 61 | size_t min_shift; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 62 | rht_hashfn_t hashfn; |
| 63 | rht_obj_hashfn_t obj_hashfn; |
| 64 | bool (*grow_decision)(const struct rhashtable *ht, |
| 65 | size_t new_size); |
| 66 | bool (*shrink_decision)(const struct rhashtable *ht, |
| 67 | size_t new_size); |
Herbert Xu | 1b2f309 | 2014-11-13 18:11:20 +0800 | [diff] [blame] | 68 | #ifdef CONFIG_PROVE_LOCKING |
Herbert Xu | 7b4ce23 | 2014-11-13 18:11:22 +0800 | [diff] [blame] | 69 | int (*mutex_is_held)(void *parent); |
| 70 | void *parent; |
Herbert Xu | 1b2f309 | 2014-11-13 18:11:20 +0800 | [diff] [blame] | 71 | #endif |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * struct rhashtable - Hash table handle |
| 76 | * @tbl: Bucket table |
| 77 | * @nelems: Number of elements in table |
| 78 | * @shift: Current size (1 << shift) |
| 79 | * @p: Configuration parameters |
| 80 | */ |
| 81 | struct rhashtable { |
| 82 | struct bucket_table __rcu *tbl; |
| 83 | size_t nelems; |
| 84 | size_t shift; |
| 85 | struct rhashtable_params p; |
| 86 | }; |
| 87 | |
| 88 | #ifdef CONFIG_PROVE_LOCKING |
| 89 | int lockdep_rht_mutex_is_held(const struct rhashtable *ht); |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 90 | 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] | 91 | #else |
| 92 | static inline int lockdep_rht_mutex_is_held(const struct rhashtable *ht) |
| 93 | { |
| 94 | return 1; |
| 95 | } |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 96 | |
| 97 | static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, |
| 98 | u32 hash) |
| 99 | { |
| 100 | return 1; |
| 101 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 102 | #endif /* CONFIG_PROVE_LOCKING */ |
| 103 | |
| 104 | int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params); |
| 105 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 106 | void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node); |
| 107 | bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 108 | void rhashtable_remove_pprev(struct rhashtable *ht, struct rhash_head *obj, |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 109 | struct rhash_head __rcu **pprev); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 110 | |
| 111 | bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size); |
| 112 | bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size); |
| 113 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 114 | int rhashtable_expand(struct rhashtable *ht); |
| 115 | int rhashtable_shrink(struct rhashtable *ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 116 | |
| 117 | void *rhashtable_lookup(const struct rhashtable *ht, const void *key); |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 118 | void *rhashtable_lookup_compare(const struct rhashtable *ht, const void *key, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 119 | bool (*compare)(void *, void *), void *arg); |
| 120 | |
| 121 | void rhashtable_destroy(const struct rhashtable *ht); |
| 122 | |
| 123 | #define rht_dereference(p, ht) \ |
| 124 | rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht)) |
| 125 | |
| 126 | #define rht_dereference_rcu(p, ht) \ |
| 127 | rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht)) |
| 128 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 129 | #define rht_dereference_bucket(p, tbl, hash) \ |
| 130 | rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 131 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 132 | #define rht_dereference_bucket_rcu(p, tbl, hash) \ |
| 133 | rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash)) |
| 134 | |
| 135 | #define rht_entry(tpos, pos, member) \ |
| 136 | ({ tpos = container_of(pos, typeof(*tpos), member); 1; }) |
| 137 | |
| 138 | /** |
| 139 | * rht_for_each_continue - continue iterating over hash chain |
| 140 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 141 | * @head: the previous &struct rhash_head to continue from |
| 142 | * @tbl: the &struct bucket_table |
| 143 | * @hash: the hash value / bucket index |
| 144 | */ |
| 145 | #define rht_for_each_continue(pos, head, tbl, hash) \ |
| 146 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
| 147 | pos; \ |
| 148 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * rht_for_each - iterate over hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 152 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 153 | * @tbl: the &struct bucket_table |
| 154 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 155 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 156 | #define rht_for_each(pos, tbl, hash) \ |
| 157 | rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash) |
| 158 | |
| 159 | /** |
| 160 | * rht_for_each_entry_continue - continue iterating over hash chain |
| 161 | * @tpos: the type * to use as a loop cursor. |
| 162 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 163 | * @head: the previous &struct rhash_head to continue from |
| 164 | * @tbl: the &struct bucket_table |
| 165 | * @hash: the hash value / bucket index |
| 166 | * @member: name of the &struct rhash_head within the hashable struct. |
| 167 | */ |
| 168 | #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \ |
| 169 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
| 170 | pos && rht_entry(tpos, pos, member); \ |
| 171 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * rht_for_each_entry - iterate over hash chain of given type |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 175 | * @tpos: the type * to use as a loop cursor. |
| 176 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 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. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 180 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 181 | #define rht_for_each_entry(tpos, pos, tbl, hash, member) \ |
| 182 | rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \ |
| 183 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * 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^] | 187 | * @tpos: the type * to use as a loop cursor. |
| 188 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 189 | * @next: the &struct rhash_head to use as next in 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 | * |
| 194 | * This hash chain list-traversal primitive allows for the looped code to |
| 195 | * remove the loop cursor from the list. |
| 196 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 197 | #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \ |
| 198 | for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \ |
| 199 | next = pos ? rht_dereference_bucket(pos->next, tbl, hash) \ |
| 200 | : NULL; \ |
| 201 | pos && rht_entry(tpos, pos, member); \ |
| 202 | pos = next) |
| 203 | |
| 204 | /** |
| 205 | * rht_for_each_rcu_continue - continue iterating over rcu hash chain |
| 206 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 207 | * @head: the previous &struct rhash_head to continue from |
| 208 | * @tbl: the &struct bucket_table |
| 209 | * @hash: the hash value / bucket index |
| 210 | * |
| 211 | * This hash chain list-traversal primitive may safely run concurrently with |
| 212 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 213 | * traversal is guarded by rcu_read_lock(). |
| 214 | */ |
| 215 | #define rht_for_each_rcu_continue(pos, head, tbl, hash) \ |
| 216 | for (({barrier(); }), \ |
| 217 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
| 218 | pos; \ |
| 219 | pos = rcu_dereference_raw(pos->next)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * rht_for_each_rcu - iterate over rcu hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 223 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 224 | * @tbl: the &struct bucket_table |
| 225 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 226 | * |
| 227 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 228 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 229 | * traversal is guarded by rcu_read_lock(). |
| 230 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 231 | #define rht_for_each_rcu(pos, tbl, hash) \ |
| 232 | rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash) |
| 233 | |
| 234 | /** |
| 235 | * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain |
| 236 | * @tpos: the type * to use as a loop cursor. |
| 237 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 238 | * @head: the previous &struct rhash_head to continue from |
| 239 | * @tbl: the &struct bucket_table |
| 240 | * @hash: the hash value / bucket index |
| 241 | * @member: name of the &struct rhash_head within the hashable struct. |
| 242 | * |
| 243 | * This hash chain list-traversal primitive may safely run concurrently with |
| 244 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 245 | * traversal is guarded by rcu_read_lock(). |
| 246 | */ |
| 247 | #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \ |
| 248 | for (({barrier(); }), \ |
| 249 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
| 250 | pos && rht_entry(tpos, pos, member); \ |
| 251 | pos = rht_dereference_bucket_rcu(pos->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * 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^] | 255 | * @tpos: the type * to use as a loop cursor. |
| 256 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 257 | * @tbl: the &struct bucket_table |
| 258 | * @hash: the hash value / bucket index |
| 259 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 260 | * |
| 261 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 262 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 263 | * traversal is guarded by rcu_read_lock(). |
| 264 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame^] | 265 | #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \ |
| 266 | rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\ |
| 267 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 268 | |
| 269 | #endif /* _LINUX_RHASHTABLE_H */ |