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 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame^] | 21 | #include <linux/compiler.h> |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 22 | #include <linux/list_nulls.h> |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 23 | #include <linux/workqueue.h> |
Ying Xue | 86b35b6 | 2015-01-04 15:25:09 +0800 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 25 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 26 | /* |
| 27 | * The end of the chain is marked with a special nulls marks which has |
| 28 | * the following format: |
| 29 | * |
| 30 | * +-------+-----------------------------------------------------+-+ |
| 31 | * | Base | Hash |1| |
| 32 | * +-------+-----------------------------------------------------+-+ |
| 33 | * |
| 34 | * Base (4 bits) : Reserved to distinguish between multiple tables. |
| 35 | * Specified via &struct rhashtable_params.nulls_base. |
| 36 | * Hash (27 bits): Full hash (unmasked) of first element added to bucket |
| 37 | * 1 (1 bit) : Nulls marker (always set) |
| 38 | * |
| 39 | * The remaining bits of the next pointer remain unused for now. |
| 40 | */ |
| 41 | #define RHT_BASE_BITS 4 |
| 42 | #define RHT_HASH_BITS 27 |
| 43 | #define RHT_BASE_SHIFT RHT_HASH_BITS |
| 44 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 45 | struct rhash_head { |
Thomas Graf | 5300fdc | 2014-08-13 16:38:29 +0200 | [diff] [blame] | 46 | struct rhash_head __rcu *next; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 49 | /** |
| 50 | * struct bucket_table - Table of hash buckets |
| 51 | * @size: Number of hash buckets |
| 52 | * @locks_mask: Mask to apply before accessing locks[] |
| 53 | * @locks: Array of spinlocks protecting individual buckets |
| 54 | * @buckets: size * hash buckets |
| 55 | */ |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 56 | struct bucket_table { |
| 57 | size_t size; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 58 | unsigned int locks_mask; |
| 59 | spinlock_t *locks; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 60 | struct rhash_head __rcu *buckets[]; |
| 61 | }; |
| 62 | |
| 63 | typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed); |
| 64 | typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed); |
| 65 | |
| 66 | struct rhashtable; |
| 67 | |
| 68 | /** |
| 69 | * struct rhashtable_params - Hash table construction parameters |
| 70 | * @nelem_hint: Hint on number of elements, should be 75% of desired size |
| 71 | * @key_len: Length of key |
| 72 | * @key_offset: Offset of key in struct to be hashed |
| 73 | * @head_offset: Offset of rhash_head in struct to be hashed |
| 74 | * @hash_rnd: Seed to use while hashing |
| 75 | * @max_shift: Maximum number of shifts while expanding |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 76 | * @min_shift: Minimum number of shifts while shrinking |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 77 | * @nulls_base: Base value to generate nulls marker |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 78 | * @locks_mul: Number of bucket locks to allocate per cpu (default: 128) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 79 | * @hashfn: Function to hash key |
| 80 | * @obj_hashfn: Function to hash object |
| 81 | * @grow_decision: If defined, may return true if table should expand |
| 82 | * @shrink_decision: If defined, may return true if table should shrink |
Ying Xue | 6f73d3b | 2015-01-12 14:52:24 +0800 | [diff] [blame] | 83 | * |
| 84 | * Note: when implementing the grow and shrink decision function, min/max |
| 85 | * shift must be enforced, otherwise, resizing watermarks they set may be |
| 86 | * useless. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 87 | */ |
| 88 | struct rhashtable_params { |
| 89 | size_t nelem_hint; |
| 90 | size_t key_len; |
| 91 | size_t key_offset; |
| 92 | size_t head_offset; |
| 93 | u32 hash_rnd; |
| 94 | size_t max_shift; |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 95 | size_t min_shift; |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 96 | u32 nulls_base; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 97 | size_t locks_mul; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 98 | rht_hashfn_t hashfn; |
| 99 | rht_obj_hashfn_t obj_hashfn; |
| 100 | bool (*grow_decision)(const struct rhashtable *ht, |
| 101 | size_t new_size); |
| 102 | bool (*shrink_decision)(const struct rhashtable *ht, |
| 103 | size_t new_size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * struct rhashtable - Hash table handle |
| 108 | * @tbl: Bucket table |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 109 | * @future_tbl: Table under construction during expansion/shrinking |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 110 | * @nelems: Number of elements in table |
| 111 | * @shift: Current size (1 << shift) |
| 112 | * @p: Configuration parameters |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 113 | * @run_work: Deferred worker to expand/shrink asynchronously |
| 114 | * @mutex: Mutex to protect current/future table swapping |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame^] | 115 | * @walkers: List of active walkers |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 116 | * @being_destroyed: True if table is set up for destruction |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 117 | */ |
| 118 | struct rhashtable { |
| 119 | struct bucket_table __rcu *tbl; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 120 | struct bucket_table __rcu *future_tbl; |
| 121 | atomic_t nelems; |
Ying Xue | c0c09bf | 2015-01-07 13:41:56 +0800 | [diff] [blame] | 122 | atomic_t shift; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 123 | struct rhashtable_params p; |
Ying Xue | 57699a4 | 2015-01-16 11:13:09 +0800 | [diff] [blame] | 124 | struct work_struct run_work; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 125 | struct mutex mutex; |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame^] | 126 | struct list_head walkers; |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 127 | bool being_destroyed; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame^] | 130 | /** |
| 131 | * struct rhashtable_walker - Hash table walker |
| 132 | * @list: List entry on list of walkers |
| 133 | * @resize: Resize event occured |
| 134 | */ |
| 135 | struct rhashtable_walker { |
| 136 | struct list_head list; |
| 137 | bool resize; |
| 138 | }; |
| 139 | |
| 140 | /** |
| 141 | * struct rhashtable_iter - Hash table iterator, fits into netlink cb |
| 142 | * @ht: Table to iterate through |
| 143 | * @p: Current pointer |
| 144 | * @walker: Associated rhashtable walker |
| 145 | * @slot: Current slot |
| 146 | * @skip: Number of entries to skip in slot |
| 147 | */ |
| 148 | struct rhashtable_iter { |
| 149 | struct rhashtable *ht; |
| 150 | struct rhash_head *p; |
| 151 | struct rhashtable_walker *walker; |
| 152 | unsigned int slot; |
| 153 | unsigned int skip; |
| 154 | }; |
| 155 | |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 156 | static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash) |
| 157 | { |
| 158 | return NULLS_MARKER(ht->p.nulls_base + hash); |
| 159 | } |
| 160 | |
| 161 | #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \ |
| 162 | ((ptr) = (typeof(ptr)) rht_marker(ht, hash)) |
| 163 | |
| 164 | static inline bool rht_is_a_nulls(const struct rhash_head *ptr) |
| 165 | { |
| 166 | return ((unsigned long) ptr & 1); |
| 167 | } |
| 168 | |
| 169 | static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr) |
| 170 | { |
| 171 | return ((unsigned long) ptr) >> 1; |
| 172 | } |
| 173 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 174 | #ifdef CONFIG_PROVE_LOCKING |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 175 | int lockdep_rht_mutex_is_held(struct rhashtable *ht); |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 176 | 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] | 177 | #else |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 178 | static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 179 | { |
| 180 | return 1; |
| 181 | } |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 182 | |
| 183 | static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, |
| 184 | u32 hash) |
| 185 | { |
| 186 | return 1; |
| 187 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 188 | #endif /* CONFIG_PROVE_LOCKING */ |
| 189 | |
| 190 | int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params); |
| 191 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 192 | void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node); |
| 193 | bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 194 | |
| 195 | bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size); |
| 196 | bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size); |
| 197 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 198 | int rhashtable_expand(struct rhashtable *ht); |
| 199 | int rhashtable_shrink(struct rhashtable *ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 200 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 201 | void *rhashtable_lookup(struct rhashtable *ht, const void *key); |
| 202 | void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 203 | bool (*compare)(void *, void *), void *arg); |
Ying Xue | 7a868d1 | 2015-01-12 14:52:22 +0800 | [diff] [blame] | 204 | |
Ying Xue | db30485 | 2015-01-07 13:41:54 +0800 | [diff] [blame] | 205 | bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj); |
Ying Xue | 7a868d1 | 2015-01-12 14:52:22 +0800 | [diff] [blame] | 206 | bool rhashtable_lookup_compare_insert(struct rhashtable *ht, |
| 207 | struct rhash_head *obj, |
| 208 | bool (*compare)(void *, void *), |
| 209 | void *arg); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 210 | |
Herbert Xu | f2dba9c | 2015-02-04 07:33:23 +1100 | [diff] [blame^] | 211 | int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter); |
| 212 | void rhashtable_walk_exit(struct rhashtable_iter *iter); |
| 213 | int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU); |
| 214 | void *rhashtable_walk_next(struct rhashtable_iter *iter); |
| 215 | void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); |
| 216 | |
Thomas Graf | 97defe1 | 2015-01-02 23:00:20 +0100 | [diff] [blame] | 217 | void rhashtable_destroy(struct rhashtable *ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 218 | |
| 219 | #define rht_dereference(p, ht) \ |
| 220 | rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht)) |
| 221 | |
| 222 | #define rht_dereference_rcu(p, ht) \ |
| 223 | rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht)) |
| 224 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 225 | #define rht_dereference_bucket(p, tbl, hash) \ |
| 226 | rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 227 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 228 | #define rht_dereference_bucket_rcu(p, tbl, hash) \ |
| 229 | rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash)) |
| 230 | |
| 231 | #define rht_entry(tpos, pos, member) \ |
| 232 | ({ tpos = container_of(pos, typeof(*tpos), member); 1; }) |
| 233 | |
| 234 | /** |
| 235 | * rht_for_each_continue - continue iterating over hash chain |
| 236 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 237 | * @head: the previous &struct rhash_head to continue from |
| 238 | * @tbl: the &struct bucket_table |
| 239 | * @hash: the hash value / bucket index |
| 240 | */ |
| 241 | #define rht_for_each_continue(pos, head, tbl, hash) \ |
| 242 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 243 | !rht_is_a_nulls(pos); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 244 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 245 | |
| 246 | /** |
| 247 | * rht_for_each - iterate over hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 248 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 249 | * @tbl: the &struct bucket_table |
| 250 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 251 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 252 | #define rht_for_each(pos, tbl, hash) \ |
| 253 | rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash) |
| 254 | |
| 255 | /** |
| 256 | * rht_for_each_entry_continue - continue iterating over hash chain |
| 257 | * @tpos: the type * to use as a loop cursor. |
| 258 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 259 | * @head: the previous &struct rhash_head to continue from |
| 260 | * @tbl: the &struct bucket_table |
| 261 | * @hash: the hash value / bucket index |
| 262 | * @member: name of the &struct rhash_head within the hashable struct. |
| 263 | */ |
| 264 | #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \ |
| 265 | for (pos = rht_dereference_bucket(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 266 | (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 267 | pos = rht_dereference_bucket((pos)->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * rht_for_each_entry - iterate over hash chain of given type |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 271 | * @tpos: the type * to use as a loop cursor. |
| 272 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 273 | * @tbl: the &struct bucket_table |
| 274 | * @hash: the hash value / bucket index |
| 275 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 276 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 277 | #define rht_for_each_entry(tpos, pos, tbl, hash, member) \ |
| 278 | rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \ |
| 279 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * 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] | 283 | * @tpos: the type * to use as a loop cursor. |
| 284 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 285 | * @next: the &struct rhash_head to use as next in loop cursor. |
| 286 | * @tbl: the &struct bucket_table |
| 287 | * @hash: the hash value / bucket index |
| 288 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 289 | * |
| 290 | * This hash chain list-traversal primitive allows for the looped code to |
| 291 | * remove the loop cursor from the list. |
| 292 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 293 | #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \ |
| 294 | for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 295 | next = !rht_is_a_nulls(pos) ? \ |
| 296 | rht_dereference_bucket(pos->next, tbl, hash) : NULL; \ |
| 297 | (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ |
Patrick McHardy | 607954b | 2015-01-21 11:12:13 +0000 | [diff] [blame] | 298 | pos = next, \ |
| 299 | next = !rht_is_a_nulls(pos) ? \ |
| 300 | rht_dereference_bucket(pos->next, tbl, hash) : NULL) |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * rht_for_each_rcu_continue - continue iterating over rcu hash chain |
| 304 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 305 | * @head: the previous &struct rhash_head to continue from |
| 306 | * @tbl: the &struct bucket_table |
| 307 | * @hash: the hash value / bucket index |
| 308 | * |
| 309 | * This hash chain list-traversal primitive may safely run concurrently with |
| 310 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 311 | * traversal is guarded by rcu_read_lock(). |
| 312 | */ |
| 313 | #define rht_for_each_rcu_continue(pos, head, tbl, hash) \ |
| 314 | for (({barrier(); }), \ |
| 315 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 316 | !rht_is_a_nulls(pos); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 317 | pos = rcu_dereference_raw(pos->next)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 318 | |
| 319 | /** |
| 320 | * rht_for_each_rcu - iterate over rcu hash chain |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 321 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 322 | * @tbl: the &struct bucket_table |
| 323 | * @hash: the hash value / bucket index |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 324 | * |
| 325 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 326 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 327 | * traversal is guarded by rcu_read_lock(). |
| 328 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 329 | #define rht_for_each_rcu(pos, tbl, hash) \ |
| 330 | rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash) |
| 331 | |
| 332 | /** |
| 333 | * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain |
| 334 | * @tpos: the type * to use as a loop cursor. |
| 335 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 336 | * @head: the previous &struct rhash_head to continue from |
| 337 | * @tbl: the &struct bucket_table |
| 338 | * @hash: the hash value / bucket index |
| 339 | * @member: name of the &struct rhash_head within the hashable struct. |
| 340 | * |
| 341 | * This hash chain list-traversal primitive may safely run concurrently with |
| 342 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
| 343 | * traversal is guarded by rcu_read_lock(). |
| 344 | */ |
| 345 | #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \ |
| 346 | for (({barrier(); }), \ |
| 347 | pos = rht_dereference_bucket_rcu(head, tbl, hash); \ |
Thomas Graf | f89bd6f | 2015-01-02 23:00:21 +0100 | [diff] [blame] | 348 | (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 349 | pos = rht_dereference_bucket_rcu(pos->next, tbl, hash)) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 350 | |
| 351 | /** |
| 352 | * 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] | 353 | * @tpos: the type * to use as a loop cursor. |
| 354 | * @pos: the &struct rhash_head to use as a loop cursor. |
| 355 | * @tbl: the &struct bucket_table |
| 356 | * @hash: the hash value / bucket index |
| 357 | * @member: name of the &struct rhash_head within the hashable struct. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 358 | * |
| 359 | * This hash chain list-traversal primitive may safely run concurrently with |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 360 | * the _rcu mutation primitives such as rhashtable_insert() as long as the |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 361 | * traversal is guarded by rcu_read_lock(). |
| 362 | */ |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 363 | #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \ |
| 364 | rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\ |
| 365 | tbl, hash, member) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 366 | |
| 367 | #endif /* _LINUX_RHASHTABLE_H */ |