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 | |
| 109 | bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size); |
| 110 | bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size); |
| 111 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 112 | int rhashtable_expand(struct rhashtable *ht); |
| 113 | int rhashtable_shrink(struct rhashtable *ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 114 | |
| 115 | void *rhashtable_lookup(const struct rhashtable *ht, const void *key); |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 116 | void *rhashtable_lookup_compare(const struct rhashtable *ht, const void *key, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 117 | bool (*compare)(void *, void *), void *arg); |
| 118 | |
| 119 | void rhashtable_destroy(const struct rhashtable *ht); |
| 120 | |
| 121 | #define rht_dereference(p, ht) \ |
| 122 | rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht)) |
| 123 | |
| 124 | #define rht_dereference_rcu(p, ht) \ |
| 125 | rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht)) |
| 126 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 127 | #define rht_dereference_bucket(p, tbl, hash) \ |
| 128 | rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 129 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 130 | #define rht_dereference_bucket_rcu(p, tbl, hash) \ |
| 131 | rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash)) |
| 132 | |
| 133 | #define rht_entry(tpos, pos, member) \ |
| 134 | ({ tpos = container_of(pos, typeof(*tpos), member); 1; }) |
| 135 | |
| 136 | /** |
| 137 | * rht_for_each_continue - continue iterating over hash chain |
| 138 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 139 | * @head: the previous &struct rhash_head to continue from |
| 140 | * @tbl: the &struct bucket_table |
| 141 | * @hash: the hash value / bucket index |
| 142 | */ |
| 143 | #define rht_for_each_continue(pos, head, tbl, hash) \ |
| 144 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
| 145 | pos; \ |
| 146 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * rht_for_each - iterate over hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 150 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 151 | * @tbl: the &struct bucket_table |
| 152 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 153 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 154 | #define rht_for_each(pos, tbl, hash) \ |
| 155 | rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash) |
| 156 | |
| 157 | /** |
| 158 | * rht_for_each_entry_continue - continue iterating over hash chain |
| 159 | * @tpos: the type * to use as a loop cursor. |
| 160 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 161 | * @head: the previous &struct rhash_head to continue from |
| 162 | * @tbl: the &struct bucket_table |
| 163 | * @hash: the hash value / bucket index |
| 164 | * @member: name of the &struct rhash_head within the hashable struct. |
| 165 | */ |
| 166 | #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \ |
| 167 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
| 168 | pos && rht_entry(tpos, pos, member); \ |
| 169 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * rht_for_each_entry - iterate over hash chain of given type |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 173 | * @tpos: the type * to use as a loop cursor. |
| 174 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 175 | * @tbl: the &struct bucket_table |
| 176 | * @hash: the hash value / bucket index |
| 177 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 178 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 179 | #define rht_for_each_entry(tpos, pos, tbl, hash, member) \ |
| 180 | rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \ |
| 181 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * 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] | 185 | * @tpos: the type * to use as a loop cursor. |
| 186 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 187 | * @next: the &struct rhash_head to use as next in loop cursor. |
| 188 | * @tbl: the &struct bucket_table |
| 189 | * @hash: the hash value / bucket index |
| 190 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 191 | * |
| 192 | * This hash chain list-traversal primitive allows for the looped code to |
| 193 | * remove the loop cursor from the list. |
| 194 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 195 | #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \ |
| 196 | for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \ |
| 197 | next = pos ? rht_dereference_bucket(pos->next, tbl, hash) \ |
| 198 | : NULL; \ |
| 199 | pos && rht_entry(tpos, pos, member); \ |
| 200 | pos = next) |
| 201 | |
| 202 | /** |
| 203 | * rht_for_each_rcu_continue - continue iterating over rcu hash chain |
| 204 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 205 | * @head: the previous &struct rhash_head to continue from |
| 206 | * @tbl: the &struct bucket_table |
| 207 | * @hash: the hash value / bucket index |
| 208 | * |
| 209 | * This hash chain list-traversal primitive may safely run concurrently with |
| 210 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 211 | * traversal is guarded by rcu_read_lock(). |
| 212 | */ |
| 213 | #define rht_for_each_rcu_continue(pos, head, tbl, hash) \ |
| 214 | for (({barrier(); }), \ |
| 215 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
| 216 | pos; \ |
| 217 | pos = rcu_dereference_raw(pos->next)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * rht_for_each_rcu - iterate over rcu hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 221 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 222 | * @tbl: the &struct bucket_table |
| 223 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 224 | * |
| 225 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 226 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 227 | * traversal is guarded by rcu_read_lock(). |
| 228 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 229 | #define rht_for_each_rcu(pos, tbl, hash) \ |
| 230 | rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash) |
| 231 | |
| 232 | /** |
| 233 | * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain |
| 234 | * @tpos: the type * to use as a loop cursor. |
| 235 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 236 | * @head: the previous &struct rhash_head to continue from |
| 237 | * @tbl: the &struct bucket_table |
| 238 | * @hash: the hash value / bucket index |
| 239 | * @member: name of the &struct rhash_head within the hashable struct. |
| 240 | * |
| 241 | * This hash chain list-traversal primitive may safely run concurrently with |
| 242 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 243 | * traversal is guarded by rcu_read_lock(). |
| 244 | */ |
| 245 | #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \ |
| 246 | for (({barrier(); }), \ |
| 247 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
| 248 | pos && rht_entry(tpos, pos, member); \ |
| 249 | pos = rht_dereference_bucket_rcu(pos->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 250 | |
| 251 | /** |
| 252 | * 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] | 253 | * @tpos: the type * to use as a loop cursor. |
| 254 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 255 | * @tbl: the &struct bucket_table |
| 256 | * @hash: the hash value / bucket index |
| 257 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 258 | * |
| 259 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 260 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 261 | * traversal is guarded by rcu_read_lock(). |
| 262 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 263 | #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \ |
| 264 | rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\ |
| 265 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 266 | |
| 267 | #endif /* _LINUX_RHASHTABLE_H */ |