test_hash.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #include <linux/compiler.h>
  17. #include <linux/types.h>
  18. #include <linux/module.h>
  19. #include <linux/hash.h>
  20. #include <linux/stringhash.h>
  21. #include <kunit/test.h>
  22. /* 32-bit XORSHIFT generator. Seed must not be zero. */
  23. static u32 __attribute_const__
  24. xorshift(u32 seed)
  25. {
  26. seed ^= seed << 13;
  27. seed ^= seed >> 17;
  28. seed ^= seed << 5;
  29. return seed;
  30. }
  31. /* Given a non-zero x, returns a non-zero byte. */
  32. static u8 __attribute_const__
  33. mod255(u32 x)
  34. {
  35. x = (x & 0xffff) + (x >> 16); /* 1 <= x <= 0x1fffe */
  36. x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0x2fd */
  37. x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0x100 */
  38. x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0xff */
  39. return x;
  40. }
  41. /* Fill the buffer with non-zero bytes. */
  42. static void fill_buf(char *buf, size_t len, u32 seed)
  43. {
  44. size_t i;
  45. for (i = 0; i < len; i++) {
  46. seed = xorshift(seed);
  47. buf[i] = mod255(seed);
  48. }
  49. }
  50. /* Holds most testing variables for the int test. */
  51. struct test_hash_params {
  52. /* Pointer to integer to be hashed. */
  53. unsigned long long *h64;
  54. /* Low 32-bits of integer to be hashed. */
  55. u32 h0;
  56. /* Arch-specific hash result. */
  57. u32 h1;
  58. /* Generic hash result. */
  59. u32 h2;
  60. /* ORed hashes of given size (in bits). */
  61. u32 (*hash_or)[33];
  62. };
  63. #ifdef HAVE_ARCH__HASH_32
  64. static void
  65. test_int__hash_32(struct kunit *test, struct test_hash_params *params)
  66. {
  67. params->hash_or[1][0] |= params->h2 = __hash_32_generic(params->h0);
  68. #if HAVE_ARCH__HASH_32 == 1
  69. KUNIT_EXPECT_EQ_MSG(test, params->h1, params->h2,
  70. "__hash_32(%#x) = %#x != __hash_32_generic() = %#x",
  71. params->h0, params->h1, params->h2);
  72. #endif
  73. }
  74. #endif
  75. #ifdef HAVE_ARCH_HASH_64
  76. static void
  77. test_int_hash_64(struct kunit *test, struct test_hash_params *params, u32 const *m, int *k)
  78. {
  79. params->h2 = hash_64_generic(*params->h64, *k);
  80. #if HAVE_ARCH_HASH_64 == 1
  81. KUNIT_EXPECT_EQ_MSG(test, params->h1, params->h2,
  82. "hash_64(%#llx, %d) = %#x != hash_64_generic() = %#x",
  83. *params->h64, *k, params->h1, params->h2);
  84. #else
  85. KUNIT_EXPECT_LE_MSG(test, params->h1, params->h2,
  86. "hash_64_generic(%#llx, %d) = %#x > %#x",
  87. *params->h64, *k, params->h1, *m);
  88. #endif
  89. }
  90. #endif
  91. /*
  92. * Test the various integer hash functions. h64 (or its low-order bits)
  93. * is the integer to hash. hash_or accumulates the OR of the hash values,
  94. * which are later checked to see that they cover all the requested bits.
  95. *
  96. * Because these functions (as opposed to the string hashes) are all
  97. * inline, the code being tested is actually in the module, and you can
  98. * recompile and re-test the module without rebooting.
  99. */
  100. static void
  101. test_int_hash(struct kunit *test, unsigned long long h64, u32 hash_or[2][33])
  102. {
  103. int k;
  104. struct test_hash_params params = { &h64, (u32)h64, 0, 0, hash_or };
  105. /* Test __hash32 */
  106. hash_or[0][0] |= params.h1 = __hash_32(params.h0);
  107. #ifdef HAVE_ARCH__HASH_32
  108. test_int__hash_32(test, &params);
  109. #endif
  110. /* Test k = 1..32 bits */
  111. for (k = 1; k <= 32; k++) {
  112. u32 const m = ((u32)2 << (k-1)) - 1; /* Low k bits set */
  113. /* Test hash_32 */
  114. hash_or[0][k] |= params.h1 = hash_32(params.h0, k);
  115. KUNIT_EXPECT_LE_MSG(test, params.h1, m,
  116. "hash_32(%#x, %d) = %#x > %#x",
  117. params.h0, k, params.h1, m);
  118. /* Test hash_64 */
  119. hash_or[1][k] |= params.h1 = hash_64(h64, k);
  120. KUNIT_EXPECT_LE_MSG(test, params.h1, m,
  121. "hash_64(%#llx, %d) = %#x > %#x",
  122. h64, k, params.h1, m);
  123. #ifdef HAVE_ARCH_HASH_64
  124. test_int_hash_64(test, &params, &m, &k);
  125. #endif
  126. }
  127. }
  128. #define SIZE 256 /* Run time is cubic in SIZE */
  129. static void test_string_or(struct kunit *test)
  130. {
  131. char buf[SIZE+1];
  132. u32 string_or = 0;
  133. int i, j;
  134. fill_buf(buf, SIZE, 1);
  135. /* Test every possible non-empty substring in the buffer. */
  136. for (j = SIZE; j > 0; --j) {
  137. buf[j] = '\0';
  138. for (i = 0; i <= j; i++) {
  139. u32 h0 = full_name_hash(buf+i, buf+i, j-i);
  140. string_or |= h0;
  141. } /* i */
  142. } /* j */
  143. /* The OR of all the hash values should cover all the bits */
  144. KUNIT_EXPECT_EQ_MSG(test, string_or, -1u,
  145. "OR of all string hash results = %#x != %#x",
  146. string_or, -1u);
  147. }
  148. static void test_hash_or(struct kunit *test)
  149. {
  150. char buf[SIZE+1];
  151. u32 hash_or[2][33] = { { 0, } };
  152. unsigned long long h64 = 0;
  153. int i, j;
  154. fill_buf(buf, SIZE, 1);
  155. /* Test every possible non-empty substring in the buffer. */
  156. for (j = SIZE; j > 0; --j) {
  157. buf[j] = '\0';
  158. for (i = 0; i <= j; i++) {
  159. u64 hashlen = hashlen_string(buf+i, buf+i);
  160. u32 h0 = full_name_hash(buf+i, buf+i, j-i);
  161. /* Check that hashlen_string gets the length right */
  162. KUNIT_EXPECT_EQ_MSG(test, hashlen_len(hashlen), j-i,
  163. "hashlen_string(%d..%d) returned length %u, expected %d",
  164. i, j, hashlen_len(hashlen), j-i);
  165. /* Check that the hashes match */
  166. KUNIT_EXPECT_EQ_MSG(test, hashlen_hash(hashlen), h0,
  167. "hashlen_string(%d..%d) = %08x != full_name_hash() = %08x",
  168. i, j, hashlen_hash(hashlen), h0);
  169. h64 = h64 << 32 | h0; /* For use with hash_64 */
  170. test_int_hash(test, h64, hash_or);
  171. } /* i */
  172. } /* j */
  173. KUNIT_EXPECT_EQ_MSG(test, hash_or[0][0], -1u,
  174. "OR of all __hash_32 results = %#x != %#x",
  175. hash_or[0][0], -1u);
  176. #ifdef HAVE_ARCH__HASH_32
  177. #if HAVE_ARCH__HASH_32 != 1 /* Test is pointless if results match */
  178. KUNIT_EXPECT_EQ_MSG(test, hash_or[1][0], -1u,
  179. "OR of all __hash_32_generic results = %#x != %#x",
  180. hash_or[1][0], -1u);
  181. #endif
  182. #endif
  183. /* Likewise for all the i-bit hash values */
  184. for (i = 1; i <= 32; i++) {
  185. u32 const m = ((u32)2 << (i-1)) - 1; /* Low i bits set */
  186. KUNIT_EXPECT_EQ_MSG(test, hash_or[0][i], m,
  187. "OR of all hash_32(%d) results = %#x (%#x expected)",
  188. i, hash_or[0][i], m);
  189. KUNIT_EXPECT_EQ_MSG(test, hash_or[1][i], m,
  190. "OR of all hash_64(%d) results = %#x (%#x expected)",
  191. i, hash_or[1][i], m);
  192. }
  193. }
  194. static struct kunit_case hash_test_cases[] __refdata = {
  195. KUNIT_CASE(test_string_or),
  196. KUNIT_CASE(test_hash_or),
  197. {}
  198. };
  199. static struct kunit_suite hash_test_suite = {
  200. .name = "hash",
  201. .test_cases = hash_test_cases,
  202. };
  203. kunit_test_suite(hash_test_suite);
  204. MODULE_LICENSE("GPL");