2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
5 * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
6 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
8 * Code partially derived from nft_hash
9 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #ifndef _LINUX_RHASHTABLE_H
18 #define _LINUX_RHASHTABLE_H
20 #include <linux/compiler.h>
21 #include <linux/errno.h>
22 #include <linux/list_nulls.h>
23 #include <linux/workqueue.h>
24 #include <linux/mutex.h>
25 #include <linux/rcupdate.h>
28 * The end of the chain is marked with a special nulls marks which has
29 * the following format:
31 * +-------+-----------------------------------------------------+-+
33 * +-------+-----------------------------------------------------+-+
35 * Base (4 bits) : Reserved to distinguish between multiple tables.
36 * Specified via &struct rhashtable_params.nulls_base.
37 * Hash (27 bits): Full hash (unmasked) of first element added to bucket
38 * 1 (1 bit) : Nulls marker (always set)
40 * The remaining bits of the next pointer remain unused for now.
42 #define RHT_BASE_BITS 4
43 #define RHT_HASH_BITS 27
44 #define RHT_BASE_SHIFT RHT_HASH_BITS
46 /* Base bits plus 1 bit for nulls marker */
47 #define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
50 struct rhash_head __rcu *next;
54 * struct bucket_table - Table of hash buckets
55 * @size: Number of hash buckets
56 * @rehash: Current bucket being rehashed
57 * @hash_rnd: Random seed to fold into hash
58 * @locks_mask: Mask to apply before accessing locks[]
59 * @locks: Array of spinlocks protecting individual buckets
60 * @walkers: List of active walkers
61 * @rcu: RCU structure for freeing the table
62 * @future_tbl: Table under construction during rehashing
63 * @buckets: size * hash buckets
69 unsigned int locks_mask;
71 struct list_head walkers;
74 struct bucket_table __rcu *future_tbl;
76 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
80 * struct rhashtable_compare_arg - Key for the function rhashtable_compare
82 * @key: Key to compare against
84 struct rhashtable_compare_arg {
85 struct rhashtable *ht;
89 typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
90 typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
91 typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
97 * struct rhashtable_params - Hash table construction parameters
98 * @nelem_hint: Hint on number of elements, should be 75% of desired size
99 * @key_len: Length of key
100 * @key_offset: Offset of key in struct to be hashed
101 * @head_offset: Offset of rhash_head in struct to be hashed
102 * @max_size: Maximum size while expanding
103 * @min_size: Minimum size while shrinking
104 * @nulls_base: Base value to generate nulls marker
105 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
106 * @hashfn: Function to hash key
107 * @obj_hashfn: Function to hash object
108 * @obj_cmpfn: Function to compare key with object
110 struct rhashtable_params {
115 unsigned int max_size;
116 unsigned int min_size;
120 rht_obj_hashfn_t obj_hashfn;
121 rht_obj_cmpfn_t obj_cmpfn;
125 * struct rhashtable - Hash table handle
127 * @nelems: Number of elements in table
128 * @p: Configuration parameters
129 * @run_work: Deferred worker to expand/shrink asynchronously
130 * @mutex: Mutex to protect current/future table swapping
131 * @being_destroyed: True if table is set up for destruction
134 struct bucket_table __rcu *tbl;
136 bool being_destroyed;
137 struct rhashtable_params p;
138 struct work_struct run_work;
143 * struct rhashtable_walker - Hash table walker
144 * @list: List entry on list of walkers
145 * @tbl: The table that we were walking over
147 struct rhashtable_walker {
148 struct list_head list;
149 struct bucket_table *tbl;
153 * struct rhashtable_iter - Hash table iterator, fits into netlink cb
154 * @ht: Table to iterate through
155 * @p: Current pointer
156 * @walker: Associated rhashtable walker
157 * @slot: Current slot
158 * @skip: Number of entries to skip in slot
160 struct rhashtable_iter {
161 struct rhashtable *ht;
162 struct rhash_head *p;
163 struct rhashtable_walker *walker;
168 static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
170 return NULLS_MARKER(ht->p.nulls_base + hash);
173 #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
174 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
176 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
178 return ((unsigned long) ptr & 1);
181 static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
183 return ((unsigned long) ptr) >> 1;
186 static inline void *rht_obj(const struct rhashtable *ht,
187 const struct rhash_head *he)
189 return (char *)he - ht->p.head_offset;
192 static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
195 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
198 static inline unsigned int rht_key_hashfn(
199 struct rhashtable *ht, const struct bucket_table *tbl,
200 const void *key, const struct rhashtable_params params)
202 /* params must be equal to ht->p if it isn't constant. */
203 unsigned key_len = __builtin_constant_p(params.key_len) ?
204 (params.key_len ?: ht->p.key_len) :
207 return rht_bucket_index(tbl, params.hashfn(key, key_len,
211 static inline unsigned int rht_head_hashfn(
212 struct rhashtable *ht, const struct bucket_table *tbl,
213 const struct rhash_head *he, const struct rhashtable_params params)
215 const char *ptr = rht_obj(ht, he);
217 return likely(params.obj_hashfn) ?
218 rht_bucket_index(tbl, params.obj_hashfn(ptr, tbl->hash_rnd)) :
219 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
223 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
225 * @tbl: current table
227 static inline bool rht_grow_above_75(const struct rhashtable *ht,
228 const struct bucket_table *tbl)
230 /* Expand table when exceeding 75% load */
231 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
232 (!ht->p.max_size || tbl->size < ht->p.max_size);
236 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
238 * @tbl: current table
240 static inline bool rht_shrink_below_30(const struct rhashtable *ht,
241 const struct bucket_table *tbl)
243 /* Shrink table beneath 30% load */
244 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
245 tbl->size > ht->p.min_size;
248 /* The bucket lock is selected based on the hash and protects mutations
249 * on a group of hash buckets.
251 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
252 * a single lock always covers both buckets which may both contains
253 * entries which link to the same bucket of the old table during resizing.
254 * This allows to simplify the locking as locking the bucket in both
255 * tables during resize always guarantee protection.
257 * IMPORTANT: When holding the bucket lock of both the old and new table
258 * during expansions and shrinking, the old bucket lock must always be
261 static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
264 return &tbl->locks[hash & tbl->locks_mask];
267 #ifdef CONFIG_PROVE_LOCKING
268 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
269 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
271 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
276 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
281 #endif /* CONFIG_PROVE_LOCKING */
283 int rhashtable_init(struct rhashtable *ht,
284 const struct rhashtable_params *params);
286 int rhashtable_insert_slow(struct rhashtable *ht, const void *key,
287 struct rhash_head *obj,
288 struct bucket_table *old_tbl);
290 int rhashtable_expand(struct rhashtable *ht);
291 int rhashtable_shrink(struct rhashtable *ht);
293 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
294 void rhashtable_walk_exit(struct rhashtable_iter *iter);
295 int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
296 void *rhashtable_walk_next(struct rhashtable_iter *iter);
297 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
299 void rhashtable_destroy(struct rhashtable *ht);
301 #define rht_dereference(p, ht) \
302 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
304 #define rht_dereference_rcu(p, ht) \
305 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
307 #define rht_dereference_bucket(p, tbl, hash) \
308 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
310 #define rht_dereference_bucket_rcu(p, tbl, hash) \
311 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
313 #define rht_entry(tpos, pos, member) \
314 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
317 * rht_for_each_continue - continue iterating over hash chain
318 * @pos: the &struct rhash_head to use as a loop cursor.
319 * @head: the previous &struct rhash_head to continue from
320 * @tbl: the &struct bucket_table
321 * @hash: the hash value / bucket index
323 #define rht_for_each_continue(pos, head, tbl, hash) \
324 for (pos = rht_dereference_bucket(head, tbl, hash); \
325 !rht_is_a_nulls(pos); \
326 pos = rht_dereference_bucket((pos)->next, tbl, hash))
329 * rht_for_each - iterate over hash chain
330 * @pos: the &struct rhash_head to use as a loop cursor.
331 * @tbl: the &struct bucket_table
332 * @hash: the hash value / bucket index
334 #define rht_for_each(pos, tbl, hash) \
335 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
338 * rht_for_each_entry_continue - continue iterating over hash chain
339 * @tpos: the type * to use as a loop cursor.
340 * @pos: the &struct rhash_head to use as a loop cursor.
341 * @head: the previous &struct rhash_head to continue from
342 * @tbl: the &struct bucket_table
343 * @hash: the hash value / bucket index
344 * @member: name of the &struct rhash_head within the hashable struct.
346 #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
347 for (pos = rht_dereference_bucket(head, tbl, hash); \
348 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
349 pos = rht_dereference_bucket((pos)->next, tbl, hash))
352 * rht_for_each_entry - iterate over hash chain of given type
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.
359 #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
360 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
364 * rht_for_each_entry_safe - safely iterate over hash chain of given type
365 * @tpos: the type * to use as a loop cursor.
366 * @pos: the &struct rhash_head to use as a loop cursor.
367 * @next: the &struct rhash_head to use as next in loop cursor.
368 * @tbl: the &struct bucket_table
369 * @hash: the hash value / bucket index
370 * @member: name of the &struct rhash_head within the hashable struct.
372 * This hash chain list-traversal primitive allows for the looped code to
373 * remove the loop cursor from the list.
375 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
376 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
377 next = !rht_is_a_nulls(pos) ? \
378 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
379 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
381 next = !rht_is_a_nulls(pos) ? \
382 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
385 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
386 * @pos: the &struct rhash_head to use as a loop cursor.
387 * @head: the previous &struct rhash_head to continue from
388 * @tbl: the &struct bucket_table
389 * @hash: the hash value / bucket index
391 * This hash chain list-traversal primitive may safely run concurrently with
392 * the _rcu mutation primitives such as rhashtable_insert() as long as the
393 * traversal is guarded by rcu_read_lock().
395 #define rht_for_each_rcu_continue(pos, head, tbl, hash) \
396 for (({barrier(); }), \
397 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
398 !rht_is_a_nulls(pos); \
399 pos = rcu_dereference_raw(pos->next))
402 * rht_for_each_rcu - iterate over rcu hash chain
403 * @pos: the &struct rhash_head to use as a loop cursor.
404 * @tbl: the &struct bucket_table
405 * @hash: the hash value / bucket index
407 * This hash chain list-traversal primitive may safely run concurrently with
408 * the _rcu mutation primitives such as rhashtable_insert() as long as the
409 * traversal is guarded by rcu_read_lock().
411 #define rht_for_each_rcu(pos, tbl, hash) \
412 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
415 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
416 * @tpos: the type * to use as a loop cursor.
417 * @pos: the &struct rhash_head to use as a loop cursor.
418 * @head: the previous &struct rhash_head to continue from
419 * @tbl: the &struct bucket_table
420 * @hash: the hash value / bucket index
421 * @member: name of the &struct rhash_head within the hashable struct.
423 * This hash chain list-traversal primitive may safely run concurrently with
424 * the _rcu mutation primitives such as rhashtable_insert() as long as the
425 * traversal is guarded by rcu_read_lock().
427 #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
428 for (({barrier(); }), \
429 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
430 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
431 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
434 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
435 * @tpos: the type * to use as a loop cursor.
436 * @pos: the &struct rhash_head to use as a loop cursor.
437 * @tbl: the &struct bucket_table
438 * @hash: the hash value / bucket index
439 * @member: name of the &struct rhash_head within the hashable struct.
441 * This hash chain list-traversal primitive may safely run concurrently with
442 * the _rcu mutation primitives such as rhashtable_insert() as long as the
443 * traversal is guarded by rcu_read_lock().
445 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
446 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
449 static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
452 struct rhashtable *ht = arg->ht;
453 const char *ptr = obj;
455 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
459 * rhashtable_lookup_fast - search hash table, inlined version
461 * @key: the pointer to the key
462 * @params: hash table parameters
464 * Computes the hash value for the key and traverses the bucket chain looking
465 * for a entry with an identical key. The first matching entry is returned.
467 * Returns the first entry on which the compare function returned true.
469 static inline void *rhashtable_lookup_fast(
470 struct rhashtable *ht, const void *key,
471 const struct rhashtable_params params)
473 struct rhashtable_compare_arg arg = {
477 const struct bucket_table *tbl;
478 struct rhash_head *he;
483 tbl = rht_dereference_rcu(ht->tbl, ht);
485 hash = rht_key_hashfn(ht, tbl, key, params);
486 rht_for_each_rcu(he, tbl, hash) {
487 if (params.obj_cmpfn ?
488 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
489 rhashtable_compare(&arg, rht_obj(ht, he)))
492 return rht_obj(ht, he);
495 /* Ensure we see any new tables. */
498 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
506 static inline int __rhashtable_insert_fast(
507 struct rhashtable *ht, const void *key, struct rhash_head *obj,
508 const struct rhashtable_params params)
510 struct rhashtable_compare_arg arg = {
515 struct bucket_table *tbl, *new_tbl;
516 struct rhash_head *head;
522 tbl = rht_dereference_rcu(ht->tbl, ht);
523 hash = rht_head_hashfn(ht, tbl, obj, params);
524 lock = rht_bucket_lock(tbl, hash);
528 /* Because we have already taken the bucket lock in tbl,
529 * if we find that future_tbl is not yet visible then
530 * that guarantees all other insertions of the same entry
531 * will also grab the bucket lock in tbl because until
532 * the rehash completes ht->tbl won't be changed.
534 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
535 if (unlikely(new_tbl)) {
536 err = rhashtable_insert_slow(ht, key, obj, new_tbl);
543 rht_for_each(head, tbl, hash) {
544 if (unlikely(!(params.obj_cmpfn ?
545 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
546 rhashtable_compare(&arg, rht_obj(ht, head)))))
553 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
555 RCU_INIT_POINTER(obj->next, head);
557 rcu_assign_pointer(tbl->buckets[hash], obj);
559 atomic_inc(&ht->nelems);
560 if (rht_grow_above_75(ht, tbl))
561 schedule_work(&ht->run_work);
564 spin_unlock_bh(lock);
571 * rhashtable_insert_fast - insert object into hash table
573 * @obj: pointer to hash head inside object
574 * @params: hash table parameters
576 * Will take a per bucket spinlock to protect against mutual mutations
577 * on the same bucket. Multiple insertions may occur in parallel unless
578 * they map to the same bucket lock.
580 * It is safe to call this function from atomic context.
582 * Will trigger an automatic deferred table resizing if the size grows
583 * beyond the watermark indicated by grow_decision() which can be passed
584 * to rhashtable_init().
586 static inline int rhashtable_insert_fast(
587 struct rhashtable *ht, struct rhash_head *obj,
588 const struct rhashtable_params params)
590 return __rhashtable_insert_fast(ht, NULL, obj, params);
594 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
596 * @obj: pointer to hash head inside object
597 * @params: hash table parameters
599 * Locks down the bucket chain in both the old and new table if a resize
600 * is in progress to ensure that writers can't remove from the old table
601 * and can't insert to the new table during the atomic operation of search
602 * and insertion. Searches for duplicates in both the old and new table if
603 * a resize is in progress.
605 * This lookup function may only be used for fixed key hash table (key_len
606 * parameter set). It will BUG() if used inappropriately.
608 * It is safe to call this function from atomic context.
610 * Will trigger an automatic deferred table resizing if the size grows
611 * beyond the watermark indicated by grow_decision() which can be passed
612 * to rhashtable_init().
614 static inline int rhashtable_lookup_insert_fast(
615 struct rhashtable *ht, struct rhash_head *obj,
616 const struct rhashtable_params params)
618 const char *key = rht_obj(ht, obj);
620 BUG_ON(ht->p.obj_hashfn);
622 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj,
627 * rhashtable_lookup_insert_key - search and insert object to hash table
631 * @obj: pointer to hash head inside object
632 * @params: hash table parameters
634 * Locks down the bucket chain in both the old and new table if a resize
635 * is in progress to ensure that writers can't remove from the old table
636 * and can't insert to the new table during the atomic operation of search
637 * and insertion. Searches for duplicates in both the old and new table if
638 * a resize is in progress.
640 * Lookups may occur in parallel with hashtable mutations and resizing.
642 * Will trigger an automatic deferred table resizing if the size grows
643 * beyond the watermark indicated by grow_decision() which can be passed
644 * to rhashtable_init().
646 * Returns zero on success.
648 static inline int rhashtable_lookup_insert_key(
649 struct rhashtable *ht, const void *key, struct rhash_head *obj,
650 const struct rhashtable_params params)
652 BUG_ON(!ht->p.obj_hashfn || !key);
654 return __rhashtable_insert_fast(ht, key, obj, params);
657 static inline int __rhashtable_remove_fast(
658 struct rhashtable *ht, struct bucket_table *tbl,
659 struct rhash_head *obj, const struct rhashtable_params params)
661 struct rhash_head __rcu **pprev;
662 struct rhash_head *he;
667 hash = rht_head_hashfn(ht, tbl, obj, params);
668 lock = rht_bucket_lock(tbl, hash);
672 pprev = &tbl->buckets[hash];
673 rht_for_each(he, tbl, hash) {
679 rcu_assign_pointer(*pprev, obj->next);
684 spin_unlock_bh(lock);
690 * rhashtable_remove_fast - remove object from hash table
692 * @obj: pointer to hash head inside object
693 * @params: hash table parameters
695 * Since the hash chain is single linked, the removal operation needs to
696 * walk the bucket chain upon removal. The removal operation is thus
697 * considerable slow if the hash table is not correctly sized.
699 * Will automatically shrink the table via rhashtable_expand() if the
700 * shrink_decision function specified at rhashtable_init() returns true.
702 * Returns zero on success, -ENOENT if the entry could not be found.
704 static inline int rhashtable_remove_fast(
705 struct rhashtable *ht, struct rhash_head *obj,
706 const struct rhashtable_params params)
708 struct bucket_table *tbl;
713 tbl = rht_dereference_rcu(ht->tbl, ht);
715 /* Because we have already taken (and released) the bucket
716 * lock in old_tbl, if we find that future_tbl is not yet
717 * visible then that guarantees the entry to still be in
718 * the old tbl if it exists.
720 while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) &&
721 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
727 atomic_dec(&ht->nelems);
728 if (rht_shrink_below_30(ht, tbl))
729 schedule_work(&ht->run_work);
737 #endif /* _LINUX_RHASHTABLE_H */