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