Linux 4.14.71
[linux/fpc-iii.git] / lib / rhashtable.c
blobcebbcec877d71a96ce2ce110a8c60fe67a5c1306
1 /*
2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
5 * Copyright (c) 2014-2015 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 #include <linux/atomic.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/log2.h>
21 #include <linux/sched.h>
22 #include <linux/rculist.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/mm.h>
26 #include <linux/jhash.h>
27 #include <linux/random.h>
28 #include <linux/rhashtable.h>
29 #include <linux/err.h>
30 #include <linux/export.h>
32 #define HASH_DEFAULT_SIZE 64UL
33 #define HASH_MIN_SIZE 4U
34 #define BUCKET_LOCKS_PER_CPU 32UL
36 union nested_table {
37 union nested_table __rcu *table;
38 struct rhash_head __rcu *bucket;
41 static u32 head_hashfn(struct rhashtable *ht,
42 const struct bucket_table *tbl,
43 const struct rhash_head *he)
45 return rht_head_hashfn(ht, tbl, he, ht->p);
48 #ifdef CONFIG_PROVE_LOCKING
49 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
51 int lockdep_rht_mutex_is_held(struct rhashtable *ht)
53 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
55 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
57 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
59 spinlock_t *lock = rht_bucket_lock(tbl, hash);
61 return (debug_locks) ? lockdep_is_held(lock) : 1;
63 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
64 #else
65 #define ASSERT_RHT_MUTEX(HT)
66 #endif
69 static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl,
70 gfp_t gfp)
72 unsigned int i, size;
73 #if defined(CONFIG_PROVE_LOCKING)
74 unsigned int nr_pcpus = 2;
75 #else
76 unsigned int nr_pcpus = num_possible_cpus();
77 #endif
79 nr_pcpus = min_t(unsigned int, nr_pcpus, 64UL);
80 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
82 /* Never allocate more than 0.5 locks per bucket */
83 size = min_t(unsigned int, size, tbl->size >> 1);
85 if (tbl->nest)
86 size = min(size, 1U << tbl->nest);
88 if (sizeof(spinlock_t) != 0) {
89 if (gfpflags_allow_blocking(gfp))
90 tbl->locks = kvmalloc(size * sizeof(spinlock_t), gfp);
91 else
92 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
93 gfp);
94 if (!tbl->locks)
95 return -ENOMEM;
96 for (i = 0; i < size; i++)
97 spin_lock_init(&tbl->locks[i]);
99 tbl->locks_mask = size - 1;
101 return 0;
104 static void nested_table_free(union nested_table *ntbl, unsigned int size)
106 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
107 const unsigned int len = 1 << shift;
108 unsigned int i;
110 ntbl = rcu_dereference_raw(ntbl->table);
111 if (!ntbl)
112 return;
114 if (size > len) {
115 size >>= shift;
116 for (i = 0; i < len; i++)
117 nested_table_free(ntbl + i, size);
120 kfree(ntbl);
123 static void nested_bucket_table_free(const struct bucket_table *tbl)
125 unsigned int size = tbl->size >> tbl->nest;
126 unsigned int len = 1 << tbl->nest;
127 union nested_table *ntbl;
128 unsigned int i;
130 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
132 for (i = 0; i < len; i++)
133 nested_table_free(ntbl + i, size);
135 kfree(ntbl);
138 static void bucket_table_free(const struct bucket_table *tbl)
140 if (tbl->nest)
141 nested_bucket_table_free(tbl);
143 kvfree(tbl->locks);
144 kvfree(tbl);
147 static void bucket_table_free_rcu(struct rcu_head *head)
149 bucket_table_free(container_of(head, struct bucket_table, rcu));
152 static union nested_table *nested_table_alloc(struct rhashtable *ht,
153 union nested_table __rcu **prev,
154 unsigned int shifted,
155 unsigned int nhash)
157 union nested_table *ntbl;
158 int i;
160 ntbl = rcu_dereference(*prev);
161 if (ntbl)
162 return ntbl;
164 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
166 if (ntbl && shifted) {
167 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0].bucket); i++)
168 INIT_RHT_NULLS_HEAD(ntbl[i].bucket, ht,
169 (i << shifted) | nhash);
172 rcu_assign_pointer(*prev, ntbl);
174 return ntbl;
177 static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
178 size_t nbuckets,
179 gfp_t gfp)
181 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
182 struct bucket_table *tbl;
183 size_t size;
185 if (nbuckets < (1 << (shift + 1)))
186 return NULL;
188 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
190 tbl = kzalloc(size, gfp);
191 if (!tbl)
192 return NULL;
194 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
195 0, 0)) {
196 kfree(tbl);
197 return NULL;
200 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
202 return tbl;
205 static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
206 size_t nbuckets,
207 gfp_t gfp)
209 struct bucket_table *tbl = NULL;
210 size_t size;
211 int i;
213 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
214 if (gfp != GFP_KERNEL)
215 tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY);
216 else
217 tbl = kvzalloc(size, gfp);
219 size = nbuckets;
221 if (tbl == NULL && gfp != GFP_KERNEL) {
222 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
223 nbuckets = 0;
225 if (tbl == NULL)
226 return NULL;
228 tbl->size = size;
230 if (alloc_bucket_locks(ht, tbl, gfp) < 0) {
231 bucket_table_free(tbl);
232 return NULL;
235 INIT_LIST_HEAD(&tbl->walkers);
237 tbl->hash_rnd = get_random_u32();
239 for (i = 0; i < nbuckets; i++)
240 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
242 return tbl;
245 static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
246 struct bucket_table *tbl)
248 struct bucket_table *new_tbl;
250 do {
251 new_tbl = tbl;
252 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
253 } while (tbl);
255 return new_tbl;
258 static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
260 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
261 struct bucket_table *new_tbl = rhashtable_last_table(ht,
262 rht_dereference_rcu(old_tbl->future_tbl, ht));
263 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
264 int err = -EAGAIN;
265 struct rhash_head *head, *next, *entry;
266 spinlock_t *new_bucket_lock;
267 unsigned int new_hash;
269 if (new_tbl->nest)
270 goto out;
272 err = -ENOENT;
274 rht_for_each(entry, old_tbl, old_hash) {
275 err = 0;
276 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
278 if (rht_is_a_nulls(next))
279 break;
281 pprev = &entry->next;
284 if (err)
285 goto out;
287 new_hash = head_hashfn(ht, new_tbl, entry);
289 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
291 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
292 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
293 new_tbl, new_hash);
295 RCU_INIT_POINTER(entry->next, head);
297 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
298 spin_unlock(new_bucket_lock);
300 rcu_assign_pointer(*pprev, next);
302 out:
303 return err;
306 static int rhashtable_rehash_chain(struct rhashtable *ht,
307 unsigned int old_hash)
309 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
310 spinlock_t *old_bucket_lock;
311 int err;
313 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
315 spin_lock_bh(old_bucket_lock);
316 while (!(err = rhashtable_rehash_one(ht, old_hash)))
319 if (err == -ENOENT) {
320 old_tbl->rehash++;
321 err = 0;
323 spin_unlock_bh(old_bucket_lock);
325 return err;
328 static int rhashtable_rehash_attach(struct rhashtable *ht,
329 struct bucket_table *old_tbl,
330 struct bucket_table *new_tbl)
332 /* Protect future_tbl using the first bucket lock. */
333 spin_lock_bh(old_tbl->locks);
335 /* Did somebody beat us to it? */
336 if (rcu_access_pointer(old_tbl->future_tbl)) {
337 spin_unlock_bh(old_tbl->locks);
338 return -EEXIST;
341 /* Make insertions go into the new, empty table right away. Deletions
342 * and lookups will be attempted in both tables until we synchronize.
344 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
346 spin_unlock_bh(old_tbl->locks);
348 return 0;
351 static int rhashtable_rehash_table(struct rhashtable *ht)
353 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
354 struct bucket_table *new_tbl;
355 struct rhashtable_walker *walker;
356 unsigned int old_hash;
357 int err;
359 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
360 if (!new_tbl)
361 return 0;
363 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
364 err = rhashtable_rehash_chain(ht, old_hash);
365 if (err)
366 return err;
367 cond_resched();
370 /* Publish the new table pointer. */
371 rcu_assign_pointer(ht->tbl, new_tbl);
373 spin_lock(&ht->lock);
374 list_for_each_entry(walker, &old_tbl->walkers, list)
375 walker->tbl = NULL;
376 spin_unlock(&ht->lock);
378 /* Wait for readers. All new readers will see the new
379 * table, and thus no references to the old table will
380 * remain.
382 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
384 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
387 static int rhashtable_rehash_alloc(struct rhashtable *ht,
388 struct bucket_table *old_tbl,
389 unsigned int size)
391 struct bucket_table *new_tbl;
392 int err;
394 ASSERT_RHT_MUTEX(ht);
396 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
397 if (new_tbl == NULL)
398 return -ENOMEM;
400 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
401 if (err)
402 bucket_table_free(new_tbl);
404 return err;
408 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
409 * @ht: the hash table to shrink
411 * This function shrinks the hash table to fit, i.e., the smallest
412 * size would not cause it to expand right away automatically.
414 * The caller must ensure that no concurrent resizing occurs by holding
415 * ht->mutex.
417 * The caller must ensure that no concurrent table mutations take place.
418 * It is however valid to have concurrent lookups if they are RCU protected.
420 * It is valid to have concurrent insertions and deletions protected by per
421 * bucket locks or concurrent RCU protected lookups and traversals.
423 static int rhashtable_shrink(struct rhashtable *ht)
425 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
426 unsigned int nelems = atomic_read(&ht->nelems);
427 unsigned int size = 0;
429 if (nelems)
430 size = roundup_pow_of_two(nelems * 3 / 2);
431 if (size < ht->p.min_size)
432 size = ht->p.min_size;
434 if (old_tbl->size <= size)
435 return 0;
437 if (rht_dereference(old_tbl->future_tbl, ht))
438 return -EEXIST;
440 return rhashtable_rehash_alloc(ht, old_tbl, size);
443 static void rht_deferred_worker(struct work_struct *work)
445 struct rhashtable *ht;
446 struct bucket_table *tbl;
447 int err = 0;
449 ht = container_of(work, struct rhashtable, run_work);
450 mutex_lock(&ht->mutex);
452 tbl = rht_dereference(ht->tbl, ht);
453 tbl = rhashtable_last_table(ht, tbl);
455 if (rht_grow_above_75(ht, tbl))
456 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
457 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
458 err = rhashtable_shrink(ht);
459 else if (tbl->nest)
460 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
462 if (!err)
463 err = rhashtable_rehash_table(ht);
465 mutex_unlock(&ht->mutex);
467 if (err)
468 schedule_work(&ht->run_work);
471 static int rhashtable_insert_rehash(struct rhashtable *ht,
472 struct bucket_table *tbl)
474 struct bucket_table *old_tbl;
475 struct bucket_table *new_tbl;
476 unsigned int size;
477 int err;
479 old_tbl = rht_dereference_rcu(ht->tbl, ht);
481 size = tbl->size;
483 err = -EBUSY;
485 if (rht_grow_above_75(ht, tbl))
486 size *= 2;
487 /* Do not schedule more than one rehash */
488 else if (old_tbl != tbl)
489 goto fail;
491 err = -ENOMEM;
493 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
494 if (new_tbl == NULL)
495 goto fail;
497 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
498 if (err) {
499 bucket_table_free(new_tbl);
500 if (err == -EEXIST)
501 err = 0;
502 } else
503 schedule_work(&ht->run_work);
505 return err;
507 fail:
508 /* Do not fail the insert if someone else did a rehash. */
509 if (likely(rcu_dereference_raw(tbl->future_tbl)))
510 return 0;
512 /* Schedule async rehash to retry allocation in process context. */
513 if (err == -ENOMEM)
514 schedule_work(&ht->run_work);
516 return err;
519 static void *rhashtable_lookup_one(struct rhashtable *ht,
520 struct bucket_table *tbl, unsigned int hash,
521 const void *key, struct rhash_head *obj)
523 struct rhashtable_compare_arg arg = {
524 .ht = ht,
525 .key = key,
527 struct rhash_head __rcu **pprev;
528 struct rhash_head *head;
529 int elasticity;
531 elasticity = RHT_ELASTICITY;
532 pprev = rht_bucket_var(tbl, hash);
533 rht_for_each_continue(head, *pprev, tbl, hash) {
534 struct rhlist_head *list;
535 struct rhlist_head *plist;
537 elasticity--;
538 if (!key ||
539 (ht->p.obj_cmpfn ?
540 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
541 rhashtable_compare(&arg, rht_obj(ht, head)))) {
542 pprev = &head->next;
543 continue;
546 if (!ht->rhlist)
547 return rht_obj(ht, head);
549 list = container_of(obj, struct rhlist_head, rhead);
550 plist = container_of(head, struct rhlist_head, rhead);
552 RCU_INIT_POINTER(list->next, plist);
553 head = rht_dereference_bucket(head->next, tbl, hash);
554 RCU_INIT_POINTER(list->rhead.next, head);
555 rcu_assign_pointer(*pprev, obj);
557 return NULL;
560 if (elasticity <= 0)
561 return ERR_PTR(-EAGAIN);
563 return ERR_PTR(-ENOENT);
566 static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
567 struct bucket_table *tbl,
568 unsigned int hash,
569 struct rhash_head *obj,
570 void *data)
572 struct rhash_head __rcu **pprev;
573 struct bucket_table *new_tbl;
574 struct rhash_head *head;
576 if (!IS_ERR_OR_NULL(data))
577 return ERR_PTR(-EEXIST);
579 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
580 return ERR_CAST(data);
582 new_tbl = rcu_dereference(tbl->future_tbl);
583 if (new_tbl)
584 return new_tbl;
586 if (PTR_ERR(data) != -ENOENT)
587 return ERR_CAST(data);
589 if (unlikely(rht_grow_above_max(ht, tbl)))
590 return ERR_PTR(-E2BIG);
592 if (unlikely(rht_grow_above_100(ht, tbl)))
593 return ERR_PTR(-EAGAIN);
595 pprev = rht_bucket_insert(ht, tbl, hash);
596 if (!pprev)
597 return ERR_PTR(-ENOMEM);
599 head = rht_dereference_bucket(*pprev, tbl, hash);
601 RCU_INIT_POINTER(obj->next, head);
602 if (ht->rhlist) {
603 struct rhlist_head *list;
605 list = container_of(obj, struct rhlist_head, rhead);
606 RCU_INIT_POINTER(list->next, NULL);
609 rcu_assign_pointer(*pprev, obj);
611 atomic_inc(&ht->nelems);
612 if (rht_grow_above_75(ht, tbl))
613 schedule_work(&ht->run_work);
615 return NULL;
618 static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
619 struct rhash_head *obj)
621 struct bucket_table *new_tbl;
622 struct bucket_table *tbl;
623 unsigned int hash;
624 spinlock_t *lock;
625 void *data;
627 tbl = rcu_dereference(ht->tbl);
629 /* All insertions must grab the oldest table containing
630 * the hashed bucket that is yet to be rehashed.
632 for (;;) {
633 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
634 lock = rht_bucket_lock(tbl, hash);
635 spin_lock_bh(lock);
637 if (tbl->rehash <= hash)
638 break;
640 spin_unlock_bh(lock);
641 tbl = rcu_dereference(tbl->future_tbl);
644 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
645 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
646 if (PTR_ERR(new_tbl) != -EEXIST)
647 data = ERR_CAST(new_tbl);
649 while (!IS_ERR_OR_NULL(new_tbl)) {
650 tbl = new_tbl;
651 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
652 spin_lock_nested(rht_bucket_lock(tbl, hash),
653 SINGLE_DEPTH_NESTING);
655 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
656 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
657 if (PTR_ERR(new_tbl) != -EEXIST)
658 data = ERR_CAST(new_tbl);
660 spin_unlock(rht_bucket_lock(tbl, hash));
663 spin_unlock_bh(lock);
665 if (PTR_ERR(data) == -EAGAIN)
666 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
667 -EAGAIN);
669 return data;
672 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
673 struct rhash_head *obj)
675 void *data;
677 do {
678 rcu_read_lock();
679 data = rhashtable_try_insert(ht, key, obj);
680 rcu_read_unlock();
681 } while (PTR_ERR(data) == -EAGAIN);
683 return data;
685 EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
688 * rhashtable_walk_enter - Initialise an iterator
689 * @ht: Table to walk over
690 * @iter: Hash table Iterator
692 * This function prepares a hash table walk.
694 * Note that if you restart a walk after rhashtable_walk_stop you
695 * may see the same object twice. Also, you may miss objects if
696 * there are removals in between rhashtable_walk_stop and the next
697 * call to rhashtable_walk_start.
699 * For a completely stable walk you should construct your own data
700 * structure outside the hash table.
702 * This function may sleep so you must not call it from interrupt
703 * context or with spin locks held.
705 * You must call rhashtable_walk_exit after this function returns.
707 void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
709 iter->ht = ht;
710 iter->p = NULL;
711 iter->slot = 0;
712 iter->skip = 0;
714 spin_lock(&ht->lock);
715 iter->walker.tbl =
716 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
717 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
718 spin_unlock(&ht->lock);
720 EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
723 * rhashtable_walk_exit - Free an iterator
724 * @iter: Hash table Iterator
726 * This function frees resources allocated by rhashtable_walk_init.
728 void rhashtable_walk_exit(struct rhashtable_iter *iter)
730 spin_lock(&iter->ht->lock);
731 if (iter->walker.tbl)
732 list_del(&iter->walker.list);
733 spin_unlock(&iter->ht->lock);
735 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
738 * rhashtable_walk_start - Start a hash table walk
739 * @iter: Hash table iterator
741 * Start a hash table walk at the current iterator position. Note that we take
742 * the RCU lock in all cases including when we return an error. So you must
743 * always call rhashtable_walk_stop to clean up.
745 * Returns zero if successful.
747 * Returns -EAGAIN if resize event occured. Note that the iterator
748 * will rewind back to the beginning and you may use it immediately
749 * by calling rhashtable_walk_next.
751 int rhashtable_walk_start(struct rhashtable_iter *iter)
752 __acquires(RCU)
754 struct rhashtable *ht = iter->ht;
756 rcu_read_lock();
758 spin_lock(&ht->lock);
759 if (iter->walker.tbl)
760 list_del(&iter->walker.list);
761 spin_unlock(&ht->lock);
763 if (!iter->walker.tbl) {
764 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
765 return -EAGAIN;
768 return 0;
770 EXPORT_SYMBOL_GPL(rhashtable_walk_start);
773 * rhashtable_walk_next - Return the next object and advance the iterator
774 * @iter: Hash table iterator
776 * Note that you must call rhashtable_walk_stop when you are finished
777 * with the walk.
779 * Returns the next object or NULL when the end of the table is reached.
781 * Returns -EAGAIN if resize event occured. Note that the iterator
782 * will rewind back to the beginning and you may continue to use it.
784 void *rhashtable_walk_next(struct rhashtable_iter *iter)
786 struct bucket_table *tbl = iter->walker.tbl;
787 struct rhlist_head *list = iter->list;
788 struct rhashtable *ht = iter->ht;
789 struct rhash_head *p = iter->p;
790 bool rhlist = ht->rhlist;
792 if (p) {
793 if (!rhlist || !(list = rcu_dereference(list->next))) {
794 p = rcu_dereference(p->next);
795 list = container_of(p, struct rhlist_head, rhead);
797 goto next;
800 for (; iter->slot < tbl->size; iter->slot++) {
801 int skip = iter->skip;
803 rht_for_each_rcu(p, tbl, iter->slot) {
804 if (rhlist) {
805 list = container_of(p, struct rhlist_head,
806 rhead);
807 do {
808 if (!skip)
809 goto next;
810 skip--;
811 list = rcu_dereference(list->next);
812 } while (list);
814 continue;
816 if (!skip)
817 break;
818 skip--;
821 next:
822 if (!rht_is_a_nulls(p)) {
823 iter->skip++;
824 iter->p = p;
825 iter->list = list;
826 return rht_obj(ht, rhlist ? &list->rhead : p);
829 iter->skip = 0;
832 iter->p = NULL;
834 /* Ensure we see any new tables. */
835 smp_rmb();
837 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
838 if (iter->walker.tbl) {
839 iter->slot = 0;
840 iter->skip = 0;
841 return ERR_PTR(-EAGAIN);
844 return 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. Does not reset the iterator to the start of the
853 * hash table.
855 void rhashtable_walk_stop(struct rhashtable_iter *iter)
856 __releases(RCU)
858 struct rhashtable *ht;
859 struct bucket_table *tbl = iter->walker.tbl;
861 if (!tbl)
862 goto out;
864 ht = iter->ht;
866 spin_lock(&ht->lock);
867 if (tbl->rehash < tbl->size)
868 list_add(&iter->walker.list, &tbl->walkers);
869 else
870 iter->walker.tbl = NULL;
871 spin_unlock(&ht->lock);
873 iter->p = NULL;
875 out:
876 rcu_read_unlock();
878 EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
880 static size_t rounded_hashtable_size(const struct rhashtable_params *params)
882 size_t retsize;
884 if (params->nelem_hint)
885 retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
886 (unsigned long)params->min_size);
887 else
888 retsize = max(HASH_DEFAULT_SIZE,
889 (unsigned long)params->min_size);
891 return retsize;
894 static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
896 return jhash2(key, length, seed);
900 * rhashtable_init - initialize a new hash table
901 * @ht: hash table to be initialized
902 * @params: configuration parameters
904 * Initializes a new hash table based on the provided configuration
905 * parameters. A table can be configured either with a variable or
906 * fixed length key:
908 * Configuration Example 1: Fixed length keys
909 * struct test_obj {
910 * int key;
911 * void * my_member;
912 * struct rhash_head node;
913 * };
915 * struct rhashtable_params params = {
916 * .head_offset = offsetof(struct test_obj, node),
917 * .key_offset = offsetof(struct test_obj, key),
918 * .key_len = sizeof(int),
919 * .hashfn = jhash,
920 * .nulls_base = (1U << RHT_BASE_SHIFT),
921 * };
923 * Configuration Example 2: Variable length keys
924 * struct test_obj {
925 * [...]
926 * struct rhash_head node;
927 * };
929 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
931 * struct test_obj *obj = data;
933 * return [... hash ...];
936 * struct rhashtable_params params = {
937 * .head_offset = offsetof(struct test_obj, node),
938 * .hashfn = jhash,
939 * .obj_hashfn = my_hash_fn,
940 * };
942 int rhashtable_init(struct rhashtable *ht,
943 const struct rhashtable_params *params)
945 struct bucket_table *tbl;
946 size_t size;
948 if ((!params->key_len && !params->obj_hashfn) ||
949 (params->obj_hashfn && !params->obj_cmpfn))
950 return -EINVAL;
952 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
953 return -EINVAL;
955 memset(ht, 0, sizeof(*ht));
956 mutex_init(&ht->mutex);
957 spin_lock_init(&ht->lock);
958 memcpy(&ht->p, params, sizeof(*params));
960 if (params->min_size)
961 ht->p.min_size = roundup_pow_of_two(params->min_size);
963 /* Cap total entries at 2^31 to avoid nelems overflow. */
964 ht->max_elems = 1u << 31;
966 if (params->max_size) {
967 ht->p.max_size = rounddown_pow_of_two(params->max_size);
968 if (ht->p.max_size < ht->max_elems / 2)
969 ht->max_elems = ht->p.max_size * 2;
972 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
974 size = rounded_hashtable_size(&ht->p);
976 if (params->locks_mul)
977 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
978 else
979 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
981 ht->key_len = ht->p.key_len;
982 if (!params->hashfn) {
983 ht->p.hashfn = jhash;
985 if (!(ht->key_len & (sizeof(u32) - 1))) {
986 ht->key_len /= sizeof(u32);
987 ht->p.hashfn = rhashtable_jhash2;
991 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
992 if (tbl == NULL)
993 return -ENOMEM;
995 atomic_set(&ht->nelems, 0);
997 RCU_INIT_POINTER(ht->tbl, tbl);
999 INIT_WORK(&ht->run_work, rht_deferred_worker);
1001 return 0;
1003 EXPORT_SYMBOL_GPL(rhashtable_init);
1006 * rhltable_init - initialize a new hash list table
1007 * @hlt: hash list table to be initialized
1008 * @params: configuration parameters
1010 * Initializes a new hash list table.
1012 * See documentation for rhashtable_init.
1014 int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1016 int err;
1018 /* No rhlist NULLs marking for now. */
1019 if (params->nulls_base)
1020 return -EINVAL;
1022 err = rhashtable_init(&hlt->ht, params);
1023 hlt->ht.rhlist = true;
1024 return err;
1026 EXPORT_SYMBOL_GPL(rhltable_init);
1028 static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1029 void (*free_fn)(void *ptr, void *arg),
1030 void *arg)
1032 struct rhlist_head *list;
1034 if (!ht->rhlist) {
1035 free_fn(rht_obj(ht, obj), arg);
1036 return;
1039 list = container_of(obj, struct rhlist_head, rhead);
1040 do {
1041 obj = &list->rhead;
1042 list = rht_dereference(list->next, ht);
1043 free_fn(rht_obj(ht, obj), arg);
1044 } while (list);
1048 * rhashtable_free_and_destroy - free elements and destroy hash table
1049 * @ht: the hash table to destroy
1050 * @free_fn: callback to release resources of element
1051 * @arg: pointer passed to free_fn
1053 * Stops an eventual async resize. If defined, invokes free_fn for each
1054 * element to releasal resources. Please note that RCU protected
1055 * readers may still be accessing the elements. Releasing of resources
1056 * must occur in a compatible manner. Then frees the bucket array.
1058 * This function will eventually sleep to wait for an async resize
1059 * to complete. The caller is responsible that no further write operations
1060 * occurs in parallel.
1062 void rhashtable_free_and_destroy(struct rhashtable *ht,
1063 void (*free_fn)(void *ptr, void *arg),
1064 void *arg)
1066 struct bucket_table *tbl;
1067 unsigned int i;
1069 cancel_work_sync(&ht->run_work);
1071 mutex_lock(&ht->mutex);
1072 tbl = rht_dereference(ht->tbl, ht);
1073 if (free_fn) {
1074 for (i = 0; i < tbl->size; i++) {
1075 struct rhash_head *pos, *next;
1077 cond_resched();
1078 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
1079 next = !rht_is_a_nulls(pos) ?
1080 rht_dereference(pos->next, ht) : NULL;
1081 !rht_is_a_nulls(pos);
1082 pos = next,
1083 next = !rht_is_a_nulls(pos) ?
1084 rht_dereference(pos->next, ht) : NULL)
1085 rhashtable_free_one(ht, pos, free_fn, arg);
1089 bucket_table_free(tbl);
1090 mutex_unlock(&ht->mutex);
1092 EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1094 void rhashtable_destroy(struct rhashtable *ht)
1096 return rhashtable_free_and_destroy(ht, NULL, NULL);
1098 EXPORT_SYMBOL_GPL(rhashtable_destroy);
1100 struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1101 unsigned int hash)
1103 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1104 static struct rhash_head __rcu *rhnull =
1105 (struct rhash_head __rcu *)NULLS_MARKER(0);
1106 unsigned int index = hash & ((1 << tbl->nest) - 1);
1107 unsigned int size = tbl->size >> tbl->nest;
1108 unsigned int subhash = hash;
1109 union nested_table *ntbl;
1111 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1112 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
1113 subhash >>= tbl->nest;
1115 while (ntbl && size > (1 << shift)) {
1116 index = subhash & ((1 << shift) - 1);
1117 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1118 tbl, hash);
1119 size >>= shift;
1120 subhash >>= shift;
1123 if (!ntbl)
1124 return &rhnull;
1126 return &ntbl[subhash].bucket;
1129 EXPORT_SYMBOL_GPL(rht_bucket_nested);
1131 struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1132 struct bucket_table *tbl,
1133 unsigned int hash)
1135 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1136 unsigned int index = hash & ((1 << tbl->nest) - 1);
1137 unsigned int size = tbl->size >> tbl->nest;
1138 union nested_table *ntbl;
1139 unsigned int shifted;
1140 unsigned int nhash;
1142 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1143 hash >>= tbl->nest;
1144 nhash = index;
1145 shifted = tbl->nest;
1146 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1147 size <= (1 << shift) ? shifted : 0, nhash);
1149 while (ntbl && size > (1 << shift)) {
1150 index = hash & ((1 << shift) - 1);
1151 size >>= shift;
1152 hash >>= shift;
1153 nhash |= index << shifted;
1154 shifted += shift;
1155 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1156 size <= (1 << shift) ? shifted : 0,
1157 nhash);
1160 if (!ntbl)
1161 return NULL;
1163 return &ntbl[hash].bucket;
1166 EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);