1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef _LINUX_RHASHTABLE_H
18#define _LINUX_RHASHTABLE_H
19
20#include <linux/atomic.h>
21#include <linux/compiler.h>
22#include <linux/err.h>
23#include <linux/errno.h>
24#include <linux/jhash.h>
25#include <linux/list_nulls.h>
26#include <linux/workqueue.h>
27#include <linux/mutex.h>
28#include <linux/rcupdate.h>
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45#define RHT_BASE_BITS 4
46#define RHT_HASH_BITS 27
47#define RHT_BASE_SHIFT RHT_HASH_BITS
48
49
50#define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
51
52struct rhash_head {
53 struct rhash_head __rcu *next;
54};
55
56
57
58
59
60
61
62
63
64
65
66
67
68struct bucket_table {
69 unsigned int size;
70 unsigned int rehash;
71 u32 hash_rnd;
72 unsigned int locks_mask;
73 spinlock_t *locks;
74 struct list_head walkers;
75 struct rcu_head rcu;
76
77 struct bucket_table __rcu *future_tbl;
78
79 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
80};
81
82
83
84
85
86
87struct rhashtable_compare_arg {
88 struct rhashtable *ht;
89 const void *key;
90};
91
92typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
93typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed);
94typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
95 const void *obj);
96
97struct rhashtable;
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116struct rhashtable_params {
117 size_t nelem_hint;
118 size_t key_len;
119 size_t key_offset;
120 size_t head_offset;
121 unsigned int insecure_max_entries;
122 unsigned int max_size;
123 unsigned int min_size;
124 u32 nulls_base;
125 bool insecure_elasticity;
126 bool automatic_shrinking;
127 size_t locks_mul;
128 rht_hashfn_t hashfn;
129 rht_obj_hashfn_t obj_hashfn;
130 rht_obj_cmpfn_t obj_cmpfn;
131};
132
133
134
135
136
137
138
139
140
141
142
143
144struct rhashtable {
145 struct bucket_table __rcu *tbl;
146 atomic_t nelems;
147 unsigned int key_len;
148 unsigned int elasticity;
149 struct rhashtable_params p;
150 struct work_struct run_work;
151 struct mutex mutex;
152 spinlock_t lock;
153};
154
155
156
157
158
159
160struct rhashtable_walker {
161 struct list_head list;
162 struct bucket_table *tbl;
163};
164
165
166
167
168
169
170
171
172
173struct rhashtable_iter {
174 struct rhashtable *ht;
175 struct rhash_head *p;
176 struct rhashtable_walker *walker;
177 unsigned int slot;
178 unsigned int skip;
179};
180
181static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
182{
183 return NULLS_MARKER(ht->p.nulls_base + hash);
184}
185
186#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
187 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
188
189static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
190{
191 return ((unsigned long) ptr & 1);
192}
193
194static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
195{
196 return ((unsigned long) ptr) >> 1;
197}
198
199static inline void *rht_obj(const struct rhashtable *ht,
200 const struct rhash_head *he)
201{
202 return (char *)he - ht->p.head_offset;
203}
204
205static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
206 unsigned int hash)
207{
208 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
209}
210
211static inline unsigned int rht_key_hashfn(
212 struct rhashtable *ht, const struct bucket_table *tbl,
213 const void *key, const struct rhashtable_params params)
214{
215 unsigned int hash;
216
217
218 if (!__builtin_constant_p(params.key_len))
219 hash = ht->p.hashfn(key, ht->key_len, tbl->hash_rnd);
220 else if (params.key_len) {
221 unsigned int key_len = params.key_len;
222
223 if (params.hashfn)
224 hash = params.hashfn(key, key_len, tbl->hash_rnd);
225 else if (key_len & (sizeof(u32) - 1))
226 hash = jhash(key, key_len, tbl->hash_rnd);
227 else
228 hash = jhash2(key, key_len / sizeof(u32),
229 tbl->hash_rnd);
230 } else {
231 unsigned int key_len = ht->p.key_len;
232
233 if (params.hashfn)
234 hash = params.hashfn(key, key_len, tbl->hash_rnd);
235 else
236 hash = jhash(key, key_len, tbl->hash_rnd);
237 }
238
239 return rht_bucket_index(tbl, hash);
240}
241
242static inline unsigned int rht_head_hashfn(
243 struct rhashtable *ht, const struct bucket_table *tbl,
244 const struct rhash_head *he, const struct rhashtable_params params)
245{
246 const char *ptr = rht_obj(ht, he);
247
248 return likely(params.obj_hashfn) ?
249 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
250 ht->p.key_len,
251 tbl->hash_rnd)) :
252 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
253}
254
255
256
257
258
259
260static inline bool rht_grow_above_75(const struct rhashtable *ht,
261 const struct bucket_table *tbl)
262{
263
264 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
265 (!ht->p.max_size || tbl->size < ht->p.max_size);
266}
267
268
269
270
271
272
273static inline bool rht_shrink_below_30(const struct rhashtable *ht,
274 const struct bucket_table *tbl)
275{
276
277 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
278 tbl->size > ht->p.min_size;
279}
280
281
282
283
284
285
286static inline bool rht_grow_above_100(const struct rhashtable *ht,
287 const struct bucket_table *tbl)
288{
289 return atomic_read(&ht->nelems) > tbl->size &&
290 (!ht->p.max_size || tbl->size < ht->p.max_size);
291}
292
293
294
295
296
297
298static inline bool rht_grow_above_max(const struct rhashtable *ht,
299 const struct bucket_table *tbl)
300{
301 return ht->p.insecure_max_entries &&
302 atomic_read(&ht->nelems) >= ht->p.insecure_max_entries;
303}
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
319 unsigned int hash)
320{
321 return &tbl->locks[hash & tbl->locks_mask];
322}
323
324#ifdef CONFIG_PROVE_LOCKING
325int lockdep_rht_mutex_is_held(struct rhashtable *ht);
326int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
327#else
328static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
329{
330 return 1;
331}
332
333static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
334 u32 hash)
335{
336 return 1;
337}
338#endif
339
340int rhashtable_init(struct rhashtable *ht,
341 const struct rhashtable_params *params);
342
343struct bucket_table *rhashtable_insert_slow(struct rhashtable *ht,
344 const void *key,
345 struct rhash_head *obj,
346 struct bucket_table *old_tbl);
347int rhashtable_insert_rehash(struct rhashtable *ht, struct bucket_table *tbl);
348
349int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
350void rhashtable_walk_exit(struct rhashtable_iter *iter);
351int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
352void *rhashtable_walk_next(struct rhashtable_iter *iter);
353void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
354
355void rhashtable_free_and_destroy(struct rhashtable *ht,
356 void (*free_fn)(void *ptr, void *arg),
357 void *arg);
358void rhashtable_destroy(struct rhashtable *ht);
359
360#define rht_dereference(p, ht) \
361 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
362
363#define rht_dereference_rcu(p, ht) \
364 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
365
366#define rht_dereference_bucket(p, tbl, hash) \
367 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
368
369#define rht_dereference_bucket_rcu(p, tbl, hash) \
370 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
371
372#define rht_entry(tpos, pos, member) \
373 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
374
375
376
377
378
379
380
381
382#define rht_for_each_continue(pos, head, tbl, hash) \
383 for (pos = rht_dereference_bucket(head, tbl, hash); \
384 !rht_is_a_nulls(pos); \
385 pos = rht_dereference_bucket((pos)->next, tbl, hash))
386
387
388
389
390
391
392
393#define rht_for_each(pos, tbl, hash) \
394 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
395
396
397
398
399
400
401
402
403
404
405#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
406 for (pos = rht_dereference_bucket(head, tbl, hash); \
407 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
408 pos = rht_dereference_bucket((pos)->next, tbl, hash))
409
410
411
412
413
414
415
416
417
418#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
419 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
420 tbl, hash, member)
421
422
423
424
425
426
427
428
429
430
431
432
433
434#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
435 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
436 next = !rht_is_a_nulls(pos) ? \
437 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
438 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
439 pos = next, \
440 next = !rht_is_a_nulls(pos) ? \
441 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
442
443
444
445
446
447
448
449
450
451
452
453
454#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
455 for (({barrier(); }), \
456 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
457 !rht_is_a_nulls(pos); \
458 pos = rcu_dereference_raw(pos->next))
459
460
461
462
463
464
465
466
467
468
469
470#define rht_for_each_rcu(pos, tbl, hash) \
471 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
487 for (({barrier(); }), \
488 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
489 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
490 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
491
492
493
494
495
496
497
498
499
500
501
502
503
504#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
505 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
506 tbl, hash, member)
507
508static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
509 const void *obj)
510{
511 struct rhashtable *ht = arg->ht;
512 const char *ptr = obj;
513
514 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
515}
516
517
518
519
520
521
522
523
524
525
526
527
528static inline void *rhashtable_lookup_fast(
529 struct rhashtable *ht, const void *key,
530 const struct rhashtable_params params)
531{
532 struct rhashtable_compare_arg arg = {
533 .ht = ht,
534 .key = key,
535 };
536 const struct bucket_table *tbl;
537 struct rhash_head *he;
538 unsigned int hash;
539
540 rcu_read_lock();
541
542 tbl = rht_dereference_rcu(ht->tbl, ht);
543restart:
544 hash = rht_key_hashfn(ht, tbl, key, params);
545 rht_for_each_rcu(he, tbl, hash) {
546 if (params.obj_cmpfn ?
547 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
548 rhashtable_compare(&arg, rht_obj(ht, he)))
549 continue;
550 rcu_read_unlock();
551 return rht_obj(ht, he);
552 }
553
554
555 smp_rmb();
556
557 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
558 if (unlikely(tbl))
559 goto restart;
560 rcu_read_unlock();
561
562 return NULL;
563}
564
565
566static inline int __rhashtable_insert_fast(
567 struct rhashtable *ht, const void *key, struct rhash_head *obj,
568 const struct rhashtable_params params)
569{
570 struct rhashtable_compare_arg arg = {
571 .ht = ht,
572 .key = key,
573 };
574 struct bucket_table *tbl, *new_tbl;
575 struct rhash_head *head;
576 spinlock_t *lock;
577 unsigned int elasticity;
578 unsigned int hash;
579 int err;
580
581restart:
582 rcu_read_lock();
583
584 tbl = rht_dereference_rcu(ht->tbl, ht);
585
586
587
588
589 for (;;) {
590 hash = rht_head_hashfn(ht, tbl, obj, params);
591 lock = rht_bucket_lock(tbl, hash);
592 spin_lock_bh(lock);
593
594 if (tbl->rehash <= hash)
595 break;
596
597 spin_unlock_bh(lock);
598 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
599 }
600
601 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
602 if (unlikely(new_tbl)) {
603 tbl = rhashtable_insert_slow(ht, key, obj, new_tbl);
604 if (!IS_ERR_OR_NULL(tbl))
605 goto slow_path;
606
607 err = PTR_ERR(tbl);
608 goto out;
609 }
610
611 err = -E2BIG;
612 if (unlikely(rht_grow_above_max(ht, tbl)))
613 goto out;
614
615 if (unlikely(rht_grow_above_100(ht, tbl))) {
616slow_path:
617 spin_unlock_bh(lock);
618 err = rhashtable_insert_rehash(ht, tbl);
619 rcu_read_unlock();
620 if (err)
621 return err;
622
623 goto restart;
624 }
625
626 err = -EEXIST;
627 elasticity = ht->elasticity;
628 rht_for_each(head, tbl, hash) {
629 if (key &&
630 unlikely(!(params.obj_cmpfn ?
631 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
632 rhashtable_compare(&arg, rht_obj(ht, head)))))
633 goto out;
634 if (!--elasticity)
635 goto slow_path;
636 }
637
638 err = 0;
639
640 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
641
642 RCU_INIT_POINTER(obj->next, head);
643
644 rcu_assign_pointer(tbl->buckets[hash], obj);
645
646 atomic_inc(&ht->nelems);
647 if (rht_grow_above_75(ht, tbl))
648 schedule_work(&ht->run_work);
649
650out:
651 spin_unlock_bh(lock);
652 rcu_read_unlock();
653
654 return err;
655}
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673static inline int rhashtable_insert_fast(
674 struct rhashtable *ht, struct rhash_head *obj,
675 const struct rhashtable_params params)
676{
677 return __rhashtable_insert_fast(ht, NULL, obj, params);
678}
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701static inline int rhashtable_lookup_insert_fast(
702 struct rhashtable *ht, struct rhash_head *obj,
703 const struct rhashtable_params params)
704{
705 const char *key = rht_obj(ht, obj);
706
707 BUG_ON(ht->p.obj_hashfn);
708
709 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj,
710 params);
711}
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735static inline int rhashtable_lookup_insert_key(
736 struct rhashtable *ht, const void *key, struct rhash_head *obj,
737 const struct rhashtable_params params)
738{
739 BUG_ON(!ht->p.obj_hashfn || !key);
740
741 return __rhashtable_insert_fast(ht, key, obj, params);
742}
743
744
745static inline int __rhashtable_remove_fast(
746 struct rhashtable *ht, struct bucket_table *tbl,
747 struct rhash_head *obj, const struct rhashtable_params params)
748{
749 struct rhash_head __rcu **pprev;
750 struct rhash_head *he;
751 spinlock_t * lock;
752 unsigned int hash;
753 int err = -ENOENT;
754
755 hash = rht_head_hashfn(ht, tbl, obj, params);
756 lock = rht_bucket_lock(tbl, hash);
757
758 spin_lock_bh(lock);
759
760 pprev = &tbl->buckets[hash];
761 rht_for_each(he, tbl, hash) {
762 if (he != obj) {
763 pprev = &he->next;
764 continue;
765 }
766
767 rcu_assign_pointer(*pprev, obj->next);
768 err = 0;
769 break;
770 }
771
772 spin_unlock_bh(lock);
773
774 return err;
775}
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792static inline int rhashtable_remove_fast(
793 struct rhashtable *ht, struct rhash_head *obj,
794 const struct rhashtable_params params)
795{
796 struct bucket_table *tbl;
797 int err;
798
799 rcu_read_lock();
800
801 tbl = rht_dereference_rcu(ht->tbl, ht);
802
803
804
805
806
807
808 while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) &&
809 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
810 ;
811
812 if (err)
813 goto out;
814
815 atomic_dec(&ht->nelems);
816 if (unlikely(ht->p.automatic_shrinking &&
817 rht_shrink_below_30(ht, tbl)))
818 schedule_work(&ht->run_work);
819
820out:
821 rcu_read_unlock();
822
823 return err;
824}
825
826
827static inline int __rhashtable_replace_fast(
828 struct rhashtable *ht, struct bucket_table *tbl,
829 struct rhash_head *obj_old, struct rhash_head *obj_new,
830 const struct rhashtable_params params)
831{
832 struct rhash_head __rcu **pprev;
833 struct rhash_head *he;
834 spinlock_t *lock;
835 unsigned int hash;
836 int err = -ENOENT;
837
838
839
840
841 hash = rht_head_hashfn(ht, tbl, obj_old, params);
842 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
843 return -EINVAL;
844
845 lock = rht_bucket_lock(tbl, hash);
846
847 spin_lock_bh(lock);
848
849 pprev = &tbl->buckets[hash];
850 rht_for_each(he, tbl, hash) {
851 if (he != obj_old) {
852 pprev = &he->next;
853 continue;
854 }
855
856 rcu_assign_pointer(obj_new->next, obj_old->next);
857 rcu_assign_pointer(*pprev, obj_new);
858 err = 0;
859 break;
860 }
861
862 spin_unlock_bh(lock);
863
864 return err;
865}
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881static inline int rhashtable_replace_fast(
882 struct rhashtable *ht, struct rhash_head *obj_old,
883 struct rhash_head *obj_new,
884 const struct rhashtable_params params)
885{
886 struct bucket_table *tbl;
887 int err;
888
889 rcu_read_lock();
890
891 tbl = rht_dereference_rcu(ht->tbl, ht);
892
893
894
895
896
897
898 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
899 obj_new, params)) &&
900 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
901 ;
902
903 rcu_read_unlock();
904
905 return err;
906}
907
908#endif
909