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,
350 gfp_t gfp);
351void rhashtable_walk_exit(struct rhashtable_iter *iter);
352int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
353void *rhashtable_walk_next(struct rhashtable_iter *iter);
354void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
355
356void rhashtable_free_and_destroy(struct rhashtable *ht,
357 void (*free_fn)(void *ptr, void *arg),
358 void *arg);
359void rhashtable_destroy(struct rhashtable *ht);
360
361#define rht_dereference(p, ht) \
362 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
363
364#define rht_dereference_rcu(p, ht) \
365 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
366
367#define rht_dereference_bucket(p, tbl, hash) \
368 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
369
370#define rht_dereference_bucket_rcu(p, tbl, hash) \
371 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
372
373#define rht_entry(tpos, pos, member) \
374 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
375
376
377
378
379
380
381
382
383#define rht_for_each_continue(pos, head, tbl, hash) \
384 for (pos = rht_dereference_bucket(head, tbl, hash); \
385 !rht_is_a_nulls(pos); \
386 pos = rht_dereference_bucket((pos)->next, tbl, hash))
387
388
389
390
391
392
393
394#define rht_for_each(pos, tbl, hash) \
395 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
396
397
398
399
400
401
402
403
404
405
406#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
407 for (pos = rht_dereference_bucket(head, tbl, hash); \
408 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
409 pos = rht_dereference_bucket((pos)->next, tbl, hash))
410
411
412
413
414
415
416
417
418
419#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
420 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
421 tbl, hash, member)
422
423
424
425
426
427
428
429
430
431
432
433
434
435#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
436 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
437 next = !rht_is_a_nulls(pos) ? \
438 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
439 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
440 pos = next, \
441 next = !rht_is_a_nulls(pos) ? \
442 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
443
444
445
446
447
448
449
450
451
452
453
454
455#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
456 for (({barrier(); }), \
457 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
458 !rht_is_a_nulls(pos); \
459 pos = rcu_dereference_raw(pos->next))
460
461
462
463
464
465
466
467
468
469
470
471#define rht_for_each_rcu(pos, tbl, hash) \
472 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
488 for (({barrier(); }), \
489 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
490 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
491 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
492
493
494
495
496
497
498
499
500
501
502
503
504
505#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
506 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
507 tbl, hash, member)
508
509static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
510 const void *obj)
511{
512 struct rhashtable *ht = arg->ht;
513 const char *ptr = obj;
514
515 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
516}
517
518
519
520
521
522
523
524
525
526
527
528
529static inline void *rhashtable_lookup_fast(
530 struct rhashtable *ht, const void *key,
531 const struct rhashtable_params params)
532{
533 struct rhashtable_compare_arg arg = {
534 .ht = ht,
535 .key = key,
536 };
537 const struct bucket_table *tbl;
538 struct rhash_head *he;
539 unsigned int hash;
540
541 rcu_read_lock();
542
543 tbl = rht_dereference_rcu(ht->tbl, ht);
544restart:
545 hash = rht_key_hashfn(ht, tbl, key, params);
546 rht_for_each_rcu(he, tbl, hash) {
547 if (params.obj_cmpfn ?
548 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
549 rhashtable_compare(&arg, rht_obj(ht, he)))
550 continue;
551 rcu_read_unlock();
552 return rht_obj(ht, he);
553 }
554
555
556 smp_rmb();
557
558 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
559 if (unlikely(tbl))
560 goto restart;
561 rcu_read_unlock();
562
563 return NULL;
564}
565
566
567static inline int __rhashtable_insert_fast(
568 struct rhashtable *ht, const void *key, struct rhash_head *obj,
569 const struct rhashtable_params params)
570{
571 struct rhashtable_compare_arg arg = {
572 .ht = ht,
573 .key = key,
574 };
575 struct bucket_table *tbl, *new_tbl;
576 struct rhash_head *head;
577 spinlock_t *lock;
578 unsigned int elasticity;
579 unsigned int hash;
580 int err;
581
582restart:
583 rcu_read_lock();
584
585 tbl = rht_dereference_rcu(ht->tbl, ht);
586
587
588
589
590 for (;;) {
591 hash = rht_head_hashfn(ht, tbl, obj, params);
592 lock = rht_bucket_lock(tbl, hash);
593 spin_lock_bh(lock);
594
595 if (tbl->rehash <= hash)
596 break;
597
598 spin_unlock_bh(lock);
599 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
600 }
601
602 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
603 if (unlikely(new_tbl)) {
604 tbl = rhashtable_insert_slow(ht, key, obj, new_tbl);
605 if (!IS_ERR_OR_NULL(tbl))
606 goto slow_path;
607
608 err = PTR_ERR(tbl);
609 goto out;
610 }
611
612 err = -E2BIG;
613 if (unlikely(rht_grow_above_max(ht, tbl)))
614 goto out;
615
616 if (unlikely(rht_grow_above_100(ht, tbl))) {
617slow_path:
618 spin_unlock_bh(lock);
619 err = rhashtable_insert_rehash(ht, tbl);
620 rcu_read_unlock();
621 if (err)
622 return err;
623
624 goto restart;
625 }
626
627 err = -EEXIST;
628 elasticity = ht->elasticity;
629 rht_for_each(head, tbl, hash) {
630 if (key &&
631 unlikely(!(params.obj_cmpfn ?
632 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
633 rhashtable_compare(&arg, rht_obj(ht, head)))))
634 goto out;
635 if (!--elasticity)
636 goto slow_path;
637 }
638
639 err = 0;
640
641 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
642
643 RCU_INIT_POINTER(obj->next, head);
644
645 rcu_assign_pointer(tbl->buckets[hash], obj);
646
647 atomic_inc(&ht->nelems);
648 if (rht_grow_above_75(ht, tbl))
649 schedule_work(&ht->run_work);
650
651out:
652 spin_unlock_bh(lock);
653 rcu_read_unlock();
654
655 return err;
656}
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674static inline int rhashtable_insert_fast(
675 struct rhashtable *ht, struct rhash_head *obj,
676 const struct rhashtable_params params)
677{
678 return __rhashtable_insert_fast(ht, NULL, obj, params);
679}
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702static inline int rhashtable_lookup_insert_fast(
703 struct rhashtable *ht, struct rhash_head *obj,
704 const struct rhashtable_params params)
705{
706 const char *key = rht_obj(ht, obj);
707
708 BUG_ON(ht->p.obj_hashfn);
709
710 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj,
711 params);
712}
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736static inline int rhashtable_lookup_insert_key(
737 struct rhashtable *ht, const void *key, struct rhash_head *obj,
738 const struct rhashtable_params params)
739{
740 BUG_ON(!ht->p.obj_hashfn || !key);
741
742 return __rhashtable_insert_fast(ht, key, obj, params);
743}
744
745
746static inline int __rhashtable_remove_fast(
747 struct rhashtable *ht, struct bucket_table *tbl,
748 struct rhash_head *obj, const struct rhashtable_params params)
749{
750 struct rhash_head __rcu **pprev;
751 struct rhash_head *he;
752 spinlock_t * lock;
753 unsigned int hash;
754 int err = -ENOENT;
755
756 hash = rht_head_hashfn(ht, tbl, obj, params);
757 lock = rht_bucket_lock(tbl, hash);
758
759 spin_lock_bh(lock);
760
761 pprev = &tbl->buckets[hash];
762 rht_for_each(he, tbl, hash) {
763 if (he != obj) {
764 pprev = &he->next;
765 continue;
766 }
767
768 rcu_assign_pointer(*pprev, obj->next);
769 err = 0;
770 break;
771 }
772
773 spin_unlock_bh(lock);
774
775 return err;
776}
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793static inline int rhashtable_remove_fast(
794 struct rhashtable *ht, struct rhash_head *obj,
795 const struct rhashtable_params params)
796{
797 struct bucket_table *tbl;
798 int err;
799
800 rcu_read_lock();
801
802 tbl = rht_dereference_rcu(ht->tbl, ht);
803
804
805
806
807
808
809 while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) &&
810 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
811 ;
812
813 if (err)
814 goto out;
815
816 atomic_dec(&ht->nelems);
817 if (unlikely(ht->p.automatic_shrinking &&
818 rht_shrink_below_30(ht, tbl)))
819 schedule_work(&ht->run_work);
820
821out:
822 rcu_read_unlock();
823
824 return err;
825}
826
827
828static inline int __rhashtable_replace_fast(
829 struct rhashtable *ht, struct bucket_table *tbl,
830 struct rhash_head *obj_old, struct rhash_head *obj_new,
831 const struct rhashtable_params params)
832{
833 struct rhash_head __rcu **pprev;
834 struct rhash_head *he;
835 spinlock_t *lock;
836 unsigned int hash;
837 int err = -ENOENT;
838
839
840
841
842 hash = rht_head_hashfn(ht, tbl, obj_old, params);
843 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
844 return -EINVAL;
845
846 lock = rht_bucket_lock(tbl, hash);
847
848 spin_lock_bh(lock);
849
850 pprev = &tbl->buckets[hash];
851 rht_for_each(he, tbl, hash) {
852 if (he != obj_old) {
853 pprev = &he->next;
854 continue;
855 }
856
857 rcu_assign_pointer(obj_new->next, obj_old->next);
858 rcu_assign_pointer(*pprev, obj_new);
859 err = 0;
860 break;
861 }
862
863 spin_unlock_bh(lock);
864
865 return err;
866}
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882static inline int rhashtable_replace_fast(
883 struct rhashtable *ht, struct rhash_head *obj_old,
884 struct rhash_head *obj_new,
885 const struct rhashtable_params params)
886{
887 struct bucket_table *tbl;
888 int err;
889
890 rcu_read_lock();
891
892 tbl = rht_dereference_rcu(ht->tbl, ht);
893
894
895
896
897
898
899 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
900 obj_new, params)) &&
901 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
902 ;
903
904 rcu_read_unlock();
905
906 return err;
907}
908
909#endif
910