This source file includes following definitions.
- rht_is_a_nulls
- rht_obj
- rht_bucket_index
- rht_key_get_hash
- rht_key_hashfn
- rht_head_hashfn
- rht_grow_above_75
- rht_shrink_below_30
- rht_grow_above_100
- rht_grow_above_max
- lockdep_rht_mutex_is_held
- lockdep_rht_bucket_is_held
- rhashtable_walk_start_check
- rht_bucket
- rht_bucket_var
- rht_bucket_insert
- rht_lock
- rht_lock_nested
- rht_unlock
- __rht_ptr
- rht_ptr_rcu
- rht_ptr
- rht_ptr_exclusive
- rht_assign_locked
- rht_assign_unlock
- rhashtable_compare
- __rhashtable_lookup
- rhashtable_lookup
- rhashtable_lookup_fast
- rhltable_lookup
- __rhashtable_insert_fast
- rhashtable_insert_fast
- rhltable_insert_key
- rhltable_insert
- rhashtable_lookup_insert_fast
- rhashtable_lookup_get_insert_fast
- rhashtable_lookup_insert_key
- rhashtable_lookup_get_insert_key
- __rhashtable_remove_fast_one
- __rhashtable_remove_fast
- rhashtable_remove_fast
- rhltable_remove
- __rhashtable_replace_fast
- rhashtable_replace_fast
- rhltable_walk_enter
- rhltable_free_and_destroy
- rhltable_destroy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #ifndef _LINUX_RHASHTABLE_H
19 #define _LINUX_RHASHTABLE_H
20
21 #include <linux/err.h>
22 #include <linux/errno.h>
23 #include <linux/jhash.h>
24 #include <linux/list_nulls.h>
25 #include <linux/workqueue.h>
26 #include <linux/rculist.h>
27 #include <linux/bit_spinlock.h>
28
29 #include <linux/rhashtable-types.h>
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 struct rhash_lock_head {};
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 #define RHT_ELASTICITY 16u
63
64
65
66
67
68
69
70
71
72
73
74
75
76 struct bucket_table {
77 unsigned int size;
78 unsigned int nest;
79 u32 hash_rnd;
80 struct list_head walkers;
81 struct rcu_head rcu;
82
83 struct bucket_table __rcu *future_tbl;
84
85 struct lockdep_map dep_map;
86
87 struct rhash_lock_head *buckets[] ____cacheline_aligned_in_smp;
88 };
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 #define RHT_NULLS_MARKER(ptr) \
104 ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))
105 #define INIT_RHT_NULLS_HEAD(ptr) \
106 ((ptr) = NULL)
107
108 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
109 {
110 return ((unsigned long) ptr & 1);
111 }
112
113 static inline void *rht_obj(const struct rhashtable *ht,
114 const struct rhash_head *he)
115 {
116 return (char *)he - ht->p.head_offset;
117 }
118
119 static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
120 unsigned int hash)
121 {
122 return hash & (tbl->size - 1);
123 }
124
125 static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
126 const void *key, const struct rhashtable_params params,
127 unsigned int hash_rnd)
128 {
129 unsigned int hash;
130
131
132 if (!__builtin_constant_p(params.key_len))
133 hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
134 else if (params.key_len) {
135 unsigned int key_len = params.key_len;
136
137 if (params.hashfn)
138 hash = params.hashfn(key, key_len, hash_rnd);
139 else if (key_len & (sizeof(u32) - 1))
140 hash = jhash(key, key_len, hash_rnd);
141 else
142 hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
143 } else {
144 unsigned int key_len = ht->p.key_len;
145
146 if (params.hashfn)
147 hash = params.hashfn(key, key_len, hash_rnd);
148 else
149 hash = jhash(key, key_len, hash_rnd);
150 }
151
152 return hash;
153 }
154
155 static inline unsigned int rht_key_hashfn(
156 struct rhashtable *ht, const struct bucket_table *tbl,
157 const void *key, const struct rhashtable_params params)
158 {
159 unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd);
160
161 return rht_bucket_index(tbl, hash);
162 }
163
164 static inline unsigned int rht_head_hashfn(
165 struct rhashtable *ht, const struct bucket_table *tbl,
166 const struct rhash_head *he, const struct rhashtable_params params)
167 {
168 const char *ptr = rht_obj(ht, he);
169
170 return likely(params.obj_hashfn) ?
171 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
172 ht->p.key_len,
173 tbl->hash_rnd)) :
174 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
175 }
176
177
178
179
180
181
182 static inline bool rht_grow_above_75(const struct rhashtable *ht,
183 const struct bucket_table *tbl)
184 {
185
186 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
187 (!ht->p.max_size || tbl->size < ht->p.max_size);
188 }
189
190
191
192
193
194
195 static inline bool rht_shrink_below_30(const struct rhashtable *ht,
196 const struct bucket_table *tbl)
197 {
198
199 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
200 tbl->size > ht->p.min_size;
201 }
202
203
204
205
206
207
208 static inline bool rht_grow_above_100(const struct rhashtable *ht,
209 const struct bucket_table *tbl)
210 {
211 return atomic_read(&ht->nelems) > tbl->size &&
212 (!ht->p.max_size || tbl->size < ht->p.max_size);
213 }
214
215
216
217
218
219
220 static inline bool rht_grow_above_max(const struct rhashtable *ht,
221 const struct bucket_table *tbl)
222 {
223 return atomic_read(&ht->nelems) >= ht->max_elems;
224 }
225
226 #ifdef CONFIG_PROVE_LOCKING
227 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
228 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
229 #else
230 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
231 {
232 return 1;
233 }
234
235 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
236 u32 hash)
237 {
238 return 1;
239 }
240 #endif
241
242 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
243 struct rhash_head *obj);
244
245 void rhashtable_walk_enter(struct rhashtable *ht,
246 struct rhashtable_iter *iter);
247 void rhashtable_walk_exit(struct rhashtable_iter *iter);
248 int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
249
250 static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
251 {
252 (void)rhashtable_walk_start_check(iter);
253 }
254
255 void *rhashtable_walk_next(struct rhashtable_iter *iter);
256 void *rhashtable_walk_peek(struct rhashtable_iter *iter);
257 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
258
259 void rhashtable_free_and_destroy(struct rhashtable *ht,
260 void (*free_fn)(void *ptr, void *arg),
261 void *arg);
262 void rhashtable_destroy(struct rhashtable *ht);
263
264 struct rhash_lock_head **rht_bucket_nested(const struct bucket_table *tbl,
265 unsigned int hash);
266 struct rhash_lock_head **__rht_bucket_nested(const struct bucket_table *tbl,
267 unsigned int hash);
268 struct rhash_lock_head **rht_bucket_nested_insert(struct rhashtable *ht,
269 struct bucket_table *tbl,
270 unsigned int hash);
271
272 #define rht_dereference(p, ht) \
273 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
274
275 #define rht_dereference_rcu(p, ht) \
276 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
277
278 #define rht_dereference_bucket(p, tbl, hash) \
279 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
280
281 #define rht_dereference_bucket_rcu(p, tbl, hash) \
282 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
283
284 #define rht_entry(tpos, pos, member) \
285 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
286
287 static inline struct rhash_lock_head *const *rht_bucket(
288 const struct bucket_table *tbl, unsigned int hash)
289 {
290 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
291 &tbl->buckets[hash];
292 }
293
294 static inline struct rhash_lock_head **rht_bucket_var(
295 struct bucket_table *tbl, unsigned int hash)
296 {
297 return unlikely(tbl->nest) ? __rht_bucket_nested(tbl, hash) :
298 &tbl->buckets[hash];
299 }
300
301 static inline struct rhash_lock_head **rht_bucket_insert(
302 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
303 {
304 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
305 &tbl->buckets[hash];
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327 static inline void rht_lock(struct bucket_table *tbl,
328 struct rhash_lock_head **bkt)
329 {
330 local_bh_disable();
331 bit_spin_lock(0, (unsigned long *)bkt);
332 lock_map_acquire(&tbl->dep_map);
333 }
334
335 static inline void rht_lock_nested(struct bucket_table *tbl,
336 struct rhash_lock_head **bucket,
337 unsigned int subclass)
338 {
339 local_bh_disable();
340 bit_spin_lock(0, (unsigned long *)bucket);
341 lock_acquire_exclusive(&tbl->dep_map, subclass, 0, NULL, _THIS_IP_);
342 }
343
344 static inline void rht_unlock(struct bucket_table *tbl,
345 struct rhash_lock_head **bkt)
346 {
347 lock_map_release(&tbl->dep_map);
348 bit_spin_unlock(0, (unsigned long *)bkt);
349 local_bh_enable();
350 }
351
352 static inline struct rhash_head __rcu *__rht_ptr(
353 struct rhash_lock_head *const *bkt)
354 {
355 return (struct rhash_head __rcu *)
356 ((unsigned long)*bkt & ~BIT(0) ?:
357 (unsigned long)RHT_NULLS_MARKER(bkt));
358 }
359
360
361
362
363
364
365
366
367 static inline struct rhash_head *rht_ptr_rcu(
368 struct rhash_lock_head *const *bkt)
369 {
370 struct rhash_head __rcu *p = __rht_ptr(bkt);
371
372 return rcu_dereference(p);
373 }
374
375 static inline struct rhash_head *rht_ptr(
376 struct rhash_lock_head *const *bkt,
377 struct bucket_table *tbl,
378 unsigned int hash)
379 {
380 return rht_dereference_bucket(__rht_ptr(bkt), tbl, hash);
381 }
382
383 static inline struct rhash_head *rht_ptr_exclusive(
384 struct rhash_lock_head *const *bkt)
385 {
386 return rcu_dereference_protected(__rht_ptr(bkt), 1);
387 }
388
389 static inline void rht_assign_locked(struct rhash_lock_head **bkt,
390 struct rhash_head *obj)
391 {
392 struct rhash_head __rcu **p = (struct rhash_head __rcu **)bkt;
393
394 if (rht_is_a_nulls(obj))
395 obj = NULL;
396 rcu_assign_pointer(*p, (void *)((unsigned long)obj | BIT(0)));
397 }
398
399 static inline void rht_assign_unlock(struct bucket_table *tbl,
400 struct rhash_lock_head **bkt,
401 struct rhash_head *obj)
402 {
403 struct rhash_head __rcu **p = (struct rhash_head __rcu **)bkt;
404
405 if (rht_is_a_nulls(obj))
406 obj = NULL;
407 lock_map_release(&tbl->dep_map);
408 rcu_assign_pointer(*p, obj);
409 preempt_enable();
410 __release(bitlock);
411 local_bh_enable();
412 }
413
414
415
416
417
418
419
420
421 #define rht_for_each_from(pos, head, tbl, hash) \
422 for (pos = head; \
423 !rht_is_a_nulls(pos); \
424 pos = rht_dereference_bucket((pos)->next, tbl, hash))
425
426
427
428
429
430
431
432 #define rht_for_each(pos, tbl, hash) \
433 rht_for_each_from(pos, rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
434 tbl, hash)
435
436
437
438
439
440
441
442
443
444
445 #define rht_for_each_entry_from(tpos, pos, head, tbl, hash, member) \
446 for (pos = head; \
447 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
448 pos = rht_dereference_bucket((pos)->next, tbl, hash))
449
450
451
452
453
454
455
456
457
458 #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
459 rht_for_each_entry_from(tpos, pos, \
460 rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
461 tbl, hash, member)
462
463
464
465
466
467
468
469
470
471
472
473
474
475 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
476 for (pos = rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
477 next = !rht_is_a_nulls(pos) ? \
478 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
479 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
480 pos = next, \
481 next = !rht_is_a_nulls(pos) ? \
482 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
483
484
485
486
487
488
489
490
491
492
493
494
495 #define rht_for_each_rcu_from(pos, head, tbl, hash) \
496 for (({barrier(); }), \
497 pos = head; \
498 !rht_is_a_nulls(pos); \
499 pos = rcu_dereference_raw(pos->next))
500
501
502
503
504
505
506
507
508
509
510
511 #define rht_for_each_rcu(pos, tbl, hash) \
512 for (({barrier(); }), \
513 pos = rht_ptr_rcu(rht_bucket(tbl, hash)); \
514 !rht_is_a_nulls(pos); \
515 pos = rcu_dereference_raw(pos->next))
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530 #define rht_for_each_entry_rcu_from(tpos, pos, head, tbl, hash, member) \
531 for (({barrier(); }), \
532 pos = head; \
533 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
534 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
535
536
537
538
539
540
541
542
543
544
545
546
547
548 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
549 rht_for_each_entry_rcu_from(tpos, pos, \
550 rht_ptr_rcu(rht_bucket(tbl, hash)), \
551 tbl, hash, member)
552
553
554
555
556
557
558
559
560
561 #define rhl_for_each_rcu(pos, list) \
562 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
563
564
565
566
567
568
569
570
571
572
573
574 #define rhl_for_each_entry_rcu(tpos, pos, list, member) \
575 for (pos = list; pos && rht_entry(tpos, pos, member); \
576 pos = rcu_dereference_raw(pos->next))
577
578 static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
579 const void *obj)
580 {
581 struct rhashtable *ht = arg->ht;
582 const char *ptr = obj;
583
584 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
585 }
586
587
588 static inline struct rhash_head *__rhashtable_lookup(
589 struct rhashtable *ht, const void *key,
590 const struct rhashtable_params params)
591 {
592 struct rhashtable_compare_arg arg = {
593 .ht = ht,
594 .key = key,
595 };
596 struct rhash_lock_head *const *bkt;
597 struct bucket_table *tbl;
598 struct rhash_head *he;
599 unsigned int hash;
600
601 tbl = rht_dereference_rcu(ht->tbl, ht);
602 restart:
603 hash = rht_key_hashfn(ht, tbl, key, params);
604 bkt = rht_bucket(tbl, hash);
605 do {
606 rht_for_each_rcu_from(he, rht_ptr_rcu(bkt), tbl, hash) {
607 if (params.obj_cmpfn ?
608 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
609 rhashtable_compare(&arg, rht_obj(ht, he)))
610 continue;
611 return he;
612 }
613
614
615
616 } while (he != RHT_NULLS_MARKER(bkt));
617
618
619 smp_rmb();
620
621 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
622 if (unlikely(tbl))
623 goto restart;
624
625 return NULL;
626 }
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641 static inline void *rhashtable_lookup(
642 struct rhashtable *ht, const void *key,
643 const struct rhashtable_params params)
644 {
645 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
646
647 return he ? rht_obj(ht, he) : NULL;
648 }
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664 static inline void *rhashtable_lookup_fast(
665 struct rhashtable *ht, const void *key,
666 const struct rhashtable_params params)
667 {
668 void *obj;
669
670 rcu_read_lock();
671 obj = rhashtable_lookup(ht, key, params);
672 rcu_read_unlock();
673
674 return obj;
675 }
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691 static inline struct rhlist_head *rhltable_lookup(
692 struct rhltable *hlt, const void *key,
693 const struct rhashtable_params params)
694 {
695 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
696
697 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
698 }
699
700
701
702
703
704 static inline void *__rhashtable_insert_fast(
705 struct rhashtable *ht, const void *key, struct rhash_head *obj,
706 const struct rhashtable_params params, bool rhlist)
707 {
708 struct rhashtable_compare_arg arg = {
709 .ht = ht,
710 .key = key,
711 };
712 struct rhash_lock_head **bkt;
713 struct rhash_head __rcu **pprev;
714 struct bucket_table *tbl;
715 struct rhash_head *head;
716 unsigned int hash;
717 int elasticity;
718 void *data;
719
720 rcu_read_lock();
721
722 tbl = rht_dereference_rcu(ht->tbl, ht);
723 hash = rht_head_hashfn(ht, tbl, obj, params);
724 elasticity = RHT_ELASTICITY;
725 bkt = rht_bucket_insert(ht, tbl, hash);
726 data = ERR_PTR(-ENOMEM);
727 if (!bkt)
728 goto out;
729 pprev = NULL;
730 rht_lock(tbl, bkt);
731
732 if (unlikely(rcu_access_pointer(tbl->future_tbl))) {
733 slow_path:
734 rht_unlock(tbl, bkt);
735 rcu_read_unlock();
736 return rhashtable_insert_slow(ht, key, obj);
737 }
738
739 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
740 struct rhlist_head *plist;
741 struct rhlist_head *list;
742
743 elasticity--;
744 if (!key ||
745 (params.obj_cmpfn ?
746 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
747 rhashtable_compare(&arg, rht_obj(ht, head)))) {
748 pprev = &head->next;
749 continue;
750 }
751
752 data = rht_obj(ht, head);
753
754 if (!rhlist)
755 goto out_unlock;
756
757
758 list = container_of(obj, struct rhlist_head, rhead);
759 plist = container_of(head, struct rhlist_head, rhead);
760
761 RCU_INIT_POINTER(list->next, plist);
762 head = rht_dereference_bucket(head->next, tbl, hash);
763 RCU_INIT_POINTER(list->rhead.next, head);
764 if (pprev) {
765 rcu_assign_pointer(*pprev, obj);
766 rht_unlock(tbl, bkt);
767 } else
768 rht_assign_unlock(tbl, bkt, obj);
769 data = NULL;
770 goto out;
771 }
772
773 if (elasticity <= 0)
774 goto slow_path;
775
776 data = ERR_PTR(-E2BIG);
777 if (unlikely(rht_grow_above_max(ht, tbl)))
778 goto out_unlock;
779
780 if (unlikely(rht_grow_above_100(ht, tbl)))
781 goto slow_path;
782
783
784 head = rht_ptr(bkt, tbl, hash);
785
786 RCU_INIT_POINTER(obj->next, head);
787 if (rhlist) {
788 struct rhlist_head *list;
789
790 list = container_of(obj, struct rhlist_head, rhead);
791 RCU_INIT_POINTER(list->next, NULL);
792 }
793
794 atomic_inc(&ht->nelems);
795 rht_assign_unlock(tbl, bkt, obj);
796
797 if (rht_grow_above_75(ht, tbl))
798 schedule_work(&ht->run_work);
799
800 data = NULL;
801 out:
802 rcu_read_unlock();
803
804 return data;
805
806 out_unlock:
807 rht_unlock(tbl, bkt);
808 goto out;
809 }
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826 static inline int rhashtable_insert_fast(
827 struct rhashtable *ht, struct rhash_head *obj,
828 const struct rhashtable_params params)
829 {
830 void *ret;
831
832 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
833 if (IS_ERR(ret))
834 return PTR_ERR(ret);
835
836 return ret == NULL ? 0 : -EEXIST;
837 }
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855 static inline int rhltable_insert_key(
856 struct rhltable *hlt, const void *key, struct rhlist_head *list,
857 const struct rhashtable_params params)
858 {
859 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
860 params, true));
861 }
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878 static inline int rhltable_insert(
879 struct rhltable *hlt, struct rhlist_head *list,
880 const struct rhashtable_params params)
881 {
882 const char *key = rht_obj(&hlt->ht, &list->rhead);
883
884 key += params.key_offset;
885
886 return rhltable_insert_key(hlt, key, list, params);
887 }
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903 static inline int rhashtable_lookup_insert_fast(
904 struct rhashtable *ht, struct rhash_head *obj,
905 const struct rhashtable_params params)
906 {
907 const char *key = rht_obj(ht, obj);
908 void *ret;
909
910 BUG_ON(ht->p.obj_hashfn);
911
912 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
913 false);
914 if (IS_ERR(ret))
915 return PTR_ERR(ret);
916
917 return ret == NULL ? 0 : -EEXIST;
918 }
919
920
921
922
923
924
925
926
927
928
929
930 static inline void *rhashtable_lookup_get_insert_fast(
931 struct rhashtable *ht, struct rhash_head *obj,
932 const struct rhashtable_params params)
933 {
934 const char *key = rht_obj(ht, obj);
935
936 BUG_ON(ht->p.obj_hashfn);
937
938 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
939 false);
940 }
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957 static inline int rhashtable_lookup_insert_key(
958 struct rhashtable *ht, const void *key, struct rhash_head *obj,
959 const struct rhashtable_params params)
960 {
961 void *ret;
962
963 BUG_ON(!ht->p.obj_hashfn || !key);
964
965 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
966 if (IS_ERR(ret))
967 return PTR_ERR(ret);
968
969 return ret == NULL ? 0 : -EEXIST;
970 }
971
972
973
974
975
976
977
978
979
980
981
982
983 static inline void *rhashtable_lookup_get_insert_key(
984 struct rhashtable *ht, const void *key, struct rhash_head *obj,
985 const struct rhashtable_params params)
986 {
987 BUG_ON(!ht->p.obj_hashfn || !key);
988
989 return __rhashtable_insert_fast(ht, key, obj, params, false);
990 }
991
992
993 static inline int __rhashtable_remove_fast_one(
994 struct rhashtable *ht, struct bucket_table *tbl,
995 struct rhash_head *obj, const struct rhashtable_params params,
996 bool rhlist)
997 {
998 struct rhash_lock_head **bkt;
999 struct rhash_head __rcu **pprev;
1000 struct rhash_head *he;
1001 unsigned int hash;
1002 int err = -ENOENT;
1003
1004 hash = rht_head_hashfn(ht, tbl, obj, params);
1005 bkt = rht_bucket_var(tbl, hash);
1006 if (!bkt)
1007 return -ENOENT;
1008 pprev = NULL;
1009 rht_lock(tbl, bkt);
1010
1011 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
1012 struct rhlist_head *list;
1013
1014 list = container_of(he, struct rhlist_head, rhead);
1015
1016 if (he != obj) {
1017 struct rhlist_head __rcu **lpprev;
1018
1019 pprev = &he->next;
1020
1021 if (!rhlist)
1022 continue;
1023
1024 do {
1025 lpprev = &list->next;
1026 list = rht_dereference_bucket(list->next,
1027 tbl, hash);
1028 } while (list && obj != &list->rhead);
1029
1030 if (!list)
1031 continue;
1032
1033 list = rht_dereference_bucket(list->next, tbl, hash);
1034 RCU_INIT_POINTER(*lpprev, list);
1035 err = 0;
1036 break;
1037 }
1038
1039 obj = rht_dereference_bucket(obj->next, tbl, hash);
1040 err = 1;
1041
1042 if (rhlist) {
1043 list = rht_dereference_bucket(list->next, tbl, hash);
1044 if (list) {
1045 RCU_INIT_POINTER(list->rhead.next, obj);
1046 obj = &list->rhead;
1047 err = 0;
1048 }
1049 }
1050
1051 if (pprev) {
1052 rcu_assign_pointer(*pprev, obj);
1053 rht_unlock(tbl, bkt);
1054 } else {
1055 rht_assign_unlock(tbl, bkt, obj);
1056 }
1057 goto unlocked;
1058 }
1059
1060 rht_unlock(tbl, bkt);
1061 unlocked:
1062 if (err > 0) {
1063 atomic_dec(&ht->nelems);
1064 if (unlikely(ht->p.automatic_shrinking &&
1065 rht_shrink_below_30(ht, tbl)))
1066 schedule_work(&ht->run_work);
1067 err = 0;
1068 }
1069
1070 return err;
1071 }
1072
1073
1074 static inline int __rhashtable_remove_fast(
1075 struct rhashtable *ht, struct rhash_head *obj,
1076 const struct rhashtable_params params, bool rhlist)
1077 {
1078 struct bucket_table *tbl;
1079 int err;
1080
1081 rcu_read_lock();
1082
1083 tbl = rht_dereference_rcu(ht->tbl, ht);
1084
1085
1086
1087
1088
1089
1090 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1091 rhlist)) &&
1092 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1093 ;
1094
1095 rcu_read_unlock();
1096
1097 return err;
1098 }
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115 static inline int rhashtable_remove_fast(
1116 struct rhashtable *ht, struct rhash_head *obj,
1117 const struct rhashtable_params params)
1118 {
1119 return __rhashtable_remove_fast(ht, obj, params, false);
1120 }
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137 static inline int rhltable_remove(
1138 struct rhltable *hlt, struct rhlist_head *list,
1139 const struct rhashtable_params params)
1140 {
1141 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
1142 }
1143
1144
1145 static inline int __rhashtable_replace_fast(
1146 struct rhashtable *ht, struct bucket_table *tbl,
1147 struct rhash_head *obj_old, struct rhash_head *obj_new,
1148 const struct rhashtable_params params)
1149 {
1150 struct rhash_lock_head **bkt;
1151 struct rhash_head __rcu **pprev;
1152 struct rhash_head *he;
1153 unsigned int hash;
1154 int err = -ENOENT;
1155
1156
1157
1158
1159 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1160 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1161 return -EINVAL;
1162
1163 bkt = rht_bucket_var(tbl, hash);
1164 if (!bkt)
1165 return -ENOENT;
1166
1167 pprev = NULL;
1168 rht_lock(tbl, bkt);
1169
1170 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
1171 if (he != obj_old) {
1172 pprev = &he->next;
1173 continue;
1174 }
1175
1176 rcu_assign_pointer(obj_new->next, obj_old->next);
1177 if (pprev) {
1178 rcu_assign_pointer(*pprev, obj_new);
1179 rht_unlock(tbl, bkt);
1180 } else {
1181 rht_assign_unlock(tbl, bkt, obj_new);
1182 }
1183 err = 0;
1184 goto unlocked;
1185 }
1186
1187 rht_unlock(tbl, bkt);
1188
1189 unlocked:
1190 return err;
1191 }
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207 static inline int rhashtable_replace_fast(
1208 struct rhashtable *ht, struct rhash_head *obj_old,
1209 struct rhash_head *obj_new,
1210 const struct rhashtable_params params)
1211 {
1212 struct bucket_table *tbl;
1213 int err;
1214
1215 rcu_read_lock();
1216
1217 tbl = rht_dereference_rcu(ht->tbl, ht);
1218
1219
1220
1221
1222
1223
1224 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1225 obj_new, params)) &&
1226 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1227 ;
1228
1229 rcu_read_unlock();
1230
1231 return err;
1232 }
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255 static inline void rhltable_walk_enter(struct rhltable *hlt,
1256 struct rhashtable_iter *iter)
1257 {
1258 return rhashtable_walk_enter(&hlt->ht, iter);
1259 }
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269 static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1270 void (*free_fn)(void *ptr,
1271 void *arg),
1272 void *arg)
1273 {
1274 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1275 }
1276
1277 static inline void rhltable_destroy(struct rhltable *hlt)
1278 {
1279 return rhltable_free_and_destroy(hlt, NULL, NULL);
1280 }
1281
1282 #endif