chash.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright 2017 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #ifndef _LINUX_CHASH_H
  24. #define _LINUX_CHASH_H
  25. #include <linux/types.h>
  26. #include <linux/hash.h>
  27. #include <linux/bug.h>
  28. #include <asm/bitsperlong.h>
  29. #if BITS_PER_LONG == 32
  30. # define _CHASH_LONG_SHIFT 5
  31. #elif BITS_PER_LONG == 64
  32. # define _CHASH_LONG_SHIFT 6
  33. #else
  34. # error "Unexpected BITS_PER_LONG"
  35. #endif
  36. struct __chash_table {
  37. u8 bits;
  38. u8 key_size;
  39. unsigned int value_size;
  40. u32 size_mask;
  41. unsigned long *occup_bitmap, *valid_bitmap;
  42. union {
  43. u32 *keys32;
  44. u64 *keys64;
  45. };
  46. u8 *values;
  47. #ifdef CONFIG_CHASH_STATS
  48. u64 hits, hits_steps, hits_time_ns;
  49. u64 miss, miss_steps, miss_time_ns;
  50. u64 relocs, reloc_dist;
  51. #endif
  52. };
  53. #define __CHASH_BITMAP_SIZE(bits) \
  54. (((1 << (bits)) + BITS_PER_LONG - 1) / BITS_PER_LONG)
  55. #define __CHASH_ARRAY_SIZE(bits, size) \
  56. ((((size) << (bits)) + sizeof(long) - 1) / sizeof(long))
  57. #define __CHASH_DATA_SIZE(bits, key_size, value_size) \
  58. (__CHASH_BITMAP_SIZE(bits) * 2 + \
  59. __CHASH_ARRAY_SIZE(bits, key_size) + \
  60. __CHASH_ARRAY_SIZE(bits, value_size))
  61. #define STRUCT_CHASH_TABLE(bits, key_size, value_size) \
  62. struct { \
  63. struct __chash_table table; \
  64. unsigned long data \
  65. [__CHASH_DATA_SIZE(bits, key_size, value_size)];\
  66. }
  67. /**
  68. * struct chash_table - Dynamically allocated closed hash table
  69. *
  70. * Use this struct for dynamically allocated hash tables (using
  71. * chash_table_alloc and chash_table_free), where the size is
  72. * determined at runtime.
  73. */
  74. struct chash_table {
  75. struct __chash_table table;
  76. unsigned long *data;
  77. };
  78. /**
  79. * DECLARE_CHASH_TABLE - macro to declare a closed hash table
  80. * @table: name of the declared hash table
  81. * @bts: Table size will be 2^bits entries
  82. * @key_sz: Size of hash keys in bytes, 4 or 8
  83. * @val_sz: Size of data values in bytes, can be 0
  84. *
  85. * This declares the hash table variable with a static size.
  86. *
  87. * The closed hash table stores key-value pairs with low memory and
  88. * lookup overhead. In operation it performs no dynamic memory
  89. * management. The data being stored does not require any
  90. * list_heads. The hash table performs best with small @val_sz and as
  91. * long as some space (about 50%) is left free in the table. But the
  92. * table can still work reasonably efficiently even when filled up to
  93. * about 90%. If bigger data items need to be stored and looked up,
  94. * store the pointer to it as value in the hash table.
  95. *
  96. * @val_sz may be 0. This can be useful when all the stored
  97. * information is contained in the key itself and the fact that it is
  98. * in the hash table (or not).
  99. */
  100. #define DECLARE_CHASH_TABLE(table, bts, key_sz, val_sz) \
  101. STRUCT_CHASH_TABLE(bts, key_sz, val_sz) table
  102. #ifdef CONFIG_CHASH_STATS
  103. #define __CHASH_STATS_INIT(prefix), \
  104. prefix.hits = 0, \
  105. prefix.hits_steps = 0, \
  106. prefix.hits_time_ns = 0, \
  107. prefix.miss = 0, \
  108. prefix.miss_steps = 0, \
  109. prefix.miss_time_ns = 0, \
  110. prefix.relocs = 0, \
  111. prefix.reloc_dist = 0
  112. #else
  113. #define __CHASH_STATS_INIT(prefix)
  114. #endif
  115. #define __CHASH_TABLE_INIT(prefix, data, bts, key_sz, val_sz) \
  116. prefix.bits = (bts), \
  117. prefix.key_size = (key_sz), \
  118. prefix.value_size = (val_sz), \
  119. prefix.size_mask = ((1 << bts) - 1), \
  120. prefix.occup_bitmap = &data[0], \
  121. prefix.valid_bitmap = &data \
  122. [__CHASH_BITMAP_SIZE(bts)], \
  123. prefix.keys64 = (u64 *)&data \
  124. [__CHASH_BITMAP_SIZE(bts) * 2], \
  125. prefix.values = (u8 *)&data \
  126. [__CHASH_BITMAP_SIZE(bts) * 2 + \
  127. __CHASH_ARRAY_SIZE(bts, key_sz)] \
  128. __CHASH_STATS_INIT(prefix)
  129. /**
  130. * DEFINE_CHASH_TABLE - macro to define and initialize a closed hash table
  131. * @tbl: name of the declared hash table
  132. * @bts: Table size will be 2^bits entries
  133. * @key_sz: Size of hash keys in bytes, 4 or 8
  134. * @val_sz: Size of data values in bytes, can be 0
  135. *
  136. * Note: the macro can be used for global and local hash table variables.
  137. */
  138. #define DEFINE_CHASH_TABLE(tbl, bts, key_sz, val_sz) \
  139. DECLARE_CHASH_TABLE(tbl, bts, key_sz, val_sz) = { \
  140. .table = { \
  141. __CHASH_TABLE_INIT(, (tbl).data, bts, key_sz, val_sz) \
  142. }, \
  143. .data = {0} \
  144. }
  145. /**
  146. * INIT_CHASH_TABLE - Initialize a hash table declared by DECLARE_CHASH_TABLE
  147. * @tbl: name of the declared hash table
  148. * @bts: Table size will be 2^bits entries
  149. * @key_sz: Size of hash keys in bytes, 4 or 8
  150. * @val_sz: Size of data values in bytes, can be 0
  151. */
  152. #define INIT_CHASH_TABLE(tbl, bts, key_sz, val_sz) \
  153. __CHASH_TABLE_INIT(((tbl).table), (tbl).data, bts, key_sz, val_sz)
  154. int chash_table_alloc(struct chash_table *table, u8 bits, u8 key_size,
  155. unsigned int value_size, gfp_t gfp_mask);
  156. void chash_table_free(struct chash_table *table);
  157. /**
  158. * chash_table_dump_stats - Dump statistics of a closed hash table
  159. * @tbl: Pointer to the table structure
  160. *
  161. * Dumps some performance statistics of the table gathered in operation
  162. * in the kernel log using pr_debug. If CONFIG_DYNAMIC_DEBUG is enabled,
  163. * user must turn on messages for chash.c (file chash.c +p).
  164. */
  165. #ifdef CONFIG_CHASH_STATS
  166. #define chash_table_dump_stats(tbl) __chash_table_dump_stats(&(*tbl).table)
  167. void __chash_table_dump_stats(struct __chash_table *table);
  168. #else
  169. #define chash_table_dump_stats(tbl)
  170. #endif
  171. /**
  172. * chash_table_reset_stats - Reset statistics of a closed hash table
  173. * @tbl: Pointer to the table structure
  174. */
  175. #ifdef CONFIG_CHASH_STATS
  176. #define chash_table_reset_stats(tbl) __chash_table_reset_stats(&(*tbl).table)
  177. static inline void __chash_table_reset_stats(struct __chash_table *table)
  178. {
  179. (void)table __CHASH_STATS_INIT((*table));
  180. }
  181. #else
  182. #define chash_table_reset_stats(tbl)
  183. #endif
  184. /**
  185. * chash_table_copy_in - Copy a new value into the hash table
  186. * @tbl: Pointer to the table structure
  187. * @key: Key of the entry to add or update
  188. * @value: Pointer to value to copy, may be NULL
  189. *
  190. * If @key already has an entry, its value is replaced. Otherwise a
  191. * new entry is added. If @value is NULL, the value is left unchanged
  192. * or uninitialized. Returns 1 if an entry already existed, 0 if a new
  193. * entry was added or %-ENOMEM if there was no free space in the
  194. * table.
  195. */
  196. #define chash_table_copy_in(tbl, key, value) \
  197. __chash_table_copy_in(&(*tbl).table, key, value)
  198. int __chash_table_copy_in(struct __chash_table *table, u64 key,
  199. const void *value);
  200. /**
  201. * chash_table_copy_out - Copy a value out of the hash table
  202. * @tbl: Pointer to the table structure
  203. * @key: Key of the entry to find
  204. * @value: Pointer to value to copy, may be NULL
  205. *
  206. * If @value is not NULL and the table has a non-0 value_size, the
  207. * value at @key is copied to @value. Returns the slot index of the
  208. * entry or %-EINVAL if @key was not found.
  209. */
  210. #define chash_table_copy_out(tbl, key, value) \
  211. __chash_table_copy_out(&(*tbl).table, key, value, false)
  212. int __chash_table_copy_out(struct __chash_table *table, u64 key,
  213. void *value, bool remove);
  214. /**
  215. * chash_table_remove - Remove an entry from the hash table
  216. * @tbl: Pointer to the table structure
  217. * @key: Key of the entry to find
  218. * @value: Pointer to value to copy, may be NULL
  219. *
  220. * If @value is not NULL and the table has a non-0 value_size, the
  221. * value at @key is copied to @value. The entry is removed from the
  222. * table. Returns the slot index of the removed entry or %-EINVAL if
  223. * @key was not found.
  224. */
  225. #define chash_table_remove(tbl, key, value) \
  226. __chash_table_copy_out(&(*tbl).table, key, value, true)
  227. /*
  228. * Low level iterator API used internally by the above functions.
  229. */
  230. struct chash_iter {
  231. struct __chash_table *table;
  232. unsigned long mask;
  233. int slot;
  234. };
  235. /**
  236. * CHASH_ITER_INIT - Initialize a hash table iterator
  237. * @tbl: Pointer to hash table to iterate over
  238. * @s: Initial slot number
  239. */
  240. #define CHASH_ITER_INIT(table, s) { \
  241. table, \
  242. 1UL << ((s) & (BITS_PER_LONG - 1)), \
  243. s \
  244. }
  245. /**
  246. * CHASH_ITER_SET - Set hash table iterator to new slot
  247. * @iter: Iterator
  248. * @s: Slot number
  249. */
  250. #define CHASH_ITER_SET(iter, s) \
  251. (iter).mask = 1UL << ((s) & (BITS_PER_LONG - 1)), \
  252. (iter).slot = (s)
  253. /**
  254. * CHASH_ITER_INC - Increment hash table iterator
  255. * @table: Hash table to iterate over
  256. *
  257. * Wraps around at the end.
  258. */
  259. #define CHASH_ITER_INC(iter) do { \
  260. (iter).mask = (iter).mask << 1 | \
  261. (iter).mask >> (BITS_PER_LONG - 1); \
  262. (iter).slot = ((iter).slot + 1) & (iter).table->size_mask; \
  263. } while (0)
  264. static inline bool chash_iter_is_valid(const struct chash_iter iter)
  265. {
  266. BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
  267. return !!(iter.table->valid_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &
  268. iter.mask);
  269. }
  270. static inline bool chash_iter_is_empty(const struct chash_iter iter)
  271. {
  272. BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
  273. return !(iter.table->occup_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &
  274. iter.mask);
  275. }
  276. static inline void chash_iter_set_valid(const struct chash_iter iter)
  277. {
  278. BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
  279. iter.table->valid_bitmap[iter.slot >> _CHASH_LONG_SHIFT] |= iter.mask;
  280. iter.table->occup_bitmap[iter.slot >> _CHASH_LONG_SHIFT] |= iter.mask;
  281. }
  282. static inline void chash_iter_set_invalid(const struct chash_iter iter)
  283. {
  284. BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
  285. iter.table->valid_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &= ~iter.mask;
  286. }
  287. static inline void chash_iter_set_empty(const struct chash_iter iter)
  288. {
  289. BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
  290. iter.table->occup_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &= ~iter.mask;
  291. }
  292. static inline u32 chash_iter_key32(const struct chash_iter iter)
  293. {
  294. BUG_ON(iter.table->key_size != 4);
  295. BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
  296. return iter.table->keys32[iter.slot];
  297. }
  298. static inline u64 chash_iter_key64(const struct chash_iter iter)
  299. {
  300. BUG_ON(iter.table->key_size != 8);
  301. BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
  302. return iter.table->keys64[iter.slot];
  303. }
  304. static inline u64 chash_iter_key(const struct chash_iter iter)
  305. {
  306. BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
  307. return (iter.table->key_size == 4) ?
  308. iter.table->keys32[iter.slot] : iter.table->keys64[iter.slot];
  309. }
  310. static inline u32 chash_iter_hash32(const struct chash_iter iter)
  311. {
  312. BUG_ON(iter.table->key_size != 4);
  313. return hash_32(chash_iter_key32(iter), iter.table->bits);
  314. }
  315. static inline u32 chash_iter_hash64(const struct chash_iter iter)
  316. {
  317. BUG_ON(iter.table->key_size != 8);
  318. return hash_64(chash_iter_key64(iter), iter.table->bits);
  319. }
  320. static inline u32 chash_iter_hash(const struct chash_iter iter)
  321. {
  322. return (iter.table->key_size == 4) ?
  323. hash_32(chash_iter_key32(iter), iter.table->bits) :
  324. hash_64(chash_iter_key64(iter), iter.table->bits);
  325. }
  326. static inline void *chash_iter_value(const struct chash_iter iter)
  327. {
  328. BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
  329. return iter.table->values +
  330. ((unsigned long)iter.slot * iter.table->value_size);
  331. }
  332. #endif /* _LINUX_CHASH_H */