test_hash.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Test cases for <linux/hash.h> and <linux/stringhash.h>
  4. * This just verifies that various ways of computing a hash
  5. * produce the same thing and, for cases where a k-bit hash
  6. * value is requested, is of the requested size.
  7. *
  8. * We fill a buffer with a 255-byte null-terminated string,
  9. * and use both full_name_hash() and hashlen_string() to hash the
  10. * substrings from i to j, where 0 <= i < j < 256.
  11. *
  12. * The returned values are used to check that __hash_32() and
  13. * __hash_32_generic() compute the same thing. Likewise hash_32()
  14. * and hash_64().
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt "\n"
  17. #include <linux/compiler.h>
  18. #include <linux/types.h>
  19. #include <linux/module.h>
  20. #include <linux/hash.h>
  21. #include <linux/stringhash.h>
  22. #include <linux/printk.h>
  23. /* 32-bit XORSHIFT generator. Seed must not be zero. */
  24. static u32 __init __attribute_const__
  25. xorshift(u32 seed)
  26. {
  27. seed ^= seed << 13;
  28. seed ^= seed >> 17;
  29. seed ^= seed << 5;
  30. return seed;
  31. }
  32. /* Given a non-zero x, returns a non-zero byte. */
  33. static u8 __init __attribute_const__
  34. mod255(u32 x)
  35. {
  36. x = (x & 0xffff) + (x >> 16); /* 1 <= x <= 0x1fffe */
  37. x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0x2fd */
  38. x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0x100 */
  39. x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0xff */
  40. return x;
  41. }
  42. /* Fill the buffer with non-zero bytes. */
  43. static void __init
  44. fill_buf(char *buf, size_t len, u32 seed)
  45. {
  46. size_t i;
  47. for (i = 0; i < len; i++) {
  48. seed = xorshift(seed);
  49. buf[i] = mod255(seed);
  50. }
  51. }
  52. /*
  53. * Test the various integer hash functions. h64 (or its low-order bits)
  54. * is the integer to hash. hash_or accumulates the OR of the hash values,
  55. * which are later checked to see that they cover all the requested bits.
  56. *
  57. * Because these functions (as opposed to the string hashes) are all
  58. * inline, the code being tested is actually in the module, and you can
  59. * recompile and re-test the module without rebooting.
  60. */
  61. static bool __init
  62. test_int_hash(unsigned long long h64, u32 hash_or[2][33])
  63. {
  64. int k;
  65. u32 h0 = (u32)h64, h1, h2;
  66. /* Test __hash32 */
  67. hash_or[0][0] |= h1 = __hash_32(h0);
  68. #ifdef HAVE_ARCH__HASH_32
  69. hash_or[1][0] |= h2 = __hash_32_generic(h0);
  70. #if HAVE_ARCH__HASH_32 == 1
  71. if (h1 != h2) {
  72. pr_err("__hash_32(%#x) = %#x != __hash_32_generic() = %#x",
  73. h0, h1, h2);
  74. return false;
  75. }
  76. #endif
  77. #endif
  78. /* Test k = 1..32 bits */
  79. for (k = 1; k <= 32; k++) {
  80. u32 const m = ((u32)2 << (k-1)) - 1; /* Low k bits set */
  81. /* Test hash_32 */
  82. hash_or[0][k] |= h1 = hash_32(h0, k);
  83. if (h1 > m) {
  84. pr_err("hash_32(%#x, %d) = %#x > %#x", h0, k, h1, m);
  85. return false;
  86. }
  87. #ifdef HAVE_ARCH_HASH_32
  88. h2 = hash_32_generic(h0, k);
  89. #if HAVE_ARCH_HASH_32 == 1
  90. if (h1 != h2) {
  91. pr_err("hash_32(%#x, %d) = %#x != hash_32_generic() "
  92. " = %#x", h0, k, h1, h2);
  93. return false;
  94. }
  95. #else
  96. if (h2 > m) {
  97. pr_err("hash_32_generic(%#x, %d) = %#x > %#x",
  98. h0, k, h1, m);
  99. return false;
  100. }
  101. #endif
  102. #endif
  103. /* Test hash_64 */
  104. hash_or[1][k] |= h1 = hash_64(h64, k);
  105. if (h1 > m) {
  106. pr_err("hash_64(%#llx, %d) = %#x > %#x", h64, k, h1, m);
  107. return false;
  108. }
  109. #ifdef HAVE_ARCH_HASH_64
  110. h2 = hash_64_generic(h64, k);
  111. #if HAVE_ARCH_HASH_64 == 1
  112. if (h1 != h2) {
  113. pr_err("hash_64(%#llx, %d) = %#x != hash_64_generic() "
  114. "= %#x", h64, k, h1, h2);
  115. return false;
  116. }
  117. #else
  118. if (h2 > m) {
  119. pr_err("hash_64_generic(%#llx, %d) = %#x > %#x",
  120. h64, k, h1, m);
  121. return false;
  122. }
  123. #endif
  124. #endif
  125. }
  126. (void)h2; /* Suppress unused variable warning */
  127. return true;
  128. }
  129. #define SIZE 256 /* Run time is cubic in SIZE */
  130. static int __init
  131. test_hash_init(void)
  132. {
  133. char buf[SIZE+1];
  134. u32 string_or = 0, hash_or[2][33] = { { 0, } };
  135. unsigned tests = 0;
  136. unsigned long long h64 = 0;
  137. int i, j;
  138. fill_buf(buf, SIZE, 1);
  139. /* Test every possible non-empty substring in the buffer. */
  140. for (j = SIZE; j > 0; --j) {
  141. buf[j] = '\0';
  142. for (i = 0; i <= j; i++) {
  143. u64 hashlen = hashlen_string(buf+i, buf+i);
  144. u32 h0 = full_name_hash(buf+i, buf+i, j-i);
  145. /* Check that hashlen_string gets the length right */
  146. if (hashlen_len(hashlen) != j-i) {
  147. pr_err("hashlen_string(%d..%d) returned length"
  148. " %u, expected %d",
  149. i, j, hashlen_len(hashlen), j-i);
  150. return -EINVAL;
  151. }
  152. /* Check that the hashes match */
  153. if (hashlen_hash(hashlen) != h0) {
  154. pr_err("hashlen_string(%d..%d) = %08x != "
  155. "full_name_hash() = %08x",
  156. i, j, hashlen_hash(hashlen), h0);
  157. return -EINVAL;
  158. }
  159. string_or |= h0;
  160. h64 = h64 << 32 | h0; /* For use with hash_64 */
  161. if (!test_int_hash(h64, hash_or))
  162. return -EINVAL;
  163. tests++;
  164. } /* i */
  165. } /* j */
  166. /* The OR of all the hash values should cover all the bits */
  167. if (~string_or) {
  168. pr_err("OR of all string hash results = %#x != %#x",
  169. string_or, -1u);
  170. return -EINVAL;
  171. }
  172. if (~hash_or[0][0]) {
  173. pr_err("OR of all __hash_32 results = %#x != %#x",
  174. hash_or[0][0], -1u);
  175. return -EINVAL;
  176. }
  177. #ifdef HAVE_ARCH__HASH_32
  178. #if HAVE_ARCH__HASH_32 != 1 /* Test is pointless if results match */
  179. if (~hash_or[1][0]) {
  180. pr_err("OR of all __hash_32_generic results = %#x != %#x",
  181. hash_or[1][0], -1u);
  182. return -EINVAL;
  183. }
  184. #endif
  185. #endif
  186. /* Likewise for all the i-bit hash values */
  187. for (i = 1; i <= 32; i++) {
  188. u32 const m = ((u32)2 << (i-1)) - 1; /* Low i bits set */
  189. if (hash_or[0][i] != m) {
  190. pr_err("OR of all hash_32(%d) results = %#x "
  191. "(%#x expected)", i, hash_or[0][i], m);
  192. return -EINVAL;
  193. }
  194. if (hash_or[1][i] != m) {
  195. pr_err("OR of all hash_64(%d) results = %#x "
  196. "(%#x expected)", i, hash_or[1][i], m);
  197. return -EINVAL;
  198. }
  199. }
  200. /* Issue notices about skipped tests. */
  201. #ifdef HAVE_ARCH__HASH_32
  202. #if HAVE_ARCH__HASH_32 != 1
  203. pr_info("__hash_32() is arch-specific; not compared to generic.");
  204. #endif
  205. #else
  206. pr_info("__hash_32() has no arch implementation to test.");
  207. #endif
  208. #ifdef HAVE_ARCH_HASH_32
  209. #if HAVE_ARCH_HASH_32 != 1
  210. pr_info("hash_32() is arch-specific; not compared to generic.");
  211. #endif
  212. #else
  213. pr_info("hash_32() has no arch implementation to test.");
  214. #endif
  215. #ifdef HAVE_ARCH_HASH_64
  216. #if HAVE_ARCH_HASH_64 != 1
  217. pr_info("hash_64() is arch-specific; not compared to generic.");
  218. #endif
  219. #else
  220. pr_info("hash_64() has no arch implementation to test.");
  221. #endif
  222. pr_notice("%u tests passed.", tests);
  223. return 0;
  224. }
  225. static void __exit test_hash_exit(void)
  226. {
  227. }
  228. module_init(test_hash_init); /* Does everything */
  229. module_exit(test_hash_exit); /* Does nothing */
  230. MODULE_LICENSE("GPL");