rhashtable.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Resizable, Scalable, Concurrent Hash Table
  3. *
  4. * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
  5. * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
  6. *
  7. * Based on the following paper by Josh Triplett, Paul E. McKenney
  8. * and Jonathan Walpole:
  9. * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf
  10. *
  11. * Code partially derived from nft_hash
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #ifndef _LINUX_RHASHTABLE_H
  18. #define _LINUX_RHASHTABLE_H
  19. #include <linux/rculist.h>
  20. struct rhash_head {
  21. struct rhash_head __rcu *next;
  22. };
  23. #define INIT_HASH_HEAD(ptr) ((ptr)->next = NULL)
  24. struct bucket_table {
  25. size_t size;
  26. struct rhash_head __rcu *buckets[];
  27. };
  28. typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
  29. typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
  30. struct rhashtable;
  31. /**
  32. * struct rhashtable_params - Hash table construction parameters
  33. * @nelem_hint: Hint on number of elements, should be 75% of desired size
  34. * @key_len: Length of key
  35. * @key_offset: Offset of key in struct to be hashed
  36. * @head_offset: Offset of rhash_head in struct to be hashed
  37. * @hash_rnd: Seed to use while hashing
  38. * @max_shift: Maximum number of shifts while expanding
  39. * @min_shift: Minimum number of shifts while shrinking
  40. * @hashfn: Function to hash key
  41. * @obj_hashfn: Function to hash object
  42. * @grow_decision: If defined, may return true if table should expand
  43. * @shrink_decision: If defined, may return true if table should shrink
  44. * @mutex_is_held: Must return true if protecting mutex is held
  45. */
  46. struct rhashtable_params {
  47. size_t nelem_hint;
  48. size_t key_len;
  49. size_t key_offset;
  50. size_t head_offset;
  51. u32 hash_rnd;
  52. size_t max_shift;
  53. size_t min_shift;
  54. rht_hashfn_t hashfn;
  55. rht_obj_hashfn_t obj_hashfn;
  56. bool (*grow_decision)(const struct rhashtable *ht,
  57. size_t new_size);
  58. bool (*shrink_decision)(const struct rhashtable *ht,
  59. size_t new_size);
  60. #ifdef CONFIG_PROVE_LOCKING
  61. int (*mutex_is_held)(void *parent);
  62. void *parent;
  63. #endif
  64. };
  65. /**
  66. * struct rhashtable - Hash table handle
  67. * @tbl: Bucket table
  68. * @nelems: Number of elements in table
  69. * @shift: Current size (1 << shift)
  70. * @p: Configuration parameters
  71. */
  72. struct rhashtable {
  73. struct bucket_table __rcu *tbl;
  74. size_t nelems;
  75. size_t shift;
  76. struct rhashtable_params p;
  77. };
  78. #ifdef CONFIG_PROVE_LOCKING
  79. int lockdep_rht_mutex_is_held(const struct rhashtable *ht);
  80. #else
  81. static inline int lockdep_rht_mutex_is_held(const struct rhashtable *ht)
  82. {
  83. return 1;
  84. }
  85. #endif /* CONFIG_PROVE_LOCKING */
  86. int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
  87. void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
  88. bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
  89. void rhashtable_remove_pprev(struct rhashtable *ht, struct rhash_head *obj,
  90. struct rhash_head __rcu **pprev);
  91. bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
  92. bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
  93. int rhashtable_expand(struct rhashtable *ht);
  94. int rhashtable_shrink(struct rhashtable *ht);
  95. void *rhashtable_lookup(const struct rhashtable *ht, const void *key);
  96. void *rhashtable_lookup_compare(const struct rhashtable *ht, const void *key,
  97. bool (*compare)(void *, void *), void *arg);
  98. void rhashtable_destroy(const struct rhashtable *ht);
  99. #define rht_dereference(p, ht) \
  100. rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
  101. #define rht_dereference_rcu(p, ht) \
  102. rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
  103. #define rht_entry(ptr, type, member) container_of(ptr, type, member)
  104. #define rht_entry_safe(ptr, type, member) \
  105. ({ \
  106. typeof(ptr) __ptr = (ptr); \
  107. __ptr ? rht_entry(__ptr, type, member) : NULL; \
  108. })
  109. #define rht_next_entry_safe(pos, ht, member) \
  110. ({ \
  111. pos ? rht_entry_safe(rht_dereference((pos)->member.next, ht), \
  112. typeof(*(pos)), member) : NULL; \
  113. })
  114. /**
  115. * rht_for_each - iterate over hash chain
  116. * @pos: &struct rhash_head to use as a loop cursor.
  117. * @head: head of the hash chain (struct rhash_head *)
  118. * @ht: pointer to your struct rhashtable
  119. */
  120. #define rht_for_each(pos, head, ht) \
  121. for (pos = rht_dereference(head, ht); \
  122. pos; \
  123. pos = rht_dereference((pos)->next, ht))
  124. /**
  125. * rht_for_each_entry - iterate over hash chain of given type
  126. * @pos: type * to use as a loop cursor.
  127. * @head: head of the hash chain (struct rhash_head *)
  128. * @ht: pointer to your struct rhashtable
  129. * @member: name of the rhash_head within the hashable struct.
  130. */
  131. #define rht_for_each_entry(pos, head, ht, member) \
  132. for (pos = rht_entry_safe(rht_dereference(head, ht), \
  133. typeof(*(pos)), member); \
  134. pos; \
  135. pos = rht_next_entry_safe(pos, ht, member))
  136. /**
  137. * rht_for_each_entry_safe - safely iterate over hash chain of given type
  138. * @pos: type * to use as a loop cursor.
  139. * @n: type * to use for temporary next object storage
  140. * @head: head of the hash chain (struct rhash_head *)
  141. * @ht: pointer to your struct rhashtable
  142. * @member: name of the rhash_head within the hashable struct.
  143. *
  144. * This hash chain list-traversal primitive allows for the looped code to
  145. * remove the loop cursor from the list.
  146. */
  147. #define rht_for_each_entry_safe(pos, n, head, ht, member) \
  148. for (pos = rht_entry_safe(rht_dereference(head, ht), \
  149. typeof(*(pos)), member), \
  150. n = rht_next_entry_safe(pos, ht, member); \
  151. pos; \
  152. pos = n, \
  153. n = rht_next_entry_safe(pos, ht, member))
  154. /**
  155. * rht_for_each_rcu - iterate over rcu hash chain
  156. * @pos: &struct rhash_head to use as a loop cursor.
  157. * @head: head of the hash chain (struct rhash_head *)
  158. * @ht: pointer to your struct rhashtable
  159. *
  160. * This hash chain list-traversal primitive may safely run concurrently with
  161. * the _rcu fkht mutation primitives such as rht_insert() as long as the
  162. * traversal is guarded by rcu_read_lock().
  163. */
  164. #define rht_for_each_rcu(pos, head, ht) \
  165. for (pos = rht_dereference_rcu(head, ht); \
  166. pos; \
  167. pos = rht_dereference_rcu((pos)->next, ht))
  168. /**
  169. * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
  170. * @pos: type * to use as a loop cursor.
  171. * @head: head of the hash chain (struct rhash_head *)
  172. * @member: name of the rhash_head within the hashable struct.
  173. *
  174. * This hash chain list-traversal primitive may safely run concurrently with
  175. * the _rcu fkht mutation primitives such as rht_insert() as long as the
  176. * traversal is guarded by rcu_read_lock().
  177. */
  178. #define rht_for_each_entry_rcu(pos, head, member) \
  179. for (pos = rht_entry_safe(rcu_dereference_raw(head), \
  180. typeof(*(pos)), member); \
  181. pos; \
  182. pos = rht_entry_safe(rcu_dereference_raw((pos)->member.next), \
  183. typeof(*(pos)), member))
  184. #endif /* _LINUX_RHASHTABLE_H */