rhashtable.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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/compiler.h>
  20. #include <linux/list_nulls.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/mutex.h>
  23. /*
  24. * The end of the chain is marked with a special nulls marks which has
  25. * the following format:
  26. *
  27. * +-------+-----------------------------------------------------+-+
  28. * | Base | Hash |1|
  29. * +-------+-----------------------------------------------------+-+
  30. *
  31. * Base (4 bits) : Reserved to distinguish between multiple tables.
  32. * Specified via &struct rhashtable_params.nulls_base.
  33. * Hash (27 bits): Full hash (unmasked) of first element added to bucket
  34. * 1 (1 bit) : Nulls marker (always set)
  35. *
  36. * The remaining bits of the next pointer remain unused for now.
  37. */
  38. #define RHT_BASE_BITS 4
  39. #define RHT_HASH_BITS 27
  40. #define RHT_BASE_SHIFT RHT_HASH_BITS
  41. struct rhash_head {
  42. struct rhash_head __rcu *next;
  43. };
  44. /**
  45. * struct bucket_table - Table of hash buckets
  46. * @size: Number of hash buckets
  47. * @locks_mask: Mask to apply before accessing locks[]
  48. * @locks: Array of spinlocks protecting individual buckets
  49. * @buckets: size * hash buckets
  50. */
  51. struct bucket_table {
  52. size_t size;
  53. unsigned int locks_mask;
  54. spinlock_t *locks;
  55. struct rhash_head __rcu *buckets[];
  56. };
  57. typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
  58. typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
  59. struct rhashtable;
  60. /**
  61. * struct rhashtable_params - Hash table construction parameters
  62. * @nelem_hint: Hint on number of elements, should be 75% of desired size
  63. * @key_len: Length of key
  64. * @key_offset: Offset of key in struct to be hashed
  65. * @head_offset: Offset of rhash_head in struct to be hashed
  66. * @hash_rnd: Seed to use while hashing
  67. * @max_shift: Maximum number of shifts while expanding
  68. * @min_shift: Minimum number of shifts while shrinking
  69. * @nulls_base: Base value to generate nulls marker
  70. * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
  71. * @hashfn: Function to hash key
  72. * @obj_hashfn: Function to hash object
  73. * @grow_decision: If defined, may return true if table should expand
  74. * @shrink_decision: If defined, may return true if table should shrink
  75. *
  76. * Note: when implementing the grow and shrink decision function, min/max
  77. * shift must be enforced, otherwise, resizing watermarks they set may be
  78. * useless.
  79. */
  80. struct rhashtable_params {
  81. size_t nelem_hint;
  82. size_t key_len;
  83. size_t key_offset;
  84. size_t head_offset;
  85. u32 hash_rnd;
  86. size_t max_shift;
  87. size_t min_shift;
  88. u32 nulls_base;
  89. size_t locks_mul;
  90. rht_hashfn_t hashfn;
  91. rht_obj_hashfn_t obj_hashfn;
  92. bool (*grow_decision)(const struct rhashtable *ht,
  93. size_t new_size);
  94. bool (*shrink_decision)(const struct rhashtable *ht,
  95. size_t new_size);
  96. };
  97. /**
  98. * struct rhashtable - Hash table handle
  99. * @tbl: Bucket table
  100. * @future_tbl: Table under construction during expansion/shrinking
  101. * @nelems: Number of elements in table
  102. * @shift: Current size (1 << shift)
  103. * @p: Configuration parameters
  104. * @run_work: Deferred worker to expand/shrink asynchronously
  105. * @mutex: Mutex to protect current/future table swapping
  106. * @walkers: List of active walkers
  107. * @being_destroyed: True if table is set up for destruction
  108. */
  109. struct rhashtable {
  110. struct bucket_table __rcu *tbl;
  111. struct bucket_table __rcu *future_tbl;
  112. atomic_t nelems;
  113. atomic_t shift;
  114. struct rhashtable_params p;
  115. struct work_struct run_work;
  116. struct mutex mutex;
  117. struct list_head walkers;
  118. bool being_destroyed;
  119. };
  120. /**
  121. * struct rhashtable_walker - Hash table walker
  122. * @list: List entry on list of walkers
  123. * @resize: Resize event occured
  124. */
  125. struct rhashtable_walker {
  126. struct list_head list;
  127. bool resize;
  128. };
  129. /**
  130. * struct rhashtable_iter - Hash table iterator, fits into netlink cb
  131. * @ht: Table to iterate through
  132. * @p: Current pointer
  133. * @walker: Associated rhashtable walker
  134. * @slot: Current slot
  135. * @skip: Number of entries to skip in slot
  136. */
  137. struct rhashtable_iter {
  138. struct rhashtable *ht;
  139. struct rhash_head *p;
  140. struct rhashtable_walker *walker;
  141. unsigned int slot;
  142. unsigned int skip;
  143. };
  144. static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
  145. {
  146. return NULLS_MARKER(ht->p.nulls_base + hash);
  147. }
  148. #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
  149. ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
  150. static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
  151. {
  152. return ((unsigned long) ptr & 1);
  153. }
  154. static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
  155. {
  156. return ((unsigned long) ptr) >> 1;
  157. }
  158. #ifdef CONFIG_PROVE_LOCKING
  159. int lockdep_rht_mutex_is_held(struct rhashtable *ht);
  160. int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
  161. #else
  162. static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
  163. {
  164. return 1;
  165. }
  166. static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
  167. u32 hash)
  168. {
  169. return 1;
  170. }
  171. #endif /* CONFIG_PROVE_LOCKING */
  172. int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
  173. void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
  174. bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
  175. bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
  176. bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
  177. int rhashtable_expand(struct rhashtable *ht);
  178. int rhashtable_shrink(struct rhashtable *ht);
  179. void *rhashtable_lookup(struct rhashtable *ht, const void *key);
  180. void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
  181. bool (*compare)(void *, void *), void *arg);
  182. bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj);
  183. bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
  184. struct rhash_head *obj,
  185. bool (*compare)(void *, void *),
  186. void *arg);
  187. int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
  188. void rhashtable_walk_exit(struct rhashtable_iter *iter);
  189. int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
  190. void *rhashtable_walk_next(struct rhashtable_iter *iter);
  191. void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
  192. void rhashtable_destroy(struct rhashtable *ht);
  193. #define rht_dereference(p, ht) \
  194. rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
  195. #define rht_dereference_rcu(p, ht) \
  196. rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
  197. #define rht_dereference_bucket(p, tbl, hash) \
  198. rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
  199. #define rht_dereference_bucket_rcu(p, tbl, hash) \
  200. rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
  201. #define rht_entry(tpos, pos, member) \
  202. ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
  203. /**
  204. * rht_for_each_continue - continue iterating over hash chain
  205. * @pos: the &struct rhash_head to use as a loop cursor.
  206. * @head: the previous &struct rhash_head to continue from
  207. * @tbl: the &struct bucket_table
  208. * @hash: the hash value / bucket index
  209. */
  210. #define rht_for_each_continue(pos, head, tbl, hash) \
  211. for (pos = rht_dereference_bucket(head, tbl, hash); \
  212. !rht_is_a_nulls(pos); \
  213. pos = rht_dereference_bucket((pos)->next, tbl, hash))
  214. /**
  215. * rht_for_each - iterate over hash chain
  216. * @pos: the &struct rhash_head to use as a loop cursor.
  217. * @tbl: the &struct bucket_table
  218. * @hash: the hash value / bucket index
  219. */
  220. #define rht_for_each(pos, tbl, hash) \
  221. rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
  222. /**
  223. * rht_for_each_entry_continue - continue iterating over hash chain
  224. * @tpos: the type * to use as a loop cursor.
  225. * @pos: the &struct rhash_head to use as a loop cursor.
  226. * @head: the previous &struct rhash_head to continue from
  227. * @tbl: the &struct bucket_table
  228. * @hash: the hash value / bucket index
  229. * @member: name of the &struct rhash_head within the hashable struct.
  230. */
  231. #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
  232. for (pos = rht_dereference_bucket(head, tbl, hash); \
  233. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  234. pos = rht_dereference_bucket((pos)->next, tbl, hash))
  235. /**
  236. * rht_for_each_entry - iterate over hash chain of given type
  237. * @tpos: the type * to use as a loop cursor.
  238. * @pos: the &struct rhash_head to use as a loop cursor.
  239. * @tbl: the &struct bucket_table
  240. * @hash: the hash value / bucket index
  241. * @member: name of the &struct rhash_head within the hashable struct.
  242. */
  243. #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
  244. rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
  245. tbl, hash, member)
  246. /**
  247. * rht_for_each_entry_safe - safely iterate over hash chain of given type
  248. * @tpos: the type * to use as a loop cursor.
  249. * @pos: the &struct rhash_head to use as a loop cursor.
  250. * @next: the &struct rhash_head to use as next in loop cursor.
  251. * @tbl: the &struct bucket_table
  252. * @hash: the hash value / bucket index
  253. * @member: name of the &struct rhash_head within the hashable struct.
  254. *
  255. * This hash chain list-traversal primitive allows for the looped code to
  256. * remove the loop cursor from the list.
  257. */
  258. #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
  259. for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
  260. next = !rht_is_a_nulls(pos) ? \
  261. rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
  262. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  263. pos = next, \
  264. next = !rht_is_a_nulls(pos) ? \
  265. rht_dereference_bucket(pos->next, tbl, hash) : NULL)
  266. /**
  267. * rht_for_each_rcu_continue - continue iterating over rcu hash chain
  268. * @pos: the &struct rhash_head to use as a loop cursor.
  269. * @head: the previous &struct rhash_head to continue from
  270. * @tbl: the &struct bucket_table
  271. * @hash: the hash value / bucket index
  272. *
  273. * This hash chain list-traversal primitive may safely run concurrently with
  274. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  275. * traversal is guarded by rcu_read_lock().
  276. */
  277. #define rht_for_each_rcu_continue(pos, head, tbl, hash) \
  278. for (({barrier(); }), \
  279. pos = rht_dereference_bucket_rcu(head, tbl, hash); \
  280. !rht_is_a_nulls(pos); \
  281. pos = rcu_dereference_raw(pos->next))
  282. /**
  283. * rht_for_each_rcu - iterate over rcu hash chain
  284. * @pos: the &struct rhash_head to use as a loop cursor.
  285. * @tbl: the &struct bucket_table
  286. * @hash: the hash value / bucket index
  287. *
  288. * This hash chain list-traversal primitive may safely run concurrently with
  289. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  290. * traversal is guarded by rcu_read_lock().
  291. */
  292. #define rht_for_each_rcu(pos, tbl, hash) \
  293. rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
  294. /**
  295. * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
  296. * @tpos: the type * to use as a loop cursor.
  297. * @pos: the &struct rhash_head to use as a loop cursor.
  298. * @head: the previous &struct rhash_head to continue from
  299. * @tbl: the &struct bucket_table
  300. * @hash: the hash value / bucket index
  301. * @member: name of the &struct rhash_head within the hashable struct.
  302. *
  303. * This hash chain list-traversal primitive may safely run concurrently with
  304. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  305. * traversal is guarded by rcu_read_lock().
  306. */
  307. #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
  308. for (({barrier(); }), \
  309. pos = rht_dereference_bucket_rcu(head, tbl, hash); \
  310. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  311. pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
  312. /**
  313. * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
  314. * @tpos: the type * to use as a loop cursor.
  315. * @pos: the &struct rhash_head to use as a loop cursor.
  316. * @tbl: the &struct bucket_table
  317. * @hash: the hash value / bucket index
  318. * @member: name of the &struct rhash_head within the hashable struct.
  319. *
  320. * This hash chain list-traversal primitive may safely run concurrently with
  321. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  322. * traversal is guarded by rcu_read_lock().
  323. */
  324. #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
  325. rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
  326. tbl, hash, member)
  327. #endif /* _LINUX_RHASHTABLE_H */