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 * @locks_mask: Mask to apply before accessing locks[]
53 * @locks: Array of spinlocks protecting individual buckets
54 * @buckets: size * hash buckets
58 unsigned int locks_mask;
61 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
64 typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
65 typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
70 * struct rhashtable_params - Hash table construction parameters
71 * @nelem_hint: Hint on number of elements, should be 75% of desired size
72 * @key_len: Length of key
73 * @key_offset: Offset of key in struct to be hashed
74 * @head_offset: Offset of rhash_head in struct to be hashed
75 * @hash_rnd: Seed to use while hashing
76 * @max_shift: Maximum number of shifts while expanding
77 * @min_shift: Minimum number of shifts while shrinking
78 * @nulls_base: Base value to generate nulls marker
79 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
80 * @hashfn: Function to hash key
81 * @obj_hashfn: Function to hash object
82 * @grow_decision: If defined, may return true if table should expand
83 * @shrink_decision: If defined, may return true if table should shrink
85 * Note: when implementing the grow and shrink decision function, min/max
86 * shift must be enforced, otherwise, resizing watermarks they set may be
89 struct rhashtable_params {
100 rht_obj_hashfn_t obj_hashfn;
101 bool (*grow_decision)(const struct rhashtable *ht,
103 bool (*shrink_decision)(const struct rhashtable *ht,
108 * struct rhashtable - Hash table handle
110 * @future_tbl: Table under construction during expansion/shrinking
111 * @nelems: Number of elements in table
112 * @shift: Current size (1 << shift)
113 * @p: Configuration parameters
114 * @run_work: Deferred worker to expand/shrink asynchronously
115 * @mutex: Mutex to protect current/future table swapping
116 * @walkers: List of active walkers
117 * @being_destroyed: True if table is set up for destruction
120 struct bucket_table __rcu *tbl;
121 struct bucket_table __rcu *future_tbl;
124 struct rhashtable_params p;
125 struct work_struct run_work;
127 struct list_head walkers;
128 bool being_destroyed;
132 * struct rhashtable_walker - Hash table walker
133 * @list: List entry on list of walkers
134 * @resize: Resize event occured
136 struct rhashtable_walker {
137 struct list_head list;
142 * struct rhashtable_iter - Hash table iterator, fits into netlink cb
143 * @ht: Table to iterate through
144 * @p: Current pointer
145 * @walker: Associated rhashtable walker
146 * @slot: Current slot
147 * @skip: Number of entries to skip in slot
149 struct rhashtable_iter {
150 struct rhashtable *ht;
151 struct rhash_head *p;
152 struct rhashtable_walker *walker;
157 static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
159 return NULLS_MARKER(ht->p.nulls_base + hash);
162 #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
163 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
165 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
167 return ((unsigned long) ptr & 1);
170 static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
172 return ((unsigned long) ptr) >> 1;
175 #ifdef CONFIG_PROVE_LOCKING
176 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
177 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
179 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
184 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
189 #endif /* CONFIG_PROVE_LOCKING */
191 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
193 void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
194 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
196 bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
197 bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
199 int rhashtable_expand(struct rhashtable *ht);
200 int rhashtable_shrink(struct rhashtable *ht);
202 void *rhashtable_lookup(struct rhashtable *ht, const void *key);
203 void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
204 bool (*compare)(void *, void *), void *arg);
206 bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj);
207 bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
208 struct rhash_head *obj,
209 bool (*compare)(void *, void *),
212 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
213 void rhashtable_walk_exit(struct rhashtable_iter *iter);
214 int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
215 void *rhashtable_walk_next(struct rhashtable_iter *iter);
216 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
218 void rhashtable_destroy(struct rhashtable *ht);
220 #define rht_dereference(p, ht) \
221 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
223 #define rht_dereference_rcu(p, ht) \
224 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
226 #define rht_dereference_bucket(p, tbl, hash) \
227 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
229 #define rht_dereference_bucket_rcu(p, tbl, hash) \
230 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
232 #define rht_entry(tpos, pos, member) \
233 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
236 * rht_for_each_continue - continue iterating over hash chain
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
242 #define rht_for_each_continue(pos, head, tbl, hash) \
243 for (pos = rht_dereference_bucket(head, tbl, hash); \
244 !rht_is_a_nulls(pos); \
245 pos = rht_dereference_bucket((pos)->next, tbl, hash))
248 * rht_for_each - iterate over hash chain
249 * @pos: the &struct rhash_head to use as a loop cursor.
250 * @tbl: the &struct bucket_table
251 * @hash: the hash value / bucket index
253 #define rht_for_each(pos, tbl, hash) \
254 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
257 * rht_for_each_entry_continue - continue iterating over hash chain
258 * @tpos: the type * to use as a loop cursor.
259 * @pos: the &struct rhash_head to use as a loop cursor.
260 * @head: the previous &struct rhash_head to continue from
261 * @tbl: the &struct bucket_table
262 * @hash: the hash value / bucket index
263 * @member: name of the &struct rhash_head within the hashable struct.
265 #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
266 for (pos = rht_dereference_bucket(head, tbl, hash); \
267 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
268 pos = rht_dereference_bucket((pos)->next, tbl, hash))
271 * rht_for_each_entry - iterate over hash chain of given type
272 * @tpos: the type * to use as a loop cursor.
273 * @pos: the &struct rhash_head to use as a loop cursor.
274 * @tbl: the &struct bucket_table
275 * @hash: the hash value / bucket index
276 * @member: name of the &struct rhash_head within the hashable struct.
278 #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
279 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
283 * rht_for_each_entry_safe - safely iterate over hash chain of given type
284 * @tpos: the type * to use as a loop cursor.
285 * @pos: the &struct rhash_head to use as a loop cursor.
286 * @next: the &struct rhash_head to use as next in loop cursor.
287 * @tbl: the &struct bucket_table
288 * @hash: the hash value / bucket index
289 * @member: name of the &struct rhash_head within the hashable struct.
291 * This hash chain list-traversal primitive allows for the looped code to
292 * remove the loop cursor from the list.
294 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
295 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
296 next = !rht_is_a_nulls(pos) ? \
297 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
298 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
300 next = !rht_is_a_nulls(pos) ? \
301 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
304 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
305 * @pos: the &struct rhash_head to use as a loop cursor.
306 * @head: the previous &struct rhash_head to continue from
307 * @tbl: the &struct bucket_table
308 * @hash: the hash value / bucket index
310 * This hash chain list-traversal primitive may safely run concurrently with
311 * the _rcu mutation primitives such as rhashtable_insert() as long as the
312 * traversal is guarded by rcu_read_lock().
314 #define rht_for_each_rcu_continue(pos, head, tbl, hash) \
315 for (({barrier(); }), \
316 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
317 !rht_is_a_nulls(pos); \
318 pos = rcu_dereference_raw(pos->next))
321 * rht_for_each_rcu - iterate over rcu hash chain
322 * @pos: the &struct rhash_head to use as a loop cursor.
323 * @tbl: the &struct bucket_table
324 * @hash: the hash value / bucket index
326 * This hash chain list-traversal primitive may safely run concurrently with
327 * the _rcu mutation primitives such as rhashtable_insert() as long as the
328 * traversal is guarded by rcu_read_lock().
330 #define rht_for_each_rcu(pos, tbl, hash) \
331 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
334 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
335 * @tpos: the type * to use as a loop cursor.
336 * @pos: the &struct rhash_head to use as a loop cursor.
337 * @head: the previous &struct rhash_head to continue from
338 * @tbl: the &struct bucket_table
339 * @hash: the hash value / bucket index
340 * @member: name of the &struct rhash_head within the hashable struct.
342 * This hash chain list-traversal primitive may safely run concurrently with
343 * the _rcu mutation primitives such as rhashtable_insert() as long as the
344 * traversal is guarded by rcu_read_lock().
346 #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
347 for (({barrier(); }), \
348 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
349 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
350 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
353 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
354 * @tpos: the type * to use as a loop cursor.
355 * @pos: the &struct rhash_head to use as a loop cursor.
356 * @tbl: the &struct bucket_table
357 * @hash: the hash value / bucket index
358 * @member: name of the &struct rhash_head within the hashable struct.
360 * This hash chain list-traversal primitive may safely run concurrently with
361 * the _rcu mutation primitives such as rhashtable_insert() as long as the
362 * traversal is guarded by rcu_read_lock().
364 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
365 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
368 #endif /* _LINUX_RHASHTABLE_H */