2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
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
11 * Code partially derived from nft_hash
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.
18 #ifndef _LINUX_RHASHTABLE_H
19 #define _LINUX_RHASHTABLE_H
21 #include <linux/compiler.h>
22 #include <linux/list_nulls.h>
23 #include <linux/workqueue.h>
24 #include <linux/mutex.h>
27 * The end of the chain is marked with a special nulls marks which has
28 * the following format:
30 * +-------+-----------------------------------------------------+-+
32 * +-------+-----------------------------------------------------+-+
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)
39 * The remaining bits of the next pointer remain unused for now.
41 #define RHT_BASE_BITS 4
42 #define RHT_HASH_BITS 27
43 #define RHT_BASE_SHIFT RHT_HASH_BITS
46 struct rhash_head __rcu *next;
50 * struct bucket_table - Table of hash buckets
51 * @size: Number of hash buckets
52 * @hash_rnd: Random seed to fold into hash
53 * @shift: Current size (1 << shift)
54 * @locks_mask: Mask to apply before accessing locks[]
55 * @locks: Array of spinlocks protecting individual buckets
56 * @walkers: List of active walkers
57 * @rcu: RCU structure for freeing the table
58 * @buckets: size * hash buckets
64 unsigned int locks_mask;
66 struct list_head walkers;
69 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
72 typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
73 typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
78 * struct rhashtable_params - Hash table construction parameters
79 * @nelem_hint: Hint on number of elements, should be 75% of desired size
80 * @key_len: Length of key
81 * @key_offset: Offset of key in struct to be hashed
82 * @head_offset: Offset of rhash_head in struct to be hashed
83 * @max_shift: Maximum number of shifts while expanding
84 * @min_shift: Minimum number of shifts while shrinking
85 * @nulls_base: Base value to generate nulls marker
86 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
87 * @hashfn: Function to hash key
88 * @obj_hashfn: Function to hash object
90 struct rhashtable_params {
100 rht_obj_hashfn_t obj_hashfn;
104 * struct rhashtable - Hash table handle
106 * @future_tbl: Table under construction during expansion/shrinking
107 * @nelems: Number of elements in table
108 * @p: Configuration parameters
109 * @run_work: Deferred worker to expand/shrink asynchronously
110 * @mutex: Mutex to protect current/future table swapping
111 * @being_destroyed: True if table is set up for destruction
114 struct bucket_table __rcu *tbl;
115 struct bucket_table __rcu *future_tbl;
117 bool being_destroyed;
118 struct rhashtable_params p;
119 struct work_struct run_work;
124 * struct rhashtable_walker - Hash table walker
125 * @list: List entry on list of walkers
126 * @tbl: The table that we were walking over
128 struct rhashtable_walker {
129 struct list_head list;
130 struct bucket_table *tbl;
134 * struct rhashtable_iter - Hash table iterator, fits into netlink cb
135 * @ht: Table to iterate through
136 * @p: Current pointer
137 * @walker: Associated rhashtable walker
138 * @slot: Current slot
139 * @skip: Number of entries to skip in slot
141 struct rhashtable_iter {
142 struct rhashtable *ht;
143 struct rhash_head *p;
144 struct rhashtable_walker *walker;
149 static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
151 return NULLS_MARKER(ht->p.nulls_base + hash);
154 #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
155 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
157 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
159 return ((unsigned long) ptr & 1);
162 static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
164 return ((unsigned long) ptr) >> 1;
167 #ifdef CONFIG_PROVE_LOCKING
168 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
169 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
171 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
176 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
181 #endif /* CONFIG_PROVE_LOCKING */
183 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
185 void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
186 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
188 int rhashtable_expand(struct rhashtable *ht);
189 int rhashtable_shrink(struct rhashtable *ht);
191 void *rhashtable_lookup(struct rhashtable *ht, const void *key);
192 void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
193 bool (*compare)(void *, void *), void *arg);
195 bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj);
196 bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
197 struct rhash_head *obj,
198 bool (*compare)(void *, void *),
201 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
202 void rhashtable_walk_exit(struct rhashtable_iter *iter);
203 int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
204 void *rhashtable_walk_next(struct rhashtable_iter *iter);
205 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
207 void rhashtable_destroy(struct rhashtable *ht);
209 #define rht_dereference(p, ht) \
210 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
212 #define rht_dereference_rcu(p, ht) \
213 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
215 #define rht_dereference_bucket(p, tbl, hash) \
216 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
218 #define rht_dereference_bucket_rcu(p, tbl, hash) \
219 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
221 #define rht_entry(tpos, pos, member) \
222 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
225 * rht_for_each_continue - continue iterating over hash chain
226 * @pos: the &struct rhash_head to use as a loop cursor.
227 * @head: the previous &struct rhash_head to continue from
228 * @tbl: the &struct bucket_table
229 * @hash: the hash value / bucket index
231 #define rht_for_each_continue(pos, head, tbl, hash) \
232 for (pos = rht_dereference_bucket(head, tbl, hash); \
233 !rht_is_a_nulls(pos); \
234 pos = rht_dereference_bucket((pos)->next, tbl, hash))
237 * rht_for_each - iterate over hash chain
238 * @pos: the &struct rhash_head to use as a loop cursor.
239 * @tbl: the &struct bucket_table
240 * @hash: the hash value / bucket index
242 #define rht_for_each(pos, tbl, hash) \
243 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
246 * rht_for_each_entry_continue - continue iterating over hash chain
247 * @tpos: the type * to use as a loop cursor.
248 * @pos: the &struct rhash_head to use as a loop cursor.
249 * @head: the previous &struct rhash_head to continue from
250 * @tbl: the &struct bucket_table
251 * @hash: the hash value / bucket index
252 * @member: name of the &struct rhash_head within the hashable struct.
254 #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
255 for (pos = rht_dereference_bucket(head, tbl, hash); \
256 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
257 pos = rht_dereference_bucket((pos)->next, tbl, hash))
260 * rht_for_each_entry - iterate over hash chain of given type
261 * @tpos: the type * to use as a loop cursor.
262 * @pos: the &struct rhash_head to use as a loop cursor.
263 * @tbl: the &struct bucket_table
264 * @hash: the hash value / bucket index
265 * @member: name of the &struct rhash_head within the hashable struct.
267 #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
268 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
272 * rht_for_each_entry_safe - safely iterate over hash chain of given type
273 * @tpos: the type * to use as a loop cursor.
274 * @pos: the &struct rhash_head to use as a loop cursor.
275 * @next: the &struct rhash_head to use as next in loop cursor.
276 * @tbl: the &struct bucket_table
277 * @hash: the hash value / bucket index
278 * @member: name of the &struct rhash_head within the hashable struct.
280 * This hash chain list-traversal primitive allows for the looped code to
281 * remove the loop cursor from the list.
283 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
284 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
285 next = !rht_is_a_nulls(pos) ? \
286 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
287 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
289 next = !rht_is_a_nulls(pos) ? \
290 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
293 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
294 * @pos: the &struct rhash_head to use as a loop cursor.
295 * @head: the previous &struct rhash_head to continue from
296 * @tbl: the &struct bucket_table
297 * @hash: the hash value / bucket index
299 * This hash chain list-traversal primitive may safely run concurrently with
300 * the _rcu mutation primitives such as rhashtable_insert() as long as the
301 * traversal is guarded by rcu_read_lock().
303 #define rht_for_each_rcu_continue(pos, head, tbl, hash) \
304 for (({barrier(); }), \
305 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
306 !rht_is_a_nulls(pos); \
307 pos = rcu_dereference_raw(pos->next))
310 * rht_for_each_rcu - iterate over rcu hash chain
311 * @pos: the &struct rhash_head to use as a loop cursor.
312 * @tbl: the &struct bucket_table
313 * @hash: the hash value / bucket index
315 * This hash chain list-traversal primitive may safely run concurrently with
316 * the _rcu mutation primitives such as rhashtable_insert() as long as the
317 * traversal is guarded by rcu_read_lock().
319 #define rht_for_each_rcu(pos, tbl, hash) \
320 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
323 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
324 * @tpos: the type * to use as a loop cursor.
325 * @pos: the &struct rhash_head to use as a loop cursor.
326 * @head: the previous &struct rhash_head to continue from
327 * @tbl: the &struct bucket_table
328 * @hash: the hash value / bucket index
329 * @member: name of the &struct rhash_head within the hashable struct.
331 * This hash chain list-traversal primitive may safely run concurrently with
332 * the _rcu mutation primitives such as rhashtable_insert() as long as the
333 * traversal is guarded by rcu_read_lock().
335 #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
336 for (({barrier(); }), \
337 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
338 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
339 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
342 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
343 * @tpos: the type * to use as a loop cursor.
344 * @pos: the &struct rhash_head to use as a loop cursor.
345 * @tbl: the &struct bucket_table
346 * @hash: the hash value / bucket index
347 * @member: name of the &struct rhash_head within the hashable struct.
349 * This hash chain list-traversal primitive may safely run concurrently with
350 * the _rcu mutation primitives such as rhashtable_insert() as long as the
351 * traversal is guarded by rcu_read_lock().
353 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
354 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
357 #endif /* _LINUX_RHASHTABLE_H */