2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 * Based on the following paper:
8 * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf
10 * Code partially derived from nft_hash
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 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/log2.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/vmalloc.h>
24 #include <linux/jhash.h>
25 #include <linux/random.h>
26 #include <linux/rhashtable.h>
27 #include <linux/err.h>
29 #define HASH_DEFAULT_SIZE 64UL
30 #define HASH_MIN_SIZE 4UL
31 #define BUCKET_LOCKS_PER_CPU 128UL
33 /* Base bits plus 1 bit for nulls marker */
34 #define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
36 /* The bucket lock is selected based on the hash and protects mutations
37 * on a group of hash buckets.
39 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
40 * a single lock always covers both buckets which may both contains
41 * entries which link to the same bucket of the old table during resizing.
42 * This allows to simplify the locking as locking the bucket in both
43 * tables during resize always guarantee protection.
45 * IMPORTANT: When holding the bucket lock of both the old and new table
46 * during expansions and shrinking, the old bucket lock must always be
49 static spinlock_t
*bucket_lock(const struct bucket_table
*tbl
, u32 hash
)
51 return &tbl
->locks
[hash
& tbl
->locks_mask
];
54 static void *rht_obj(const struct rhashtable
*ht
, const struct rhash_head
*he
)
56 return (void *) he
- ht
->p
.head_offset
;
59 static u32
rht_bucket_index(const struct bucket_table
*tbl
, u32 hash
)
61 return (hash
>> HASH_RESERVED_SPACE
) & (tbl
->size
- 1);
64 static u32
key_hashfn(struct rhashtable
*ht
, const struct bucket_table
*tbl
,
67 return rht_bucket_index(tbl
, ht
->p
.hashfn(key
, ht
->p
.key_len
,
71 static u32
head_hashfn(struct rhashtable
*ht
,
72 const struct bucket_table
*tbl
,
73 const struct rhash_head
*he
)
75 const char *ptr
= rht_obj(ht
, he
);
77 return likely(ht
->p
.key_len
) ?
78 key_hashfn(ht
, tbl
, ptr
+ ht
->p
.key_offset
) :
79 rht_bucket_index(tbl
, ht
->p
.obj_hashfn(ptr
, tbl
->hash_rnd
));
82 #ifdef CONFIG_PROVE_LOCKING
83 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
85 int lockdep_rht_mutex_is_held(struct rhashtable
*ht
)
87 return (debug_locks
) ? lockdep_is_held(&ht
->mutex
) : 1;
89 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held
);
91 int lockdep_rht_bucket_is_held(const struct bucket_table
*tbl
, u32 hash
)
93 spinlock_t
*lock
= bucket_lock(tbl
, hash
);
95 return (debug_locks
) ? lockdep_is_held(lock
) : 1;
97 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held
);
99 #define ASSERT_RHT_MUTEX(HT)
103 static int alloc_bucket_locks(struct rhashtable
*ht
, struct bucket_table
*tbl
)
105 unsigned int i
, size
;
106 #if defined(CONFIG_PROVE_LOCKING)
107 unsigned int nr_pcpus
= 2;
109 unsigned int nr_pcpus
= num_possible_cpus();
112 nr_pcpus
= min_t(unsigned int, nr_pcpus
, 32UL);
113 size
= roundup_pow_of_two(nr_pcpus
* ht
->p
.locks_mul
);
115 /* Never allocate more than 0.5 locks per bucket */
116 size
= min_t(unsigned int, size
, tbl
->size
>> 1);
118 if (sizeof(spinlock_t
) != 0) {
120 if (size
* sizeof(spinlock_t
) > PAGE_SIZE
)
121 tbl
->locks
= vmalloc(size
* sizeof(spinlock_t
));
124 tbl
->locks
= kmalloc_array(size
, sizeof(spinlock_t
),
128 for (i
= 0; i
< size
; i
++)
129 spin_lock_init(&tbl
->locks
[i
]);
131 tbl
->locks_mask
= size
- 1;
136 static void bucket_table_free(const struct bucket_table
*tbl
)
144 static void bucket_table_free_rcu(struct rcu_head
*head
)
146 bucket_table_free(container_of(head
, struct bucket_table
, rcu
));
149 static struct bucket_table
*bucket_table_alloc(struct rhashtable
*ht
,
152 struct bucket_table
*tbl
= NULL
;
156 size
= sizeof(*tbl
) + nbuckets
* sizeof(tbl
->buckets
[0]);
157 if (size
<= (PAGE_SIZE
<< PAGE_ALLOC_COSTLY_ORDER
))
158 tbl
= kzalloc(size
, GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
);
164 tbl
->size
= nbuckets
;
165 tbl
->shift
= ilog2(nbuckets
);
167 if (alloc_bucket_locks(ht
, tbl
) < 0) {
168 bucket_table_free(tbl
);
172 INIT_LIST_HEAD(&tbl
->walkers
);
174 get_random_bytes(&tbl
->hash_rnd
, sizeof(tbl
->hash_rnd
));
176 for (i
= 0; i
< nbuckets
; i
++)
177 INIT_RHT_NULLS_HEAD(tbl
->buckets
[i
], ht
, i
);
183 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
185 * @tbl: current table
187 static bool rht_grow_above_75(const struct rhashtable
*ht
,
188 const struct bucket_table
*tbl
)
190 /* Expand table when exceeding 75% load */
191 return atomic_read(&ht
->nelems
) > (tbl
->size
/ 4 * 3) &&
192 (!ht
->p
.max_shift
|| tbl
->shift
< ht
->p
.max_shift
);
196 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
198 * @tbl: current table
200 static bool rht_shrink_below_30(const struct rhashtable
*ht
,
201 const struct bucket_table
*tbl
)
203 /* Shrink table beneath 30% load */
204 return atomic_read(&ht
->nelems
) < (tbl
->size
* 3 / 10) &&
205 tbl
->shift
> ht
->p
.min_shift
;
208 static int rhashtable_rehash_one(struct rhashtable
*ht
, unsigned old_hash
)
210 struct bucket_table
*new_tbl
= rht_dereference(ht
->future_tbl
, ht
);
211 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
212 struct rhash_head __rcu
**pprev
= &old_tbl
->buckets
[old_hash
];
214 struct rhash_head
*head
, *next
, *entry
;
215 spinlock_t
*new_bucket_lock
;
218 rht_for_each(entry
, old_tbl
, old_hash
) {
220 next
= rht_dereference_bucket(entry
->next
, old_tbl
, old_hash
);
222 if (rht_is_a_nulls(next
))
225 pprev
= &entry
->next
;
231 new_hash
= head_hashfn(ht
, new_tbl
, entry
);
233 new_bucket_lock
= bucket_lock(new_tbl
, new_hash
);
235 spin_lock_nested(new_bucket_lock
, SINGLE_DEPTH_NESTING
);
236 head
= rht_dereference_bucket(new_tbl
->buckets
[new_hash
],
239 if (rht_is_a_nulls(head
))
240 INIT_RHT_NULLS_HEAD(entry
->next
, ht
, new_hash
);
242 RCU_INIT_POINTER(entry
->next
, head
);
244 rcu_assign_pointer(new_tbl
->buckets
[new_hash
], entry
);
245 spin_unlock(new_bucket_lock
);
247 rcu_assign_pointer(*pprev
, next
);
253 static void rhashtable_rehash_chain(struct rhashtable
*ht
, unsigned old_hash
)
255 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
256 spinlock_t
*old_bucket_lock
;
258 old_bucket_lock
= bucket_lock(old_tbl
, old_hash
);
260 spin_lock_bh(old_bucket_lock
);
261 while (!rhashtable_rehash_one(ht
, old_hash
))
263 spin_unlock_bh(old_bucket_lock
);
266 static void rhashtable_rehash(struct rhashtable
*ht
,
267 struct bucket_table
*new_tbl
)
269 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
270 struct rhashtable_walker
*walker
;
273 /* Make insertions go into the new, empty table right away. Deletions
274 * and lookups will be attempted in both tables until we synchronize.
275 * The synchronize_rcu() guarantees for the new table to be picked up
276 * so no new additions go into the old table while we relink.
278 rcu_assign_pointer(ht
->future_tbl
, new_tbl
);
280 /* Ensure the new table is visible to readers. */
283 for (old_hash
= 0; old_hash
< old_tbl
->size
; old_hash
++)
284 rhashtable_rehash_chain(ht
, old_hash
);
286 /* Publish the new table pointer. */
287 rcu_assign_pointer(ht
->tbl
, new_tbl
);
289 list_for_each_entry(walker
, &old_tbl
->walkers
, list
)
292 /* Wait for readers. All new readers will see the new
293 * table, and thus no references to the old table will
296 call_rcu(&old_tbl
->rcu
, bucket_table_free_rcu
);
300 * rhashtable_expand - Expand hash table while allowing concurrent lookups
301 * @ht: the hash table to expand
303 * A secondary bucket array is allocated and the hash entries are migrated.
305 * This function may only be called in a context where it is safe to call
306 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
308 * The caller must ensure that no concurrent resizing occurs by holding
311 * It is valid to have concurrent insertions and deletions protected by per
312 * bucket locks or concurrent RCU protected lookups and traversals.
314 int rhashtable_expand(struct rhashtable
*ht
)
316 struct bucket_table
*new_tbl
, *old_tbl
= rht_dereference(ht
->tbl
, ht
);
318 ASSERT_RHT_MUTEX(ht
);
320 new_tbl
= bucket_table_alloc(ht
, old_tbl
->size
* 2);
324 rhashtable_rehash(ht
, new_tbl
);
327 EXPORT_SYMBOL_GPL(rhashtable_expand
);
330 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
331 * @ht: the hash table to shrink
333 * This function may only be called in a context where it is safe to call
334 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
336 * The caller must ensure that no concurrent resizing occurs by holding
339 * The caller must ensure that no concurrent table mutations take place.
340 * It is however valid to have concurrent lookups if they are RCU protected.
342 * It is valid to have concurrent insertions and deletions protected by per
343 * bucket locks or concurrent RCU protected lookups and traversals.
345 int rhashtable_shrink(struct rhashtable
*ht
)
347 struct bucket_table
*new_tbl
, *old_tbl
= rht_dereference(ht
->tbl
, ht
);
349 ASSERT_RHT_MUTEX(ht
);
351 new_tbl
= bucket_table_alloc(ht
, old_tbl
->size
/ 2);
355 rhashtable_rehash(ht
, new_tbl
);
358 EXPORT_SYMBOL_GPL(rhashtable_shrink
);
360 static void rht_deferred_worker(struct work_struct
*work
)
362 struct rhashtable
*ht
;
363 struct bucket_table
*tbl
;
365 ht
= container_of(work
, struct rhashtable
, run_work
);
366 mutex_lock(&ht
->mutex
);
367 if (ht
->being_destroyed
)
370 tbl
= rht_dereference(ht
->tbl
, ht
);
372 if (rht_grow_above_75(ht
, tbl
))
373 rhashtable_expand(ht
);
374 else if (rht_shrink_below_30(ht
, tbl
))
375 rhashtable_shrink(ht
);
377 mutex_unlock(&ht
->mutex
);
380 static bool __rhashtable_insert(struct rhashtable
*ht
, struct rhash_head
*obj
,
381 bool (*compare
)(void *, void *), void *arg
)
383 struct bucket_table
*tbl
, *old_tbl
;
384 struct rhash_head
*head
;
385 bool no_resize_running
;
391 old_tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
392 hash
= head_hashfn(ht
, old_tbl
, obj
);
394 spin_lock_bh(bucket_lock(old_tbl
, hash
));
396 /* Because we have already taken the bucket lock in old_tbl,
397 * if we find that future_tbl is not yet visible then that
398 * guarantees all other insertions of the same entry will
399 * also grab the bucket lock in old_tbl because until the
400 * rehash completes ht->tbl won't be changed.
402 tbl
= rht_dereference_rcu(ht
->future_tbl
, ht
);
403 if (tbl
!= old_tbl
) {
404 hash
= head_hashfn(ht
, tbl
, obj
);
405 spin_lock_nested(bucket_lock(tbl
, hash
), SINGLE_DEPTH_NESTING
);
409 rhashtable_lookup_compare(ht
, rht_obj(ht
, obj
) + ht
->p
.key_offset
,
415 no_resize_running
= tbl
== old_tbl
;
417 head
= rht_dereference_bucket(tbl
->buckets
[hash
], tbl
, hash
);
419 if (rht_is_a_nulls(head
))
420 INIT_RHT_NULLS_HEAD(obj
->next
, ht
, hash
);
422 RCU_INIT_POINTER(obj
->next
, head
);
424 rcu_assign_pointer(tbl
->buckets
[hash
], obj
);
426 atomic_inc(&ht
->nelems
);
427 if (no_resize_running
&& rht_grow_above_75(ht
, tbl
))
428 schedule_work(&ht
->run_work
);
431 if (tbl
!= old_tbl
) {
432 hash
= head_hashfn(ht
, tbl
, obj
);
433 spin_unlock(bucket_lock(tbl
, hash
));
436 hash
= head_hashfn(ht
, old_tbl
, obj
);
437 spin_unlock_bh(bucket_lock(old_tbl
, hash
));
445 * rhashtable_insert - insert object into hash table
447 * @obj: pointer to hash head inside object
449 * Will take a per bucket spinlock to protect against mutual mutations
450 * on the same bucket. Multiple insertions may occur in parallel unless
451 * they map to the same bucket lock.
453 * It is safe to call this function from atomic context.
455 * Will trigger an automatic deferred table resizing if the size grows
456 * beyond the watermark indicated by grow_decision() which can be passed
457 * to rhashtable_init().
459 void rhashtable_insert(struct rhashtable
*ht
, struct rhash_head
*obj
)
461 __rhashtable_insert(ht
, obj
, NULL
, NULL
);
463 EXPORT_SYMBOL_GPL(rhashtable_insert
);
465 static bool __rhashtable_remove(struct rhashtable
*ht
,
466 struct bucket_table
*tbl
,
467 struct rhash_head
*obj
)
469 struct rhash_head __rcu
**pprev
;
470 struct rhash_head
*he
;
475 hash
= head_hashfn(ht
, tbl
, obj
);
476 lock
= bucket_lock(tbl
, hash
);
480 pprev
= &tbl
->buckets
[hash
];
481 rht_for_each(he
, tbl
, hash
) {
487 rcu_assign_pointer(*pprev
, obj
->next
);
492 spin_unlock_bh(lock
);
498 * rhashtable_remove - remove object from hash table
500 * @obj: pointer to hash head inside object
502 * Since the hash chain is single linked, the removal operation needs to
503 * walk the bucket chain upon removal. The removal operation is thus
504 * considerable slow if the hash table is not correctly sized.
506 * Will automatically shrink the table via rhashtable_expand() if the
507 * shrink_decision function specified at rhashtable_init() returns true.
509 * The caller must ensure that no concurrent table mutations occur. It is
510 * however valid to have concurrent lookups if they are RCU protected.
512 bool rhashtable_remove(struct rhashtable
*ht
, struct rhash_head
*obj
)
514 struct bucket_table
*tbl
, *old_tbl
;
519 old_tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
520 ret
= __rhashtable_remove(ht
, old_tbl
, obj
);
522 /* Because we have already taken (and released) the bucket
523 * lock in old_tbl, if we find that future_tbl is not yet
524 * visible then that guarantees the entry to still be in
525 * old_tbl if it exists.
527 tbl
= rht_dereference_rcu(ht
->future_tbl
, ht
);
528 if (!ret
&& old_tbl
!= tbl
)
529 ret
= __rhashtable_remove(ht
, tbl
, obj
);
532 bool no_resize_running
= tbl
== old_tbl
;
534 atomic_dec(&ht
->nelems
);
535 if (no_resize_running
&& rht_shrink_below_30(ht
, tbl
))
536 schedule_work(&ht
->run_work
);
543 EXPORT_SYMBOL_GPL(rhashtable_remove
);
545 struct rhashtable_compare_arg
{
546 struct rhashtable
*ht
;
550 static bool rhashtable_compare(void *ptr
, void *arg
)
552 struct rhashtable_compare_arg
*x
= arg
;
553 struct rhashtable
*ht
= x
->ht
;
555 return !memcmp(ptr
+ ht
->p
.key_offset
, x
->key
, ht
->p
.key_len
);
559 * rhashtable_lookup - lookup key in hash table
561 * @key: pointer to key
563 * Computes the hash value for the key and traverses the bucket chain looking
564 * for a entry with an identical key. The first matching entry is returned.
566 * This lookup function may only be used for fixed key hash table (key_len
567 * parameter set). It will BUG() if used inappropriately.
569 * Lookups may occur in parallel with hashtable mutations and resizing.
571 void *rhashtable_lookup(struct rhashtable
*ht
, const void *key
)
573 struct rhashtable_compare_arg arg
= {
578 BUG_ON(!ht
->p
.key_len
);
580 return rhashtable_lookup_compare(ht
, key
, &rhashtable_compare
, &arg
);
582 EXPORT_SYMBOL_GPL(rhashtable_lookup
);
585 * rhashtable_lookup_compare - search hash table with compare function
587 * @key: the pointer to the key
588 * @compare: compare function, must return true on match
589 * @arg: argument passed on to compare function
591 * Traverses the bucket chain behind the provided hash value and calls the
592 * specified compare function for each entry.
594 * Lookups may occur in parallel with hashtable mutations and resizing.
596 * Returns the first entry on which the compare function returned true.
598 void *rhashtable_lookup_compare(struct rhashtable
*ht
, const void *key
,
599 bool (*compare
)(void *, void *), void *arg
)
601 const struct bucket_table
*tbl
, *old_tbl
;
602 struct rhash_head
*he
;
607 tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
609 hash
= key_hashfn(ht
, tbl
, key
);
610 rht_for_each_rcu(he
, tbl
, hash
) {
611 if (!compare(rht_obj(ht
, he
), arg
))
614 return rht_obj(ht
, he
);
617 /* Ensure we see any new tables. */
621 tbl
= rht_dereference_rcu(ht
->future_tbl
, ht
);
622 if (unlikely(tbl
!= old_tbl
))
628 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare
);
631 * rhashtable_lookup_insert - lookup and insert object into hash table
633 * @obj: pointer to hash head inside object
635 * Locks down the bucket chain in both the old and new table if a resize
636 * is in progress to ensure that writers can't remove from the old table
637 * and can't insert to the new table during the atomic operation of search
638 * and insertion. Searches for duplicates in both the old and new table if
639 * a resize is in progress.
641 * This lookup function may only be used for fixed key hash table (key_len
642 * parameter set). It will BUG() if used inappropriately.
644 * It is safe to call this function from atomic context.
646 * Will trigger an automatic deferred table resizing if the size grows
647 * beyond the watermark indicated by grow_decision() which can be passed
648 * to rhashtable_init().
650 bool rhashtable_lookup_insert(struct rhashtable
*ht
, struct rhash_head
*obj
)
652 struct rhashtable_compare_arg arg
= {
654 .key
= rht_obj(ht
, obj
) + ht
->p
.key_offset
,
657 BUG_ON(!ht
->p
.key_len
);
659 return rhashtable_lookup_compare_insert(ht
, obj
, &rhashtable_compare
,
662 EXPORT_SYMBOL_GPL(rhashtable_lookup_insert
);
665 * rhashtable_lookup_compare_insert - search and insert object to hash table
666 * with compare function
668 * @obj: pointer to hash head inside object
669 * @compare: compare function, must return true on match
670 * @arg: argument passed on to compare function
672 * Locks down the bucket chain in both the old and new table if a resize
673 * is in progress to ensure that writers can't remove from the old table
674 * and can't insert to the new table during the atomic operation of search
675 * and insertion. Searches for duplicates in both the old and new table if
676 * a resize is in progress.
678 * Lookups may occur in parallel with hashtable mutations and resizing.
680 * Will trigger an automatic deferred table resizing if the size grows
681 * beyond the watermark indicated by grow_decision() which can be passed
682 * to rhashtable_init().
684 bool rhashtable_lookup_compare_insert(struct rhashtable
*ht
,
685 struct rhash_head
*obj
,
686 bool (*compare
)(void *, void *),
689 BUG_ON(!ht
->p
.key_len
);
691 return __rhashtable_insert(ht
, obj
, compare
, arg
);
693 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert
);
696 * rhashtable_walk_init - Initialise an iterator
697 * @ht: Table to walk over
698 * @iter: Hash table Iterator
700 * This function prepares a hash table walk.
702 * Note that if you restart a walk after rhashtable_walk_stop you
703 * may see the same object twice. Also, you may miss objects if
704 * there are removals in between rhashtable_walk_stop and the next
705 * call to rhashtable_walk_start.
707 * For a completely stable walk you should construct your own data
708 * structure outside the hash table.
710 * This function may sleep so you must not call it from interrupt
711 * context or with spin locks held.
713 * You must call rhashtable_walk_exit if this function returns
716 int rhashtable_walk_init(struct rhashtable
*ht
, struct rhashtable_iter
*iter
)
723 iter
->walker
= kmalloc(sizeof(*iter
->walker
), GFP_KERNEL
);
727 mutex_lock(&ht
->mutex
);
728 iter
->walker
->tbl
= rht_dereference(ht
->tbl
, ht
);
729 list_add(&iter
->walker
->list
, &iter
->walker
->tbl
->walkers
);
730 mutex_unlock(&ht
->mutex
);
734 EXPORT_SYMBOL_GPL(rhashtable_walk_init
);
737 * rhashtable_walk_exit - Free an iterator
738 * @iter: Hash table Iterator
740 * This function frees resources allocated by rhashtable_walk_init.
742 void rhashtable_walk_exit(struct rhashtable_iter
*iter
)
744 mutex_lock(&iter
->ht
->mutex
);
745 if (iter
->walker
->tbl
)
746 list_del(&iter
->walker
->list
);
747 mutex_unlock(&iter
->ht
->mutex
);
750 EXPORT_SYMBOL_GPL(rhashtable_walk_exit
);
753 * rhashtable_walk_start - Start a hash table walk
754 * @iter: Hash table iterator
756 * Start a hash table walk. Note that we take the RCU lock in all
757 * cases including when we return an error. So you must always call
758 * rhashtable_walk_stop to clean up.
760 * Returns zero if successful.
762 * Returns -EAGAIN if resize event occured. Note that the iterator
763 * will rewind back to the beginning and you may use it immediately
764 * by calling rhashtable_walk_next.
766 int rhashtable_walk_start(struct rhashtable_iter
*iter
)
768 struct rhashtable
*ht
= iter
->ht
;
770 mutex_lock(&ht
->mutex
);
772 if (iter
->walker
->tbl
)
773 list_del(&iter
->walker
->list
);
777 mutex_unlock(&ht
->mutex
);
779 if (!iter
->walker
->tbl
) {
780 iter
->walker
->tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
786 EXPORT_SYMBOL_GPL(rhashtable_walk_start
);
789 * rhashtable_walk_next - Return the next object and advance the iterator
790 * @iter: Hash table iterator
792 * Note that you must call rhashtable_walk_stop when you are finished
795 * Returns the next object or NULL when the end of the table is reached.
797 * Returns -EAGAIN if resize event occured. Note that the iterator
798 * will rewind back to the beginning and you may continue to use it.
800 void *rhashtable_walk_next(struct rhashtable_iter
*iter
)
802 struct bucket_table
*tbl
= iter
->walker
->tbl
;
803 struct rhashtable
*ht
= iter
->ht
;
804 struct rhash_head
*p
= iter
->p
;
808 p
= rht_dereference_bucket_rcu(p
->next
, tbl
, iter
->slot
);
812 for (; iter
->slot
< tbl
->size
; iter
->slot
++) {
813 int skip
= iter
->skip
;
815 rht_for_each_rcu(p
, tbl
, iter
->slot
) {
822 if (!rht_is_a_nulls(p
)) {
825 obj
= rht_obj(ht
, p
);
832 iter
->walker
->tbl
= rht_dereference_rcu(ht
->future_tbl
, ht
);
833 if (iter
->walker
->tbl
!= tbl
) {
836 return ERR_PTR(-EAGAIN
);
839 iter
->walker
->tbl
= NULL
;
846 EXPORT_SYMBOL_GPL(rhashtable_walk_next
);
849 * rhashtable_walk_stop - Finish a hash table walk
850 * @iter: Hash table iterator
852 * Finish a hash table walk.
854 void rhashtable_walk_stop(struct rhashtable_iter
*iter
)
856 struct rhashtable
*ht
;
857 struct bucket_table
*tbl
= iter
->walker
->tbl
;
866 mutex_lock(&ht
->mutex
);
867 if (rht_dereference(ht
->tbl
, ht
) == tbl
||
868 rht_dereference(ht
->future_tbl
, ht
) == tbl
)
869 list_add(&iter
->walker
->list
, &tbl
->walkers
);
871 iter
->walker
->tbl
= NULL
;
872 mutex_unlock(&ht
->mutex
);
876 EXPORT_SYMBOL_GPL(rhashtable_walk_stop
);
878 static size_t rounded_hashtable_size(struct rhashtable_params
*params
)
880 return max(roundup_pow_of_two(params
->nelem_hint
* 4 / 3),
881 1UL << params
->min_shift
);
885 * rhashtable_init - initialize a new hash table
886 * @ht: hash table to be initialized
887 * @params: configuration parameters
889 * Initializes a new hash table based on the provided configuration
890 * parameters. A table can be configured either with a variable or
893 * Configuration Example 1: Fixed length keys
897 * struct rhash_head node;
900 * struct rhashtable_params params = {
901 * .head_offset = offsetof(struct test_obj, node),
902 * .key_offset = offsetof(struct test_obj, key),
903 * .key_len = sizeof(int),
905 * .nulls_base = (1U << RHT_BASE_SHIFT),
908 * Configuration Example 2: Variable length keys
911 * struct rhash_head node;
914 * u32 my_hash_fn(const void *data, u32 seed)
916 * struct test_obj *obj = data;
918 * return [... hash ...];
921 * struct rhashtable_params params = {
922 * .head_offset = offsetof(struct test_obj, node),
924 * .obj_hashfn = my_hash_fn,
927 int rhashtable_init(struct rhashtable
*ht
, struct rhashtable_params
*params
)
929 struct bucket_table
*tbl
;
932 size
= HASH_DEFAULT_SIZE
;
934 if ((params
->key_len
&& !params
->hashfn
) ||
935 (!params
->key_len
&& !params
->obj_hashfn
))
938 if (params
->nulls_base
&& params
->nulls_base
< (1U << RHT_BASE_SHIFT
))
941 params
->min_shift
= max_t(size_t, params
->min_shift
,
942 ilog2(HASH_MIN_SIZE
));
944 if (params
->nelem_hint
)
945 size
= rounded_hashtable_size(params
);
947 memset(ht
, 0, sizeof(*ht
));
948 mutex_init(&ht
->mutex
);
949 memcpy(&ht
->p
, params
, sizeof(*params
));
951 if (params
->locks_mul
)
952 ht
->p
.locks_mul
= roundup_pow_of_two(params
->locks_mul
);
954 ht
->p
.locks_mul
= BUCKET_LOCKS_PER_CPU
;
956 tbl
= bucket_table_alloc(ht
, size
);
960 atomic_set(&ht
->nelems
, 0);
962 RCU_INIT_POINTER(ht
->tbl
, tbl
);
963 RCU_INIT_POINTER(ht
->future_tbl
, tbl
);
965 INIT_WORK(&ht
->run_work
, rht_deferred_worker
);
969 EXPORT_SYMBOL_GPL(rhashtable_init
);
972 * rhashtable_destroy - destroy hash table
973 * @ht: the hash table to destroy
975 * Frees the bucket array. This function is not rcu safe, therefore the caller
976 * has to make sure that no resizing may happen by unpublishing the hashtable
977 * and waiting for the quiescent cycle before releasing the bucket array.
979 void rhashtable_destroy(struct rhashtable
*ht
)
981 ht
->being_destroyed
= true;
983 cancel_work_sync(&ht
->run_work
);
985 mutex_lock(&ht
->mutex
);
986 bucket_table_free(rht_dereference(ht
->tbl
, ht
));
987 mutex_unlock(&ht
->mutex
);
989 EXPORT_SYMBOL_GPL(rhashtable_destroy
);