rhashtable.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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);
  62. #endif
  63. };
  64. /**
  65. * struct rhashtable - Hash table handle
  66. * @tbl: Bucket table
  67. * @nelems: Number of elements in table
  68. * @shift: Current size (1 << shift)
  69. * @p: Configuration parameters
  70. */
  71. struct rhashtable {
  72. struct bucket_table __rcu *tbl;
  73. size_t nelems;
  74. size_t shift;
  75. struct rhashtable_params p;
  76. };
  77. #ifdef CONFIG_PROVE_LOCKING
  78. int lockdep_rht_mutex_is_held(const struct rhashtable *ht);
  79. #else
  80. static inline int lockdep_rht_mutex_is_held(const struct rhashtable *ht)
  81. {
  82. return 1;
  83. }
  84. #endif /* CONFIG_PROVE_LOCKING */
  85. int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
  86. u32 rhashtable_hashfn(const struct rhashtable *ht, const void *key, u32 len);
  87. u32 rhashtable_obj_hashfn(const struct rhashtable *ht, void *ptr);
  88. void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node, gfp_t);
  89. bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node, gfp_t);
  90. void rhashtable_remove_pprev(struct rhashtable *ht, struct rhash_head *obj,
  91. struct rhash_head __rcu **pprev, gfp_t flags);
  92. bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
  93. bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
  94. int rhashtable_expand(struct rhashtable *ht, gfp_t flags);
  95. int rhashtable_shrink(struct rhashtable *ht, gfp_t flags);
  96. void *rhashtable_lookup(const struct rhashtable *ht, const void *key);
  97. void *rhashtable_lookup_compare(const struct rhashtable *ht, u32 hash,
  98. bool (*compare)(void *, void *), void *arg);
  99. void rhashtable_destroy(const struct rhashtable *ht);
  100. #define rht_dereference(p, ht) \
  101. rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
  102. #define rht_dereference_rcu(p, ht) \
  103. rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
  104. #define rht_entry(ptr, type, member) container_of(ptr, type, member)
  105. #define rht_entry_safe(ptr, type, member) \
  106. ({ \
  107. typeof(ptr) __ptr = (ptr); \
  108. __ptr ? rht_entry(__ptr, type, member) : NULL; \
  109. })
  110. #define rht_next_entry_safe(pos, ht, member) \
  111. ({ \
  112. pos ? rht_entry_safe(rht_dereference((pos)->member.next, ht), \
  113. typeof(*(pos)), member) : NULL; \
  114. })
  115. /**
  116. * rht_for_each - iterate over hash chain
  117. * @pos: &struct rhash_head to use as a loop cursor.
  118. * @head: head of the hash chain (struct rhash_head *)
  119. * @ht: pointer to your struct rhashtable
  120. */
  121. #define rht_for_each(pos, head, ht) \
  122. for (pos = rht_dereference(head, ht); \
  123. pos; \
  124. pos = rht_dereference((pos)->next, ht))
  125. /**
  126. * rht_for_each_entry - iterate over hash chain of given type
  127. * @pos: type * to use as a loop cursor.
  128. * @head: head of the hash chain (struct rhash_head *)
  129. * @ht: pointer to your struct rhashtable
  130. * @member: name of the rhash_head within the hashable struct.
  131. */
  132. #define rht_for_each_entry(pos, head, ht, member) \
  133. for (pos = rht_entry_safe(rht_dereference(head, ht), \
  134. typeof(*(pos)), member); \
  135. pos; \
  136. pos = rht_next_entry_safe(pos, ht, member))
  137. /**
  138. * rht_for_each_entry_safe - safely iterate over hash chain of given type
  139. * @pos: type * to use as a loop cursor.
  140. * @n: type * to use for temporary next object storage
  141. * @head: head of the hash chain (struct rhash_head *)
  142. * @ht: pointer to your struct rhashtable
  143. * @member: name of the rhash_head within the hashable struct.
  144. *
  145. * This hash chain list-traversal primitive allows for the looped code to
  146. * remove the loop cursor from the list.
  147. */
  148. #define rht_for_each_entry_safe(pos, n, head, ht, member) \
  149. for (pos = rht_entry_safe(rht_dereference(head, ht), \
  150. typeof(*(pos)), member), \
  151. n = rht_next_entry_safe(pos, ht, member); \
  152. pos; \
  153. pos = n, \
  154. n = rht_next_entry_safe(pos, ht, member))
  155. /**
  156. * rht_for_each_rcu - iterate over rcu hash chain
  157. * @pos: &struct rhash_head to use as a loop cursor.
  158. * @head: head of the hash chain (struct rhash_head *)
  159. * @ht: pointer to your struct rhashtable
  160. *
  161. * This hash chain list-traversal primitive may safely run concurrently with
  162. * the _rcu fkht mutation primitives such as rht_insert() as long as the
  163. * traversal is guarded by rcu_read_lock().
  164. */
  165. #define rht_for_each_rcu(pos, head, ht) \
  166. for (pos = rht_dereference_rcu(head, ht); \
  167. pos; \
  168. pos = rht_dereference_rcu((pos)->next, ht))
  169. /**
  170. * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
  171. * @pos: type * to use as a loop cursor.
  172. * @head: head of the hash chain (struct rhash_head *)
  173. * @member: name of the rhash_head within the hashable struct.
  174. *
  175. * This hash chain list-traversal primitive may safely run concurrently with
  176. * the _rcu fkht mutation primitives such as rht_insert() as long as the
  177. * traversal is guarded by rcu_read_lock().
  178. */
  179. #define rht_for_each_entry_rcu(pos, head, member) \
  180. for (pos = rht_entry_safe(rcu_dereference_raw(head), \
  181. typeof(*(pos)), member); \
  182. pos; \
  183. pos = rht_entry_safe(rcu_dereference_raw((pos)->member.next), \
  184. typeof(*(pos)), member))
  185. #endif /* _LINUX_RHASHTABLE_H */