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